blob: b7986da049c4d391607fe16e3d5b756949d01f98 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
33import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
34
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070053import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040054import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070056import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070057import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070058import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070059import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Sunny Goyala5ace712017-11-15 17:12:51 -080063import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020067import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070069import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080070import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070071import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070072import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070073import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070076import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070078import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070079import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.HapticFeedbackConstants;
82import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070083import android.view.KeyboardShortcutGroup;
84import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080085import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080089import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070092import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070097import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070098import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700100import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700101import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700104import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700105import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800106import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700109import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700110import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700111import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700114import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700115import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700116import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700120import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800121import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800122import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700123import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700124import com.android.launcher3.states.AllAppsState;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800125import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800126import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800127import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530128import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
129import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
130import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700131import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700132import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700133import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700134import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700135import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700136import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800137import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700138import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700139import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700140import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700141import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700142import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700143import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700144import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700145import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800146import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700147import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800148import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700149import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700150import com.android.launcher3.widget.WidgetListRowEntry;
151import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700152import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700153
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700154import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700155import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700156import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700157import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700158import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700159import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800160import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700161import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163/**
164 * Default launcher application.
165 */
Sunny Goyal27835952017-01-13 12:15:53 -0800166public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700167 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
168 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700169 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700170 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700171 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chunga2413752012-04-03 14:22:34 -0700173 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700179 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Michael Jurka8b805b12012-04-18 14:23:14 -0700181 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700182 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
183 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700184
Jon Mirandac476d6e2017-05-04 16:30:01 -0700185 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700186
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700187 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
188
Mathew Inwood876a8462013-06-14 14:12:41 +0100189 /**
190 * IntentStarter uses request codes starting with this. This must be greater than all activity
191 * request codes used internally.
192 */
193 protected static final int REQUEST_LAST = 100;
194
Winson Chung2672ff92012-05-04 16:22:30 -0700195 // The Intent extra that defines whether to ignore the launch animation
196 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400197 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 // Type: int
200 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700201 // Type: int
202 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700203 // Type: PendingRequestArgs
204 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
205 // Type: ActivityResultInfo
206 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700207 // Type: SparseArray<Parcelable>
208 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700210 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
219 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700225
Sunny Goyalffe83f12014-08-14 17:39:34 -0700226 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700229 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700230
Sunny Goyal316490e2015-06-02 09:38:28 -0700231 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700232
Michael Jurka838a4ca2011-02-07 13:33:06 -0800233 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700234
Sunny Goyal47328fd2016-05-25 18:56:41 -0700235 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700236
237 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700238 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700239 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700240
Winson Chung8ae41982017-11-10 11:42:13 -0800241 // UI and state for the overview panel
242 private ViewGroup mOverviewPanel;
243
Jon Miranda6bed3502017-09-19 14:43:17 -0700244 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
245 // that results in widgets being inflated in the wrong orientation.
246 private int mOrientation;
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private SpannableStringBuilder mDefaultKeySsb = null;
249
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800252 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700253 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700255 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700256 private OnResumeCallback mOnResumeCallback;
257
Sunny Goyal527c7d32015-08-28 15:19:36 -0700258 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700259
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800261 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800262 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700263 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700264 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Jon Miranda2d89ea82017-05-04 11:47:53 -0700266 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700267 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700268
Tony Wickham010d2552017-01-20 08:15:28 -0800269 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700270
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700271 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700272
Winson Chung46353de2012-02-16 14:05:10 -0800273 // We only want to get the SharedPreferences once since it does an FS stat each time we get
274 // it from the context.
275 private SharedPreferences mSharedPrefs;
276
Sunny Goyal2100c782016-08-22 16:00:03 -0700277 // Activity result which needs to be processed after workspace has loaded.
278 private ActivityResultInfo mPendingActivityResult;
279 /**
280 * Holds extra information required to handle a result from an external call, like
281 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
282 */
283 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800284
Jon Miranda379198e2016-09-23 08:54:40 -0700285 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700286
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700287 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700288 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700289 private boolean mAppLaunchSuccess;
290
Sunny Goyal745bad92016-05-02 10:54:12 -0700291 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 @Override
294 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700295 if (DEBUG_STRICT_MODE) {
296 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
297 .detectDiskReads()
298 .detectDiskWrites()
299 .detectNetwork() // or .detectAll() for all detectable problems
300 .penaltyLog()
301 .build());
302 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
303 .detectLeakedSqlLiteObjects()
304 .detectLeakedClosableObjects()
305 .penaltyLog()
306 .penaltyDeath()
307 .build());
308 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700309 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700310
Adam Cohen9211d422014-10-07 18:14:53 -0700311 if (mLauncherCallbacks != null) {
312 mLauncherCallbacks.preOnCreate();
313 }
314
Mario Bertschler27288382017-05-24 15:35:09 -0700315 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
316 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700317 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700320 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400321
Sunny Goyal87f784c2017-01-11 10:48:34 -0800322 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700323
Adam Cohen2e6da152015-05-06 11:42:25 -0700324 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800325 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700326 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700327 Display display = getWindowManager().getDefaultDisplay();
328 Point mwSize = new Point();
329 display.getSize(mwSize);
330 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
331 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700332
Jon Miranda6bed3502017-09-19 14:43:17 -0700333 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700334 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700335 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800336 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800337 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800338 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700339 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700340
Joe Onorato41a12d22009-10-31 18:30:00 -0400341 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700342 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800343 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700344
Sunny Goyalffe83f12014-08-14 17:39:34 -0700345 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700346
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700347 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700348 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700349
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700350 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
351 // this also ensures that any synchronous binding below doesn't re-trigger another
352 // LauncherModel load.
353 mPaused = false;
354
Sunny Goyal60820d72017-05-09 12:40:11 -0700355 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 setupViews();
Winson1f064272016-07-18 17:18:02 -0700358 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359
Tony Wickham010d2552017-01-20 08:15:28 -0800360 mPopupDataProvider = new PopupDataProvider(this);
361
Sunny Goyald3864fa2017-11-14 15:06:36 -0800362 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
363 // In case we are on a device with locked rotation, we should look at preferences to check
364 // if the user has specifically allowed rotation.
365 if (!mRotationEnabled) {
366 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
367 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
368 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370
Sunny Goyald3864fa2017-11-14 15:06:36 -0800371 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
372 if (internalStateHandled) {
373 // Temporarily enable the rotation
374 mRotationEnabled = true;
375
376 if (savedInstanceState != null) {
377 // InternalStateHandler has already set the appropriate state.
378 // We dont need to do anything.
379 savedInstanceState.remove(RUNTIME_STATE);
380 }
381 }
382 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800383
Sunny Goyal2100c782016-08-22 16:00:03 -0700384 // We only load the page synchronously if the user rotates (or triggers a
385 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700386 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
387 if (savedInstanceState != null) {
388 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
389 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800390
Sunny Goyalfe770c92016-09-27 14:38:58 -0700391 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800392 if (!internalStateHandled) {
393 // If we are not binding synchronously, show a fade in animation when
394 // the first page bind completes.
395 mDragLayer.setAlpha(0);
396 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700397 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700398 // Pages bound synchronously.
399 mWorkspace.setCurrentPage(currentScreen);
400
Sunny Goyal2100c782016-08-22 16:00:03 -0700401 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
403
404 // For handling default keys
405 mDefaultKeySsb = new SpannableStringBuilder();
406 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800407
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700408 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
409 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700410 setRequestedOrientation(mRotationEnabled
411 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700412
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800413 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700414
Jon Miranda7fda2852017-07-19 16:07:01 -0700415 // Listen for broadcasts
416 IntentFilter filter = new IntentFilter();
417 filter.addAction(Intent.ACTION_SCREEN_OFF);
418 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
419 registerReceiver(mReceiver, filter);
420 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700421
Sunny Goyal8392c822017-06-20 10:03:56 -0700422 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
423 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700424
425 if (mLauncherCallbacks != null) {
426 mLauncherCallbacks.onCreate(savedInstanceState);
427 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700428
429 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700430 }
431
Sunny Goyal7779d622015-06-11 16:18:39 -0700432 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700433 public void onThemeChanged() {
434 recreate();
435 }
436
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700437 public LauncherStateManager getStateManager() {
438 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700439 }
440
Mario Bertschlera6936942017-05-31 14:48:19 -0700441 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700442 if (isDark) {
443 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700444 } else if (supportsDarkText) {
445 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700446 }
447 }
448
449 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700450 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800451 return mLauncherView.findViewById(id);
452 }
453
454 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700455 public void onAppWidgetHostReset() {
456 if (mAppWidgetHost != null) {
457 mAppWidgetHost.startListening();
458 }
459 }
460
Adam Cohen9211d422014-10-07 18:14:53 -0700461 private LauncherCallbacks mLauncherCallbacks;
462
Winson1f064272016-07-18 17:18:02 -0700463 public void onInsetsChanged(Rect insets) {
464 mDeviceProfile.updateInsets(insets);
465 mDeviceProfile.layout(this, true /* notifyListeners */);
466 }
467
Sunny Goyal32554d12015-12-03 15:31:25 -0800468 /**
469 * Call this after onCreate to set or clear overlay.
470 */
471 public void setLauncherOverlay(LauncherOverlay overlay) {
472 if (overlay != null) {
473 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
474 }
475 mWorkspace.setLauncherOverlay(overlay);
476 }
477
Adam Cohen9211d422014-10-07 18:14:53 -0700478 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
479 mLauncherCallbacks = callbacks;
480 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700481 }
482
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700483 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700484 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700485 if (mLauncherCallbacks != null) {
486 mLauncherCallbacks.onLauncherProviderChange();
487 }
488 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700489
Hyunyoung Song3f471442015-04-08 19:01:34 -0700490 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700491 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
492 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700493 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700494 }
495
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000496 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700497 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
498 // This cast is safe as long as the id < 0x00FFFFFF
499 // Since we jail all the dynamically generated views, there should be no clashes
500 // with any other views.
501 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000502 }
503
Tony Wickham010d2552017-01-20 08:15:28 -0800504 public PopupDataProvider getPopupDataProvider() {
505 return mPopupDataProvider;
506 }
507
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000508 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700509 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
510 * a configuration step, this allows the proper animations to run after other transitions.
511 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 private long completeAdd(
513 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
514 long screenId = info.screenId;
515 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700516 // When the screen id represents an actual screen (as opposed to a rank) we make sure
517 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700518 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700519 }
Adam Cohendb364c32014-05-20 14:23:40 -0700520
Sunny Goyal2100c782016-08-22 16:00:03 -0700521 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700523 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700524 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800525 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700527 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700528 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700529 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700530 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700531 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700532 int widgetId = appWidgetId;
533 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700534 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700535 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700536 // Since the view was just bound, also launch the configure activity if needed
537 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
538 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800539 if (provider != null) {
540 new WidgetAddFlowHandler(provider)
541 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700542 }
543 }
544 break;
545 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800546 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700547
Adam Cohendb364c32014-05-20 14:23:40 -0700548 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800549 }
550
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800551 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700552 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700553 if (isWorkspaceLoading()) {
554 // process the result once the workspace has loaded.
555 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
556 return;
557 }
558 mPendingActivityResult = null;
559
Winson Chunge341d302013-08-16 14:31:00 -0700560 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700561 final PendingRequestArgs requestArgs = mPendingRequestArgs;
562 setWaitingForResult(null);
563 if (requestArgs == null) {
564 return;
565 }
566
567 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700568
Adam Cohenad4e15c2013-10-17 16:21:35 -0700569 Runnable exitSpringLoaded = new Runnable() {
570 @Override
571 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700572 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700573 }
574 };
575
Michael Jurka8b805b12012-04-18 14:23:14 -0700576 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700577 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700578 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700579 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
580 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700581 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700582 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700583 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700585 addAppWidgetImpl(
586 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800587 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700588 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700589 }
590 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200591 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700592 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700593 // User could have free-scrolled between pages before picking a wallpaper; make sure
594 // we move to the closest one now.
595 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700596 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200597 }
598 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700599 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200600
Adam Cohened66b2b2012-01-23 17:28:51 -0800601 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700602 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700603
Adam Cohened66b2b2012-01-23 17:28:51 -0800604 // We have special handling for widgets
605 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800606 final int appWidgetId;
607 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
608 : -1;
609 if (widgetId < 0) {
610 appWidgetId = pendingAddWidgetId;
611 } else {
612 appWidgetId = widgetId;
613 }
614
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800616 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700617 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
618 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700619 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700621 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700622 @Override
623 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700624 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700625 }
626 };
Adam Cohendb364c32014-05-20 14:23:40 -0700627
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
629 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
630 } else {
631 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
632 // When the screen id represents an actual screen (as opposed to a rank)
633 // we make sure that the drop page actually exists.
634 requestArgs.screenId =
635 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700636 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700637 final CellLayout dropLayout =
638 mWorkspace.getScreenWithId(requestArgs.screenId);
639
640 dropLayout.setDropPending(true);
641 final Runnable onComplete = new Runnable() {
642 @Override
643 public void run() {
644 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
645 dropLayout.setDropPending(false);
646 }
647 };
648 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
649 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700650 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 return;
652 }
653
Sunny Goyald478c832016-04-01 12:04:16 -0700654 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
655 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700656 if (resultCode == RESULT_OK) {
657 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700658 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700659 }
660 // Leave the widget in the pending state if the user canceled the configure.
661 return;
662 }
663
Sunny Goyalaad90582015-07-09 14:22:50 -0700664 if (requestCode == REQUEST_CREATE_SHORTCUT) {
665 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
667 completeAdd(requestCode, data, -1, requestArgs);
668 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
669 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
670
Sunny Goyalaad90582015-07-09 14:22:50 -0700671 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700672 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
673 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800676 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800677 }
678
679 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700680 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800681 final int requestCode, final int resultCode, final Intent data) {
682 handleActivityResult(requestCode, resultCode, data);
683 if (mLauncherCallbacks != null) {
684 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
685 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 }
687
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700688 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700689 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600690 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700691 PendingRequestArgs pendingArgs = mPendingRequestArgs;
692 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
693 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
694 setWaitingForResult(null);
695
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700698 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700699 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700700 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700701 Intent intent = pendingArgs.getPendingIntent();
702
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 if (grantResults.length > 0
704 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700705 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700706 } else {
707 // TODO: Show a snack bar with link to settings
708 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700709 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700710 }
711 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600712 if (mLauncherCallbacks != null) {
713 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
714 grantResults);
715 }
716 }
717
Adam Cohendb364c32014-05-20 14:23:40 -0700718 /**
719 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
720 *
721 * @param screenId the screen id to check
722 * @return the new screen, or screenId if it exists
723 */
724 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800725 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700726 if (dropLayout == null) {
727 // it's possible that the add screen was removed because it was
728 // empty and a re-bind occurred
729 mWorkspace.addExtraEmptyScreen();
730 return mWorkspace.commitExtraEmptyScreen();
731 } else {
732 return screenId;
733 }
734 }
735
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 @Thunk void completeTwoStageWidgetDrop(
737 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
738 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 Runnable onCompleteRunnable = null;
740 int animationType = 0;
741
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700742 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 if (resultCode == RESULT_OK) {
744 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
745 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800746 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700747 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 onCompleteRunnable = new Runnable() {
749 @Override
750 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700752 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 }
754 };
755 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800756 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700759 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700760 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700761 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
762 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700764 // The animated view may be null in the case of a rotation during widget configuration
765 onCompleteRunnable.run();
766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768
769 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700770 protected void onStop() {
771 super.onStop();
772 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700773
774 if (mLauncherCallbacks != null) {
775 mLauncherCallbacks.onStop();
776 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800777 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800778
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700779 if (!mAppLaunchSuccess) {
780 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700781 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700782 }
Tony Wickham010d2552017-01-20 08:15:28 -0800783 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700784 }
785
786 @Override
787 protected void onStart() {
788 super.onStart();
789 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700790
791 if (mLauncherCallbacks != null) {
792 mLauncherCallbacks.onStart();
793 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800794 mAppWidgetHost.setListenIfResumed(true);
Tony Wickham010d2552017-01-20 08:15:28 -0800795
796 if (!isWorkspaceLoading()) {
797 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
798 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700799
Jon Miranda7fda2852017-07-19 16:07:01 -0700800 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700801 if (mScrimAnimator != null) {
802 mScrimAnimator.cancel();
803 }
804 mDragLayer.getBackground().setAlpha(0);
805 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
806 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
807 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
808 @Override
809 public void onAnimationEnd(Animator animation) {
810 mScrimAnimator = null;
811 }
812 });
813 mScrimAnimator.setDuration(600);
814 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
815 mScrimAnimator.start();
816 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700817 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700818 }
819
820 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700822 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700824 TraceHelper.partitionSection("ON_RESUME", "superCall");
825
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700826 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700827 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800828 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700829 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700830 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700831 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700832 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700834 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200835 // We might have postponed some bind calls until onResume (see waitUntilResume) --
836 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700837 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
838 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200839 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700840 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200841 }
Winson Chunge4e50662012-01-23 14:45:13 -0800842
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700843 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700844 // Process any items that were added while Launcher was away.
845 InstallShortcutReceiver.disableAndFlushInstallQueue(
846 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700847
Sunny Goyala474a9b2017-05-04 16:47:11 -0700848 // Refresh shortcuts if the permission changed.
849 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700850
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700851 if (shouldShowDiscoveryBounce()) {
852 mAllAppsController.showDiscoveryBounce();
853 }
Adam Cohen9211d422014-10-07 18:14:53 -0700854 if (mLauncherCallbacks != null) {
855 mLauncherCallbacks.onResume();
856 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800857
Sunny Goyala502aa32017-10-02 16:04:06 -0700858 clearTypedText();
859
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700860 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700861 }
862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700864 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700865 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700866 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700867
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700868 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700869 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800870 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700871 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700872
Adam Cohen9211d422014-10-07 18:14:53 -0700873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700875 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700876 }
877
Adam Cohenc2d6e892014-10-16 09:49:24 -0700878 public interface LauncherOverlay {
879
880 /**
881 * Touch interaction leading to overscroll has begun
882 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700883 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884
885 /**
886 * Touch interaction related to overscroll has ended
887 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700888 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700889
890 /**
891 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
892 * screen (or in the case of RTL, the rightmost screen).
893 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700894 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700895
896 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800897 * Called when the launcher is ready to use the overlay
898 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700899 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700900 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700901 }
902
903 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700904 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700905 }
906
907 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
908
Sunny Goyalc86df472016-02-25 09:19:38 -0800909 public void onScrollChanged(float progress) {
910 if (mWorkspace != null) {
911 mWorkspace.onOverlayScrollChanged(progress);
912 }
913 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700914 }
915
Sunny Goyal16764582017-11-06 16:00:34 -0800916 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700917 if (mLauncherCallbacks != null) {
918 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700919 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800920 // On O and above we there is always some setting present settings (add icon to
921 // home screen or icon badging). On earlier APIs we will have the allow rotation
922 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700923 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700924 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800925 }
926
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700927 @Override
928 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700930 if (mModel.isCurrentCallbacks(this)) {
931 mModel.stopLoader();
932 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700933 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
934
Romain Guy13c2e7b2010-03-10 19:45:00 -0800935 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700936 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700937
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800938 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800939 @Override
940 public void onWindowFocusChanged(boolean hasFocus) {
941 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700942 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
946 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800947 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 private boolean acceptFilter() {
950 final InputMethodManager inputManager = (InputMethodManager)
951 getSystemService(Context.INPUT_METHOD_SERVICE);
952 return !inputManager.isFullscreenMode();
953 }
954
955 @Override
956 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700957 final int uniChar = event.getUnicodeChar();
958 final boolean handled = super.onKeyDown(keyCode, event);
959 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
960 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
962 keyCode, event);
963 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700965 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 // showSearchDialog()
967 // If there are multiple keystrokes before the search dialog takes focus,
968 // onSearchRequested() will be called for every keystroke,
969 // but it is idempotent, so it's fine.
970 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 }
972 }
973
Joe Onorato8a9625e2010-01-28 15:55:35 -0800974 // Eat the long press event so the keyboard doesn't come up.
975 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
976 return true;
977 }
978
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 return handled;
980 }
981
Karl Rosaen138a0412009-04-23 19:00:21 -0700982 private String getTypedText() {
983 return mDefaultKeySsb.toString();
984 }
985
Sunny Goyal6f28e712016-09-13 14:19:29 -0700986 @Override
987 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700988 mDefaultKeySsb.clear();
989 mDefaultKeySsb.clearSpans();
990 Selection.setSelection(mDefaultKeySsb, 0);
991 }
992
Sunny Goyalf9403d92017-10-18 10:55:56 -0700993 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700994 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700995 }
996
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 /**
998 * Restores the previous state, if it exists.
999 *
1000 * @param savedState The previous state.
1001 */
1002 private void restoreState(Bundle savedState) {
1003 if (savedState == null) {
1004 return;
1005 }
1006
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001007 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001008 LauncherState[] stateValues = LauncherState.values();
1009 LauncherState state = stateValues[stateOrdinal];
1010 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001011 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001012 }
1013
Sunny Goyal2100c782016-08-22 16:00:03 -07001014 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1015 if (requestArgs != null) {
1016 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001018
1019 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001020
1021 SparseArray<Parcelable> widgetsState =
1022 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1023 if (widgetsState != null) {
1024 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027
1028 /**
1029 * Finds all the views we need and configure them properly.
1030 */
1031 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001032 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001033 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001034 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001035 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001036 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001037
Sunny Goyal784f9c32016-03-23 16:56:54 -07001038 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1039 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1040 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041
Winson Chung4c98d922011-05-31 16:50:48 -07001042 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001043 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001044
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 // Setup the hotseat
1046 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1047 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001048 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001049 }
1050
Winson Chung4c98d922011-05-31 16:50:48 -07001051 // Setup the workspace
1052 mWorkspace.setHapticFeedbackEnabled(false);
1053 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001054 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001055 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1056 // default state, otherwise we will update to the wrong offsets in RTL
1057 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001058 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001059 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001060
Tony Wickham34d2c912015-09-11 09:27:58 -07001061 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001062 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001063
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001064 // Setup Apps
1065 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001066
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001068 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001069 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001070
Hyunyoung Songd725f642017-08-17 22:26:35 -07001071 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
1073
1074 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001075 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001076 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001077 */
1078 public void setAllAppsButton(View allAppsButton) {
1079 mAllAppsButton = allAppsButton;
1080 }
1081
Anjali Koppal5ad44842014-03-10 20:34:39 -07001082 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 * Creates a view representing a shortcut.
1084 *
1085 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001087 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001088 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 }
1090
1091 /**
1092 * Creates a view representing a shortcut inflated from the specified resource.
1093 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 * @param parent The group the shortcut belongs to.
1095 * @param info The data structure describing the shortcut.
1096 *
1097 * @return A View inflated from layoutResId.
1098 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001099 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001100 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1101 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001102 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001104 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 return favorite;
1106 }
1107
1108 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001109 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 *
1111 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001113 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001114 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001115 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1116 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001117 return;
1118 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001119
Jon Mirandac476d6e2017-05-04 16:30:01 -07001120 int[] cellXY = mTmpAddItemCellCoordinates;
1121 CellLayout layout = getCellLayout(container, screenId);
1122
Sunny Goyal782f0c92017-01-19 10:27:54 -08001123 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001124 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001125 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1126 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001127 }
1128
1129 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001130 // Legacy shortcuts are only supported for primary profile.
1131 info = Process.myUserHandle().equals(args.user)
1132 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001133
Sunny Goyalb57645f2017-03-20 13:57:28 -07001134 if (info == null) {
1135 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1136 return;
1137 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001138 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001139 // The app is trying to add a shortcut without sufficient permissions
1140 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1141 return;
1142 }
1143 }
1144
Jon Mirandac476d6e2017-05-04 16:30:01 -07001145 if (container < 0) {
1146 // Adding a shortcut to the Workspace.
1147 final View view = createShortcut(info);
1148 boolean foundCellSpan = false;
1149 // First we check if we already know the exact location where we want to add this item.
1150 if (cellX >= 0 && cellY >= 0) {
1151 cellXY[0] = cellX;
1152 cellXY[1] = cellY;
1153 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001154
Jon Mirandac476d6e2017-05-04 16:30:01 -07001155 // If appropriate, either create a folder or add to an existing folder
1156 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001157 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001158 return;
1159 }
1160 DragObject dragObject = new DragObject();
1161 dragObject.dragInfo = info;
1162 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1163 true)) {
1164 return;
1165 }
1166 } else {
1167 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1168 }
1169
1170 if (!foundCellSpan) {
1171 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001172 return;
1173 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001174
1175 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1176 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001177 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001178 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001179 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001180 if (folderIcon != null) {
1181 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1182 folderInfo.add(info, args.rank, false);
1183 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001184 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001185 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 }
1188
Sunny Goyale29897f2017-07-20 10:09:42 -07001189 public FolderIcon findFolderIcon(final long folderIconId) {
1190 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1191 @Override
1192 public boolean evaluate(ItemInfo info, View view) {
1193 return info != null && info.id == folderIconId;
1194 }
1195 });
1196 }
1197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001199 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001201 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001203 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001204 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1205
Adam Cohened66b2b2012-01-23 17:28:51 -08001206 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001207 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001208 }
Romain Guycbb89e42009-06-08 15:52:54 -07001209
Adam Cohen59400422014-03-05 18:07:04 -08001210 LauncherAppWidgetInfo launcherInfo;
1211 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001212 launcherInfo.spanX = itemInfo.spanX;
1213 launcherInfo.spanY = itemInfo.spanY;
1214 launcherInfo.minSpanX = itemInfo.minSpanX;
1215 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001216 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001217
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001218 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001219 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
Sunny Goyal2100c782016-08-22 16:00:03 -07001221 if (hostView == null) {
1222 // Perform actual inflation because we're live
1223 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001225 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301226 prepareAppWidget(hostView, launcherInfo);
1227 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
Romain Guycbb89e42009-06-08 15:52:54 -07001229
Sunny Goyald5462aa2016-11-22 16:52:39 +05301230 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001231 hostView.setTag(item);
1232 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001233 hostView.setFocusable(true);
1234 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001235 }
1236
Adam Cohended9f8d2010-11-03 13:25:16 -07001237 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1238 @Override
1239 public void onReceive(Context context, Intent intent) {
1240 final String action = intent.getAction();
1241 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001242 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001243 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001244 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001245 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001246 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001247 mShouldFadeInScrim = true;
1248 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1249 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1250 // the user unlocked and the Launcher is not in the foreground.
1251 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001252 }
1253 }
1254 };
1255
Tony Wickham010d2552017-01-20 08:15:28 -08001256 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1257 Runnable r = new Runnable() {
1258 @Override
1259 public void run() {
1260 mWorkspace.updateIconBadges(updatedBadges);
1261 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001262
Sunny Goyal00ac9202017-11-09 15:24:06 -08001263 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1264 if (popup != null) {
1265 popup.updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001266 }
Tony Wickham010d2552017-01-20 08:15:28 -08001267 }
1268 };
1269 if (!waitUntilResume(r)) {
1270 r.run();
1271 }
1272 }
1273
Adam Cohended9f8d2010-11-03 13:25:16 -07001274 @Override
1275 public void onAttachedToWindow() {
1276 super.onAttachedToWindow();
1277
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001278 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001279 if (mLauncherCallbacks != null) {
1280 mLauncherCallbacks.onAttachedToWindow();
1281 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001282 }
1283
1284 @Override
1285 public void onDetachedFromWindow() {
1286 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001287
1288 if (mLauncherCallbacks != null) {
1289 mLauncherCallbacks.onDetachedFromWindow();
1290 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001291 }
1292
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001293 public AllAppsTransitionController getAllAppsController() {
1294 return mAllAppsController;
1295 }
1296
Winson Chunga6945242014-01-08 14:04:34 -08001297 public DragLayer getDragLayer() {
1298 return mDragLayer;
1299 }
1300
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001301 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001302 return mAppsView;
1303 }
1304
Winson Chunga6945242014-01-08 14:04:34 -08001305 public Workspace getWorkspace() {
1306 return mWorkspace;
1307 }
1308
1309 public Hotseat getHotseat() {
1310 return mHotseat;
1311 }
1312
Winson Chung8ae41982017-11-10 11:42:13 -08001313 public <T extends ViewGroup> T getOverviewPanel() {
1314 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001315 }
1316
Sunny Goyal47328fd2016-05-25 18:56:41 -07001317 public DropTargetBar getDropTargetBar() {
1318 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001319 }
1320
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001321 public LauncherAppWidgetHost getAppWidgetHost() {
1322 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
Romain Guycbb89e42009-06-08 15:52:54 -07001324
Winson Chunga9abd0e2010-10-27 17:18:37 -07001325 public LauncherModel getModel() {
1326 return mModel;
1327 }
1328
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001329 public ModelWriter getModelWriter() {
1330 return mModelWriter;
1331 }
1332
Adam Cohen79d90c52016-04-22 13:29:20 -07001333 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001334 return mSharedPrefs;
1335 }
1336
Jon Miranda6bed3502017-09-19 14:43:17 -07001337 public int getOrientation() { return mOrientation; }
1338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 @Override
1340 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001341 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 super.onNewIntent(intent);
1343
Winson15f8b172015-08-19 11:02:39 -07001344 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1345 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1346 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001347
1348 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001349 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001350 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001351 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001352 boolean internalStateHandled = InternalStateHandler
1353 .handleNewIntent(this, intent, alreadyOnHome);
1354
Winson15f8b172015-08-19 11:02:39 -07001355 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001356 if (!internalStateHandled) {
1357 // Note: There should be at most one log per method call. This is enforced
1358 // implicitly by using if-else statements.
1359 UserEventDispatcher ued = getUserEventDispatcher();
1360 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1361 if (topOpenView != null) {
1362 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1363 } else if (alreadyOnHome) {
1364 Target target = newContainerTarget(mStateManager.getState().containerType);
1365 target.pageIndex = mWorkspace.getCurrentPage();
1366 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1367 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001368
Sunny Goyald3864fa2017-11-14 15:06:36 -08001369 // In all these cases, only animate if we're already on home
1370 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001371
Sunny Goyald3864fa2017-11-14 15:06:36 -08001372 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1373
1374 // Reset the apps view
1375 if (!alreadyOnHome && mAppsView != null) {
1376 mAppsView.reset();
1377 }
1378
1379 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1380 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1381 }
1382 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001383
1384 final View v = getWindow().peekDecorView();
1385 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001386 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001387 }
1388
Adam Cohen9211d422014-10-07 18:14:53 -07001389 if (mLauncherCallbacks != null) {
1390 mLauncherCallbacks.onHomeIntent();
1391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
Winson15f8b172015-08-19 11:02:39 -07001393
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001394 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001398 public void onRestoreInstanceState(Bundle state) {
1399 super.onRestoreInstanceState(state);
1400 for (int page: mSynchronouslyBoundPages) {
1401 mWorkspace.restoreInstanceStateForChild(page);
1402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404
1405 @Override
1406 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001407 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001408 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001409
Adam Cohen21cd0022013-10-09 18:57:02 -07001410 }
Sunny Goyalea609262017-10-25 15:47:38 -07001411 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001412
1413
1414 AbstractFloatingView widgets = AbstractFloatingView
1415 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1416 if (widgets != null) {
1417 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1418 widgets.saveHierarchyState(widgetsState);
1419 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1420 } else {
1421 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1422 }
1423
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001424 // We close any open folders and shortcut containers since they will not be re-opened,
1425 // and we need to make sure this state is reflected.
1426 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001427
Sunny Goyal2100c782016-08-22 16:00:03 -07001428 if (mPendingRequestArgs != null) {
1429 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1430 }
1431 if (mPendingActivityResult != null) {
1432 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 }
1434
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001435 super.onSaveInstanceState(outState);
1436
Adam Cohen9211d422014-10-07 18:14:53 -07001437 if (mLauncherCallbacks != null) {
1438 mLauncherCallbacks.onSaveInstanceState(outState);
1439 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
1441
1442 @Override
1443 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001445
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001446 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001447 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001448
Winson Chungcd2b0142011-06-08 16:02:26 -07001449 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001450 // It's possible to receive onDestroy after a new Launcher activity has
1451 // been created. In this case, don't interfere with the new Launcher.
1452 if (mModel.isCurrentCallbacks(this)) {
1453 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001454 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001455 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001456
Sunny Goyal745bad92016-05-02 10:54:12 -07001457 if (mRotationPrefChangeHandler != null) {
1458 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1459 }
1460
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001462 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001464 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001466 mAppWidgetHost = null;
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001469 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1470
Michael Jurka2ecf9952012-06-18 12:52:28 -07001471 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001472
Tony2917a8b2017-07-31 23:29:42 -07001473 clearPendingBinds();
1474
Adam Cohen9211d422014-10-07 18:14:53 -07001475 if (mLauncherCallbacks != null) {
1476 mLauncherCallbacks.onDestroy();
1477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479
Sunny Goyalae502842016-06-17 08:43:56 -07001480 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1481 return mAccessibilityDelegate;
1482 }
1483
Adam Cohena9cf38f2011-05-02 15:36:58 -07001484 public DragController getDragController() {
1485 return mDragController;
1486 }
1487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001489 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001490 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001491 }
1492
1493 @Override
1494 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1495 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001496 try {
1497 super.startIntentSenderForResult(intent, requestCode,
1498 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1499 } catch (IntentSender.SendIntentException e) {
1500 throw new ActivityNotFoundException();
1501 }
1502 }
1503
Winson Chung70d72102011-08-12 11:24:35 -07001504 /**
1505 * Indicates that we want global search for this activity by setting the globalSearch
1506 * argument for {@link #startSearch} to true.
1507 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001509 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001511
Karl Rosaen138a0412009-04-23 19:00:21 -07001512 if (initialQuery == null) {
1513 // Use any text typed in the launcher as the initial query
1514 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 if (appSearchData == null) {
1517 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001518 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001520
Sunny Goyal6f28e712016-09-13 14:19:29 -07001521 if (mLauncherCallbacks == null ||
1522 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1523 // Starting search from the callbacks failed. Start the default global search.
1524 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001525 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001526
1527 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001528 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001529 }
1530
Ian Parkinson047036e2014-09-01 15:40:53 +01001531 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001532 * Starts the global search activity. This code is a copied from SearchManager
1533 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001534 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001535 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001536 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001537 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1538 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1539 if (globalSearchActivity == null) {
1540 Log.w(TAG, "No global search activity found.");
1541 return;
1542 }
1543 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1544 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1545 intent.setComponent(globalSearchActivity);
1546 // Make sure that we have a Bundle to put source in
1547 if (appSearchData == null) {
1548 appSearchData = new Bundle();
1549 } else {
1550 appSearchData = new Bundle(appSearchData);
1551 }
Adam Cohen9211d422014-10-07 18:14:53 -07001552 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001553 if (!appSearchData.containsKey("source")) {
1554 appSearchData.putString("source", getPackageName());
1555 }
1556 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1557 if (!TextUtils.isEmpty(initialQuery)) {
1558 intent.putExtra(SearchManager.QUERY, initialQuery);
1559 }
1560 if (selectInitialQuery) {
1561 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1562 }
1563 intent.setSourceBounds(sourceBounds);
1564 try {
1565 startActivity(intent);
1566 } catch (ActivityNotFoundException ex) {
1567 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570
Winson Chung70d72102011-08-12 11:24:35 -07001571 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001572 public boolean onPrepareOptionsMenu(Menu menu) {
1573 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001574 if (mLauncherCallbacks != null) {
1575 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1576 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001577 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001580 @Override
1581 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001582 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001583 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001584 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001585 }
1586
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001588 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001589 }
1590
Adam Cohen517a7f52014-03-01 12:12:59 -08001591 public boolean isWorkspaceLoading() {
1592 return mWorkspaceLoading;
1593 }
1594
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001595 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001596 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001597 }
1598
Sunny Goyal04a324a2017-01-20 21:08:59 -08001599 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001600 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001601 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001602
Sunny Goyal2100c782016-08-22 16:00:03 -07001603 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001604 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001605 if (LOGD) {
1606 Log.d(TAG, "Adding widget from drop");
1607 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001608 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001609 }
1610
Sunny Goyal2100c782016-08-22 16:00:03 -07001611 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001612 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1613 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1614 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001615
Adam Cohenad4e15c2013-10-17 16:21:35 -07001616 Runnable onComplete = new Runnable() {
1617 @Override
1618 public void run() {
1619 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001620 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001621 }
1622 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001623 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001624 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
1626 }
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001628 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1629 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001630 info.container = container;
1631 info.screenId = screenId;
1632 if (cell != null) {
1633 info.cellX = cell[0];
1634 info.cellY = cell[1];
1635 }
1636 info.spanX = spanX;
1637 info.spanY = spanY;
1638
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001639 switch (info.itemType) {
1640 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1641 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001642 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001643 break;
1644 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001645 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001646 break;
1647 default:
1648 throw new IllegalStateException("Unknown item type: " + info.itemType);
1649 }
1650 }
1651
Adam Cohenfbba09b2011-07-18 21:43:05 -07001652 /**
1653 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001654 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001655 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001656 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1657 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001658 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1659 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1660 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001661 }
1662
Adam Cohenfbba09b2011-07-18 21:43:05 -07001663 /**
1664 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001665 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001666 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001667 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001668 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001669 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001670 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001671 // In the case where we've prebound the widget, we remove it from the DragLayer
1672 if (LOGD) {
1673 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1674 }
1675 getDragLayer().removeView(hostView);
1676
Adam Cohened66b2b2012-01-23 17:28:51 -08001677 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001678 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001679
1680 // Clear the boundWidget so that it doesn't get destroyed.
1681 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001682 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001683 // In this case, we either need to start an activity to get permission to bind
1684 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001685 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1686 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1687 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1688 this, info.componentName);
1689 } else {
1690 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1691 }
Adam Cohendd70d662012-10-04 16:53:44 -07001692 Bundle options = info.bindOptions;
1693
Sunny Goyalffe83f12014-08-14 17:39:34 -07001694 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1695 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001696 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001697 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001698 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001699 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001700 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001701 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001702 }
1703
Adam Cohendcd297f2013-06-18 13:13:40 -07001704 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001705 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001706 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 folderInfo.title = getText(R.string.folder_name);
1708
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001710 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711
1712 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001713 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301714 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001715 // Force measure the new folder icon
1716 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1717 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001718 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
Romain Guycbb89e42009-06-08 15:52:54 -07001720
Winsonc0b52fe2015-09-09 16:38:15 -07001721 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001722 * Unbinds the view for the specified item, and removes the item and all its children.
1723 *
1724 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001725 * @param itemInfo the {@link ItemInfo} for this view.
1726 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001727 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001728 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001729 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001730 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001731 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1732 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001733 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001734 } else {
1735 mWorkspace.removeWorkspaceItem(v);
1736 }
Winsonc0b52fe2015-09-09 16:38:15 -07001737 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001738 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001739 }
1740 } else if (itemInfo instanceof FolderInfo) {
1741 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001742 if (v instanceof FolderIcon) {
1743 ((FolderIcon) v).removeListeners();
1744 }
Winsonc0b52fe2015-09-09 16:38:15 -07001745 mWorkspace.removeWorkspaceItem(v);
1746 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001747 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001748 }
1749 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1750 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001751 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001752 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001753 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001754 }
1755 } else {
1756 return false;
1757 }
1758 return true;
1759 }
1760
1761 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001762 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001763 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001764 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001765 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001766 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001767 // Deleting an app widget ID is a void call but writes to disk before returning
1768 // to the caller...
1769 new AsyncTask<Void, Void, Void>() {
1770 public Void doInBackground(Void ... args) {
1771 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1772 return null;
1773 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001774 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001775 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001776 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001777 }
1778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 @Override
1780 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001781 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783
Joe Onorato88ec0992009-11-19 13:16:06 -08001784 @Override
1785 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001786 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1787 return;
1788 }
1789
Sunny Goyal45478022015-06-08 16:52:41 -07001790 if (mDragController.isDragging()) {
1791 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001792 return;
1793 }
1794
Jon Mirandafeba90f2016-10-06 10:53:29 -07001795 // Note: There should be at most one log per method call. This is enforced implicitly
1796 // by using if-else statements.
1797 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001798 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1799 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001800 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001801 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001802 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001803 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001804 } else {
1805 // Back button is a no-op here, but give at least some feedback for the button press
1806 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001807 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001808 }
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 * Launches the intent referred by the clicked shortcut.
1812 *
1813 * @param v The view representing the clicked shortcut.
1814 */
1815 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001816 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1817 // view has detached (it's possible for this to happen if the view is removed mid touch).
1818 if (v.getWindowToken() == null) {
1819 return;
1820 }
1821
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001822 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001823 return;
1824 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001825
Adam Cohen1697b792013-09-17 19:08:21 -07001826 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001827 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001828 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1829 LauncherLogProto.Action.Direction.NONE,
1830 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001831 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001832 }
1833 return;
1834 }
1835
1836 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001837 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001838 int page = mWorkspace.indexOfChild(v);
1839 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1840 LauncherLogProto.Action.Direction.NONE,
1841 LauncherLogProto.ContainerType.OVERVIEW, page);
1842 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001843 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001844 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001845 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001846 }
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001849 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001850 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001852 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001853 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001854 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001855 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001856 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001857 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001858 } else if (tag instanceof AppInfo) {
1859 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001860 } else if (tag instanceof LauncherAppWidgetInfo) {
1861 if (v instanceof PendingAppWidgetHostView) {
1862 onClickPendingWidget((PendingAppWidgetHostView) v);
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
1865 }
1866
Sunny Goyal70660032015-05-14 00:07:08 -07001867 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001868 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001869 return false;
1870 }
1871
Michael Jurkaaf442092010-06-10 17:01:57 -07001872 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001873 * Event handler for the app widget view which has not fully restored.
1874 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001875 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001876 if (mIsSafeModeEnabled) {
1877 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1878 return;
1879 }
1880
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001881 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001882 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001883 LauncherAppWidgetProviderInfo appWidgetInfo =
1884 mAppWidgetManager.findProvider(info.providerName, info.user);
1885 if (appWidgetInfo == null) {
1886 return;
1887 }
1888 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1889
Sunny Goyald478c832016-04-01 12:04:16 -07001890 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1891 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1892 // This should not happen, as we make sure that an Id is allocated during bind.
1893 return;
1894 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001895 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1896 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001897 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001898 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001899 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001900 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001901 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001902 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001903 }
1904 }
1905
1906 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001907 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1908 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1909 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001910 *
1911 * @param v The view that was clicked.
1912 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001913 protected void onClickAllAppsButton(View v) {
1914 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001915 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301916 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1917 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001918 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001919 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001920 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001921 }
1922 }
1923
Sunny Goyale6e72002017-01-12 16:55:36 -08001924 private void onClickPendingAppItem(final View v, final String packageName,
1925 boolean downloadStarted) {
1926 if (downloadStarted) {
1927 // If the download has started, simply direct to the market app.
1928 startMarketIntentForPackage(v, packageName);
1929 return;
1930 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001931 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001932 .setTitle(R.string.abandoned_promises_title)
1933 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001934 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1935 @Override
1936 public void onClick(DialogInterface dialogInterface, int i) {
1937 startMarketIntentForPackage(v, packageName);
1938 }
1939 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001940 .setNeutralButton(R.string.abandoned_clean_this,
1941 new DialogInterface.OnClickListener() {
1942 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001943 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001944 mWorkspace.removeAbandonedPromise(packageName, user);
1945 }
1946 })
1947 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001948 }
1949
1950 private void startMarketIntentForPackage(View v, String packageName) {
1951 ItemInfo item = (ItemInfo) v.getTag();
1952 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001953 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001954 }
1955
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001956 /**
1957 * Event handler for an app shortcut click.
1958 *
1959 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1960 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001961 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001962 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1963 Object tag = v.getTag();
1964 if (!(tag instanceof ShortcutInfo)) {
1965 throw new IllegalArgumentException("Input must be a Shortcut");
1966 }
1967
1968 // Open shortcut
1969 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001970
Sunny Goyal076839c2017-10-30 13:52:20 -07001971 if (shortcut.isDisabled()) {
1972 if ((shortcut.runtimeStatusFlags &
1973 ~FLAG_DISABLED_SUSPENDED &
1974 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001975 // If the app is only disabled because of the above flags, launch activity anyway.
1976 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001977 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001978 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1979 // Use a message specific to this shortcut, if it has one.
1980 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1981 return;
1982 }
1983 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001984 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001985 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001986 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001987 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1988 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001989 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001990 }
1991 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1992 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001993 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001994 }
1995
Chris Wren40c5ed32014-06-24 18:24:23 -04001996 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001997 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001998 String packageName = shortcut.intent.getComponent() != null ?
1999 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2000 if (!TextUtils.isEmpty(packageName)) {
2001 onClickPendingAppItem(v, packageName,
2002 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2003 return;
2004 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002005 }
2006
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002007 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002008 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002009 }
2010
Sunny Goyala7ce1662016-05-31 15:01:35 -07002011 private void startAppShortcutOrInfoActivity(View v) {
2012 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002013 Intent intent;
2014 if (item instanceof PromiseAppInfo) {
2015 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2016 intent = promiseAppInfo.getMarketIntent();
2017 } else {
2018 intent = item.getIntent();
2019 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002020 if (intent == null) {
2021 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002022 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002023 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002024 }
2025
2026 /**
2027 * Event handler for a folder icon click.
2028 *
2029 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2030 */
2031 protected void onClickFolderIcon(View v) {
2032 if (LOGD) Log.d(TAG, "onClickFolder");
2033 if (!(v instanceof FolderIcon)){
2034 throw new IllegalArgumentException("Input must be a FolderIcon");
2035 }
2036
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002037 Folder folder = ((FolderIcon) v).getFolder();
2038 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002039 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002040 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002041 }
Michael Jurka5130e402011-10-13 04:55:35 -07002042 }
2043
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002044 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002045 * Event handler for the wallpaper picker button that appears after a long press
2046 * on the home screen.
2047 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002048 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002049 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002050 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2051 return;
2052 }
2053
Tony Wickham785f7a52015-08-31 17:28:32 -07002054 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2055 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002056 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002057 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002058 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002059
2060 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002061 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2062 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002063 intent.setPackage(pickerPackage);
2064 }
2065
Sunny Goyala5ace712017-11-15 17:12:51 -08002066 final Bundle launchOptions;
2067 if (v != null) {
2068 intent.setSourceBounds(getViewBounds(v));
2069 // If there is no target package, use the default intent chooser animation
2070 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
2071 } else {
2072 launchOptions = null;
2073 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002074 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08002075 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002076 } catch (ActivityNotFoundException e) {
2077 setWaitingForResult(null);
2078 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2079 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002080 }
2081
Sunny Goyala7ce1662016-05-31 15:01:35 -07002082 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002084 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2085 try {
2086 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2087 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2088 // is enabled by default on NYC.
2089 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2090 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002091
2092 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2093 String id = ((ShortcutInfo) info).getDeepShortcutId();
2094 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302095 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002096 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002097 } else {
2098 // Could be launching some bookkeeping activity
2099 startActivity(intent, optsBundle);
2100 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002101 } finally {
2102 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002105 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2106 // corresponding permission. Show the appropriate permission prompt if that
2107 // is the case.
2108 if (intent.getComponent() == null
2109 && Intent.ACTION_CALL.equals(intent.getAction())
2110 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2111 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002112
2113 setWaitingForResult(PendingRequestArgs
2114 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002115 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2116 REQUEST_PERMISSION_CALL_PHONE);
2117 } else {
2118 // No idea why this was thrown.
2119 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 }
2122 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002123
Sunny Goyalfe770c92016-09-27 14:38:58 -07002124 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002125 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002126 if (Utilities.ATLEAST_MARSHMALLOW) {
2127 int left = 0, top = 0;
2128 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002129 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002130 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002131 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002132 if (icon != null) {
2133 Rect bounds = icon.getBounds();
2134 left = (width - bounds.width()) / 2;
2135 top = v.getPaddingTop();
2136 width = bounds.width();
2137 height = bounds.height();
2138 }
2139 }
2140 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2141 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2142 // On L devices, we use the device default slide-up transition.
2143 // On L MR1 devices, we use a custom version of the slide-up transition which
2144 // doesn't have the delay present in the device default.
2145 return ActivityOptions.makeCustomAnimation(
2146 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2147 }
2148 return null;
2149 }
2150
Tonye3c59252017-05-02 21:32:27 -07002151 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002152 int[] pos = new int[2];
2153 v.getLocationOnScreen(pos);
2154 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2155 }
2156
Sunny Goyala7ce1662016-05-31 15:01:35 -07002157 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002158 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002159 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2160 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002161 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002162 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002163 // Only launch using the new animation if the shortcut has not opted out (this is a
2164 // private contract between launcher and may be ignored in the future).
2165 boolean useLaunchAnimation = (v != null) &&
2166 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2167 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2168
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002169 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002170
2171 // Prepare intent
2172 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2173 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002174 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002175 }
2176 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002177 if (Utilities.ATLEAST_MARSHMALLOW
2178 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002179 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002180 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002181 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002182 // Shortcuts need some special checks due to legacy reasons.
2183 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002184 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002185 // Could be launching some bookkeeping activity
2186 startActivity(intent, optsBundle);
2187 } else {
2188 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2189 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2190 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002191
2192 if (v instanceof BubbleTextView) {
2193 // This is set to the view that launched the activity that navigated the user away
2194 // from launcher. Since there is no callback for when the activity has finished
2195 // launching, enable the press state and keep this reference to reset the press
2196 // state when we return to launcher.
2197 BubbleTextView btv = (BubbleTextView) v;
2198 btv.setStayPressed(true);
2199 setOnResumeCallback(btv);
2200 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002201 mAppLaunchSuccess = true;
2202 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002203 } catch (ActivityNotFoundException|SecurityException e) {
2204 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2205 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2206 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002207 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002208 }
2209
Tony Wickhamdadb3042016-02-24 11:07:00 -08002210 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002211 public boolean dispatchTouchEvent(MotionEvent ev) {
2212 mLastDispatchTouchEventX = ev.getX();
2213 return super.dispatchTouchEvent(ev);
2214 }
2215
2216 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002218 if (!isDraggingEnabled()) return false;
2219 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002220 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221
Jon Miranda51f037d2016-11-07 08:37:20 -08002222 boolean ignoreLongPressToOverview =
2223 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002224
Adam Cohen1697b792013-09-17 19:08:21 -07002225 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002226 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002227 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302228 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2229 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002230 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002231 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002232 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2233 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2234 return true;
2235 } else {
2236 return false;
2237 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002238 } else {
2239 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002240 }
Adam Cohen1697b792013-09-17 19:08:21 -07002241 }
2242
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002243 CellLayout.CellInfo longClickCellInfo = null;
2244 View itemUnderLongClick = null;
2245 if (v.getTag() instanceof ItemInfo) {
2246 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002247 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002248 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002249 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 }
2251
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 // The hotseat touch handling does not go through Workspace, and we always allow long press
2253 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002254 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002256 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002257 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002258 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302259 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2260 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002261 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002262 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002263 return false;
2264 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302265 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2266 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002267 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002268 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002269 }
Jon Miranda379198e2016-09-23 08:54:40 -07002270 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2271 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002273 final boolean isAllAppsButton =
2274 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2275 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2276 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002277 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002279 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 }
2281 }
2282 }
2283 return true;
2284 }
2285
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002287 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 return mHotseat != null && layout != null &&
2289 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2290 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002291
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 /**
2293 * Returns the CellLayout of the specified container at the specified screen.
2294 */
Sunny Goyal92820592015-03-02 11:31:35 -08002295 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2297 if (mHotseat != null) {
2298 return mHotseat.getLayout();
2299 } else {
2300 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002301 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002302 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002303 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002304 }
2305 }
2306
Michael Jurkae326f182011-11-21 14:05:46 -08002307 @Override
2308 public void onTrimMemory(int level) {
2309 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002310 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2311 // The widget preview db can result in holding onto over
2312 // 3MB of memory for caching which isn't necessary.
2313 SQLiteDatabase.releaseMemory();
2314
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002315 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002316 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002317 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002318 if (mLauncherCallbacks != null) {
2319 mLauncherCallbacks.onTrimMemory(level);
2320 }
Michael Jurkae326f182011-11-21 14:05:46 -08002321 }
2322
Michael Jurkad7c28052012-04-27 15:43:36 -07002323 @Override
2324 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002325 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002326 final List<CharSequence> text = event.getText();
2327 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002328 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002329 // TODO: When can workspace be null?
2330 text.add(mWorkspace == null
2331 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002332 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002333 return result;
2334 }
2335
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002336 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002337 * If the activity is currently paused, signal that we need to run the passed Runnable
2338 * in onResume.
2339 *
2340 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002341 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2342 * wrong when we're not running, and if the activity comes back to what the configuration was
2343 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002344 *
2345 * Implementation of the method from LauncherModel.Callbacks.
2346 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002347 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002348 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002349 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002350 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002351 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002352 if (run instanceof RunnableWithId) {
2353 // Remove any runnables which have the same id
2354 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002355 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002356 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002357 return true;
2358 } else {
2359 return false;
2360 }
2361 }
2362
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002363 public void setOnResumeCallback(OnResumeCallback callback) {
2364 if (mOnResumeCallback != null) {
2365 mOnResumeCallback.onLauncherResume();
2366 }
2367 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002368 }
2369
Michael Jurka7607c2f2013-04-03 14:33:19 -07002370 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002371 * If the activity is currently paused, signal that we need to re-run the loader
2372 * in onResume.
2373 *
2374 * This needs to be called from incoming places where resources might have been loaded
2375 * while we are paused. That is becaues the Configuration might be wrong
2376 * when we're not running, and if it comes back to what it was when we
2377 * were paused, we are not restarted.
2378 *
2379 * Implementation of the method from LauncherModel.Callbacks.
2380 *
2381 * @return true if we are currently paused. The caller might be able to
2382 * skip some work in that case since we will come back again.
2383 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002384 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002385 public boolean setLoadOnResume() {
2386 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002387 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002388 mOnResumeNeedsLoad = true;
2389 return true;
2390 } else {
2391 return false;
2392 }
2393 }
2394
2395 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002398 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002399 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002400 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002401 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002402 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002403 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002404 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002405 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002406
Joe Onorato9c1289c2009-08-17 11:03:03 -04002407 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002408 * Clear any pending bind callbacks. This is called when is loader is planning to
2409 * perform a full rebind from scratch.
2410 */
2411 @Override
2412 public void clearPendingBinds() {
2413 mBindOnResumeCallbacks.clear();
2414 if (mPendingExecutor != null) {
2415 mPendingExecutor.markCompleted();
2416 mPendingExecutor = null;
2417 }
2418 }
2419
2420 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002421 * Refreshes the shortcuts shown on the workspace.
2422 *
2423 * Implementation of the method from LauncherModel.Callbacks.
2424 */
2425 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002426 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002427 // Floating panels (except the full widget sheet) are associated with individual icons. If
2428 // we are starting a fresh bind, close all such panels as all the icons are about
2429 // to go away.
2430 AbstractFloatingView.closeOpenViews(this, true,
2431 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002432
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002433 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002434
Winson Chungd64d1762013-08-20 14:37:16 -07002435 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002436 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002437 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002438
Winson Chung3d503fb2011-07-13 17:25:49 -07002439 if (mHotseat != null) {
2440 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002442 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 }
2444
Adam Cohendcd297f2013-06-18 13:13:40 -07002445 @Override
2446 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002447 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002448 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2449 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002450 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2451 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002452 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002453 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2454 // If there are no screens, we need to have an empty screen
2455 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002456 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002457 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002458
Winsonc7d2e832016-07-28 12:24:55 -07002459 // After we have added all the screens, if the wallpaper was locked to the default state,
2460 // then notify to indicate that it can be released and a proper wallpaper offset can be
2461 // computed before the next layout
2462 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002463 }
2464
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002465 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002466 int count = orderedScreenIds.size();
2467 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002468 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002469 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002470 // No need to bind the first screen, as its always bound.
2471 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2472 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002473 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 }
2475
Sunny Goyalb23980c2017-08-17 07:45:25 -07002476 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002477 public void bindAppsAdded(final ArrayList<Long> newScreens,
2478 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002479 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002480 Runnable r = new Runnable() {
2481 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002482 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002483 }
2484 };
2485 if (waitUntilResume(r)) {
2486 return;
2487 }
2488
Winson Chungd64d1762013-08-20 14:37:16 -07002489 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002490 if (newScreens != null) {
2491 bindAddScreens(newScreens);
2492 }
Winson Chungd64d1762013-08-20 14:37:16 -07002493
2494 // We add the items without animation on non-visible pages, and with
2495 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002496 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002497 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002498 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002499 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002500 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002501 }
Winson Chungc58497e2013-09-03 17:48:37 -07002502
Winson Chung87412982013-10-03 18:34:14 -07002503 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002504 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002505 }
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002508 * Bind the items start-end from the list.
2509 *
2510 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002512 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002513 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002514 Runnable r = new Runnable() {
2515 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002516 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002517 }
2518 };
2519 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002520 return;
2521 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002522
Sunny Goyal3be633b2016-12-08 09:59:25 -08002523 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002524 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002525 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002526 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002527 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002528 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002529 int end = items.size();
2530 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002531 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002532
2533 // Short circuit if we are loading dock items for a configuration which has no dock
2534 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2535 mHotseat == null) {
2536 continue;
2537 }
2538
Sunny Goyal639e9062015-08-19 19:17:06 -07002539 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002540 switch (item.itemType) {
2541 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2542 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002543 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002544 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002545 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002546 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002547 }
2548 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002549 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002550 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002551 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002553 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002554 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2555 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002556 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002557 if (view == null) {
2558 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002559 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002560 break;
2561 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002562 default:
2563 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002564 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002565
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002566 /*
2567 * Remove colliding items.
2568 */
2569 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2570 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2571 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2572 View v = cl.getChildAt(item.cellX, item.cellY);
2573 Object tag = v.getTag();
2574 String desc = "Collision while binding workspace item: " + item
2575 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002576 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002577 throw (new RuntimeException(desc));
2578 } else {
2579 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002580 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002581 continue;
2582 }
2583 }
2584 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302585 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002586 if (animateIcons) {
2587 // Animate all the applications up now
2588 view.setAlpha(0f);
2589 view.setScaleX(0f);
2590 view.setScaleY(0f);
2591 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002592 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002595
Winson Chung997a9232013-07-24 15:33:46 -07002596 if (animateIcons) {
2597 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002598 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002599 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002600 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002601 final Runnable startBounceAnimRunnable = new Runnable() {
2602 public void run() {
2603 anim.playTogether(bounceAnims);
2604 anim.start();
2605 }
2606 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002607 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002608 // We post the animation slightly delayed to prevent slowdowns
2609 // when we are loading right after we return to launcher.
2610 mWorkspace.postDelayed(new Runnable() {
2611 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002612 if (mWorkspace != null) {
2613 mWorkspace.snapToPage(newScreenIndex);
2614 mWorkspace.postDelayed(startBounceAnimRunnable,
2615 NEW_APPS_ANIMATION_DELAY);
2616 }
Winson Chung94d67682013-09-25 16:29:40 -07002617 }
2618 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002619 } else {
2620 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002621 }
2622 }
Winson Chung64359a52013-07-08 17:17:08 -07002623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002624 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 }
2626
Joe Onorato9c1289c2009-08-17 11:03:03 -04002627 /**
2628 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002629 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002630 public void bindAppWidget(LauncherAppWidgetInfo item) {
2631 View view = inflateAppWidget(item);
2632 if (view != null) {
2633 mWorkspace.addInScreen(view, item);
2634 mWorkspace.requestLayout();
2635 }
2636 }
2637
2638 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002639 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302640 PendingAppWidgetHostView view =
2641 new PendingAppWidgetHostView(this, item, mIconCache, true);
2642 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002643 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002644 }
2645
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002646 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002647
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002648 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002649
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002650 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2651 // If the provider is not ready, bind as a pending widget.
2652 appWidgetInfo = null;
2653 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2654 // The widget id is not valid. Try to find the widget based on the provider info.
2655 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2656 } else {
2657 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2658 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002659
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002660 // If the provider is ready, but the width is not yet restored, try to restore it.
2661 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2662 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002663 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002664 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2665 + " belongs to component " + item.providerName
2666 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002667 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002668 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002669 }
Sunny Goyalff572272014-07-23 13:58:07 -07002670
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002671 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002672 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002673 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2674 // Id has not been allocated yet. Allocate a new id.
2675 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2676 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002677
Sunny Goyald478c832016-04-01 12:04:16 -07002678 // Also try to bind the widget. If the bind fails, the user will be shown
2679 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002680 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002681 pendingInfo.spanX = item.spanX;
2682 pendingInfo.spanY = item.spanY;
2683 pendingInfo.minSpanX = item.minSpanX;
2684 pendingInfo.minSpanY = item.minSpanY;
2685 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002686
2687 boolean isDirectConfig =
2688 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2689 if (isDirectConfig && item.bindOptions != null) {
2690 Bundle newOptions = item.bindOptions.getExtras();
2691 if (options != null) {
2692 newOptions.putAll(options);
2693 }
2694 options = newOptions;
2695 }
Sunny Goyald478c832016-04-01 12:04:16 -07002696 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2697 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002698
Sunny Goyal86df1382016-08-10 15:03:22 -07002699 // We tried to bind once. If we were not able to bind, we would need to
2700 // go through the permission dialog, which means we cannot skip the config
2701 // activity.
2702 item.bindOptions = null;
2703 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2704
Sunny Goyald478c832016-04-01 12:04:16 -07002705 // Bind succeeded
2706 if (success) {
2707 // If the widget has a configure activity, it is still needs to set it up,
2708 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002709 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002710 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2711 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002712 }
Sunny Goyald478c832016-04-01 12:04:16 -07002713
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002714 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002715 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002716 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002717 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002718 // The widget was marked as UI not ready, but there is no configure activity to
2719 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002720 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002721 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002722 }
Sunny Goyalff572272014-07-23 13:58:07 -07002723 }
2724
Sunny Goyald5462aa2016-11-22 16:52:39 +05302725 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002726 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002727 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002728 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002729 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002730 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002731 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002732 }
2733
Sunny Goyal233ee962015-08-03 13:05:01 -07002734 item.minSpanX = appWidgetInfo.minSpanX;
2735 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302736 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002737 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302738 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002739 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302740 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002742 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002743 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002744 }
2745
Sunny Goyalff572272014-07-23 13:58:07 -07002746 /**
2747 * Restores a pending widget.
2748 *
2749 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002750 */
Sunny Goyald478c832016-04-01 12:04:16 -07002751 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002752 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2753 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2754 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002755 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002756 }
2757
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002758 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002759 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002760 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2761 info.pendingItemInfo = null;
2762 }
Sunny Goyalff572272014-07-23 13:58:07 -07002763
Sunny Goyalba47faa2017-10-04 16:50:57 -07002764 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2765 view.reinflate();
2766 }
2767
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002768 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002769 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002770 }
2771
Adam Cohen1462de32012-07-24 22:34:36 -07002772 public void onPageBoundSynchronously(int page) {
2773 mSynchronouslyBoundPages.add(page);
2774 }
2775
Sunny Goyal527c7d32015-08-28 15:19:36 -07002776 @Override
2777 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2778 if (mPendingExecutor != null) {
2779 mPendingExecutor.markCompleted();
2780 }
2781 mPendingExecutor = executor;
2782 executor.attachTo(this);
2783 }
2784
2785 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2786 if (mPendingExecutor == executor) {
2787 mPendingExecutor = null;
2788 }
2789 }
2790
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002791 @Override
2792 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2793 Runnable r = new Runnable() {
2794 public void run() {
2795 finishFirstPageBind(executor);
2796 }
2797 };
2798 if (waitUntilResume(r)) {
2799 return;
2800 }
2801
2802 Runnable onComplete = new Runnable() {
2803 @Override
2804 public void run() {
2805 if (executor != null) {
2806 executor.onLoadAnimationCompleted();
2807 }
2808 }
2809 };
2810 if (mDragLayer.getAlpha() < 1) {
2811 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2812 } else {
2813 onComplete.run();
2814 }
2815 }
2816
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 /**
2818 * Callback saying that there aren't any more items to bind.
2819 *
2820 * Implementation of the method from LauncherModel.Callbacks.
2821 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002822 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002823 Runnable r = new Runnable() {
2824 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002825 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002826 }
2827 };
2828 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002829 return;
2830 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002831 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002832 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002834 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002835
Sunny Goyal2100c782016-08-22 16:00:03 -07002836 if (mPendingActivityResult != null) {
2837 handleActivityResult(mPendingActivityResult.requestCode,
2838 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2839 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002840 }
2841
Sunny Goyala474a9b2017-05-04 16:47:11 -07002842 InstallShortcutReceiver.disableAndFlushInstallQueue(
2843 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002844
Tony Wickham010d2552017-01-20 08:15:28 -08002845 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002846 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002847 }
2848
Winson Chunga2413752012-04-03 14:22:34 -07002849 private boolean canRunNewAppsAnimation() {
2850 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002851 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002852 }
2853
Winson Chungc9168342013-06-26 14:54:55 -07002854 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002855 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002856 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2857 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002858 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002859 return bounceAnim;
2860 }
2861
Adam Cohen27772732013-11-11 14:00:56 +00002862 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002863 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002864 }
2865
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002866 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 * Add the icons for all apps.
2868 *
2869 * Implementation of the method from LauncherModel.Callbacks.
2870 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002871 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002872 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2873 public void run() {
2874 bindAllApplications(apps);
2875 }
2876 };
2877 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002878 return;
2879 }
2880
Winson Chungb745afb2015-03-02 11:51:23 -08002881 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002882 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002883 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002884 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002885 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002886 return;
2887 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002888
Winson Chungb745afb2015-03-02 11:51:23 -08002889 mAppsView.setApps(apps);
2890 }
Adam Cohen9211d422014-10-07 18:14:53 -07002891 if (mLauncherCallbacks != null) {
2892 mLauncherCallbacks.bindAllApplications(apps);
2893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 }
2895
2896 /**
Tony2917a8b2017-07-31 23:29:42 -07002897 * Returns an Executor that will run after the launcher is first drawn (including after the
2898 * initial fade in animation). Returns null if the first draw has already occurred.
2899 */
2900 public @Nullable Executor getPendingExecutor() {
2901 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2902 }
2903
2904 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002905 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2906 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2907 */
2908 @Override
2909 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002910 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002911 }
2912
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002913 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 * A package was updated.
2915 *
2916 * Implementation of the method from LauncherModel.Callbacks.
2917 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002918 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002919 Runnable r = new Runnable() {
2920 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002921 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002922 }
2923 };
2924 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002925 return;
2926 }
2927
Winson Chungb745afb2015-03-02 11:51:23 -08002928 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002929 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931 }
2932
Sunny Goyal4390ace2014-10-13 11:33:11 -07002933 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002934 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2935 Runnable r = new Runnable() {
2936 public void run() {
2937 bindPromiseAppProgressUpdated(app);
2938 }
2939 };
2940 if (waitUntilResume(r)) {
2941 return;
2942 }
2943
2944 if (mAppsView != null) {
2945 mAppsView.updatePromiseAppProgress(app);
2946 }
2947 }
2948
2949 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002950 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2951 Runnable r = new Runnable() {
2952 public void run() {
2953 bindWidgetsRestored(widgets);
2954 }
2955 };
2956 if (waitUntilResume(r)) {
2957 return;
2958 }
2959 mWorkspace.widgetsRestored(widgets);
2960 }
2961
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002963 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002964 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002965 *
2966 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002967 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002968 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002969 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002970 Runnable r = new Runnable() {
2971 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002972 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002973 }
2974 };
2975 if (waitUntilResume(r)) {
2976 return;
2977 }
2978
Sunny Goyal4390ace2014-10-13 11:33:11 -07002979 if (!updated.isEmpty()) {
2980 mWorkspace.updateShortcuts(updated);
2981 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002982 }
2983
2984 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002985 * Update the state of a package, typically related to install state.
2986 *
2987 * Implementation of the method from LauncherModel.Callbacks.
2988 */
Sunny Goyale755d462014-07-22 13:48:29 -07002989 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002990 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
2991 Runnable r = new Runnable() {
2992 public void run() {
2993 bindRestoreItemsChange(updates);
2994 }
2995 };
2996 if (waitUntilResume(r)) {
2997 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002998 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002999
Sunny Goyal756adbc2015-04-16 15:20:51 -07003000 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003001 }
3002
3003 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003004 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003005 * in addition to specific applications to remove, the reason being that
3006 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003007 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003008 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003010 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003011 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003012 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003013 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003014 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003015 }
Winson Chungd64d1762013-08-20 14:37:16 -07003016 };
3017 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003018 return;
3019 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003020 mWorkspace.removeItemsByMatcher(matcher);
3021 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003022 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003023
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003024 @Override
3025 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3026 Runnable r = new Runnable() {
3027 public void run() {
3028 bindAppInfosRemoved(appInfos);
3029 }
3030 };
3031 if (waitUntilResume(r)) {
3032 return;
Joe Onorato36115782010-06-17 13:28:48 -04003033 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003034
Winson Chungdf95eb12013-10-16 14:57:07 -07003035 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003036 if (mAppsView != null) {
3037 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003038 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 }
Joe Onoratobe386092009-11-17 17:32:16 -08003040
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003041 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003042 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3043 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003044 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3045 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003046 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003047 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003048 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003049 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003050 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003051 return;
3052 }
3053
Tony Wickham26b17462017-03-20 17:12:24 -07003054 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3055 if (topView != null) {
3056 topView.onWidgetsBound();
3057 }
3058 }
3059
Tony Wickham86222d22017-03-29 15:30:43 -07003060 /**
3061 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3062 * refreshes the widgets and shortcuts associated with the given package/user
3063 */
3064 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003065 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003066 }
3067
Sunny Goyalc99cb172017-10-19 16:15:09 -07003068 public boolean isRotationEnabled () {
3069 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003070 }
3071
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003072 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003073 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003074 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003075 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003076 }
3077
Winson Chung80baf5a2010-08-09 16:03:15 -07003078 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003079 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003080 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003081 @Override
3082 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3083 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003084
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003085 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3086 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003087 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003088 writer.println(prefix + " Homescreen " + i);
3089
3090 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3091 for (int j = 0; j < layout.getChildCount(); j++) {
3092 Object tag = layout.getChildAt(j).getTag();
3093 if (tag != null) {
3094 writer.println(prefix + " " + tag.toString());
3095 }
3096 }
3097 }
3098
3099 writer.println(prefix + " Hotseat");
3100 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003101 for (int j = 0; j < layout.getChildCount(); j++) {
3102 Object tag = layout.getChildAt(j).getTag();
3103 if (tag != null) {
3104 writer.println(prefix + " " + tag.toString());
3105 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003106 }
Sunny Goyala1365452015-10-01 15:46:24 -07003107
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003108 try {
3109 FileLog.flushAll(writer);
3110 } catch (Exception e) {
3111 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003112 }
3113 }
3114
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003115 writer.println(prefix + "Misc:");
3116 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3117 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3118 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3119
3120 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003121
Adam Cohen9211d422014-10-07 18:14:53 -07003122 if (mLauncherCallbacks != null) {
3123 mLauncherCallbacks.dump(prefix, fd, writer, args);
3124 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003125 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003126
Sunny Goyal66b24572016-09-21 15:57:55 -07003127 @Override
3128 @TargetApi(Build.VERSION_CODES.N)
3129 public void onProvideKeyboardShortcuts(
3130 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3131
3132 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003133 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003134 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3135 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3136 }
3137 View currentFocus = getCurrentFocus();
3138 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3139 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3140 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3141 }
Tony18c4aa42017-05-10 21:23:57 -05003142 if (currentFocus.getTag() instanceof ItemInfo
3143 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003144 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3145 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3146 }
3147 if (!shortcutInfos.isEmpty()) {
3148 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3149 }
3150
3151 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3152 }
3153
3154 @Override
3155 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3156 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3157 switch (keyCode) {
3158 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003159 if (isInState(NORMAL)) {
3160 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003161 return true;
3162 }
3163 break;
3164 case KeyEvent.KEYCODE_S: {
3165 View focusedView = getCurrentFocus();
3166 if (focusedView instanceof BubbleTextView
3167 && focusedView.getTag() instanceof ItemInfo
3168 && mAccessibilityDelegate.performAction(focusedView,
3169 (ItemInfo) focusedView.getTag(),
3170 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08003171 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003172 return true;
3173 }
3174 break;
3175 }
3176 case KeyEvent.KEYCODE_O:
3177 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3178 return true;
3179 }
3180 break;
3181 }
3182 }
3183 return super.onKeyShortcut(keyCode, event);
3184 }
3185
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003186 public static Launcher getLauncher(Context context) {
3187 if (context instanceof Launcher) {
3188 return (Launcher) context;
3189 }
3190 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3191 }
3192
Sunny Goyal5a81c382017-03-20 15:08:06 -07003193 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003194
3195 @Override
3196 public void onSharedPreferenceChanged(
3197 SharedPreferences sharedPreferences, String key) {
3198 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003199 // Recreate the activity so that it initializes the rotation preference again.
3200 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003201 }
3202 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003203 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003204
3205 /**
3206 * Callback for listening for onResume
3207 */
3208 public interface OnResumeCallback {
3209
3210 void onLauncherResume();
3211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212}