blob: fa4a1c843e184af36fc0938b2ac2e358d6524607 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
33import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
34
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070053import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040054import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070056import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070057import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070058import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070059import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070065import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020066import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080069import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070070import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070071import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070072import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070075import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070077import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070078import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.HapticFeedbackConstants;
81import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070082import android.view.KeyboardShortcutGroup;
83import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080084import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080088import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070097import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070098import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700100import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700103import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700108import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700109import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800110import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700111import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700114import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700115import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700116import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700120import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700121import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800122import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800123import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700124import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700125import com.android.launcher3.states.AllAppsState;
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
225 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
Sunny Goyalffe83f12014-08-14 17:39:34 -0700227 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700229
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700230 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231
Sunny Goyal316490e2015-06-02 09:38:28 -0700232 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800233 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700234
Michael Jurka838a4ca2011-02-07 13:33:06 -0800235 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700236
Sunny Goyal47328fd2016-05-25 18:56:41 -0700237 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700238
239 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700240 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700241 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700242
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 Goyalfe770c92016-09-27 14:38:58 -0700361 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362
Sunny Goyal2100c782016-08-22 16:00:03 -0700363 // We only load the page synchronously if the user rotates (or triggers a
364 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700365 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
366 if (savedInstanceState != null) {
367 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
368 }
369 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700370 // If we are not binding synchronously, show a fade in animation when
371 // the first page bind completes.
372 mDragLayer.setAlpha(0);
373 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700374 // Pages bound synchronously.
375 mWorkspace.setCurrentPage(currentScreen);
376
Sunny Goyal2100c782016-08-22 16:00:03 -0700377 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
379
380 // For handling default keys
381 mDefaultKeySsb = new SpannableStringBuilder();
382 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800383
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800384 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700385 // In case we are on a device with locked rotation, we should look at preferences to check
386 // if the user has specifically allowed rotation.
387 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700388 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700389 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
390 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700391 }
392
Sunny Goyal5a81c382017-03-20 15:08:06 -0700393 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
394 // Temporarily enable the rotation
395 mRotationEnabled = true;
396 }
397
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700398 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
399 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700400 setRequestedOrientation(mRotationEnabled
401 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700402
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800403 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700404
Jon Miranda7fda2852017-07-19 16:07:01 -0700405 // Listen for broadcasts
406 IntentFilter filter = new IntentFilter();
407 filter.addAction(Intent.ACTION_SCREEN_OFF);
408 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
409 registerReceiver(mReceiver, filter);
410 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700411
Sunny Goyal8392c822017-06-20 10:03:56 -0700412 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
413 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700414
415 if (mLauncherCallbacks != null) {
416 mLauncherCallbacks.onCreate(savedInstanceState);
417 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700418
419 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700420 }
421
Sunny Goyal7779d622015-06-11 16:18:39 -0700422 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700423 public void onThemeChanged() {
424 recreate();
425 }
426
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700427 public LauncherStateManager getStateManager() {
428 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700429 }
430
Mario Bertschlera6936942017-05-31 14:48:19 -0700431 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700432 if (isDark) {
433 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700434 } else if (supportsDarkText) {
435 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700436 }
437 }
438
439 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700440 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800441 return mLauncherView.findViewById(id);
442 }
443
444 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700445 public void onAppWidgetHostReset() {
446 if (mAppWidgetHost != null) {
447 mAppWidgetHost.startListening();
448 }
449 }
450
Adam Cohen9211d422014-10-07 18:14:53 -0700451 private LauncherCallbacks mLauncherCallbacks;
452
Winson1f064272016-07-18 17:18:02 -0700453 public void onInsetsChanged(Rect insets) {
454 mDeviceProfile.updateInsets(insets);
455 mDeviceProfile.layout(this, true /* notifyListeners */);
456 }
457
Sunny Goyal32554d12015-12-03 15:31:25 -0800458 /**
459 * Call this after onCreate to set or clear overlay.
460 */
461 public void setLauncherOverlay(LauncherOverlay overlay) {
462 if (overlay != null) {
463 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
464 }
465 mWorkspace.setLauncherOverlay(overlay);
466 }
467
Adam Cohen9211d422014-10-07 18:14:53 -0700468 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
469 mLauncherCallbacks = callbacks;
470 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700471 }
472
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700473 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700474 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700475 if (mLauncherCallbacks != null) {
476 mLauncherCallbacks.onLauncherProviderChange();
477 }
478 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700479
Hyunyoung Song3f471442015-04-08 19:01:34 -0700480 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700481 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
482 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700483 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700484 }
485
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000486 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700487 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
488 // This cast is safe as long as the id < 0x00FFFFFF
489 // Since we jail all the dynamically generated views, there should be no clashes
490 // with any other views.
491 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000492 }
493
Tony Wickham010d2552017-01-20 08:15:28 -0800494 public PopupDataProvider getPopupDataProvider() {
495 return mPopupDataProvider;
496 }
497
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000498 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700499 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
500 * a configuration step, this allows the proper animations to run after other transitions.
501 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 private long completeAdd(
503 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
504 long screenId = info.screenId;
505 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700506 // When the screen id represents an actual screen (as opposed to a rank) we make sure
507 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700509 }
Adam Cohendb364c32014-05-20 14:23:40 -0700510
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800512 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700513 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700514 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700518 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700520 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700521 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 int widgetId = appWidgetId;
523 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700524 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700526 // Since the view was just bound, also launch the configure activity if needed
527 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
528 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800529 if (provider != null) {
530 new WidgetAddFlowHandler(provider)
531 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700532 }
533 }
534 break;
535 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700537
Adam Cohendb364c32014-05-20 14:23:40 -0700538 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 }
540
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800541 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700542 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 if (isWorkspaceLoading()) {
544 // process the result once the workspace has loaded.
545 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
546 return;
547 }
548 mPendingActivityResult = null;
549
Winson Chunge341d302013-08-16 14:31:00 -0700550 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700551 final PendingRequestArgs requestArgs = mPendingRequestArgs;
552 setWaitingForResult(null);
553 if (requestArgs == null) {
554 return;
555 }
556
557 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700558
Adam Cohenad4e15c2013-10-17 16:21:35 -0700559 Runnable exitSpringLoaded = new Runnable() {
560 @Override
561 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700562 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 }
564 };
565
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700567 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
570 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700571 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700572 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700573 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700575 addAppWidgetImpl(
576 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800577 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700579 }
580 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200581 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700582 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700583 // User could have free-scrolled between pages before picking a wallpaper; make sure
584 // we move to the closest one now.
585 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700586 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200587 }
588 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700589 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200590
Adam Cohened66b2b2012-01-23 17:28:51 -0800591 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700592 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700593
Adam Cohened66b2b2012-01-23 17:28:51 -0800594 // We have special handling for widgets
595 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800596 final int appWidgetId;
597 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
598 : -1;
599 if (widgetId < 0) {
600 appWidgetId = pendingAddWidgetId;
601 } else {
602 appWidgetId = widgetId;
603 }
604
Adam Cohenad4e15c2013-10-17 16:21:35 -0700605 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800606 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700607 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
608 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700609 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700610 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700611 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700612 @Override
613 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700614 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 }
616 };
Adam Cohendb364c32014-05-20 14:23:40 -0700617
Sunny Goyal2100c782016-08-22 16:00:03 -0700618 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
619 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
620 } else {
621 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
622 // When the screen id represents an actual screen (as opposed to a rank)
623 // we make sure that the drop page actually exists.
624 requestArgs.screenId =
625 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700626 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700627 final CellLayout dropLayout =
628 mWorkspace.getScreenWithId(requestArgs.screenId);
629
630 dropLayout.setDropPending(true);
631 final Runnable onComplete = new Runnable() {
632 @Override
633 public void run() {
634 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
635 dropLayout.setDropPending(false);
636 }
637 };
638 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
639 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700640 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800641 return;
642 }
643
Sunny Goyald478c832016-04-01 12:04:16 -0700644 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
645 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700646 if (resultCode == RESULT_OK) {
647 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700649 }
650 // Leave the widget in the pending state if the user canceled the configure.
651 return;
652 }
653
Sunny Goyalaad90582015-07-09 14:22:50 -0700654 if (requestCode == REQUEST_CREATE_SHORTCUT) {
655 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700656 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
657 completeAdd(requestCode, data, -1, requestArgs);
658 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
659 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
660
Sunny Goyalaad90582015-07-09 14:22:50 -0700661 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700662 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
663 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800667 }
668
669 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700670 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800671 final int requestCode, final int resultCode, final Intent data) {
672 handleActivityResult(requestCode, resultCode, data);
673 if (mLauncherCallbacks != null) {
674 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
675 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800676 }
677
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700678 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700679 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600680 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 PendingRequestArgs pendingArgs = mPendingRequestArgs;
682 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
683 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
684 setWaitingForResult(null);
685
Sunny Goyal28c6b962015-10-12 11:42:05 -0700686 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700690 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700691 Intent intent = pendingArgs.getPendingIntent();
692
Sunny Goyal28c6b962015-10-12 11:42:05 -0700693 if (grantResults.length > 0
694 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700695 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 } else {
697 // TODO: Show a snack bar with link to settings
698 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700699 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700700 }
701 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600702 if (mLauncherCallbacks != null) {
703 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
704 grantResults);
705 }
706 }
707
Adam Cohendb364c32014-05-20 14:23:40 -0700708 /**
709 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
710 *
711 * @param screenId the screen id to check
712 * @return the new screen, or screenId if it exists
713 */
714 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800715 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700716 if (dropLayout == null) {
717 // it's possible that the add screen was removed because it was
718 // empty and a re-bind occurred
719 mWorkspace.addExtraEmptyScreen();
720 return mWorkspace.commitExtraEmptyScreen();
721 } else {
722 return screenId;
723 }
724 }
725
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 @Thunk void completeTwoStageWidgetDrop(
727 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
728 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 Runnable onCompleteRunnable = null;
730 int animationType = 0;
731
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 if (resultCode == RESULT_OK) {
734 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
735 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800736 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700737 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 onCompleteRunnable = new Runnable() {
739 @Override
740 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700741 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700742 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
744 };
745 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700750 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700751 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
752 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 // The animated view may be null in the case of a rotation during widget configuration
755 onCompleteRunnable.run();
756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
758
759 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700760 protected void onStop() {
761 super.onStop();
762 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700763
764 if (mLauncherCallbacks != null) {
765 mLauncherCallbacks.onStop();
766 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700767
Sunny Goyalf5e37442016-11-02 10:31:24 -0700768 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700769 mAppWidgetHost.stopListening();
770 }
Tony Wickham010d2552017-01-20 08:15:28 -0800771
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700772 if (!mAppLaunchSuccess) {
773 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700774 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700775 }
Tony Wickham010d2552017-01-20 08:15:28 -0800776 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700777 }
778
779 @Override
780 protected void onStart() {
781 super.onStart();
782 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700783
784 if (mLauncherCallbacks != null) {
785 mLauncherCallbacks.onStart();
786 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700787
Sunny Goyalf5e37442016-11-02 10:31:24 -0700788 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700789 mAppWidgetHost.startListening();
790 }
Tony Wickham010d2552017-01-20 08:15:28 -0800791
792 if (!isWorkspaceLoading()) {
793 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
794 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700795
Jon Miranda7fda2852017-07-19 16:07:01 -0700796 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700797 if (mScrimAnimator != null) {
798 mScrimAnimator.cancel();
799 }
800 mDragLayer.getBackground().setAlpha(0);
801 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
802 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
803 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
804 @Override
805 public void onAnimationEnd(Animator animation) {
806 mScrimAnimator = null;
807 }
808 });
809 mScrimAnimator.setDuration(600);
810 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
811 mScrimAnimator.start();
812 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700813 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700814 }
815
816 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700818 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700820 TraceHelper.partitionSection("ON_RESUME", "superCall");
821
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700822 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700823 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800824 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700825 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700826 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700827 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700828 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700830 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200831 // We might have postponed some bind calls until onResume (see waitUntilResume) --
832 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700833 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
834 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200835 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700836 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200837 }
Winson Chunge4e50662012-01-23 14:45:13 -0800838
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700839 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700840 // Process any items that were added while Launcher was away.
841 InstallShortcutReceiver.disableAndFlushInstallQueue(
842 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700843
Sunny Goyala474a9b2017-05-04 16:47:11 -0700844 // Refresh shortcuts if the permission changed.
845 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700846
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700847 if (shouldShowDiscoveryBounce()) {
848 mAllAppsController.showDiscoveryBounce();
849 }
Adam Cohen9211d422014-10-07 18:14:53 -0700850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onResume();
852 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800853
Sunny Goyala502aa32017-10-02 16:04:06 -0700854 clearTypedText();
855
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700856 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700860 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700861 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700862 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700863
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700864 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700865 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800866 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700867 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700868
Adam Cohen9211d422014-10-07 18:14:53 -0700869 if (mLauncherCallbacks != null) {
870 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700871 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700872 }
873
Adam Cohenc2d6e892014-10-16 09:49:24 -0700874 public interface LauncherOverlay {
875
876 /**
877 * Touch interaction leading to overscroll has begun
878 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700879 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880
881 /**
882 * Touch interaction related to overscroll has ended
883 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700884 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885
886 /**
887 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
888 * screen (or in the case of RTL, the rightmost screen).
889 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700890 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700891
892 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800893 * Called when the launcher is ready to use the overlay
894 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700895 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700896 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700897 }
898
899 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700900 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700901 }
902
903 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
904
Sunny Goyalc86df472016-02-25 09:19:38 -0800905 public void onScrollChanged(float progress) {
906 if (mWorkspace != null) {
907 mWorkspace.onOverlayScrollChanged(progress);
908 }
909 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700910 }
911
Sunny Goyal16764582017-11-06 16:00:34 -0800912 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700913 if (mLauncherCallbacks != null) {
914 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700915 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800916 // On O and above we there is always some setting present settings (add icon to
917 // home screen or icon badging). On earlier APIs we will have the allow rotation
918 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700919 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700920 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800921 }
922
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700923 @Override
924 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700926 if (mModel.isCurrentCallbacks(this)) {
927 mModel.stopLoader();
928 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700929 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
930
Romain Guy13c2e7b2010-03-10 19:45:00 -0800931 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700932 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700933
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800934 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800935 @Override
936 public void onWindowFocusChanged(boolean hasFocus) {
937 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700938 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700939
940 if (mLauncherCallbacks != null) {
941 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
942 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800943 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 private boolean acceptFilter() {
946 final InputMethodManager inputManager = (InputMethodManager)
947 getSystemService(Context.INPUT_METHOD_SERVICE);
948 return !inputManager.isFullscreenMode();
949 }
950
951 @Override
952 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700953 final int uniChar = event.getUnicodeChar();
954 final boolean handled = super.onKeyDown(keyCode, event);
955 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
956 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
958 keyCode, event);
959 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700961 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 // showSearchDialog()
963 // If there are multiple keystrokes before the search dialog takes focus,
964 // onSearchRequested() will be called for every keystroke,
965 // but it is idempotent, so it's fine.
966 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968 }
969
Joe Onorato8a9625e2010-01-28 15:55:35 -0800970 // Eat the long press event so the keyboard doesn't come up.
971 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
972 return true;
973 }
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 return handled;
976 }
977
Karl Rosaen138a0412009-04-23 19:00:21 -0700978 private String getTypedText() {
979 return mDefaultKeySsb.toString();
980 }
981
Sunny Goyal6f28e712016-09-13 14:19:29 -0700982 @Override
983 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700984 mDefaultKeySsb.clear();
985 mDefaultKeySsb.clearSpans();
986 Selection.setSelection(mDefaultKeySsb, 0);
987 }
988
Sunny Goyalf9403d92017-10-18 10:55:56 -0700989 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700990 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 /**
994 * Restores the previous state, if it exists.
995 *
996 * @param savedState The previous state.
997 */
998 private void restoreState(Bundle savedState) {
999 if (savedState == null) {
1000 return;
1001 }
1002
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001003 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001004 LauncherState[] stateValues = LauncherState.values();
1005 LauncherState state = stateValues[stateOrdinal];
1006 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001007 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001008 }
1009
Sunny Goyal2100c782016-08-22 16:00:03 -07001010 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1011 if (requestArgs != null) {
1012 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001014
1015 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001016
1017 SparseArray<Parcelable> widgetsState =
1018 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1019 if (widgetsState != null) {
1020 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023
1024 /**
1025 * Finds all the views we need and configure them properly.
1026 */
1027 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001028 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001029 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001030 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001031 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001032 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001033
Sunny Goyal784f9c32016-03-23 16:56:54 -07001034 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1035 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1036 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
Winson Chung4c98d922011-05-31 16:50:48 -07001038 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001039 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001040
Winson Chung3d503fb2011-07-13 17:25:49 -07001041 // Setup the hotseat
1042 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1043 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001044 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 }
1046
Winson Chung4c98d922011-05-31 16:50:48 -07001047 // Setup the workspace
1048 mWorkspace.setHapticFeedbackEnabled(false);
1049 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001050 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001051 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1052 // default state, otherwise we will update to the wrong offsets in RTL
1053 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001054 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001055 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001056
Tony Wickham34d2c912015-09-11 09:27:58 -07001057 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001058 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001059
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001060 // Setup Apps
1061 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001062
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001064 mDragController.setMoveTarget(mWorkspace);
1065 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001066 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001067
Hyunyoung Songd725f642017-08-17 22:26:35 -07001068 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 }
1070
1071 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001072 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001073 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001074 */
1075 public void setAllAppsButton(View allAppsButton) {
1076 mAllAppsButton = allAppsButton;
1077 }
1078
Anjali Koppal5ad44842014-03-10 20:34:39 -07001079 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 * Creates a view representing a shortcut.
1081 *
1082 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001084 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001085 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 }
1087
1088 /**
1089 * Creates a view representing a shortcut inflated from the specified resource.
1090 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 * @param parent The group the shortcut belongs to.
1092 * @param info The data structure describing the shortcut.
1093 *
1094 * @return A View inflated from layoutResId.
1095 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001096 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001097 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1098 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001099 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001101 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 return favorite;
1103 }
1104
1105 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001106 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 *
1108 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001110 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001111 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001112 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1113 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001114 return;
1115 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001116
Jon Mirandac476d6e2017-05-04 16:30:01 -07001117 int[] cellXY = mTmpAddItemCellCoordinates;
1118 CellLayout layout = getCellLayout(container, screenId);
1119
Sunny Goyal782f0c92017-01-19 10:27:54 -08001120 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001121 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001122 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1123 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001124 }
1125
1126 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001127 // Legacy shortcuts are only supported for primary profile.
1128 info = Process.myUserHandle().equals(args.user)
1129 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001130
Sunny Goyalb57645f2017-03-20 13:57:28 -07001131 if (info == null) {
1132 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1133 return;
1134 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001135 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001136 // The app is trying to add a shortcut without sufficient permissions
1137 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1138 return;
1139 }
1140 }
1141
Jon Mirandac476d6e2017-05-04 16:30:01 -07001142 if (container < 0) {
1143 // Adding a shortcut to the Workspace.
1144 final View view = createShortcut(info);
1145 boolean foundCellSpan = false;
1146 // First we check if we already know the exact location where we want to add this item.
1147 if (cellX >= 0 && cellY >= 0) {
1148 cellXY[0] = cellX;
1149 cellXY[1] = cellY;
1150 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001151
Jon Mirandac476d6e2017-05-04 16:30:01 -07001152 // If appropriate, either create a folder or add to an existing folder
1153 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001154 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001155 return;
1156 }
1157 DragObject dragObject = new DragObject();
1158 dragObject.dragInfo = info;
1159 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1160 true)) {
1161 return;
1162 }
1163 } else {
1164 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1165 }
1166
1167 if (!foundCellSpan) {
1168 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001169 return;
1170 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001171
1172 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1173 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001174 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001175 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001176 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001177 if (folderIcon != null) {
1178 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1179 folderInfo.add(info, args.rank, false);
1180 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001181 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001182 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
1185
Sunny Goyale29897f52017-07-20 10:09:42 -07001186 public FolderIcon findFolderIcon(final long folderIconId) {
1187 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1188 @Override
1189 public boolean evaluate(ItemInfo info, View view) {
1190 return info != null && info.id == folderIconId;
1191 }
1192 });
1193 }
1194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001196 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001198 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001200 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001201 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1202
Adam Cohened66b2b2012-01-23 17:28:51 -08001203 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001204 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001205 }
Romain Guycbb89e42009-06-08 15:52:54 -07001206
Adam Cohen59400422014-03-05 18:07:04 -08001207 LauncherAppWidgetInfo launcherInfo;
1208 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001209 launcherInfo.spanX = itemInfo.spanX;
1210 launcherInfo.spanY = itemInfo.spanY;
1211 launcherInfo.minSpanX = itemInfo.minSpanX;
1212 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001213 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001214
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001215 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001216 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217
Sunny Goyal2100c782016-08-22 16:00:03 -07001218 if (hostView == null) {
1219 // Perform actual inflation because we're live
1220 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001222 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301223 prepareAppWidget(hostView, launcherInfo);
1224 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
Romain Guycbb89e42009-06-08 15:52:54 -07001226
Sunny Goyald5462aa2016-11-22 16:52:39 +05301227 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001228 hostView.setTag(item);
1229 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001230 hostView.setFocusable(true);
1231 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001232 }
1233
Adam Cohended9f8d2010-11-03 13:25:16 -07001234 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1235 @Override
1236 public void onReceive(Context context, Intent intent) {
1237 final String action = intent.getAction();
1238 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001239 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001240 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001241 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001242 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001243 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001244 mShouldFadeInScrim = true;
1245 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1246 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1247 // the user unlocked and the Launcher is not in the foreground.
1248 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001249 }
1250 }
1251 };
1252
Tony Wickham010d2552017-01-20 08:15:28 -08001253 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1254 Runnable r = new Runnable() {
1255 @Override
1256 public void run() {
1257 mWorkspace.updateIconBadges(updatedBadges);
1258 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001259
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001260 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1261 if (popup instanceof PopupContainerWithArrow) {
1262 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001263 }
Tony Wickham010d2552017-01-20 08:15:28 -08001264 }
1265 };
1266 if (!waitUntilResume(r)) {
1267 r.run();
1268 }
1269 }
1270
Adam Cohended9f8d2010-11-03 13:25:16 -07001271 @Override
1272 public void onAttachedToWindow() {
1273 super.onAttachedToWindow();
1274
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001275 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001276 if (mLauncherCallbacks != null) {
1277 mLauncherCallbacks.onAttachedToWindow();
1278 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001279 }
1280
1281 @Override
1282 public void onDetachedFromWindow() {
1283 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001284
1285 if (mLauncherCallbacks != null) {
1286 mLauncherCallbacks.onDetachedFromWindow();
1287 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 }
1289
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001290 public AllAppsTransitionController getAllAppsController() {
1291 return mAllAppsController;
1292 }
1293
Winson Chunga6945242014-01-08 14:04:34 -08001294 public DragLayer getDragLayer() {
1295 return mDragLayer;
1296 }
1297
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001298 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001299 return mAppsView;
1300 }
1301
Winson Chunga6945242014-01-08 14:04:34 -08001302 public Workspace getWorkspace() {
1303 return mWorkspace;
1304 }
1305
1306 public Hotseat getHotseat() {
1307 return mHotseat;
1308 }
1309
Jorim Jaggid017f882014-01-14 17:08:48 -08001310 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001311 return mOverviewPanel;
1312 }
1313
Sunny Goyal47328fd2016-05-25 18:56:41 -07001314 public DropTargetBar getDropTargetBar() {
1315 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001316 }
1317
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001318 public LauncherAppWidgetHost getAppWidgetHost() {
1319 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
Romain Guycbb89e42009-06-08 15:52:54 -07001321
Winson Chunga9abd0e2010-10-27 17:18:37 -07001322 public LauncherModel getModel() {
1323 return mModel;
1324 }
1325
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001326 public ModelWriter getModelWriter() {
1327 return mModelWriter;
1328 }
1329
Adam Cohen79d90c52016-04-22 13:29:20 -07001330 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001331 return mSharedPrefs;
1332 }
1333
Jon Miranda6bed3502017-09-19 14:43:17 -07001334 public int getOrientation() { return mOrientation; }
1335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 @Override
1337 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001338 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 super.onNewIntent(intent);
1340
Winson15f8b172015-08-19 11:02:39 -07001341 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1342 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1343 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001344
1345 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001346 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001347 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001348
Winson15f8b172015-08-19 11:02:39 -07001349 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1350 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001351 if (mWorkspace == null) {
1352 // Can be cases where mWorkspace is null, this prevents a NPE
1353 return;
1354 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001355
1356 // Note: There should be at most one log per method call. This is enforced implicitly
1357 // by using if-else statements.
1358 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001359 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001360 if (topOpenView != null) {
1361 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001362 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001363 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001364 target.pageIndex = mWorkspace.getCurrentPage();
1365 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001366 }
1367
1368 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001369 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001370 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001371
1372 final View v = getWindow().peekDecorView();
1373 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001374 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001375 }
1376
Winson Chungb745afb2015-03-02 11:51:23 -08001377 // Reset the apps view
1378 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001379 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001380 }
1381
Adam Cohen9211d422014-10-07 18:14:53 -07001382 if (mLauncherCallbacks != null) {
1383 mLauncherCallbacks.onHomeIntent();
1384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001386 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001387
Adam Cohen9211d422014-10-07 18:14:53 -07001388 if (mLauncherCallbacks != null) {
1389 mLauncherCallbacks.onNewIntent(intent);
1390 }
Winson15f8b172015-08-19 11:02:39 -07001391
1392 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1393 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1394 // animation.
1395 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001396 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001397
Winson15f8b172015-08-19 11:02:39 -07001398 mWorkspace.post(new Runnable() {
1399 @Override
1400 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001401 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001402 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001403 }
Winson15f8b172015-08-19 11:02:39 -07001404 }
1405 });
1406 }
1407 }
1408
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001409 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001410 }
1411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001413 public void onRestoreInstanceState(Bundle state) {
1414 super.onRestoreInstanceState(state);
1415 for (int page: mSynchronouslyBoundPages) {
1416 mWorkspace.restoreInstanceStateForChild(page);
1417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
1420 @Override
1421 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001422 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001423 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001424
Adam Cohen21cd0022013-10-09 18:57:02 -07001425 }
Sunny Goyalea609262017-10-25 15:47:38 -07001426 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001427
1428
1429 AbstractFloatingView widgets = AbstractFloatingView
1430 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1431 if (widgets != null) {
1432 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1433 widgets.saveHierarchyState(widgetsState);
1434 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1435 } else {
1436 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1437 }
1438
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001439 // We close any open folders and shortcut containers since they will not be re-opened,
1440 // and we need to make sure this state is reflected.
1441 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001442
Sunny Goyal2100c782016-08-22 16:00:03 -07001443 if (mPendingRequestArgs != null) {
1444 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1445 }
1446 if (mPendingActivityResult != null) {
1447 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
1449
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001450 super.onSaveInstanceState(outState);
1451
Adam Cohen9211d422014-10-07 18:14:53 -07001452 if (mLauncherCallbacks != null) {
1453 mLauncherCallbacks.onSaveInstanceState(outState);
1454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
1456
1457 @Override
1458 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001460
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001461 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001462 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001463
Winson Chungcd2b0142011-06-08 16:02:26 -07001464 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001465 // It's possible to receive onDestroy after a new Launcher activity has
1466 // been created. In this case, don't interfere with the new Launcher.
1467 if (mModel.isCurrentCallbacks(this)) {
1468 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001469 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001470 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001471
Sunny Goyal745bad92016-05-02 10:54:12 -07001472 if (mRotationPrefChangeHandler != null) {
1473 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1474 }
1475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001477 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001479 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001481 mAppWidgetHost = null;
1482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001484 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1485
Michael Jurka2ecf9952012-06-18 12:52:28 -07001486 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001487
Tony2917a8b2017-07-31 23:29:42 -07001488 clearPendingBinds();
1489
Adam Cohen9211d422014-10-07 18:14:53 -07001490 if (mLauncherCallbacks != null) {
1491 mLauncherCallbacks.onDestroy();
1492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494
Sunny Goyalae502842016-06-17 08:43:56 -07001495 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1496 return mAccessibilityDelegate;
1497 }
1498
Adam Cohena9cf38f2011-05-02 15:36:58 -07001499 public DragController getDragController() {
1500 return mDragController;
1501 }
1502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001504 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001505 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001506 }
1507
1508 @Override
1509 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1510 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001511 try {
1512 super.startIntentSenderForResult(intent, requestCode,
1513 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1514 } catch (IntentSender.SendIntentException e) {
1515 throw new ActivityNotFoundException();
1516 }
1517 }
1518
Winson Chung70d72102011-08-12 11:24:35 -07001519 /**
1520 * Indicates that we want global search for this activity by setting the globalSearch
1521 * argument for {@link #startSearch} to true.
1522 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001524 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001526
Karl Rosaen138a0412009-04-23 19:00:21 -07001527 if (initialQuery == null) {
1528 // Use any text typed in the launcher as the initial query
1529 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 if (appSearchData == null) {
1532 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001533 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001535
Sunny Goyal6f28e712016-09-13 14:19:29 -07001536 if (mLauncherCallbacks == null ||
1537 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1538 // Starting search from the callbacks failed. Start the default global search.
1539 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001540 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001541
1542 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001543 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001544 }
1545
Ian Parkinson047036e2014-09-01 15:40:53 +01001546 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001547 * Starts the global search activity. This code is a copied from SearchManager
1548 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001549 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001550 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001551 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001552 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1553 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1554 if (globalSearchActivity == null) {
1555 Log.w(TAG, "No global search activity found.");
1556 return;
1557 }
1558 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1559 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1560 intent.setComponent(globalSearchActivity);
1561 // Make sure that we have a Bundle to put source in
1562 if (appSearchData == null) {
1563 appSearchData = new Bundle();
1564 } else {
1565 appSearchData = new Bundle(appSearchData);
1566 }
Adam Cohen9211d422014-10-07 18:14:53 -07001567 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001568 if (!appSearchData.containsKey("source")) {
1569 appSearchData.putString("source", getPackageName());
1570 }
1571 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1572 if (!TextUtils.isEmpty(initialQuery)) {
1573 intent.putExtra(SearchManager.QUERY, initialQuery);
1574 }
1575 if (selectInitialQuery) {
1576 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1577 }
1578 intent.setSourceBounds(sourceBounds);
1579 try {
1580 startActivity(intent);
1581 } catch (ActivityNotFoundException ex) {
1582 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585
Winson Chung70d72102011-08-12 11:24:35 -07001586 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001587 public boolean onPrepareOptionsMenu(Menu menu) {
1588 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001589 if (mLauncherCallbacks != null) {
1590 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1591 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001592 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001595 @Override
1596 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001597 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001598 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001599 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001600 }
1601
Joe Onorato9c1289c2009-08-17 11:03:03 -04001602 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001603 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001604 }
1605
Adam Cohen517a7f52014-03-01 12:12:59 -08001606 public boolean isWorkspaceLoading() {
1607 return mWorkspaceLoading;
1608 }
1609
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001610 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001611 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001612 }
1613
Sunny Goyal04a324a2017-01-20 21:08:59 -08001614 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001615 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001616 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001617
Sunny Goyal2100c782016-08-22 16:00:03 -07001618 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001619 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001620 if (LOGD) {
1621 Log.d(TAG, "Adding widget from drop");
1622 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001623 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001624 }
1625
Sunny Goyal2100c782016-08-22 16:00:03 -07001626 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001627 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1628 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1629 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001630
Adam Cohenad4e15c2013-10-17 16:21:35 -07001631 Runnable onComplete = new Runnable() {
1632 @Override
1633 public void run() {
1634 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001635 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001636 }
1637 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001638 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001639 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
1641 }
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001643 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1644 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001645 info.container = container;
1646 info.screenId = screenId;
1647 if (cell != null) {
1648 info.cellX = cell[0];
1649 info.cellY = cell[1];
1650 }
1651 info.spanX = spanX;
1652 info.spanY = spanY;
1653
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001654 switch (info.itemType) {
1655 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1656 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001657 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001658 break;
1659 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001660 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001661 break;
1662 default:
1663 throw new IllegalStateException("Unknown item type: " + info.itemType);
1664 }
1665 }
1666
Adam Cohenfbba09b2011-07-18 21:43:05 -07001667 /**
1668 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001669 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001670 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001671 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1672 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001673 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1674 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1675 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001676 }
1677
Adam Cohenfbba09b2011-07-18 21:43:05 -07001678 /**
1679 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001680 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001681 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001682 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001683 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001684 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001685 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001686 // In the case where we've prebound the widget, we remove it from the DragLayer
1687 if (LOGD) {
1688 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1689 }
1690 getDragLayer().removeView(hostView);
1691
Adam Cohened66b2b2012-01-23 17:28:51 -08001692 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001693 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001694
1695 // Clear the boundWidget so that it doesn't get destroyed.
1696 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001697 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001698 // In this case, we either need to start an activity to get permission to bind
1699 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001700 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1701 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1702 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1703 this, info.componentName);
1704 } else {
1705 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1706 }
Adam Cohendd70d662012-10-04 16:53:44 -07001707 Bundle options = info.bindOptions;
1708
Sunny Goyalffe83f12014-08-14 17:39:34 -07001709 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1710 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001711 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001712 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001713 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001714 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001715 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001716 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001717 }
1718
Adam Cohendcd297f2013-06-18 13:13:40 -07001719 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001720 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001721 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 folderInfo.title = getText(R.string.folder_name);
1723
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001725 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726
1727 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001728 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301729 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001730 // Force measure the new folder icon
1731 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1732 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001733 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
Romain Guycbb89e42009-06-08 15:52:54 -07001735
Winsonc0b52fe2015-09-09 16:38:15 -07001736 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001737 * Unbinds the view for the specified item, and removes the item and all its children.
1738 *
1739 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001740 * @param itemInfo the {@link ItemInfo} for this view.
1741 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001742 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001743 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001744 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001745 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001746 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1747 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001748 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001749 } else {
1750 mWorkspace.removeWorkspaceItem(v);
1751 }
Winsonc0b52fe2015-09-09 16:38:15 -07001752 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001753 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001754 }
1755 } else if (itemInfo instanceof FolderInfo) {
1756 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001757 if (v instanceof FolderIcon) {
1758 ((FolderIcon) v).removeListeners();
1759 }
Winsonc0b52fe2015-09-09 16:38:15 -07001760 mWorkspace.removeWorkspaceItem(v);
1761 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001762 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001763 }
1764 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1765 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001766 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001767 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001768 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001769 }
1770 } else {
1771 return false;
1772 }
1773 return true;
1774 }
1775
1776 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001777 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001778 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001779 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001780 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001781 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001782 // Deleting an app widget ID is a void call but writes to disk before returning
1783 // to the caller...
1784 new AsyncTask<Void, Void, Void>() {
1785 public Void doInBackground(Void ... args) {
1786 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1787 return null;
1788 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001789 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001790 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001791 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001792 }
1793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 @Override
1795 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001796 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798
Joe Onorato88ec0992009-11-19 13:16:06 -08001799 @Override
1800 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001801 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1802 return;
1803 }
1804
Sunny Goyal45478022015-06-08 16:52:41 -07001805 if (mDragController.isDragging()) {
1806 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001807 return;
1808 }
1809
Jon Mirandafeba90f2016-10-06 10:53:29 -07001810 // Note: There should be at most one log per method call. This is enforced implicitly
1811 // by using if-else statements.
1812 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001813 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1814 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001815 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001816 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001817 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001818 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001819 } else {
1820 // Back button is a no-op here, but give at least some feedback for the button press
1821 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001822 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001823 }
1824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 * Launches the intent referred by the clicked shortcut.
1827 *
1828 * @param v The view representing the clicked shortcut.
1829 */
1830 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001831 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1832 // view has detached (it's possible for this to happen if the view is removed mid touch).
1833 if (v.getWindowToken() == null) {
1834 return;
1835 }
1836
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001837 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001838 return;
1839 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001840
Adam Cohen1697b792013-09-17 19:08:21 -07001841 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001842 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001843 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1844 LauncherLogProto.Action.Direction.NONE,
1845 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001846 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001847 }
1848 return;
1849 }
1850
1851 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001852 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001853 int page = mWorkspace.indexOfChild(v);
1854 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1855 LauncherLogProto.Action.Direction.NONE,
1856 LauncherLogProto.ContainerType.OVERVIEW, page);
1857 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001858 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001859 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001860 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001861 }
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001864 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001865 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001867 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001868 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001869 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001870 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001871 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001872 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001873 } else if (tag instanceof AppInfo) {
1874 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001875 } else if (tag instanceof LauncherAppWidgetInfo) {
1876 if (v instanceof PendingAppWidgetHostView) {
1877 onClickPendingWidget((PendingAppWidgetHostView) v);
1878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880 }
1881
Sunny Goyal70660032015-05-14 00:07:08 -07001882 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001883 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001884 return false;
1885 }
1886
Michael Jurkaaf442092010-06-10 17:01:57 -07001887 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001888 * Event handler for the app widget view which has not fully restored.
1889 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001890 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001891 if (mIsSafeModeEnabled) {
1892 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1893 return;
1894 }
1895
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001896 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001897 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001898 LauncherAppWidgetProviderInfo appWidgetInfo =
1899 mAppWidgetManager.findProvider(info.providerName, info.user);
1900 if (appWidgetInfo == null) {
1901 return;
1902 }
1903 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1904
Sunny Goyald478c832016-04-01 12:04:16 -07001905 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1906 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1907 // This should not happen, as we make sure that an Id is allocated during bind.
1908 return;
1909 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001910 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1911 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001912 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001913 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001914 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001915 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001916 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001917 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001918 }
1919 }
1920
1921 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001922 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1923 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1924 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001925 *
1926 * @param v The view that was clicked.
1927 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001928 protected void onClickAllAppsButton(View v) {
1929 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001930 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301931 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1932 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001933 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001934 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001935 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001936 }
1937 }
1938
Sunny Goyale6e72002017-01-12 16:55:36 -08001939 private void onClickPendingAppItem(final View v, final String packageName,
1940 boolean downloadStarted) {
1941 if (downloadStarted) {
1942 // If the download has started, simply direct to the market app.
1943 startMarketIntentForPackage(v, packageName);
1944 return;
1945 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001946 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001947 .setTitle(R.string.abandoned_promises_title)
1948 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001949 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1950 @Override
1951 public void onClick(DialogInterface dialogInterface, int i) {
1952 startMarketIntentForPackage(v, packageName);
1953 }
1954 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001955 .setNeutralButton(R.string.abandoned_clean_this,
1956 new DialogInterface.OnClickListener() {
1957 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001958 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001959 mWorkspace.removeAbandonedPromise(packageName, user);
1960 }
1961 })
1962 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001963 }
1964
1965 private void startMarketIntentForPackage(View v, String packageName) {
1966 ItemInfo item = (ItemInfo) v.getTag();
1967 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001968 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001969 }
1970
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001971 /**
1972 * Event handler for an app shortcut click.
1973 *
1974 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1975 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001976 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001977 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1978 Object tag = v.getTag();
1979 if (!(tag instanceof ShortcutInfo)) {
1980 throw new IllegalArgumentException("Input must be a Shortcut");
1981 }
1982
1983 // Open shortcut
1984 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001985
Sunny Goyal076839c2017-10-30 13:52:20 -07001986 if (shortcut.isDisabled()) {
1987 if ((shortcut.runtimeStatusFlags &
1988 ~FLAG_DISABLED_SUSPENDED &
1989 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001990 // If the app is only disabled because of the above flags, launch activity anyway.
1991 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001992 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001993 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1994 // Use a message specific to this shortcut, if it has one.
1995 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1996 return;
1997 }
1998 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001999 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07002000 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002001 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07002002 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2003 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002004 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002005 }
2006 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2007 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002008 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002009 }
2010
Chris Wren40c5ed32014-06-24 18:24:23 -04002011 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002012 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002013 String packageName = shortcut.intent.getComponent() != null ?
2014 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2015 if (!TextUtils.isEmpty(packageName)) {
2016 onClickPendingAppItem(v, packageName,
2017 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2018 return;
2019 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002020 }
2021
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002022 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002023 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002024 }
2025
Sunny Goyala7ce1662016-05-31 15:01:35 -07002026 private void startAppShortcutOrInfoActivity(View v) {
2027 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002028 Intent intent;
2029 if (item instanceof PromiseAppInfo) {
2030 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2031 intent = promiseAppInfo.getMarketIntent();
2032 } else {
2033 intent = item.getIntent();
2034 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002035 if (intent == null) {
2036 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002037 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002038 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002039 }
2040
2041 /**
2042 * Event handler for a folder icon click.
2043 *
2044 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2045 */
2046 protected void onClickFolderIcon(View v) {
2047 if (LOGD) Log.d(TAG, "onClickFolder");
2048 if (!(v instanceof FolderIcon)){
2049 throw new IllegalArgumentException("Input must be a FolderIcon");
2050 }
2051
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002052 Folder folder = ((FolderIcon) v).getFolder();
2053 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002054 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002055 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002056 }
Michael Jurka5130e402011-10-13 04:55:35 -07002057 }
2058
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002059 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002060 * Event handler for the wallpaper picker button that appears after a long press
2061 * on the home screen.
2062 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002063 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002064 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002065 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2066 return;
2067 }
2068
Tony Wickham785f7a52015-08-31 17:28:32 -07002069 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2070 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002071 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002072 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002073 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002074
2075 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002076 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2077 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002078 intent.setPackage(pickerPackage);
2079 }
2080
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002081 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002082 try {
2083 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2084 // If there is no target package, use the default intent chooser animation
2085 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2086 } catch (ActivityNotFoundException e) {
2087 setWaitingForResult(null);
2088 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2089 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002090 }
2091
Sunny Goyala7ce1662016-05-31 15:01:35 -07002092 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002094 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2095 try {
2096 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2097 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2098 // is enabled by default on NYC.
2099 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2100 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002101
2102 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2103 String id = ((ShortcutInfo) info).getDeepShortcutId();
2104 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302105 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002106 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002107 } else {
2108 // Could be launching some bookkeeping activity
2109 startActivity(intent, optsBundle);
2110 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002111 } finally {
2112 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002115 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2116 // corresponding permission. Show the appropriate permission prompt if that
2117 // is the case.
2118 if (intent.getComponent() == null
2119 && Intent.ACTION_CALL.equals(intent.getAction())
2120 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2121 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002122
2123 setWaitingForResult(PendingRequestArgs
2124 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002125 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2126 REQUEST_PERMISSION_CALL_PHONE);
2127 } else {
2128 // No idea why this was thrown.
2129 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 }
2132 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002133
Sunny Goyalfe770c92016-09-27 14:38:58 -07002134 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002135 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002136 if (Utilities.ATLEAST_MARSHMALLOW) {
2137 int left = 0, top = 0;
2138 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002139 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002140 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002141 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002142 if (icon != null) {
2143 Rect bounds = icon.getBounds();
2144 left = (width - bounds.width()) / 2;
2145 top = v.getPaddingTop();
2146 width = bounds.width();
2147 height = bounds.height();
2148 }
2149 }
2150 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2151 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2152 // On L devices, we use the device default slide-up transition.
2153 // On L MR1 devices, we use a custom version of the slide-up transition which
2154 // doesn't have the delay present in the device default.
2155 return ActivityOptions.makeCustomAnimation(
2156 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2157 }
2158 return null;
2159 }
2160
Tonye3c59252017-05-02 21:32:27 -07002161 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002162 int[] pos = new int[2];
2163 v.getLocationOnScreen(pos);
2164 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2165 }
2166
Sunny Goyala7ce1662016-05-31 15:01:35 -07002167 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002168 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002169 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2170 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002171 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002172 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002173 // Only launch using the new animation if the shortcut has not opted out (this is a
2174 // private contract between launcher and may be ignored in the future).
2175 boolean useLaunchAnimation = (v != null) &&
2176 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2177 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2178
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002179 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002180
2181 // Prepare intent
2182 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2183 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002184 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002185 }
2186 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002187 if (Utilities.ATLEAST_MARSHMALLOW
2188 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002189 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002190 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002191 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002192 // Shortcuts need some special checks due to legacy reasons.
2193 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002194 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002195 // Could be launching some bookkeeping activity
2196 startActivity(intent, optsBundle);
2197 } else {
2198 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2199 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2200 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002201
2202 if (v instanceof BubbleTextView) {
2203 // This is set to the view that launched the activity that navigated the user away
2204 // from launcher. Since there is no callback for when the activity has finished
2205 // launching, enable the press state and keep this reference to reset the press
2206 // state when we return to launcher.
2207 BubbleTextView btv = (BubbleTextView) v;
2208 btv.setStayPressed(true);
2209 setOnResumeCallback(btv);
2210 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002211 mAppLaunchSuccess = true;
2212 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002213 } catch (ActivityNotFoundException|SecurityException e) {
2214 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2215 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2216 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002217 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002218 }
2219
Tony Wickhamdadb3042016-02-24 11:07:00 -08002220 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002221 public boolean dispatchTouchEvent(MotionEvent ev) {
2222 mLastDispatchTouchEventX = ev.getX();
2223 return super.dispatchTouchEvent(ev);
2224 }
2225
2226 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002228 if (!isDraggingEnabled()) return false;
2229 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002230 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231
Jon Miranda51f037d2016-11-07 08:37:20 -08002232 boolean ignoreLongPressToOverview =
2233 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002234
Adam Cohen1697b792013-09-17 19:08:21 -07002235 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002236 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002237 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302238 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2239 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002240 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002241 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002242 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2243 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2244 return true;
2245 } else {
2246 return false;
2247 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002248 } else {
2249 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002250 }
Adam Cohen1697b792013-09-17 19:08:21 -07002251 }
2252
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002253 CellLayout.CellInfo longClickCellInfo = null;
2254 View itemUnderLongClick = null;
2255 if (v.getTag() instanceof ItemInfo) {
2256 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002257 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002258 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002259 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 }
2261
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 // The hotseat touch handling does not go through Workspace, and we always allow long press
2263 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002264 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002266 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002267 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002268 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302269 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2270 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002271 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002272 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002273 return false;
2274 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302275 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2276 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002277 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002278 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002279 }
Jon Miranda379198e2016-09-23 08:54:40 -07002280 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2281 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002283 final boolean isAllAppsButton =
2284 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2285 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2286 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002287 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002289 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 }
2291 }
2292 }
2293 return true;
2294 }
2295
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002297 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 return mHotseat != null && layout != null &&
2299 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2300 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002301
Winson Chung3d503fb2011-07-13 17:25:49 -07002302 /**
2303 * Returns the CellLayout of the specified container at the specified screen.
2304 */
Sunny Goyal92820592015-03-02 11:31:35 -08002305 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002306 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2307 if (mHotseat != null) {
2308 return mHotseat.getLayout();
2309 } else {
2310 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002311 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002312 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002313 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002314 }
2315 }
2316
Michael Jurkae326f182011-11-21 14:05:46 -08002317 @Override
2318 public void onTrimMemory(int level) {
2319 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002320 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2321 // The widget preview db can result in holding onto over
2322 // 3MB of memory for caching which isn't necessary.
2323 SQLiteDatabase.releaseMemory();
2324
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002325 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002326 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002327 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002328 if (mLauncherCallbacks != null) {
2329 mLauncherCallbacks.onTrimMemory(level);
2330 }
Michael Jurkae326f182011-11-21 14:05:46 -08002331 }
2332
Michael Jurkad7c28052012-04-27 15:43:36 -07002333 @Override
2334 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002335 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002336 final List<CharSequence> text = event.getText();
2337 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002338 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002339 // TODO: When can workspace be null?
2340 text.add(mWorkspace == null
2341 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002342 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002343 return result;
2344 }
2345
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002346 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002347 * If the activity is currently paused, signal that we need to run the passed Runnable
2348 * in onResume.
2349 *
2350 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002351 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2352 * wrong when we're not running, and if the activity comes back to what the configuration was
2353 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002354 *
2355 * Implementation of the method from LauncherModel.Callbacks.
2356 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002357 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002358 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002359 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002360 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002361 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002362 if (run instanceof RunnableWithId) {
2363 // Remove any runnables which have the same id
2364 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002365 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002366 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002367 return true;
2368 } else {
2369 return false;
2370 }
2371 }
2372
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002373 public void setOnResumeCallback(OnResumeCallback callback) {
2374 if (mOnResumeCallback != null) {
2375 mOnResumeCallback.onLauncherResume();
2376 }
2377 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002378 }
2379
Michael Jurka7607c2f2013-04-03 14:33:19 -07002380 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002381 * If the activity is currently paused, signal that we need to re-run the loader
2382 * in onResume.
2383 *
2384 * This needs to be called from incoming places where resources might have been loaded
2385 * while we are paused. That is becaues the Configuration might be wrong
2386 * when we're not running, and if it comes back to what it was when we
2387 * were paused, we are not restarted.
2388 *
2389 * Implementation of the method from LauncherModel.Callbacks.
2390 *
2391 * @return true if we are currently paused. The caller might be able to
2392 * skip some work in that case since we will come back again.
2393 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002394 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002395 public boolean setLoadOnResume() {
2396 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002397 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002398 mOnResumeNeedsLoad = true;
2399 return true;
2400 } else {
2401 return false;
2402 }
2403 }
2404
2405 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002406 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002408 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002409 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002410 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002411 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002412 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002413 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002416
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002418 * Clear any pending bind callbacks. This is called when is loader is planning to
2419 * perform a full rebind from scratch.
2420 */
2421 @Override
2422 public void clearPendingBinds() {
2423 mBindOnResumeCallbacks.clear();
2424 if (mPendingExecutor != null) {
2425 mPendingExecutor.markCompleted();
2426 mPendingExecutor = null;
2427 }
2428 }
2429
2430 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002431 * Refreshes the shortcuts shown on the workspace.
2432 *
2433 * Implementation of the method from LauncherModel.Callbacks.
2434 */
2435 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002436 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002437 // Floating panels (except the full widget sheet) are associated with individual icons. If
2438 // we are starting a fresh bind, close all such panels as all the icons are about
2439 // to go away.
2440 AbstractFloatingView.closeOpenViews(this, true,
2441 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002442
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002443 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002444
Winson Chungd64d1762013-08-20 14:37:16 -07002445 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002446 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002447 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002448
Winson Chung3d503fb2011-07-13 17:25:49 -07002449 if (mHotseat != null) {
2450 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002452 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 }
2454
Adam Cohendcd297f2013-06-18 13:13:40 -07002455 @Override
2456 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002457 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002458 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2459 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002460 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2461 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002462 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002463 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2464 // If there are no screens, we need to have an empty screen
2465 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002466 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002467 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002468
Winsonc7d2e832016-07-28 12:24:55 -07002469 // After we have added all the screens, if the wallpaper was locked to the default state,
2470 // then notify to indicate that it can be released and a proper wallpaper offset can be
2471 // computed before the next layout
2472 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002473 }
2474
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002475 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002476 int count = orderedScreenIds.size();
2477 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002478 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002479 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002480 // No need to bind the first screen, as its always bound.
2481 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2482 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002483 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002484 }
2485
Sunny Goyalb23980c2017-08-17 07:45:25 -07002486 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002487 public void bindAppsAdded(final ArrayList<Long> newScreens,
2488 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002489 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002490 Runnable r = new Runnable() {
2491 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002492 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002493 }
2494 };
2495 if (waitUntilResume(r)) {
2496 return;
2497 }
2498
Winson Chungd64d1762013-08-20 14:37:16 -07002499 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002500 if (newScreens != null) {
2501 bindAddScreens(newScreens);
2502 }
Winson Chungd64d1762013-08-20 14:37:16 -07002503
2504 // We add the items without animation on non-visible pages, and with
2505 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002506 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002507 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002508 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002509 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002510 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002511 }
Winson Chungc58497e2013-09-03 17:48:37 -07002512
Winson Chung87412982013-10-03 18:34:14 -07002513 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002514 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002515 }
2516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 * Bind the items start-end from the list.
2519 *
2520 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002522 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002523 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002524 Runnable r = new Runnable() {
2525 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002526 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002527 }
2528 };
2529 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002530 return;
2531 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002532
Sunny Goyal3be633b2016-12-08 09:59:25 -08002533 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002534 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002535 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002536 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002537 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002538 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002539 int end = items.size();
2540 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002541 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002542
2543 // Short circuit if we are loading dock items for a configuration which has no dock
2544 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2545 mHotseat == null) {
2546 continue;
2547 }
2548
Sunny Goyal639e9062015-08-19 19:17:06 -07002549 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002550 switch (item.itemType) {
2551 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2552 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002553 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002554 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002555 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002556 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002557 }
2558 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002559 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002560 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002561 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002562 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002563 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002564 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2565 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002566 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002567 if (view == null) {
2568 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002569 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002570 break;
2571 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002572 default:
2573 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002574 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002575
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002576 /*
2577 * Remove colliding items.
2578 */
2579 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2580 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2581 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2582 View v = cl.getChildAt(item.cellX, item.cellY);
2583 Object tag = v.getTag();
2584 String desc = "Collision while binding workspace item: " + item
2585 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002586 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002587 throw (new RuntimeException(desc));
2588 } else {
2589 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002590 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002591 continue;
2592 }
2593 }
2594 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302595 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002596 if (animateIcons) {
2597 // Animate all the applications up now
2598 view.setAlpha(0f);
2599 view.setScaleX(0f);
2600 view.setScaleY(0f);
2601 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002602 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002605
Winson Chung997a9232013-07-24 15:33:46 -07002606 if (animateIcons) {
2607 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002608 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002609 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002610 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002611 final Runnable startBounceAnimRunnable = new Runnable() {
2612 public void run() {
2613 anim.playTogether(bounceAnims);
2614 anim.start();
2615 }
2616 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002617 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002618 // We post the animation slightly delayed to prevent slowdowns
2619 // when we are loading right after we return to launcher.
2620 mWorkspace.postDelayed(new Runnable() {
2621 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002622 if (mWorkspace != null) {
2623 mWorkspace.snapToPage(newScreenIndex);
2624 mWorkspace.postDelayed(startBounceAnimRunnable,
2625 NEW_APPS_ANIMATION_DELAY);
2626 }
Winson Chung94d67682013-09-25 16:29:40 -07002627 }
2628 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002629 } else {
2630 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002631 }
2632 }
Winson Chung64359a52013-07-08 17:17:08 -07002633 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002634 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 }
2636
Joe Onorato9c1289c2009-08-17 11:03:03 -04002637 /**
2638 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002639 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002640 public void bindAppWidget(LauncherAppWidgetInfo item) {
2641 View view = inflateAppWidget(item);
2642 if (view != null) {
2643 mWorkspace.addInScreen(view, item);
2644 mWorkspace.requestLayout();
2645 }
2646 }
2647
2648 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002649 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302650 PendingAppWidgetHostView view =
2651 new PendingAppWidgetHostView(this, item, mIconCache, true);
2652 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002653 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002654 }
2655
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002656 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002657
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002658 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002659
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002660 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2661 // If the provider is not ready, bind as a pending widget.
2662 appWidgetInfo = null;
2663 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2664 // The widget id is not valid. Try to find the widget based on the provider info.
2665 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2666 } else {
2667 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2668 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002669
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002670 // If the provider is ready, but the width is not yet restored, try to restore it.
2671 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2672 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002673 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002674 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2675 + " belongs to component " + item.providerName
2676 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002677 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002678 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002679 }
Sunny Goyalff572272014-07-23 13:58:07 -07002680
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002681 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002682 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002683 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2684 // Id has not been allocated yet. Allocate a new id.
2685 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2686 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002687
Sunny Goyald478c832016-04-01 12:04:16 -07002688 // Also try to bind the widget. If the bind fails, the user will be shown
2689 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002690 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002691 pendingInfo.spanX = item.spanX;
2692 pendingInfo.spanY = item.spanY;
2693 pendingInfo.minSpanX = item.minSpanX;
2694 pendingInfo.minSpanY = item.minSpanY;
2695 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002696
2697 boolean isDirectConfig =
2698 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2699 if (isDirectConfig && item.bindOptions != null) {
2700 Bundle newOptions = item.bindOptions.getExtras();
2701 if (options != null) {
2702 newOptions.putAll(options);
2703 }
2704 options = newOptions;
2705 }
Sunny Goyald478c832016-04-01 12:04:16 -07002706 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2707 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002708
Sunny Goyal86df1382016-08-10 15:03:22 -07002709 // We tried to bind once. If we were not able to bind, we would need to
2710 // go through the permission dialog, which means we cannot skip the config
2711 // activity.
2712 item.bindOptions = null;
2713 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2714
Sunny Goyald478c832016-04-01 12:04:16 -07002715 // Bind succeeded
2716 if (success) {
2717 // If the widget has a configure activity, it is still needs to set it up,
2718 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002719 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002720 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2721 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002722 }
Sunny Goyald478c832016-04-01 12:04:16 -07002723
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002724 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002725 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002726 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002727 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002728 // The widget was marked as UI not ready, but there is no configure activity to
2729 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002730 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002731 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002732 }
Sunny Goyalff572272014-07-23 13:58:07 -07002733 }
2734
Sunny Goyald5462aa2016-11-22 16:52:39 +05302735 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002736 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002737 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002738 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002739 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002740 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002741 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002742 }
2743
Sunny Goyal233ee962015-08-03 13:05:01 -07002744 item.minSpanX = appWidgetInfo.minSpanX;
2745 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302746 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002747 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302748 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002749 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302750 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002751
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002752 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002753 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002754 }
2755
Sunny Goyalff572272014-07-23 13:58:07 -07002756 /**
2757 * Restores a pending widget.
2758 *
2759 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002760 */
Sunny Goyald478c832016-04-01 12:04:16 -07002761 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002762 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2763 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2764 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002765 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002766 }
2767
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002768 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002769 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002770 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2771 info.pendingItemInfo = null;
2772 }
Sunny Goyalff572272014-07-23 13:58:07 -07002773
Sunny Goyalba47faa2017-10-04 16:50:57 -07002774 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2775 view.reinflate();
2776 }
2777
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002778 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002779 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002780 }
2781
Adam Cohen1462de32012-07-24 22:34:36 -07002782 public void onPageBoundSynchronously(int page) {
2783 mSynchronouslyBoundPages.add(page);
2784 }
2785
Sunny Goyal527c7d32015-08-28 15:19:36 -07002786 @Override
2787 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2788 if (mPendingExecutor != null) {
2789 mPendingExecutor.markCompleted();
2790 }
2791 mPendingExecutor = executor;
2792 executor.attachTo(this);
2793 }
2794
2795 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2796 if (mPendingExecutor == executor) {
2797 mPendingExecutor = null;
2798 }
2799 }
2800
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002801 @Override
2802 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2803 Runnable r = new Runnable() {
2804 public void run() {
2805 finishFirstPageBind(executor);
2806 }
2807 };
2808 if (waitUntilResume(r)) {
2809 return;
2810 }
2811
2812 Runnable onComplete = new Runnable() {
2813 @Override
2814 public void run() {
2815 if (executor != null) {
2816 executor.onLoadAnimationCompleted();
2817 }
2818 }
2819 };
2820 if (mDragLayer.getAlpha() < 1) {
2821 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2822 } else {
2823 onComplete.run();
2824 }
2825 }
2826
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827 /**
2828 * Callback saying that there aren't any more items to bind.
2829 *
2830 * Implementation of the method from LauncherModel.Callbacks.
2831 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002832 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002833 Runnable r = new Runnable() {
2834 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002835 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002836 }
2837 };
2838 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002839 return;
2840 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002841 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002842 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002844 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002845
Sunny Goyal2100c782016-08-22 16:00:03 -07002846 if (mPendingActivityResult != null) {
2847 handleActivityResult(mPendingActivityResult.requestCode,
2848 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2849 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002850 }
2851
Sunny Goyala474a9b2017-05-04 16:47:11 -07002852 InstallShortcutReceiver.disableAndFlushInstallQueue(
2853 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002854
Tony Wickham010d2552017-01-20 08:15:28 -08002855 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002856 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002857 }
2858
Winson Chunga2413752012-04-03 14:22:34 -07002859 private boolean canRunNewAppsAnimation() {
2860 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002861 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002862 }
2863
Winson Chungc9168342013-06-26 14:54:55 -07002864 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002865 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002866 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2867 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002868 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002869 return bounceAnim;
2870 }
2871
Adam Cohen27772732013-11-11 14:00:56 +00002872 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002873 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002874 }
2875
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002876 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 * Add the icons for all apps.
2878 *
2879 * Implementation of the method from LauncherModel.Callbacks.
2880 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002881 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002882 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2883 public void run() {
2884 bindAllApplications(apps);
2885 }
2886 };
2887 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002888 return;
2889 }
2890
Winson Chungb745afb2015-03-02 11:51:23 -08002891 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002892 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002893 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002894 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002895 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002896 return;
2897 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002898
Winson Chungb745afb2015-03-02 11:51:23 -08002899 mAppsView.setApps(apps);
2900 }
Adam Cohen9211d422014-10-07 18:14:53 -07002901 if (mLauncherCallbacks != null) {
2902 mLauncherCallbacks.bindAllApplications(apps);
2903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 }
2905
2906 /**
Tony2917a8b2017-07-31 23:29:42 -07002907 * Returns an Executor that will run after the launcher is first drawn (including after the
2908 * initial fade in animation). Returns null if the first draw has already occurred.
2909 */
2910 public @Nullable Executor getPendingExecutor() {
2911 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2912 }
2913
2914 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002915 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2916 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2917 */
2918 @Override
2919 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002920 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002921 }
2922
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002923 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 * A package was updated.
2925 *
2926 * Implementation of the method from LauncherModel.Callbacks.
2927 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002928 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002929 Runnable r = new Runnable() {
2930 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002931 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002932 }
2933 };
2934 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002935 return;
2936 }
2937
Winson Chungb745afb2015-03-02 11:51:23 -08002938 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002939 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002941 }
2942
Sunny Goyal4390ace2014-10-13 11:33:11 -07002943 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002944 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2945 Runnable r = new Runnable() {
2946 public void run() {
2947 bindPromiseAppProgressUpdated(app);
2948 }
2949 };
2950 if (waitUntilResume(r)) {
2951 return;
2952 }
2953
2954 if (mAppsView != null) {
2955 mAppsView.updatePromiseAppProgress(app);
2956 }
2957 }
2958
2959 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002960 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2961 Runnable r = new Runnable() {
2962 public void run() {
2963 bindWidgetsRestored(widgets);
2964 }
2965 };
2966 if (waitUntilResume(r)) {
2967 return;
2968 }
2969 mWorkspace.widgetsRestored(widgets);
2970 }
2971
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002973 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002974 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002975 *
2976 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002977 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002978 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002979 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002980 Runnable r = new Runnable() {
2981 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002982 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002983 }
2984 };
2985 if (waitUntilResume(r)) {
2986 return;
2987 }
2988
Sunny Goyal4390ace2014-10-13 11:33:11 -07002989 if (!updated.isEmpty()) {
2990 mWorkspace.updateShortcuts(updated);
2991 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002992 }
2993
2994 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002995 * Update the state of a package, typically related to install state.
2996 *
2997 * Implementation of the method from LauncherModel.Callbacks.
2998 */
Sunny Goyale755d462014-07-22 13:48:29 -07002999 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003000 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3001 Runnable r = new Runnable() {
3002 public void run() {
3003 bindRestoreItemsChange(updates);
3004 }
3005 };
3006 if (waitUntilResume(r)) {
3007 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003008 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003009
Sunny Goyal756adbc2015-04-16 15:20:51 -07003010 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003011 }
3012
3013 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003014 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003015 * in addition to specific applications to remove, the reason being that
3016 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003017 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003018 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003020 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003021 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003022 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003023 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003024 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003025 }
Winson Chungd64d1762013-08-20 14:37:16 -07003026 };
3027 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003028 return;
3029 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003030 mWorkspace.removeItemsByMatcher(matcher);
3031 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003032 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003033
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003034 @Override
3035 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3036 Runnable r = new Runnable() {
3037 public void run() {
3038 bindAppInfosRemoved(appInfos);
3039 }
3040 };
3041 if (waitUntilResume(r)) {
3042 return;
Joe Onorato36115782010-06-17 13:28:48 -04003043 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044
Winson Chungdf95eb12013-10-16 14:57:07 -07003045 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003046 if (mAppsView != null) {
3047 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 }
Joe Onoratobe386092009-11-17 17:32:16 -08003050
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003051 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003052 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3053 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003054 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3055 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003056 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003057 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003058 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003059 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003060 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003061 return;
3062 }
3063
Tony Wickham26b17462017-03-20 17:12:24 -07003064 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3065 if (topView != null) {
3066 topView.onWidgetsBound();
3067 }
3068 }
3069
Tony Wickham86222d22017-03-29 15:30:43 -07003070 /**
3071 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3072 * refreshes the widgets and shortcuts associated with the given package/user
3073 */
3074 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003075 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003076 }
3077
Sunny Goyalc99cb172017-10-19 16:15:09 -07003078 public boolean isRotationEnabled () {
3079 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003080 }
3081
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003082 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003083 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003084 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003085 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003086 }
3087
Winson Chung80baf5a2010-08-09 16:03:15 -07003088 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003089 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003090 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003091 @Override
3092 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3093 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003094
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003095 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3096 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003097 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003098 writer.println(prefix + " Homescreen " + i);
3099
3100 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3101 for (int j = 0; j < layout.getChildCount(); j++) {
3102 Object tag = layout.getChildAt(j).getTag();
3103 if (tag != null) {
3104 writer.println(prefix + " " + tag.toString());
3105 }
3106 }
3107 }
3108
3109 writer.println(prefix + " Hotseat");
3110 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003111 for (int j = 0; j < layout.getChildCount(); j++) {
3112 Object tag = layout.getChildAt(j).getTag();
3113 if (tag != null) {
3114 writer.println(prefix + " " + tag.toString());
3115 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003116 }
Sunny Goyala1365452015-10-01 15:46:24 -07003117
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003118 try {
3119 FileLog.flushAll(writer);
3120 } catch (Exception e) {
3121 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003122 }
3123 }
3124
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003125 writer.println(prefix + "Misc:");
3126 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3127 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3128 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3129
3130 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003131
Adam Cohen9211d422014-10-07 18:14:53 -07003132 if (mLauncherCallbacks != null) {
3133 mLauncherCallbacks.dump(prefix, fd, writer, args);
3134 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003135 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003136
Sunny Goyal66b24572016-09-21 15:57:55 -07003137 @Override
3138 @TargetApi(Build.VERSION_CODES.N)
3139 public void onProvideKeyboardShortcuts(
3140 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3141
3142 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003143 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003144 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3145 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3146 }
3147 View currentFocus = getCurrentFocus();
3148 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3149 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3150 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3151 }
Tony18c4aa42017-05-10 21:23:57 -05003152 if (currentFocus.getTag() instanceof ItemInfo
3153 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003154 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3155 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3156 }
3157 if (!shortcutInfos.isEmpty()) {
3158 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3159 }
3160
3161 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3162 }
3163
3164 @Override
3165 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3166 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3167 switch (keyCode) {
3168 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003169 if (isInState(NORMAL)) {
3170 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003171 return true;
3172 }
3173 break;
3174 case KeyEvent.KEYCODE_S: {
3175 View focusedView = getCurrentFocus();
3176 if (focusedView instanceof BubbleTextView
3177 && focusedView.getTag() instanceof ItemInfo
3178 && mAccessibilityDelegate.performAction(focusedView,
3179 (ItemInfo) focusedView.getTag(),
3180 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003181 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003182 return true;
3183 }
3184 break;
3185 }
3186 case KeyEvent.KEYCODE_O:
3187 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3188 return true;
3189 }
3190 break;
3191 }
3192 }
3193 return super.onKeyShortcut(keyCode, event);
3194 }
3195
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003196 public static Launcher getLauncher(Context context) {
3197 if (context instanceof Launcher) {
3198 return (Launcher) context;
3199 }
3200 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3201 }
3202
Sunny Goyal5a81c382017-03-20 15:08:06 -07003203 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003204
3205 @Override
3206 public void onSharedPreferenceChanged(
3207 SharedPreferences sharedPreferences, String key) {
3208 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003209 // Recreate the activity so that it initializes the rotation preference again.
3210 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003211 }
3212 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003213 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003214
3215 /**
3216 * Callback for listening for onResume
3217 */
3218 public interface OnResumeCallback {
3219
3220 void onLauncherResume();
3221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222}