blob: 44660ecc10269aacfd6352a81530a0b1ad8376aa [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;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080045import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070046import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080048import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080049import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070051import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040052import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070054import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070055import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070056import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070057import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070059import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070060import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070063import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020064import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070066import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080067import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070068import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070070import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070074import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070078import android.view.KeyboardShortcutGroup;
79import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080080import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal651077b2014-06-30 14:15:31 -070090import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070091import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070092import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070093import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080096import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070099import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700101import com.android.launcher3.dragndrop.DragController;
102import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700103import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700105import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000106import com.android.launcher3.folder.Folder;
107import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700108import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700109import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700110import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800114import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700116import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800117import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800118import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800119import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530120import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
121import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700122import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700124import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700125import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700126import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700127import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800128import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700129import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700130import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700131import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700132import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700133import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700134import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700135import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700136import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800137import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800139import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700140import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700141import com.android.launcher3.widget.WidgetListRowEntry;
142import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700143import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700144
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700147import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700148import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700149import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700150import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800151import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700152import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154/**
155 * Default launcher application.
156 */
Sunny Goyal27835952017-01-13 12:15:53 -0800157public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700158 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
159 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700170 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
Winson Chung2672ff92012-05-04 16:22:30 -0700186 // The Intent extra that defines whether to ignore the launch animation
187 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400188 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
196 // Type: ActivityResultInfo
197 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700198 // Type: SparseArray<Parcelable>
199 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700201 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700203 private boolean mIsSafeModeEnabled;
204
Adam Cohenad4e15c2013-10-17 16:21:35 -0700205 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Winson Chunga2413752012-04-03 14:22:34 -0700207 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700208 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
209 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
210 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700211
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700213 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800215 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700216
Sunny Goyalffe83f12014-08-14 17:39:34 -0700217 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700218 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700219
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700220 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700221
Sunny Goyal316490e2015-06-02 09:38:28 -0700222 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700223
Sunny Goyal47328fd2016-05-25 18:56:41 -0700224 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700225
226 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700227 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700228 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700229
Winson Chung8ae41982017-11-10 11:42:13 -0800230 // UI and state for the overview panel
231 private ViewGroup mOverviewPanel;
232
Jon Miranda6bed3502017-09-19 14:43:17 -0700233 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
234 // that results in widgets being inflated in the wrong orientation.
235 private int mOrientation;
236
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400238
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800239 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700240 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700242 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700243 private OnResumeCallback mOnResumeCallback;
244
Sunny Goyal527c7d32015-08-28 15:19:36 -0700245 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700246
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800248 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700250 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Jon Miranda2d89ea82017-05-04 11:47:53 -0700252 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700253 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700254
Tony Wickham010d2552017-01-20 08:15:28 -0800255 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700256
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800257 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700258
Winson Chung46353de2012-02-16 14:05:10 -0800259 // We only want to get the SharedPreferences once since it does an FS stat each time we get
260 // it from the context.
261 private SharedPreferences mSharedPrefs;
262
Sunny Goyal2100c782016-08-22 16:00:03 -0700263 // Activity result which needs to be processed after workspace has loaded.
264 private ActivityResultInfo mPendingActivityResult;
265 /**
266 * Holds extra information required to handle a result from an external call, like
267 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
268 */
269 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800270
Jon Miranda379198e2016-09-23 08:54:40 -0700271 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700272
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700273 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700274 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700275 private boolean mAppLaunchSuccess;
276
Sunny Goyal745bad92016-05-02 10:54:12 -0700277 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 @Override
280 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700281 if (DEBUG_STRICT_MODE) {
282 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
283 .detectDiskReads()
284 .detectDiskWrites()
285 .detectNetwork() // or .detectAll() for all detectable problems
286 .penaltyLog()
287 .build());
288 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
289 .detectLeakedSqlLiteObjects()
290 .detectLeakedClosableObjects()
291 .penaltyLog()
292 .penaltyDeath()
293 .build());
294 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700295 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700296
Adam Cohen9211d422014-10-07 18:14:53 -0700297 if (mLauncherCallbacks != null) {
298 mLauncherCallbacks.preOnCreate();
299 }
300
Mario Bertschler27288382017-05-24 15:35:09 -0700301 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
302 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700303 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700306 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400307
Sunny Goyal87f784c2017-01-11 10:48:34 -0800308 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700309
Adam Cohen2e6da152015-05-06 11:42:25 -0700310 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800311 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700312 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700313 Display display = getWindowManager().getDefaultDisplay();
314 Point mwSize = new Point();
315 display.getSize(mwSize);
316 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
317 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700318
Jon Miranda6bed3502017-09-19 14:43:17 -0700319 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700320 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700321 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800322 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800323 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800324 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700325 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700326
Joe Onorato41a12d22009-10-31 18:30:00 -0400327 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700328 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800329 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700330
Sunny Goyalffe83f12014-08-14 17:39:34 -0700331 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700332
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700333 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700334 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700335
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700336 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
337 // this also ensures that any synchronous binding below doesn't re-trigger another
338 // LauncherModel load.
339 mPaused = false;
340
Sunny Goyal60820d72017-05-09 12:40:11 -0700341 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 setupViews();
Winson1f064272016-07-18 17:18:02 -0700344 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345
Tony Wickham010d2552017-01-20 08:15:28 -0800346 mPopupDataProvider = new PopupDataProvider(this);
347
Sunny Goyald3864fa2017-11-14 15:06:36 -0800348 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
349 // In case we are on a device with locked rotation, we should look at preferences to check
350 // if the user has specifically allowed rotation.
351 if (!mRotationEnabled) {
352 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
353 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
354 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356
Sunny Goyald3864fa2017-11-14 15:06:36 -0800357 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
358 if (internalStateHandled) {
359 // Temporarily enable the rotation
360 mRotationEnabled = true;
361
362 if (savedInstanceState != null) {
363 // InternalStateHandler has already set the appropriate state.
364 // We dont need to do anything.
365 savedInstanceState.remove(RUNTIME_STATE);
366 }
367 }
368 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800369
Sunny Goyal2100c782016-08-22 16:00:03 -0700370 // We only load the page synchronously if the user rotates (or triggers a
371 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700372 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
373 if (savedInstanceState != null) {
374 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
375 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800376
Sunny Goyalfe770c92016-09-27 14:38:58 -0700377 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800378 if (!internalStateHandled) {
379 // If we are not binding synchronously, show a fade in animation when
380 // the first page bind completes.
381 mDragLayer.setAlpha(0);
382 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700383 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700384 // Pages bound synchronously.
385 mWorkspace.setCurrentPage(currentScreen);
386
Sunny Goyal2100c782016-08-22 16:00:03 -0700387 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
389
390 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800391 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800392
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700393 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
394 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700395 setRequestedOrientation(mRotationEnabled
396 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700397
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800398 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700399
Jon Miranda7fda2852017-07-19 16:07:01 -0700400 // Listen for broadcasts
401 IntentFilter filter = new IntentFilter();
402 filter.addAction(Intent.ACTION_SCREEN_OFF);
403 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
404 registerReceiver(mReceiver, filter);
405 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700406
Sunny Goyal8392c822017-06-20 10:03:56 -0700407 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
408 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700409
410 if (mLauncherCallbacks != null) {
411 mLauncherCallbacks.onCreate(savedInstanceState);
412 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700413
414 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700415 }
416
Sunny Goyal7779d622015-06-11 16:18:39 -0700417 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700418 public void onThemeChanged() {
419 recreate();
420 }
421
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700422 public LauncherStateManager getStateManager() {
423 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700424 }
425
Mario Bertschlera6936942017-05-31 14:48:19 -0700426 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700427 if (isDark) {
428 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700429 } else if (supportsDarkText) {
430 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700431 }
432 }
433
434 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700435 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800436 return mLauncherView.findViewById(id);
437 }
438
439 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700440 public void onAppWidgetHostReset() {
441 if (mAppWidgetHost != null) {
442 mAppWidgetHost.startListening();
443 }
444 }
445
Adam Cohen9211d422014-10-07 18:14:53 -0700446 private LauncherCallbacks mLauncherCallbacks;
447
Winson1f064272016-07-18 17:18:02 -0700448 public void onInsetsChanged(Rect insets) {
449 mDeviceProfile.updateInsets(insets);
450 mDeviceProfile.layout(this, true /* notifyListeners */);
451 }
452
Sunny Goyal32554d12015-12-03 15:31:25 -0800453 /**
454 * Call this after onCreate to set or clear overlay.
455 */
456 public void setLauncherOverlay(LauncherOverlay overlay) {
457 if (overlay != null) {
458 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
459 }
460 mWorkspace.setLauncherOverlay(overlay);
461 }
462
Adam Cohen9211d422014-10-07 18:14:53 -0700463 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
464 mLauncherCallbacks = callbacks;
465 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700466 }
467
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700468 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700469 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700470 if (mLauncherCallbacks != null) {
471 mLauncherCallbacks.onLauncherProviderChange();
472 }
473 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700474
Hyunyoung Song3f471442015-04-08 19:01:34 -0700475 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700476 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
477 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700478 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700479 }
480
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000481 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700482 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
483 // This cast is safe as long as the id < 0x00FFFFFF
484 // Since we jail all the dynamically generated views, there should be no clashes
485 // with any other views.
486 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000487 }
488
Tony Wickham010d2552017-01-20 08:15:28 -0800489 public PopupDataProvider getPopupDataProvider() {
490 return mPopupDataProvider;
491 }
492
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000493 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700494 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
495 * a configuration step, this allows the proper animations to run after other transitions.
496 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 private long completeAdd(
498 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
499 long screenId = info.screenId;
500 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700501 // When the screen id represents an actual screen (as opposed to a rank) we make sure
502 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700504 }
Adam Cohendb364c32014-05-20 14:23:40 -0700505
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700508 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800510 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700512 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700513 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700515 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700516 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 int widgetId = appWidgetId;
518 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700519 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700521 // Since the view was just bound, also launch the configure activity if needed
522 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
523 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800524 if (provider != null) {
525 new WidgetAddFlowHandler(provider)
526 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700527 }
528 }
529 break;
530 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700532
Adam Cohendb364c32014-05-20 14:23:40 -0700533 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 }
535
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800536 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700537 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700538 if (isWorkspaceLoading()) {
539 // process the result once the workspace has loaded.
540 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
541 return;
542 }
543 mPendingActivityResult = null;
544
Winson Chunge341d302013-08-16 14:31:00 -0700545 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 final PendingRequestArgs requestArgs = mPendingRequestArgs;
547 setWaitingForResult(null);
548 if (requestArgs == null) {
549 return;
550 }
551
552 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700553
Adam Cohenad4e15c2013-10-17 16:21:35 -0700554 Runnable exitSpringLoaded = new Runnable() {
555 @Override
556 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700557 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 }
559 };
560
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700562 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700563 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
565 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700567 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700568 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 addAppWidgetImpl(
571 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800572 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 }
575 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200576 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700577 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700578 // User could have free-scrolled between pages before picking a wallpaper; make sure
579 // we move to the closest one now.
580 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700581 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582 }
583 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700587 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700588
Adam Cohened66b2b2012-01-23 17:28:51 -0800589 // We have special handling for widgets
590 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800591 final int appWidgetId;
592 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
593 : -1;
594 if (widgetId < 0) {
595 appWidgetId = pendingAddWidgetId;
596 } else {
597 appWidgetId = widgetId;
598 }
599
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800601 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700602 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
603 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700605 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700606 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 @Override
608 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700609 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 }
611 };
Adam Cohendb364c32014-05-20 14:23:40 -0700612
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
614 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
615 } else {
616 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
617 // When the screen id represents an actual screen (as opposed to a rank)
618 // we make sure that the drop page actually exists.
619 requestArgs.screenId =
620 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700621 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 final CellLayout dropLayout =
623 mWorkspace.getScreenWithId(requestArgs.screenId);
624
625 dropLayout.setDropPending(true);
626 final Runnable onComplete = new Runnable() {
627 @Override
628 public void run() {
629 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
630 dropLayout.setDropPending(false);
631 }
632 };
633 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700635 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 return;
637 }
638
Sunny Goyald478c832016-04-01 12:04:16 -0700639 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
640 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700641 if (resultCode == RESULT_OK) {
642 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700644 }
645 // Leave the widget in the pending state if the user canceled the configure.
646 return;
647 }
648
Sunny Goyalaad90582015-07-09 14:22:50 -0700649 if (requestCode == REQUEST_CREATE_SHORTCUT) {
650 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
652 completeAdd(requestCode, data, -1, requestArgs);
653 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
654 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
655
Sunny Goyalaad90582015-07-09 14:22:50 -0700656 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700657 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
658 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800662 }
663
664 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700665 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800666 final int requestCode, final int resultCode, final Intent data) {
667 handleActivityResult(requestCode, resultCode, data);
668 if (mLauncherCallbacks != null) {
669 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 }
672
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700673 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700674 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600675 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 PendingRequestArgs pendingArgs = mPendingRequestArgs;
677 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
678 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
679 setWaitingForResult(null);
680
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700683 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 Intent intent = pendingArgs.getPendingIntent();
687
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 if (grantResults.length > 0
689 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700690 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 } else {
692 // TODO: Show a snack bar with link to settings
693 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700694 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 }
696 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600697 if (mLauncherCallbacks != null) {
698 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
699 grantResults);
700 }
701 }
702
Adam Cohendb364c32014-05-20 14:23:40 -0700703 /**
704 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
705 *
706 * @param screenId the screen id to check
707 * @return the new screen, or screenId if it exists
708 */
709 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800710 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700711 if (dropLayout == null) {
712 // it's possible that the add screen was removed because it was
713 // empty and a re-bind occurred
714 mWorkspace.addExtraEmptyScreen();
715 return mWorkspace.commitExtraEmptyScreen();
716 } else {
717 return screenId;
718 }
719 }
720
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 @Thunk void completeTwoStageWidgetDrop(
722 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
723 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 Runnable onCompleteRunnable = null;
725 int animationType = 0;
726
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700727 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 if (resultCode == RESULT_OK) {
729 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
730 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800731 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 onCompleteRunnable = new Runnable() {
734 @Override
735 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700737 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 }
739 };
740 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800741 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700745 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
747 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 // The animated view may be null in the case of a rotation during widget configuration
750 onCompleteRunnable.run();
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700755 protected void onStop() {
756 super.onStop();
757 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700758
759 if (mLauncherCallbacks != null) {
760 mLauncherCallbacks.onStop();
761 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800762 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800763
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700764 if (!mAppLaunchSuccess) {
765 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700766 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700767 }
Tony Wickham010d2552017-01-20 08:15:28 -0800768 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700769 }
770
771 @Override
772 protected void onStart() {
773 super.onStart();
774 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700775
776 if (mLauncherCallbacks != null) {
777 mLauncherCallbacks.onStart();
778 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800779 mAppWidgetHost.setListenIfResumed(true);
Tony Wickham010d2552017-01-20 08:15:28 -0800780
781 if (!isWorkspaceLoading()) {
782 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
783 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700784
Jon Miranda7fda2852017-07-19 16:07:01 -0700785 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700786 if (mScrimAnimator != null) {
787 mScrimAnimator.cancel();
788 }
789 mDragLayer.getBackground().setAlpha(0);
790 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
791 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
792 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
793 @Override
794 public void onAnimationEnd(Animator animation) {
795 mScrimAnimator = null;
796 }
797 });
798 mScrimAnimator.setDuration(600);
799 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
800 mScrimAnimator.start();
801 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700802 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700803 }
804
805 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700807 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700809 TraceHelper.partitionSection("ON_RESUME", "superCall");
810
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700811 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700812 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800813 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700814 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700815 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700816 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700817 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700819 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200820 // We might have postponed some bind calls until onResume (see waitUntilResume) --
821 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700822 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
823 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200824 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700825 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200826 }
Winson Chunge4e50662012-01-23 14:45:13 -0800827
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700828 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700829 // Process any items that were added while Launcher was away.
830 InstallShortcutReceiver.disableAndFlushInstallQueue(
831 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700832
Sunny Goyala474a9b2017-05-04 16:47:11 -0700833 // Refresh shortcuts if the permission changed.
834 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700835
Sunny Goyal7ede6112017-12-05 15:11:21 -0800836 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700837 if (mLauncherCallbacks != null) {
838 mLauncherCallbacks.onResume();
839 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800840
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700841 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700842 }
843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700846 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700847 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700848
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700850 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800851 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700852 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700853
Adam Cohen9211d422014-10-07 18:14:53 -0700854 if (mLauncherCallbacks != null) {
855 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700856 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700857 }
858
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859 public interface LauncherOverlay {
860
861 /**
862 * Touch interaction leading to overscroll has begun
863 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700864 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700865
866 /**
867 * Touch interaction related to overscroll has ended
868 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700869 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700870
871 /**
872 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
873 * screen (or in the case of RTL, the rightmost screen).
874 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700875 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700876
877 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800878 * Called when the launcher is ready to use the overlay
879 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700881 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700882 }
883
884 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700885 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700886 }
887
888 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
889
Sunny Goyalc86df472016-02-25 09:19:38 -0800890 public void onScrollChanged(float progress) {
891 if (mWorkspace != null) {
892 mWorkspace.onOverlayScrollChanged(progress);
893 }
894 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700895 }
896
Sunny Goyal16764582017-11-06 16:00:34 -0800897 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700898 if (mLauncherCallbacks != null) {
899 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700900 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800901 // On O and above we there is always some setting present settings (add icon to
902 // home screen or icon badging). On earlier APIs we will have the allow rotation
903 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700904 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700905 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800906 }
907
Sunny Goyalf9403d92017-10-18 10:55:56 -0700908 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700909 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700910 }
911
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 /**
913 * Restores the previous state, if it exists.
914 *
915 * @param savedState The previous state.
916 */
917 private void restoreState(Bundle savedState) {
918 if (savedState == null) {
919 return;
920 }
921
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700922 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700923 LauncherState[] stateValues = LauncherState.values();
924 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800925 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700926 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800927 }
928
Sunny Goyal2100c782016-08-22 16:00:03 -0700929 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
930 if (requestArgs != null) {
931 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700933
934 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700935
936 SparseArray<Parcelable> widgetsState =
937 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
938 if (widgetsState != null) {
939 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942
943 /**
944 * Finds all the views we need and configure them properly.
945 */
946 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -0700947 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700948 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700949 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700950 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800951 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700952
Sunny Goyal784f9c32016-03-23 16:56:54 -0700953 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
954 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
955 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956
Winson Chung4c98d922011-05-31 16:50:48 -0700957 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800958 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700959
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 // Setup the hotseat
961 mHotseat = (Hotseat) findViewById(R.id.hotseat);
962 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700963 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 }
965
Winson Chung4c98d922011-05-31 16:50:48 -0700966 // Setup the workspace
967 mWorkspace.setHapticFeedbackEnabled(false);
968 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700969 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700970 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
971 // default state, otherwise we will update to the wrong offsets in RTL
972 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700973 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700974 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700975
Tony Wickham34d2c912015-09-11 09:27:58 -0700976 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700977 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700978
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700979 // Setup Apps
980 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700981
Winson Chung3d503fb2011-07-13 17:25:49 -0700982 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700983 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700984 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400985
Hyunyoung Songd725f642017-08-17 22:26:35 -0700986 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988
989 /**
990 * Creates a view representing a shortcut.
991 *
992 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800994 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700995 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
998 /**
999 * Creates a view representing a shortcut inflated from the specified resource.
1000 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 * @param parent The group the shortcut belongs to.
1002 * @param info The data structure describing the shortcut.
1003 *
1004 * @return A View inflated from layoutResId.
1005 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001006 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001007 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1008 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001009 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001011 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 return favorite;
1013 }
1014
1015 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001016 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 *
1018 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001020 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001021 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001022 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1023 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001024 return;
1025 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001026
Jon Mirandac476d6e2017-05-04 16:30:01 -07001027 int[] cellXY = mTmpAddItemCellCoordinates;
1028 CellLayout layout = getCellLayout(container, screenId);
1029
Sunny Goyal782f0c92017-01-19 10:27:54 -08001030 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001031 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001032 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1033 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001034 }
1035
1036 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001037 // Legacy shortcuts are only supported for primary profile.
1038 info = Process.myUserHandle().equals(args.user)
1039 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001040
Sunny Goyalb57645f2017-03-20 13:57:28 -07001041 if (info == null) {
1042 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1043 return;
1044 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001045 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001046 // The app is trying to add a shortcut without sufficient permissions
1047 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1048 return;
1049 }
1050 }
1051
Jon Mirandac476d6e2017-05-04 16:30:01 -07001052 if (container < 0) {
1053 // Adding a shortcut to the Workspace.
1054 final View view = createShortcut(info);
1055 boolean foundCellSpan = false;
1056 // First we check if we already know the exact location where we want to add this item.
1057 if (cellX >= 0 && cellY >= 0) {
1058 cellXY[0] = cellX;
1059 cellXY[1] = cellY;
1060 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001061
Jon Mirandac476d6e2017-05-04 16:30:01 -07001062 // If appropriate, either create a folder or add to an existing folder
1063 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001064 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001065 return;
1066 }
1067 DragObject dragObject = new DragObject();
1068 dragObject.dragInfo = info;
1069 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1070 true)) {
1071 return;
1072 }
1073 } else {
1074 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1075 }
1076
1077 if (!foundCellSpan) {
1078 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001079 return;
1080 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001081
1082 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1083 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001084 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001085 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001086 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001087 if (folderIcon != null) {
1088 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1089 folderInfo.add(info, args.rank, false);
1090 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001091 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001092 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095
Sunny Goyale29897f2017-07-20 10:09:42 -07001096 public FolderIcon findFolderIcon(final long folderIconId) {
1097 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1098 @Override
1099 public boolean evaluate(ItemInfo info, View view) {
1100 return info != null && info.id == folderIconId;
1101 }
1102 });
1103 }
1104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001106 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001108 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001110 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001111 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1112
Adam Cohened66b2b2012-01-23 17:28:51 -08001113 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001114 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001115 }
Romain Guycbb89e42009-06-08 15:52:54 -07001116
Adam Cohen59400422014-03-05 18:07:04 -08001117 LauncherAppWidgetInfo launcherInfo;
1118 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001119 launcherInfo.spanX = itemInfo.spanX;
1120 launcherInfo.spanY = itemInfo.spanY;
1121 launcherInfo.minSpanX = itemInfo.minSpanX;
1122 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001123 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001125 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001126 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127
Sunny Goyal2100c782016-08-22 16:00:03 -07001128 if (hostView == null) {
1129 // Perform actual inflation because we're live
1130 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001132 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301133 prepareAppWidget(hostView, launcherInfo);
1134 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
Romain Guycbb89e42009-06-08 15:52:54 -07001136
Sunny Goyald5462aa2016-11-22 16:52:39 +05301137 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001138 hostView.setTag(item);
1139 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001140 hostView.setFocusable(true);
1141 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001142 }
1143
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1145 @Override
1146 public void onReceive(Context context, Intent intent) {
1147 final String action = intent.getAction();
1148 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001149 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001150 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001151 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001152 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001153 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001154 mShouldFadeInScrim = true;
1155 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1156 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1157 // the user unlocked and the Launcher is not in the foreground.
1158 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001159 }
1160 }
1161 };
1162
Tony Wickham010d2552017-01-20 08:15:28 -08001163 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1164 Runnable r = new Runnable() {
1165 @Override
1166 public void run() {
1167 mWorkspace.updateIconBadges(updatedBadges);
1168 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001169
Sunny Goyal00ac9202017-11-09 15:24:06 -08001170 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1171 if (popup != null) {
1172 popup.updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001173 }
Tony Wickham010d2552017-01-20 08:15:28 -08001174 }
1175 };
1176 if (!waitUntilResume(r)) {
1177 r.run();
1178 }
1179 }
1180
Adam Cohended9f8d2010-11-03 13:25:16 -07001181 @Override
1182 public void onAttachedToWindow() {
1183 super.onAttachedToWindow();
1184
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001185 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001186 if (mLauncherCallbacks != null) {
1187 mLauncherCallbacks.onAttachedToWindow();
1188 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001189 }
1190
1191 @Override
1192 public void onDetachedFromWindow() {
1193 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001194
1195 if (mLauncherCallbacks != null) {
1196 mLauncherCallbacks.onDetachedFromWindow();
1197 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001198 }
1199
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001200 public AllAppsTransitionController getAllAppsController() {
1201 return mAllAppsController;
1202 }
1203
Winson Chunga6945242014-01-08 14:04:34 -08001204 public DragLayer getDragLayer() {
1205 return mDragLayer;
1206 }
1207
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001208 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001209 return mAppsView;
1210 }
1211
Winson Chunga6945242014-01-08 14:04:34 -08001212 public Workspace getWorkspace() {
1213 return mWorkspace;
1214 }
1215
1216 public Hotseat getHotseat() {
1217 return mHotseat;
1218 }
1219
Winson Chung8ae41982017-11-10 11:42:13 -08001220 public <T extends ViewGroup> T getOverviewPanel() {
1221 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001222 }
1223
Sunny Goyal47328fd2016-05-25 18:56:41 -07001224 public DropTargetBar getDropTargetBar() {
1225 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001226 }
1227
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001228 public LauncherAppWidgetHost getAppWidgetHost() {
1229 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
Romain Guycbb89e42009-06-08 15:52:54 -07001231
Winson Chunga9abd0e2010-10-27 17:18:37 -07001232 public LauncherModel getModel() {
1233 return mModel;
1234 }
1235
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001236 public ModelWriter getModelWriter() {
1237 return mModelWriter;
1238 }
1239
Adam Cohen79d90c52016-04-22 13:29:20 -07001240 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001241 return mSharedPrefs;
1242 }
1243
Jon Miranda6bed3502017-09-19 14:43:17 -07001244 public int getOrientation() { return mOrientation; }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 @Override
1247 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001248 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 super.onNewIntent(intent);
1250
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001251 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001252 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1253 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001254
1255 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001256 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001257 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001258 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001259 boolean internalStateHandled = InternalStateHandler
1260 .handleNewIntent(this, intent, alreadyOnHome);
1261
Winson15f8b172015-08-19 11:02:39 -07001262 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001263 if (!internalStateHandled) {
1264 // Note: There should be at most one log per method call. This is enforced
1265 // implicitly by using if-else statements.
1266 UserEventDispatcher ued = getUserEventDispatcher();
1267 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1268 if (topOpenView != null) {
1269 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1270 } else if (alreadyOnHome) {
1271 Target target = newContainerTarget(mStateManager.getState().containerType);
1272 target.pageIndex = mWorkspace.getCurrentPage();
1273 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1274 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001275
Sunny Goyald3864fa2017-11-14 15:06:36 -08001276 // In all these cases, only animate if we're already on home
1277 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001278
Sunny Goyald3864fa2017-11-14 15:06:36 -08001279 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1280
1281 // Reset the apps view
1282 if (!alreadyOnHome && mAppsView != null) {
1283 mAppsView.reset();
1284 }
1285
1286 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1287 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1288 }
1289 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001290
1291 final View v = getWindow().peekDecorView();
1292 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001293 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001294 }
1295
Adam Cohen9211d422014-10-07 18:14:53 -07001296 if (mLauncherCallbacks != null) {
1297 mLauncherCallbacks.onHomeIntent();
1298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
Winson15f8b172015-08-19 11:02:39 -07001300
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001301 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001302 }
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001305 public void onRestoreInstanceState(Bundle state) {
1306 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001307 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 @Override
1311 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001312 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001313 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001314
Adam Cohen21cd0022013-10-09 18:57:02 -07001315 }
Sunny Goyalea609262017-10-25 15:47:38 -07001316 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001317
1318
1319 AbstractFloatingView widgets = AbstractFloatingView
1320 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1321 if (widgets != null) {
1322 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1323 widgets.saveHierarchyState(widgetsState);
1324 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1325 } else {
1326 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1327 }
1328
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001329 // We close any open folders and shortcut containers since they will not be re-opened,
1330 // and we need to make sure this state is reflected.
1331 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001332
Sunny Goyal2100c782016-08-22 16:00:03 -07001333 if (mPendingRequestArgs != null) {
1334 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1335 }
1336 if (mPendingActivityResult != null) {
1337 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001340 super.onSaveInstanceState(outState);
1341
Adam Cohen9211d422014-10-07 18:14:53 -07001342 if (mLauncherCallbacks != null) {
1343 mLauncherCallbacks.onSaveInstanceState(outState);
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 @Override
1348 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001350
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001351 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001352 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001353
Winson Chungcd2b0142011-06-08 16:02:26 -07001354 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001355 // It's possible to receive onDestroy after a new Launcher activity has
1356 // been created. In this case, don't interfere with the new Launcher.
1357 if (mModel.isCurrentCallbacks(this)) {
1358 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001359 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001360 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001361
Sunny Goyal745bad92016-05-02 10:54:12 -07001362 if (mRotationPrefChangeHandler != null) {
1363 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1364 }
1365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001367 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001369 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001373 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1374
Michael Jurka2ecf9952012-06-18 12:52:28 -07001375 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001376
Tony2917a8b2017-07-31 23:29:42 -07001377 clearPendingBinds();
1378
Adam Cohen9211d422014-10-07 18:14:53 -07001379 if (mLauncherCallbacks != null) {
1380 mLauncherCallbacks.onDestroy();
1381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
1383
Sunny Goyalae502842016-06-17 08:43:56 -07001384 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1385 return mAccessibilityDelegate;
1386 }
1387
Adam Cohena9cf38f2011-05-02 15:36:58 -07001388 public DragController getDragController() {
1389 return mDragController;
1390 }
1391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001393 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001394 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001395 }
1396
1397 @Override
1398 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1399 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001400 try {
1401 super.startIntentSenderForResult(intent, requestCode,
1402 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1403 } catch (IntentSender.SendIntentException e) {
1404 throw new ActivityNotFoundException();
1405 }
1406 }
1407
Winson Chung70d72102011-08-12 11:24:35 -07001408 /**
1409 * Indicates that we want global search for this activity by setting the globalSearch
1410 * argument for {@link #startSearch} to true.
1411 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001413 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 Bundle appSearchData, boolean globalSearch) {
1415 if (appSearchData == null) {
1416 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001417 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001419
Sunny Goyal6f28e712016-09-13 14:19:29 -07001420 if (mLauncherCallbacks == null ||
1421 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1422 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001423 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001424 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001425
1426 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001427 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001428 }
1429
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001431 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 }
1433
Adam Cohen517a7f52014-03-01 12:12:59 -08001434 public boolean isWorkspaceLoading() {
1435 return mWorkspaceLoading;
1436 }
1437
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001438 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001439 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001440 }
1441
Sunny Goyal04a324a2017-01-20 21:08:59 -08001442 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001443 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001444 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001445
Sunny Goyal2100c782016-08-22 16:00:03 -07001446 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001447 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001448 if (LOGD) {
1449 Log.d(TAG, "Adding widget from drop");
1450 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001451 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001452 }
1453
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001455 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1456 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1457 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001458
Adam Cohenad4e15c2013-10-17 16:21:35 -07001459 Runnable onComplete = new Runnable() {
1460 @Override
1461 public void run() {
1462 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001463 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001464 }
1465 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001466 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001467 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469 }
Romain Guycbb89e42009-06-08 15:52:54 -07001470
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001471 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1472 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001473 info.container = container;
1474 info.screenId = screenId;
1475 if (cell != null) {
1476 info.cellX = cell[0];
1477 info.cellY = cell[1];
1478 }
1479 info.spanX = spanX;
1480 info.spanY = spanY;
1481
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001482 switch (info.itemType) {
1483 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1484 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001485 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001486 break;
1487 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001488 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001489 break;
1490 default:
1491 throw new IllegalStateException("Unknown item type: " + info.itemType);
1492 }
1493 }
1494
Adam Cohenfbba09b2011-07-18 21:43:05 -07001495 /**
1496 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001497 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001498 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001499 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1500 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001501 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1502 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1503 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 }
1505
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 /**
1507 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001509 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001510 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001511 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001512 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001513 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001514 // In the case where we've prebound the widget, we remove it from the DragLayer
1515 if (LOGD) {
1516 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1517 }
1518 getDragLayer().removeView(hostView);
1519
Adam Cohened66b2b2012-01-23 17:28:51 -08001520 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001521 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001522
1523 // Clear the boundWidget so that it doesn't get destroyed.
1524 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001525 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001526 // In this case, we either need to start an activity to get permission to bind
1527 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001528 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1529 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1530 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1531 this, info.componentName);
1532 } else {
1533 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1534 }
Adam Cohendd70d662012-10-04 16:53:44 -07001535 Bundle options = info.bindOptions;
1536
Sunny Goyalffe83f12014-08-14 17:39:34 -07001537 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1538 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001539 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001540 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001541 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001542 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001543 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001544 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001545 }
1546
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001549 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 folderInfo.title = getText(R.string.folder_name);
1551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001553 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
1555 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001556 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301557 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001558 // Force measure the new folder icon
1559 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1560 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001561 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Winsonc0b52fe2015-09-09 16:38:15 -07001564 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001565 * Unbinds the view for the specified item, and removes the item and all its children.
1566 *
1567 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001568 * @param itemInfo the {@link ItemInfo} for this view.
1569 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001570 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001571 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001572 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001573 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001574 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1575 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001576 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001577 } else {
1578 mWorkspace.removeWorkspaceItem(v);
1579 }
Winsonc0b52fe2015-09-09 16:38:15 -07001580 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001581 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001582 }
1583 } else if (itemInfo instanceof FolderInfo) {
1584 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001585 if (v instanceof FolderIcon) {
1586 ((FolderIcon) v).removeListeners();
1587 }
Winsonc0b52fe2015-09-09 16:38:15 -07001588 mWorkspace.removeWorkspaceItem(v);
1589 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001590 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001591 }
1592 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1593 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001594 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001595 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001596 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001597 }
1598 } else {
1599 return false;
1600 }
1601 return true;
1602 }
1603
1604 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001605 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001606 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001607 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001608 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001609 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001610 // Deleting an app widget ID is a void call but writes to disk before returning
1611 // to the caller...
1612 new AsyncTask<Void, Void, Void>() {
1613 public Void doInBackground(Void ... args) {
1614 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1615 return null;
1616 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001617 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001618 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001619 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001620 }
1621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 @Override
1623 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001624 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
1626
Joe Onorato88ec0992009-11-19 13:16:06 -08001627 @Override
1628 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001629 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1630 return;
1631 }
1632
Sunny Goyal45478022015-06-08 16:52:41 -07001633 if (mDragController.isDragging()) {
1634 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001635 return;
1636 }
1637
Jon Mirandafeba90f2016-10-06 10:53:29 -07001638 // Note: There should be at most one log per method call. This is enforced implicitly
1639 // by using if-else statements.
1640 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001641 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1642 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001643 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001644 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001645 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001646 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001647 } else {
1648 // Back button is a no-op here, but give at least some feedback for the button press
1649 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001650 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001651 }
1652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 * Launches the intent referred by the clicked shortcut.
1655 *
1656 * @param v The view representing the clicked shortcut.
1657 */
1658 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001659 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1660 // view has detached (it's possible for this to happen if the view is removed mid touch).
1661 if (v.getWindowToken() == null) {
1662 return;
1663 }
1664
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001665 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001666 return;
1667 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001668
Adam Cohen1697b792013-09-17 19:08:21 -07001669 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001670 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001671 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1672 LauncherLogProto.Action.Direction.NONE,
1673 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001674 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001675 }
1676 return;
1677 }
1678
1679 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001680 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001681 int page = mWorkspace.indexOfChild(v);
1682 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1683 LauncherLogProto.Action.Direction.NONE,
1684 LauncherLogProto.ContainerType.OVERVIEW, page);
1685 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001686 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001687 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001688 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001689 }
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001692 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001693 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001695 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001696 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001697 }
Sunny Goyal508da152014-08-14 10:53:27 -07001698 } else if (tag instanceof AppInfo) {
1699 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001700 } else if (tag instanceof LauncherAppWidgetInfo) {
1701 if (v instanceof PendingAppWidgetHostView) {
1702 onClickPendingWidget((PendingAppWidgetHostView) v);
1703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705 }
1706
Sunny Goyal70660032015-05-14 00:07:08 -07001707 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001708 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001709 return false;
1710 }
1711
Michael Jurkaaf442092010-06-10 17:01:57 -07001712 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001713 * Event handler for the app widget view which has not fully restored.
1714 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001715 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001716 if (mIsSafeModeEnabled) {
1717 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1718 return;
1719 }
1720
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001721 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001722 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001723 LauncherAppWidgetProviderInfo appWidgetInfo =
1724 mAppWidgetManager.findProvider(info.providerName, info.user);
1725 if (appWidgetInfo == null) {
1726 return;
1727 }
1728 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1729
Sunny Goyald478c832016-04-01 12:04:16 -07001730 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1731 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1732 // This should not happen, as we make sure that an Id is allocated during bind.
1733 return;
1734 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001735 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1736 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001737 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001738 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001739 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001740 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001741 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001742 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001743 }
1744 }
1745
Sunny Goyale6e72002017-01-12 16:55:36 -08001746 private void onClickPendingAppItem(final View v, final String packageName,
1747 boolean downloadStarted) {
1748 if (downloadStarted) {
1749 // If the download has started, simply direct to the market app.
1750 startMarketIntentForPackage(v, packageName);
1751 return;
1752 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001753 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001754 .setTitle(R.string.abandoned_promises_title)
1755 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001756 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1757 @Override
1758 public void onClick(DialogInterface dialogInterface, int i) {
1759 startMarketIntentForPackage(v, packageName);
1760 }
1761 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001762 .setNeutralButton(R.string.abandoned_clean_this,
1763 new DialogInterface.OnClickListener() {
1764 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001765 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001766 mWorkspace.removeAbandonedPromise(packageName, user);
1767 }
1768 })
1769 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001770 }
1771
1772 private void startMarketIntentForPackage(View v, String packageName) {
1773 ItemInfo item = (ItemInfo) v.getTag();
1774 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001775 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001776 }
1777
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001778 /**
1779 * Event handler for an app shortcut click.
1780 *
1781 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1782 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001783 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001784 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1785 Object tag = v.getTag();
1786 if (!(tag instanceof ShortcutInfo)) {
1787 throw new IllegalArgumentException("Input must be a Shortcut");
1788 }
1789
1790 // Open shortcut
1791 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001792
Sunny Goyal076839c2017-10-30 13:52:20 -07001793 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001794 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1795 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001796 ~FLAG_DISABLED_SUSPENDED &
1797 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001798 // If the app is only disabled because of the above flags, launch activity anyway.
1799 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001800 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001801 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1802 // Use a message specific to this shortcut, if it has one.
1803 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1804 return;
1805 }
1806 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001807 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001808 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001809 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001810 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1811 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001812 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001813 }
1814 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1815 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001816 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001817 }
1818
Chris Wren40c5ed32014-06-24 18:24:23 -04001819 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001820 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001821 String packageName = shortcut.intent.getComponent() != null ?
1822 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1823 if (!TextUtils.isEmpty(packageName)) {
1824 onClickPendingAppItem(v, packageName,
1825 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1826 return;
1827 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001828 }
1829
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001830 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001831 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001832 }
1833
Sunny Goyala7ce1662016-05-31 15:01:35 -07001834 private void startAppShortcutOrInfoActivity(View v) {
1835 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001836 Intent intent;
1837 if (item instanceof PromiseAppInfo) {
1838 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
1839 intent = promiseAppInfo.getMarketIntent();
1840 } else {
1841 intent = item.getIntent();
1842 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001843 if (intent == null) {
1844 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001845 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001846 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001847 }
1848
1849 /**
1850 * Event handler for a folder icon click.
1851 *
1852 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1853 */
1854 protected void onClickFolderIcon(View v) {
1855 if (LOGD) Log.d(TAG, "onClickFolder");
1856 if (!(v instanceof FolderIcon)){
1857 throw new IllegalArgumentException("Input must be a FolderIcon");
1858 }
1859
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001860 Folder folder = ((FolderIcon) v).getFolder();
1861 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001862 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001863 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001864 }
Michael Jurka5130e402011-10-13 04:55:35 -07001865 }
1866
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001867 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001868 * Event handler for the wallpaper picker button that appears after a long press
1869 * on the home screen.
1870 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001871 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001872 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001873 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1874 return;
1875 }
1876
Tony Wickham785f7a52015-08-31 17:28:32 -07001877 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1878 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001879 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001880 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001881 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001882
1883 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001884 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1885 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001886 intent.setPackage(pickerPackage);
1887 }
1888
Sunny Goyala5ace712017-11-15 17:12:51 -08001889 final Bundle launchOptions;
1890 if (v != null) {
1891 intent.setSourceBounds(getViewBounds(v));
1892 // If there is no target package, use the default intent chooser animation
1893 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
1894 } else {
1895 launchOptions = null;
1896 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001897 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001898 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001899 } catch (ActivityNotFoundException e) {
1900 setWaitingForResult(null);
1901 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1902 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001903 }
1904
Sunny Goyala7ce1662016-05-31 15:01:35 -07001905 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001907 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1908 try {
1909 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1910 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1911 // is enabled by default on NYC.
1912 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1913 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001914
1915 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1916 String id = ((ShortcutInfo) info).getDeepShortcutId();
1917 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301918 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001919 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001920 } else {
1921 // Could be launching some bookkeeping activity
1922 startActivity(intent, optsBundle);
1923 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001924 } finally {
1925 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001928 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1929 // corresponding permission. Show the appropriate permission prompt if that
1930 // is the case.
1931 if (intent.getComponent() == null
1932 && Intent.ACTION_CALL.equals(intent.getAction())
1933 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1934 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001935
1936 setWaitingForResult(PendingRequestArgs
1937 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001938 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1939 REQUEST_PERMISSION_CALL_PHONE);
1940 } else {
1941 // No idea why this was thrown.
1942 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001946
Sunny Goyalfe770c92016-09-27 14:38:58 -07001947 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07001948 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001949 if (Utilities.ATLEAST_MARSHMALLOW) {
1950 int left = 0, top = 0;
1951 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001952 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001953 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001954 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07001955 if (icon != null) {
1956 Rect bounds = icon.getBounds();
1957 left = (width - bounds.width()) / 2;
1958 top = v.getPaddingTop();
1959 width = bounds.width();
1960 height = bounds.height();
1961 }
1962 }
1963 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
1964 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
1965 // On L devices, we use the device default slide-up transition.
1966 // On L MR1 devices, we use a custom version of the slide-up transition which
1967 // doesn't have the delay present in the device default.
1968 return ActivityOptions.makeCustomAnimation(
1969 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
1970 }
1971 return null;
1972 }
1973
Tonye3c59252017-05-02 21:32:27 -07001974 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001975 int[] pos = new int[2];
1976 v.getLocationOnScreen(pos);
1977 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1978 }
1979
Sunny Goyala7ce1662016-05-31 15:01:35 -07001980 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001981 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001982 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1983 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001984 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001985 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001986 // Only launch using the new animation if the shortcut has not opted out (this is a
1987 // private contract between launcher and may be ignored in the future).
1988 boolean useLaunchAnimation = (v != null) &&
1989 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1990 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
1991
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001992 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001993
1994 // Prepare intent
1995 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1996 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001997 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001998 }
1999 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002000 if (Utilities.ATLEAST_MARSHMALLOW
2001 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002002 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002003 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002004 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002005 // Shortcuts need some special checks due to legacy reasons.
2006 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002007 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002008 // Could be launching some bookkeeping activity
2009 startActivity(intent, optsBundle);
2010 } else {
2011 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2012 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2013 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002014
2015 if (v instanceof BubbleTextView) {
2016 // This is set to the view that launched the activity that navigated the user away
2017 // from launcher. Since there is no callback for when the activity has finished
2018 // launching, enable the press state and keep this reference to reset the press
2019 // state when we return to launcher.
2020 BubbleTextView btv = (BubbleTextView) v;
2021 btv.setStayPressed(true);
2022 setOnResumeCallback(btv);
2023 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002024 mAppLaunchSuccess = true;
2025 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002026 } catch (ActivityNotFoundException|SecurityException e) {
2027 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2028 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2029 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002030 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002031 }
2032
Tony Wickhamdadb3042016-02-24 11:07:00 -08002033 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002034 public boolean dispatchTouchEvent(MotionEvent ev) {
2035 mLastDispatchTouchEventX = ev.getX();
2036 return super.dispatchTouchEvent(ev);
2037 }
2038
2039 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002041 if (!isDraggingEnabled()) return false;
2042 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002043 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044
Jon Miranda51f037d2016-11-07 08:37:20 -08002045 boolean ignoreLongPressToOverview =
2046 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002047
Adam Cohen1697b792013-09-17 19:08:21 -07002048 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002049 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002050 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302051 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2052 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002053 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002054 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002055 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2056 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2057 return true;
2058 } else {
2059 return false;
2060 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002061 } else {
2062 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002063 }
Adam Cohen1697b792013-09-17 19:08:21 -07002064 }
2065
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002066 CellLayout.CellInfo longClickCellInfo = null;
2067 View itemUnderLongClick = null;
2068 if (v.getTag() instanceof ItemInfo) {
2069 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002070 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002071 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002072 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074
Winson Chung3d503fb2011-07-13 17:25:49 -07002075 // The hotseat touch handling does not go through Workspace, and we always allow long press
2076 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002077 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002078 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002079 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002080 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002081 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302082 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2083 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002084 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002085 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002086 return false;
2087 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302088 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2089 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002090 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002091 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002092 }
Jon Miranda379198e2016-09-23 08:54:40 -07002093 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2094 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002096 final boolean isAllAppsButton =
2097 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2098 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2099 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002100 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002102 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104 }
2105 }
2106 return true;
2107 }
2108
Winson Chung3d503fb2011-07-13 17:25:49 -07002109 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002110 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002111 return mHotseat != null && layout != null &&
2112 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2113 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002114
Winson Chung3d503fb2011-07-13 17:25:49 -07002115 /**
2116 * Returns the CellLayout of the specified container at the specified screen.
2117 */
Sunny Goyal92820592015-03-02 11:31:35 -08002118 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002119 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2120 if (mHotseat != null) {
2121 return mHotseat.getLayout();
2122 } else {
2123 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002124 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002125 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002126 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002127 }
2128 }
2129
Michael Jurkae326f182011-11-21 14:05:46 -08002130 @Override
2131 public void onTrimMemory(int level) {
2132 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002133 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2134 // The widget preview db can result in holding onto over
2135 // 3MB of memory for caching which isn't necessary.
2136 SQLiteDatabase.releaseMemory();
2137
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002138 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002139 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002140 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002141 if (mLauncherCallbacks != null) {
2142 mLauncherCallbacks.onTrimMemory(level);
2143 }
Michael Jurkae326f182011-11-21 14:05:46 -08002144 }
2145
Michael Jurkad7c28052012-04-27 15:43:36 -07002146 @Override
2147 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002148 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002149 final List<CharSequence> text = event.getText();
2150 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002151 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002152 // TODO: When can workspace be null?
2153 text.add(mWorkspace == null
2154 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002155 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002156 return result;
2157 }
2158
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002159 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002160 * If the activity is currently paused, signal that we need to run the passed Runnable
2161 * in onResume.
2162 *
2163 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002164 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2165 * wrong when we're not running, and if the activity comes back to what the configuration was
2166 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002167 *
2168 * Implementation of the method from LauncherModel.Callbacks.
2169 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002170 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002171 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002172 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002173 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002174 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002175 if (run instanceof RunnableWithId) {
2176 // Remove any runnables which have the same id
2177 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002178 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002179 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002180 return true;
2181 } else {
2182 return false;
2183 }
2184 }
2185
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002186 public void setOnResumeCallback(OnResumeCallback callback) {
2187 if (mOnResumeCallback != null) {
2188 mOnResumeCallback.onLauncherResume();
2189 }
2190 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002191 }
2192
Michael Jurka7607c2f2013-04-03 14:33:19 -07002193 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002194 * If the activity is currently paused, signal that we need to re-run the loader
2195 * in onResume.
2196 *
2197 * This needs to be called from incoming places where resources might have been loaded
2198 * while we are paused. That is becaues the Configuration might be wrong
2199 * when we're not running, and if it comes back to what it was when we
2200 * were paused, we are not restarted.
2201 *
2202 * Implementation of the method from LauncherModel.Callbacks.
2203 *
2204 * @return true if we are currently paused. The caller might be able to
2205 * skip some work in that case since we will come back again.
2206 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002207 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002208 public boolean setLoadOnResume() {
2209 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002210 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002211 mOnResumeNeedsLoad = true;
2212 return true;
2213 } else {
2214 return false;
2215 }
2216 }
2217
2218 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002219 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002221 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002222 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002223 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002224 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002225 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002226 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002227 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002228 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002229
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002231 * Clear any pending bind callbacks. This is called when is loader is planning to
2232 * perform a full rebind from scratch.
2233 */
2234 @Override
2235 public void clearPendingBinds() {
2236 mBindOnResumeCallbacks.clear();
2237 if (mPendingExecutor != null) {
2238 mPendingExecutor.markCompleted();
2239 mPendingExecutor = null;
2240 }
2241 }
2242
2243 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002244 * Refreshes the shortcuts shown on the workspace.
2245 *
2246 * Implementation of the method from LauncherModel.Callbacks.
2247 */
2248 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002249 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002250 // Floating panels (except the full widget sheet) are associated with individual icons. If
2251 // we are starting a fresh bind, close all such panels as all the icons are about
2252 // to go away.
2253 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002254 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002255
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002256 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002257
Winson Chungd64d1762013-08-20 14:37:16 -07002258 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002259 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002260 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002261
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 if (mHotseat != null) {
2263 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002265 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 }
2267
Adam Cohendcd297f2013-06-18 13:13:40 -07002268 @Override
2269 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002270 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002271 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2272 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002273 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2274 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002275 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002276 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2277 // If there are no screens, we need to have an empty screen
2278 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002279 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002280 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002281
Winsonc7d2e832016-07-28 12:24:55 -07002282 // After we have added all the screens, if the wallpaper was locked to the default state,
2283 // then notify to indicate that it can be released and a proper wallpaper offset can be
2284 // computed before the next layout
2285 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002286 }
2287
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002288 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 int count = orderedScreenIds.size();
2290 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002291 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002292 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002293 // No need to bind the first screen, as its always bound.
2294 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2295 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 }
2298
Sunny Goyalb23980c2017-08-17 07:45:25 -07002299 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002300 public void bindAppsAdded(final ArrayList<Long> newScreens,
2301 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002302 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002303 Runnable r = new Runnable() {
2304 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002305 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002306 }
2307 };
2308 if (waitUntilResume(r)) {
2309 return;
2310 }
2311
Winson Chungd64d1762013-08-20 14:37:16 -07002312 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002313 if (newScreens != null) {
2314 bindAddScreens(newScreens);
2315 }
Winson Chungd64d1762013-08-20 14:37:16 -07002316
2317 // We add the items without animation on non-visible pages, and with
2318 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002319 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002320 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002321 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002322 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002323 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002324 }
Winson Chungc58497e2013-09-03 17:48:37 -07002325
Winson Chung87412982013-10-03 18:34:14 -07002326 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002327 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002328 }
2329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002331 * Bind the items start-end from the list.
2332 *
2333 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002335 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002336 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002337 Runnable r = new Runnable() {
2338 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002339 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002340 }
2341 };
2342 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002343 return;
2344 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002345
Sunny Goyal3be633b2016-12-08 09:59:25 -08002346 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002347 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002348 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002349 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002350 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002351 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002352 int end = items.size();
2353 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002354 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002355
2356 // Short circuit if we are loading dock items for a configuration which has no dock
2357 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2358 mHotseat == null) {
2359 continue;
2360 }
2361
Sunny Goyal639e9062015-08-19 19:17:06 -07002362 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002363 switch (item.itemType) {
2364 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2365 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002366 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002367 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002368 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002369 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002370 }
2371 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002372 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002373 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002374 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002375 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002376 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002377 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2378 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002379 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002380 if (view == null) {
2381 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002382 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002383 break;
2384 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002385 default:
2386 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002387 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002388
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002389 /*
2390 * Remove colliding items.
2391 */
2392 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2393 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2394 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2395 View v = cl.getChildAt(item.cellX, item.cellY);
2396 Object tag = v.getTag();
2397 String desc = "Collision while binding workspace item: " + item
2398 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002399 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002400 throw (new RuntimeException(desc));
2401 } else {
2402 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002403 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002404 continue;
2405 }
2406 }
2407 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302408 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002409 if (animateIcons) {
2410 // Animate all the applications up now
2411 view.setAlpha(0f);
2412 view.setScaleX(0f);
2413 view.setScaleY(0f);
2414 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002415 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002418
Winson Chung997a9232013-07-24 15:33:46 -07002419 if (animateIcons) {
2420 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002421 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002422 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002423 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002424 final Runnable startBounceAnimRunnable = new Runnable() {
2425 public void run() {
2426 anim.playTogether(bounceAnims);
2427 anim.start();
2428 }
2429 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002430 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002431 // We post the animation slightly delayed to prevent slowdowns
2432 // when we are loading right after we return to launcher.
2433 mWorkspace.postDelayed(new Runnable() {
2434 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002435 if (mWorkspace != null) {
2436 mWorkspace.snapToPage(newScreenIndex);
2437 mWorkspace.postDelayed(startBounceAnimRunnable,
2438 NEW_APPS_ANIMATION_DELAY);
2439 }
Winson Chung94d67682013-09-25 16:29:40 -07002440 }
2441 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002442 } else {
2443 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002444 }
2445 }
Winson Chung64359a52013-07-08 17:17:08 -07002446 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002447 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 }
2449
Joe Onorato9c1289c2009-08-17 11:03:03 -04002450 /**
2451 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002452 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002453 public void bindAppWidget(LauncherAppWidgetInfo item) {
2454 View view = inflateAppWidget(item);
2455 if (view != null) {
2456 mWorkspace.addInScreen(view, item);
2457 mWorkspace.requestLayout();
2458 }
2459 }
2460
2461 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002462 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302463 PendingAppWidgetHostView view =
2464 new PendingAppWidgetHostView(this, item, mIconCache, true);
2465 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002466 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002467 }
2468
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002469 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002470
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002471 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002472
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002473 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2474 // If the provider is not ready, bind as a pending widget.
2475 appWidgetInfo = null;
2476 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2477 // The widget id is not valid. Try to find the widget based on the provider info.
2478 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2479 } else {
2480 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2481 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002482
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002483 // If the provider is ready, but the width is not yet restored, try to restore it.
2484 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2485 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002486 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002487 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2488 + " belongs to component " + item.providerName
2489 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002490 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002491 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002492 }
Sunny Goyalff572272014-07-23 13:58:07 -07002493
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002494 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002495 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002496 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2497 // Id has not been allocated yet. Allocate a new id.
2498 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2499 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002500
Sunny Goyald478c832016-04-01 12:04:16 -07002501 // Also try to bind the widget. If the bind fails, the user will be shown
2502 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002503 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002504 pendingInfo.spanX = item.spanX;
2505 pendingInfo.spanY = item.spanY;
2506 pendingInfo.minSpanX = item.minSpanX;
2507 pendingInfo.minSpanY = item.minSpanY;
2508 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002509
2510 boolean isDirectConfig =
2511 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2512 if (isDirectConfig && item.bindOptions != null) {
2513 Bundle newOptions = item.bindOptions.getExtras();
2514 if (options != null) {
2515 newOptions.putAll(options);
2516 }
2517 options = newOptions;
2518 }
Sunny Goyald478c832016-04-01 12:04:16 -07002519 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2520 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002521
Sunny Goyal86df1382016-08-10 15:03:22 -07002522 // We tried to bind once. If we were not able to bind, we would need to
2523 // go through the permission dialog, which means we cannot skip the config
2524 // activity.
2525 item.bindOptions = null;
2526 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2527
Sunny Goyald478c832016-04-01 12:04:16 -07002528 // Bind succeeded
2529 if (success) {
2530 // If the widget has a configure activity, it is still needs to set it up,
2531 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002532 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002533 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2534 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002535 }
Sunny Goyald478c832016-04-01 12:04:16 -07002536
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002537 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002538 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002539 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002540 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002541 // The widget was marked as UI not ready, but there is no configure activity to
2542 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002543 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002544 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002545 }
Sunny Goyalff572272014-07-23 13:58:07 -07002546 }
2547
Sunny Goyald5462aa2016-11-22 16:52:39 +05302548 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002549 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002550 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002551 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002552 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002553 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002554 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002555 }
2556
Sunny Goyal233ee962015-08-03 13:05:01 -07002557 item.minSpanX = appWidgetInfo.minSpanX;
2558 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302559 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002560 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302561 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002562 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302563 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002564
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002565 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002566 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002567 }
2568
Sunny Goyalff572272014-07-23 13:58:07 -07002569 /**
2570 * Restores a pending widget.
2571 *
2572 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002573 */
Sunny Goyald478c832016-04-01 12:04:16 -07002574 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002575 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2576 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2577 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002578 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002579 }
2580
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002581 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002582 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002583 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2584 info.pendingItemInfo = null;
2585 }
Sunny Goyalff572272014-07-23 13:58:07 -07002586
Sunny Goyalba47faa2017-10-04 16:50:57 -07002587 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2588 view.reinflate();
2589 }
2590
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002591 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002592 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002593 }
2594
Adam Cohen1462de32012-07-24 22:34:36 -07002595 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002596 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002597 }
2598
Sunny Goyal527c7d32015-08-28 15:19:36 -07002599 @Override
2600 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2601 if (mPendingExecutor != null) {
2602 mPendingExecutor.markCompleted();
2603 }
2604 mPendingExecutor = executor;
2605 executor.attachTo(this);
2606 }
2607
2608 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2609 if (mPendingExecutor == executor) {
2610 mPendingExecutor = null;
2611 }
2612 }
2613
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002614 @Override
2615 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2616 Runnable r = new Runnable() {
2617 public void run() {
2618 finishFirstPageBind(executor);
2619 }
2620 };
2621 if (waitUntilResume(r)) {
2622 return;
2623 }
2624
2625 Runnable onComplete = new Runnable() {
2626 @Override
2627 public void run() {
2628 if (executor != null) {
2629 executor.onLoadAnimationCompleted();
2630 }
2631 }
2632 };
2633 if (mDragLayer.getAlpha() < 1) {
2634 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2635 } else {
2636 onComplete.run();
2637 }
2638 }
2639
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 /**
2641 * Callback saying that there aren't any more items to bind.
2642 *
2643 * Implementation of the method from LauncherModel.Callbacks.
2644 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002645 public void finishBindingItems() {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002646 Runnable r = this::finishBindingItems;
Winson Chungd64d1762013-08-20 14:37:16 -07002647 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002648 return;
2649 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002650 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002651 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002652
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002653 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002654
Sunny Goyal2100c782016-08-22 16:00:03 -07002655 if (mPendingActivityResult != null) {
2656 handleActivityResult(mPendingActivityResult.requestCode,
2657 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2658 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002659 }
2660
Sunny Goyala474a9b2017-05-04 16:47:11 -07002661 InstallShortcutReceiver.disableAndFlushInstallQueue(
2662 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002663
Tony Wickham010d2552017-01-20 08:15:28 -08002664 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002665 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002666 }
2667
Winson Chunga2413752012-04-03 14:22:34 -07002668 private boolean canRunNewAppsAnimation() {
2669 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002670 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002671 }
2672
Winson Chungc9168342013-06-26 14:54:55 -07002673 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002674 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002675 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2676 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002677 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002678 return bounceAnim;
2679 }
2680
Adam Cohen27772732013-11-11 14:00:56 +00002681 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002682 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002683 }
2684
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002685 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002686 * Add the icons for all apps.
2687 *
2688 * Implementation of the method from LauncherModel.Callbacks.
2689 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002690 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002691 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2692 public void run() {
2693 bindAllApplications(apps);
2694 }
2695 };
2696 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002697 return;
2698 }
2699
Winson Chungb745afb2015-03-02 11:51:23 -08002700 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002701 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002702 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002703 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002704 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002705 return;
2706 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002707
Winson Chungb745afb2015-03-02 11:51:23 -08002708 mAppsView.setApps(apps);
2709 }
Adam Cohen9211d422014-10-07 18:14:53 -07002710 if (mLauncherCallbacks != null) {
2711 mLauncherCallbacks.bindAllApplications(apps);
2712 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002713 }
2714
2715 /**
Tony2917a8b2017-07-31 23:29:42 -07002716 * Returns an Executor that will run after the launcher is first drawn (including after the
2717 * initial fade in animation). Returns null if the first draw has already occurred.
2718 */
2719 public @Nullable Executor getPendingExecutor() {
2720 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2721 }
2722
2723 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002724 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2725 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2726 */
2727 @Override
2728 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002729 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002730 }
2731
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002733 * A package was updated.
2734 *
2735 * Implementation of the method from LauncherModel.Callbacks.
2736 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002737 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002738 Runnable r = new Runnable() {
2739 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002740 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002741 }
2742 };
2743 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002744 return;
2745 }
2746
Winson Chungb745afb2015-03-02 11:51:23 -08002747 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002748 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002750 }
2751
Sunny Goyal4390ace2014-10-13 11:33:11 -07002752 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002753 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2754 Runnable r = new Runnable() {
2755 public void run() {
2756 bindPromiseAppProgressUpdated(app);
2757 }
2758 };
2759 if (waitUntilResume(r)) {
2760 return;
2761 }
2762
2763 if (mAppsView != null) {
2764 mAppsView.updatePromiseAppProgress(app);
2765 }
2766 }
2767
2768 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002769 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2770 Runnable r = new Runnable() {
2771 public void run() {
2772 bindWidgetsRestored(widgets);
2773 }
2774 };
2775 if (waitUntilResume(r)) {
2776 return;
2777 }
2778 mWorkspace.widgetsRestored(widgets);
2779 }
2780
Joe Onorato9c1289c2009-08-17 11:03:03 -04002781 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002782 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002783 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002784 *
2785 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002786 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002787 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002788 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002789 Runnable r = new Runnable() {
2790 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002791 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002792 }
2793 };
2794 if (waitUntilResume(r)) {
2795 return;
2796 }
2797
Sunny Goyal4390ace2014-10-13 11:33:11 -07002798 if (!updated.isEmpty()) {
2799 mWorkspace.updateShortcuts(updated);
2800 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002801 }
2802
2803 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002804 * Update the state of a package, typically related to install state.
2805 *
2806 * Implementation of the method from LauncherModel.Callbacks.
2807 */
Sunny Goyale755d462014-07-22 13:48:29 -07002808 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002809 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
2810 Runnable r = new Runnable() {
2811 public void run() {
2812 bindRestoreItemsChange(updates);
2813 }
2814 };
2815 if (waitUntilResume(r)) {
2816 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002817 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002818
Sunny Goyal756adbc2015-04-16 15:20:51 -07002819 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002820 }
2821
2822 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002823 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002824 * in addition to specific applications to remove, the reason being that
2825 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002826 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002827 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002828 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002829 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002830 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07002831 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07002832 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002833 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07002834 }
Winson Chungd64d1762013-08-20 14:37:16 -07002835 };
2836 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07002837 return;
2838 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002839 mWorkspace.removeItemsByMatcher(matcher);
2840 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002841 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002842
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002843 @Override
2844 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
2845 Runnable r = new Runnable() {
2846 public void run() {
2847 bindAppInfosRemoved(appInfos);
2848 }
2849 };
2850 if (waitUntilResume(r)) {
2851 return;
Joe Onorato36115782010-06-17 13:28:48 -04002852 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002853
Winson Chungdf95eb12013-10-16 14:57:07 -07002854 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002855 if (mAppsView != null) {
2856 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 }
Joe Onoratobe386092009-11-17 17:32:16 -08002859
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002860 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002861 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2862 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002863 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
2864 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07002865 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002866 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07002867 }
Michael Jurkac402cd92013-05-20 15:49:32 +02002868 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002869 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07002870 return;
2871 }
2872
Tony Wickham26b17462017-03-20 17:12:24 -07002873 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2874 if (topView != null) {
2875 topView.onWidgetsBound();
2876 }
2877 }
2878
Tony Wickham86222d22017-03-29 15:30:43 -07002879 /**
2880 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2881 * refreshes the widgets and shortcuts associated with the given package/user
2882 */
2883 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002884 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002885 }
2886
Sunny Goyalc99cb172017-10-19 16:15:09 -07002887 public boolean isRotationEnabled () {
2888 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002889 }
2890
Winson Chung80baf5a2010-08-09 16:03:15 -07002891 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002892 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002893 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002894 @Override
2895 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2896 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002897
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002898 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2899 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002900 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002901 writer.println(prefix + " Homescreen " + i);
2902
2903 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2904 for (int j = 0; j < layout.getChildCount(); j++) {
2905 Object tag = layout.getChildAt(j).getTag();
2906 if (tag != null) {
2907 writer.println(prefix + " " + tag.toString());
2908 }
2909 }
2910 }
2911
2912 writer.println(prefix + " Hotseat");
2913 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002914 for (int j = 0; j < layout.getChildCount(); j++) {
2915 Object tag = layout.getChildAt(j).getTag();
2916 if (tag != null) {
2917 writer.println(prefix + " " + tag.toString());
2918 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002919 }
Sunny Goyala1365452015-10-01 15:46:24 -07002920
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002921 try {
2922 FileLog.flushAll(writer);
2923 } catch (Exception e) {
2924 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002925 }
2926 }
2927
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002928 writer.println(prefix + "Misc:");
2929 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2930 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2931 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2932
2933 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002934
Adam Cohen9211d422014-10-07 18:14:53 -07002935 if (mLauncherCallbacks != null) {
2936 mLauncherCallbacks.dump(prefix, fd, writer, args);
2937 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002938 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002939
Sunny Goyal66b24572016-09-21 15:57:55 -07002940 @Override
2941 @TargetApi(Build.VERSION_CODES.N)
2942 public void onProvideKeyboardShortcuts(
2943 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2944
2945 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002946 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002947 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2948 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2949 }
2950 View currentFocus = getCurrentFocus();
2951 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2952 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2953 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2954 }
Tony18c4aa42017-05-10 21:23:57 -05002955 if (currentFocus.getTag() instanceof ItemInfo
2956 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002957 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2958 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2959 }
2960 if (!shortcutInfos.isEmpty()) {
2961 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2962 }
2963
2964 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2965 }
2966
2967 @Override
2968 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2969 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2970 switch (keyCode) {
2971 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002972 if (isInState(NORMAL)) {
2973 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002974 return true;
2975 }
2976 break;
2977 case KeyEvent.KEYCODE_S: {
2978 View focusedView = getCurrentFocus();
2979 if (focusedView instanceof BubbleTextView
2980 && focusedView.getTag() instanceof ItemInfo
2981 && mAccessibilityDelegate.performAction(focusedView,
2982 (ItemInfo) focusedView.getTag(),
2983 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002984 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002985 return true;
2986 }
2987 break;
2988 }
2989 case KeyEvent.KEYCODE_O:
2990 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2991 return true;
2992 }
2993 break;
2994 }
2995 }
2996 return super.onKeyShortcut(keyCode, event);
2997 }
2998
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002999 public static Launcher getLauncher(Context context) {
3000 if (context instanceof Launcher) {
3001 return (Launcher) context;
3002 }
3003 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3004 }
3005
Sunny Goyal5a81c382017-03-20 15:08:06 -07003006 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003007
3008 @Override
3009 public void onSharedPreferenceChanged(
3010 SharedPreferences sharedPreferences, String key) {
3011 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003012 // Recreate the activity so that it initializes the rotation preference again.
3013 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003014 }
3015 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003016 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003017
3018 /**
3019 * Callback for listening for onResume
3020 */
3021 public interface OnResumeCallback {
3022
3023 void onLauncherResume();
3024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025}