blob: 44b64d94aafbffbd93e5e48963090ae7f4a04960 [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()) {
Tony Makf6308652017-12-02 16:56:22 +00001972 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1973 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001974 ~FLAG_DISABLED_SUSPENDED &
1975 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001976 // If the app is only disabled because of the above flags, launch activity anyway.
1977 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001978 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001979 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1980 // Use a message specific to this shortcut, if it has one.
1981 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1982 return;
1983 }
1984 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001985 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001986 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001987 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001988 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1989 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001990 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001991 }
1992 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1993 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001994 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001995 }
1996
Chris Wren40c5ed32014-06-24 18:24:23 -04001997 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001998 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001999 String packageName = shortcut.intent.getComponent() != null ?
2000 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2001 if (!TextUtils.isEmpty(packageName)) {
2002 onClickPendingAppItem(v, packageName,
2003 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2004 return;
2005 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002006 }
2007
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002008 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002009 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002010 }
2011
Sunny Goyala7ce1662016-05-31 15:01:35 -07002012 private void startAppShortcutOrInfoActivity(View v) {
2013 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002014 Intent intent;
2015 if (item instanceof PromiseAppInfo) {
2016 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2017 intent = promiseAppInfo.getMarketIntent();
2018 } else {
2019 intent = item.getIntent();
2020 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002021 if (intent == null) {
2022 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002023 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002024 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002025 }
2026
2027 /**
2028 * Event handler for a folder icon click.
2029 *
2030 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2031 */
2032 protected void onClickFolderIcon(View v) {
2033 if (LOGD) Log.d(TAG, "onClickFolder");
2034 if (!(v instanceof FolderIcon)){
2035 throw new IllegalArgumentException("Input must be a FolderIcon");
2036 }
2037
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002038 Folder folder = ((FolderIcon) v).getFolder();
2039 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002040 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002041 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002042 }
Michael Jurka5130e402011-10-13 04:55:35 -07002043 }
2044
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002045 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002046 * Event handler for the wallpaper picker button that appears after a long press
2047 * on the home screen.
2048 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002049 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002050 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002051 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2052 return;
2053 }
2054
Tony Wickham785f7a52015-08-31 17:28:32 -07002055 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2056 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002057 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002058 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002059 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002060
2061 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002062 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2063 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002064 intent.setPackage(pickerPackage);
2065 }
2066
Sunny Goyala5ace712017-11-15 17:12:51 -08002067 final Bundle launchOptions;
2068 if (v != null) {
2069 intent.setSourceBounds(getViewBounds(v));
2070 // If there is no target package, use the default intent chooser animation
2071 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
2072 } else {
2073 launchOptions = null;
2074 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002075 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08002076 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002077 } catch (ActivityNotFoundException e) {
2078 setWaitingForResult(null);
2079 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2080 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002081 }
2082
Sunny Goyala7ce1662016-05-31 15:01:35 -07002083 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002085 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2086 try {
2087 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2088 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2089 // is enabled by default on NYC.
2090 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2091 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002092
2093 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2094 String id = ((ShortcutInfo) info).getDeepShortcutId();
2095 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302096 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002097 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002098 } else {
2099 // Could be launching some bookkeeping activity
2100 startActivity(intent, optsBundle);
2101 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002102 } finally {
2103 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002106 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2107 // corresponding permission. Show the appropriate permission prompt if that
2108 // is the case.
2109 if (intent.getComponent() == null
2110 && Intent.ACTION_CALL.equals(intent.getAction())
2111 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2112 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002113
2114 setWaitingForResult(PendingRequestArgs
2115 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002116 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2117 REQUEST_PERMISSION_CALL_PHONE);
2118 } else {
2119 // No idea why this was thrown.
2120 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002124
Sunny Goyalfe770c92016-09-27 14:38:58 -07002125 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002126 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002127 if (Utilities.ATLEAST_MARSHMALLOW) {
2128 int left = 0, top = 0;
2129 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002130 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002131 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002132 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002133 if (icon != null) {
2134 Rect bounds = icon.getBounds();
2135 left = (width - bounds.width()) / 2;
2136 top = v.getPaddingTop();
2137 width = bounds.width();
2138 height = bounds.height();
2139 }
2140 }
2141 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2142 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2143 // On L devices, we use the device default slide-up transition.
2144 // On L MR1 devices, we use a custom version of the slide-up transition which
2145 // doesn't have the delay present in the device default.
2146 return ActivityOptions.makeCustomAnimation(
2147 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2148 }
2149 return null;
2150 }
2151
Tonye3c59252017-05-02 21:32:27 -07002152 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002153 int[] pos = new int[2];
2154 v.getLocationOnScreen(pos);
2155 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2156 }
2157
Sunny Goyala7ce1662016-05-31 15:01:35 -07002158 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002159 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002160 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2161 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002162 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002163 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002164 // Only launch using the new animation if the shortcut has not opted out (this is a
2165 // private contract between launcher and may be ignored in the future).
2166 boolean useLaunchAnimation = (v != null) &&
2167 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2168 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2169
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002170 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002171
2172 // Prepare intent
2173 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2174 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002175 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002176 }
2177 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002178 if (Utilities.ATLEAST_MARSHMALLOW
2179 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002180 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002181 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002182 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002183 // Shortcuts need some special checks due to legacy reasons.
2184 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002185 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002186 // Could be launching some bookkeeping activity
2187 startActivity(intent, optsBundle);
2188 } else {
2189 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2190 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2191 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002192
2193 if (v instanceof BubbleTextView) {
2194 // This is set to the view that launched the activity that navigated the user away
2195 // from launcher. Since there is no callback for when the activity has finished
2196 // launching, enable the press state and keep this reference to reset the press
2197 // state when we return to launcher.
2198 BubbleTextView btv = (BubbleTextView) v;
2199 btv.setStayPressed(true);
2200 setOnResumeCallback(btv);
2201 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002202 mAppLaunchSuccess = true;
2203 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002204 } catch (ActivityNotFoundException|SecurityException e) {
2205 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2206 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2207 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002208 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002209 }
2210
Tony Wickhamdadb3042016-02-24 11:07:00 -08002211 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002212 public boolean dispatchTouchEvent(MotionEvent ev) {
2213 mLastDispatchTouchEventX = ev.getX();
2214 return super.dispatchTouchEvent(ev);
2215 }
2216
2217 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002219 if (!isDraggingEnabled()) return false;
2220 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002221 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222
Jon Miranda51f037d2016-11-07 08:37:20 -08002223 boolean ignoreLongPressToOverview =
2224 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002225
Adam Cohen1697b792013-09-17 19:08:21 -07002226 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002227 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002228 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302229 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2230 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002231 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002232 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002233 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2234 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2235 return true;
2236 } else {
2237 return false;
2238 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002239 } else {
2240 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002241 }
Adam Cohen1697b792013-09-17 19:08:21 -07002242 }
2243
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002244 CellLayout.CellInfo longClickCellInfo = null;
2245 View itemUnderLongClick = null;
2246 if (v.getTag() instanceof ItemInfo) {
2247 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002248 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002249 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002250 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 }
2252
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 // The hotseat touch handling does not go through Workspace, and we always allow long press
2254 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002255 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002257 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002258 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002259 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302260 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2261 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002262 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002263 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002264 return false;
2265 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302266 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2267 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002268 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002269 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002270 }
Jon Miranda379198e2016-09-23 08:54:40 -07002271 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2272 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002274 final boolean isAllAppsButton =
2275 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2276 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2277 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002278 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002280 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 }
2282 }
2283 }
2284 return true;
2285 }
2286
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002288 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002289 return mHotseat != null && layout != null &&
2290 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2291 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002292
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 /**
2294 * Returns the CellLayout of the specified container at the specified screen.
2295 */
Sunny Goyal92820592015-03-02 11:31:35 -08002296 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2298 if (mHotseat != null) {
2299 return mHotseat.getLayout();
2300 } else {
2301 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002302 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002304 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002305 }
2306 }
2307
Michael Jurkae326f182011-11-21 14:05:46 -08002308 @Override
2309 public void onTrimMemory(int level) {
2310 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002311 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2312 // The widget preview db can result in holding onto over
2313 // 3MB of memory for caching which isn't necessary.
2314 SQLiteDatabase.releaseMemory();
2315
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002316 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002317 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002318 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002319 if (mLauncherCallbacks != null) {
2320 mLauncherCallbacks.onTrimMemory(level);
2321 }
Michael Jurkae326f182011-11-21 14:05:46 -08002322 }
2323
Michael Jurkad7c28052012-04-27 15:43:36 -07002324 @Override
2325 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002326 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002327 final List<CharSequence> text = event.getText();
2328 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002329 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002330 // TODO: When can workspace be null?
2331 text.add(mWorkspace == null
2332 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002333 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002334 return result;
2335 }
2336
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002337 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002338 * If the activity is currently paused, signal that we need to run the passed Runnable
2339 * in onResume.
2340 *
2341 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002342 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2343 * wrong when we're not running, and if the activity comes back to what the configuration was
2344 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002345 *
2346 * Implementation of the method from LauncherModel.Callbacks.
2347 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002348 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002349 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002350 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002351 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002352 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002353 if (run instanceof RunnableWithId) {
2354 // Remove any runnables which have the same id
2355 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002356 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002357 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002358 return true;
2359 } else {
2360 return false;
2361 }
2362 }
2363
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002364 public void setOnResumeCallback(OnResumeCallback callback) {
2365 if (mOnResumeCallback != null) {
2366 mOnResumeCallback.onLauncherResume();
2367 }
2368 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002369 }
2370
Michael Jurka7607c2f2013-04-03 14:33:19 -07002371 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002372 * If the activity is currently paused, signal that we need to re-run the loader
2373 * in onResume.
2374 *
2375 * This needs to be called from incoming places where resources might have been loaded
2376 * while we are paused. That is becaues the Configuration might be wrong
2377 * when we're not running, and if it comes back to what it was when we
2378 * were paused, we are not restarted.
2379 *
2380 * Implementation of the method from LauncherModel.Callbacks.
2381 *
2382 * @return true if we are currently paused. The caller might be able to
2383 * skip some work in that case since we will come back again.
2384 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002385 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002386 public boolean setLoadOnResume() {
2387 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002388 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002389 mOnResumeNeedsLoad = true;
2390 return true;
2391 } else {
2392 return false;
2393 }
2394 }
2395
2396 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002397 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002399 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002400 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002401 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002402 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002403 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002404 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002406 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002407
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002409 * Clear any pending bind callbacks. This is called when is loader is planning to
2410 * perform a full rebind from scratch.
2411 */
2412 @Override
2413 public void clearPendingBinds() {
2414 mBindOnResumeCallbacks.clear();
2415 if (mPendingExecutor != null) {
2416 mPendingExecutor.markCompleted();
2417 mPendingExecutor = null;
2418 }
2419 }
2420
2421 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 * Refreshes the shortcuts shown on the workspace.
2423 *
2424 * Implementation of the method from LauncherModel.Callbacks.
2425 */
2426 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002427 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002428 // Floating panels (except the full widget sheet) are associated with individual icons. If
2429 // we are starting a fresh bind, close all such panels as all the icons are about
2430 // to go away.
2431 AbstractFloatingView.closeOpenViews(this, true,
2432 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002433
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002434 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002435
Winson Chungd64d1762013-08-20 14:37:16 -07002436 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002437 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002438 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002439
Winson Chung3d503fb2011-07-13 17:25:49 -07002440 if (mHotseat != null) {
2441 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002443 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 }
2445
Adam Cohendcd297f2013-06-18 13:13:40 -07002446 @Override
2447 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002448 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002449 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2450 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002451 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2452 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002453 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002454 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2455 // If there are no screens, we need to have an empty screen
2456 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002457 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002458 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002459
Winsonc7d2e832016-07-28 12:24:55 -07002460 // After we have added all the screens, if the wallpaper was locked to the default state,
2461 // then notify to indicate that it can be released and a proper wallpaper offset can be
2462 // computed before the next layout
2463 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002464 }
2465
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002466 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002467 int count = orderedScreenIds.size();
2468 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002469 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002470 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002471 // No need to bind the first screen, as its always bound.
2472 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2473 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 }
2476
Sunny Goyalb23980c2017-08-17 07:45:25 -07002477 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002478 public void bindAppsAdded(final ArrayList<Long> newScreens,
2479 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002480 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002481 Runnable r = new Runnable() {
2482 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002483 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002484 }
2485 };
2486 if (waitUntilResume(r)) {
2487 return;
2488 }
2489
Winson Chungd64d1762013-08-20 14:37:16 -07002490 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002491 if (newScreens != null) {
2492 bindAddScreens(newScreens);
2493 }
Winson Chungd64d1762013-08-20 14:37:16 -07002494
2495 // We add the items without animation on non-visible pages, and with
2496 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002497 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002498 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002499 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002500 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002501 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002502 }
Winson Chungc58497e2013-09-03 17:48:37 -07002503
Winson Chung87412982013-10-03 18:34:14 -07002504 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002505 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002509 * Bind the items start-end from the list.
2510 *
2511 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002513 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002514 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002515 Runnable r = new Runnable() {
2516 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002517 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002518 }
2519 };
2520 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002521 return;
2522 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002523
Sunny Goyal3be633b2016-12-08 09:59:25 -08002524 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002525 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002526 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002527 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002528 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002529 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002530 int end = items.size();
2531 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002532 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002533
2534 // Short circuit if we are loading dock items for a configuration which has no dock
2535 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2536 mHotseat == null) {
2537 continue;
2538 }
2539
Sunny Goyal639e9062015-08-19 19:17:06 -07002540 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002541 switch (item.itemType) {
2542 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2543 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002544 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002545 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002546 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002548 }
2549 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002550 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002551 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002552 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002553 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002554 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002555 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2556 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002557 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002558 if (view == null) {
2559 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002560 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002561 break;
2562 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002563 default:
2564 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002566
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002567 /*
2568 * Remove colliding items.
2569 */
2570 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2571 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2572 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2573 View v = cl.getChildAt(item.cellX, item.cellY);
2574 Object tag = v.getTag();
2575 String desc = "Collision while binding workspace item: " + item
2576 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002577 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002578 throw (new RuntimeException(desc));
2579 } else {
2580 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002581 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002582 continue;
2583 }
2584 }
2585 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302586 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002587 if (animateIcons) {
2588 // Animate all the applications up now
2589 view.setAlpha(0f);
2590 view.setScaleX(0f);
2591 view.setScaleY(0f);
2592 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002593 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002596
Winson Chung997a9232013-07-24 15:33:46 -07002597 if (animateIcons) {
2598 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002599 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002600 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002601 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002602 final Runnable startBounceAnimRunnable = new Runnable() {
2603 public void run() {
2604 anim.playTogether(bounceAnims);
2605 anim.start();
2606 }
2607 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002608 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002609 // We post the animation slightly delayed to prevent slowdowns
2610 // when we are loading right after we return to launcher.
2611 mWorkspace.postDelayed(new Runnable() {
2612 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002613 if (mWorkspace != null) {
2614 mWorkspace.snapToPage(newScreenIndex);
2615 mWorkspace.postDelayed(startBounceAnimRunnable,
2616 NEW_APPS_ANIMATION_DELAY);
2617 }
Winson Chung94d67682013-09-25 16:29:40 -07002618 }
2619 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002620 } else {
2621 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002622 }
2623 }
Winson Chung64359a52013-07-08 17:17:08 -07002624 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002625 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 }
2627
Joe Onorato9c1289c2009-08-17 11:03:03 -04002628 /**
2629 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002631 public void bindAppWidget(LauncherAppWidgetInfo item) {
2632 View view = inflateAppWidget(item);
2633 if (view != null) {
2634 mWorkspace.addInScreen(view, item);
2635 mWorkspace.requestLayout();
2636 }
2637 }
2638
2639 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002640 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302641 PendingAppWidgetHostView view =
2642 new PendingAppWidgetHostView(this, item, mIconCache, true);
2643 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002644 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002645 }
2646
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002647 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002648
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002649 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002650
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002651 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2652 // If the provider is not ready, bind as a pending widget.
2653 appWidgetInfo = null;
2654 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2655 // The widget id is not valid. Try to find the widget based on the provider info.
2656 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2657 } else {
2658 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2659 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002660
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002661 // If the provider is ready, but the width is not yet restored, try to restore it.
2662 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2663 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002664 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002665 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2666 + " belongs to component " + item.providerName
2667 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002668 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002669 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002670 }
Sunny Goyalff572272014-07-23 13:58:07 -07002671
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002672 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002673 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002674 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2675 // Id has not been allocated yet. Allocate a new id.
2676 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2677 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002678
Sunny Goyald478c832016-04-01 12:04:16 -07002679 // Also try to bind the widget. If the bind fails, the user will be shown
2680 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002681 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002682 pendingInfo.spanX = item.spanX;
2683 pendingInfo.spanY = item.spanY;
2684 pendingInfo.minSpanX = item.minSpanX;
2685 pendingInfo.minSpanY = item.minSpanY;
2686 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002687
2688 boolean isDirectConfig =
2689 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2690 if (isDirectConfig && item.bindOptions != null) {
2691 Bundle newOptions = item.bindOptions.getExtras();
2692 if (options != null) {
2693 newOptions.putAll(options);
2694 }
2695 options = newOptions;
2696 }
Sunny Goyald478c832016-04-01 12:04:16 -07002697 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2698 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002699
Sunny Goyal86df1382016-08-10 15:03:22 -07002700 // We tried to bind once. If we were not able to bind, we would need to
2701 // go through the permission dialog, which means we cannot skip the config
2702 // activity.
2703 item.bindOptions = null;
2704 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2705
Sunny Goyald478c832016-04-01 12:04:16 -07002706 // Bind succeeded
2707 if (success) {
2708 // If the widget has a configure activity, it is still needs to set it up,
2709 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002710 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002711 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2712 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002713 }
Sunny Goyald478c832016-04-01 12:04:16 -07002714
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002715 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002716 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002717 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002718 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002719 // The widget was marked as UI not ready, but there is no configure activity to
2720 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002721 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002722 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002723 }
Sunny Goyalff572272014-07-23 13:58:07 -07002724 }
2725
Sunny Goyald5462aa2016-11-22 16:52:39 +05302726 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002727 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002728 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002729 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002730 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002731 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002732 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002733 }
2734
Sunny Goyal233ee962015-08-03 13:05:01 -07002735 item.minSpanX = appWidgetInfo.minSpanX;
2736 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302737 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002738 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302739 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002740 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302741 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002742
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002743 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002744 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002745 }
2746
Sunny Goyalff572272014-07-23 13:58:07 -07002747 /**
2748 * Restores a pending widget.
2749 *
2750 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002751 */
Sunny Goyald478c832016-04-01 12:04:16 -07002752 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002753 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2754 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2755 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002756 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002757 }
2758
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002759 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002760 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002761 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2762 info.pendingItemInfo = null;
2763 }
Sunny Goyalff572272014-07-23 13:58:07 -07002764
Sunny Goyalba47faa2017-10-04 16:50:57 -07002765 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2766 view.reinflate();
2767 }
2768
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002769 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002770 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002771 }
2772
Adam Cohen1462de32012-07-24 22:34:36 -07002773 public void onPageBoundSynchronously(int page) {
2774 mSynchronouslyBoundPages.add(page);
2775 }
2776
Sunny Goyal527c7d32015-08-28 15:19:36 -07002777 @Override
2778 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2779 if (mPendingExecutor != null) {
2780 mPendingExecutor.markCompleted();
2781 }
2782 mPendingExecutor = executor;
2783 executor.attachTo(this);
2784 }
2785
2786 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2787 if (mPendingExecutor == executor) {
2788 mPendingExecutor = null;
2789 }
2790 }
2791
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002792 @Override
2793 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2794 Runnable r = new Runnable() {
2795 public void run() {
2796 finishFirstPageBind(executor);
2797 }
2798 };
2799 if (waitUntilResume(r)) {
2800 return;
2801 }
2802
2803 Runnable onComplete = new Runnable() {
2804 @Override
2805 public void run() {
2806 if (executor != null) {
2807 executor.onLoadAnimationCompleted();
2808 }
2809 }
2810 };
2811 if (mDragLayer.getAlpha() < 1) {
2812 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2813 } else {
2814 onComplete.run();
2815 }
2816 }
2817
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 /**
2819 * Callback saying that there aren't any more items to bind.
2820 *
2821 * Implementation of the method from LauncherModel.Callbacks.
2822 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002823 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002824 Runnable r = new Runnable() {
2825 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002826 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002827 }
2828 };
2829 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002830 return;
2831 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002832 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002833 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002835 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002836
Sunny Goyal2100c782016-08-22 16:00:03 -07002837 if (mPendingActivityResult != null) {
2838 handleActivityResult(mPendingActivityResult.requestCode,
2839 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2840 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002841 }
2842
Sunny Goyala474a9b2017-05-04 16:47:11 -07002843 InstallShortcutReceiver.disableAndFlushInstallQueue(
2844 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002845
Tony Wickham010d2552017-01-20 08:15:28 -08002846 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002847 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002848 }
2849
Winson Chunga2413752012-04-03 14:22:34 -07002850 private boolean canRunNewAppsAnimation() {
2851 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002852 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002853 }
2854
Winson Chungc9168342013-06-26 14:54:55 -07002855 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002856 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002857 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2858 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002859 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002860 return bounceAnim;
2861 }
2862
Adam Cohen27772732013-11-11 14:00:56 +00002863 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002864 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002865 }
2866
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002867 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002868 * Add the icons for all apps.
2869 *
2870 * Implementation of the method from LauncherModel.Callbacks.
2871 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002872 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002873 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2874 public void run() {
2875 bindAllApplications(apps);
2876 }
2877 };
2878 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002879 return;
2880 }
2881
Winson Chungb745afb2015-03-02 11:51:23 -08002882 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002883 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002884 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002885 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002886 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002887 return;
2888 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002889
Winson Chungb745afb2015-03-02 11:51:23 -08002890 mAppsView.setApps(apps);
2891 }
Adam Cohen9211d422014-10-07 18:14:53 -07002892 if (mLauncherCallbacks != null) {
2893 mLauncherCallbacks.bindAllApplications(apps);
2894 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002895 }
2896
2897 /**
Tony2917a8b2017-07-31 23:29:42 -07002898 * Returns an Executor that will run after the launcher is first drawn (including after the
2899 * initial fade in animation). Returns null if the first draw has already occurred.
2900 */
2901 public @Nullable Executor getPendingExecutor() {
2902 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2903 }
2904
2905 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002906 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2907 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2908 */
2909 @Override
2910 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002911 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002912 }
2913
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002914 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 * A package was updated.
2916 *
2917 * Implementation of the method from LauncherModel.Callbacks.
2918 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002919 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002920 Runnable r = new Runnable() {
2921 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002922 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002923 }
2924 };
2925 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002926 return;
2927 }
2928
Winson Chungb745afb2015-03-02 11:51:23 -08002929 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002930 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 }
2933
Sunny Goyal4390ace2014-10-13 11:33:11 -07002934 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002935 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2936 Runnable r = new Runnable() {
2937 public void run() {
2938 bindPromiseAppProgressUpdated(app);
2939 }
2940 };
2941 if (waitUntilResume(r)) {
2942 return;
2943 }
2944
2945 if (mAppsView != null) {
2946 mAppsView.updatePromiseAppProgress(app);
2947 }
2948 }
2949
2950 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002951 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2952 Runnable r = new Runnable() {
2953 public void run() {
2954 bindWidgetsRestored(widgets);
2955 }
2956 };
2957 if (waitUntilResume(r)) {
2958 return;
2959 }
2960 mWorkspace.widgetsRestored(widgets);
2961 }
2962
Joe Onorato9c1289c2009-08-17 11:03:03 -04002963 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002964 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002965 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002966 *
2967 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002968 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002969 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002970 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002971 Runnable r = new Runnable() {
2972 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002973 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002974 }
2975 };
2976 if (waitUntilResume(r)) {
2977 return;
2978 }
2979
Sunny Goyal4390ace2014-10-13 11:33:11 -07002980 if (!updated.isEmpty()) {
2981 mWorkspace.updateShortcuts(updated);
2982 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002983 }
2984
2985 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002986 * Update the state of a package, typically related to install state.
2987 *
2988 * Implementation of the method from LauncherModel.Callbacks.
2989 */
Sunny Goyale755d462014-07-22 13:48:29 -07002990 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002991 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
2992 Runnable r = new Runnable() {
2993 public void run() {
2994 bindRestoreItemsChange(updates);
2995 }
2996 };
2997 if (waitUntilResume(r)) {
2998 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002999 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003000
Sunny Goyal756adbc2015-04-16 15:20:51 -07003001 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003002 }
3003
3004 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003005 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003006 * in addition to specific applications to remove, the reason being that
3007 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003008 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003009 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003010 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003011 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003012 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003013 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003014 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003015 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003016 }
Winson Chungd64d1762013-08-20 14:37:16 -07003017 };
3018 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003019 return;
3020 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003021 mWorkspace.removeItemsByMatcher(matcher);
3022 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003023 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003024
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003025 @Override
3026 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3027 Runnable r = new Runnable() {
3028 public void run() {
3029 bindAppInfosRemoved(appInfos);
3030 }
3031 };
3032 if (waitUntilResume(r)) {
3033 return;
Joe Onorato36115782010-06-17 13:28:48 -04003034 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003035
Winson Chungdf95eb12013-10-16 14:57:07 -07003036 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003037 if (mAppsView != null) {
3038 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 }
Joe Onoratobe386092009-11-17 17:32:16 -08003041
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003042 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003043 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3044 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003045 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3046 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003047 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003048 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003049 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003050 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003051 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003052 return;
3053 }
3054
Tony Wickham26b17462017-03-20 17:12:24 -07003055 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3056 if (topView != null) {
3057 topView.onWidgetsBound();
3058 }
3059 }
3060
Tony Wickham86222d22017-03-29 15:30:43 -07003061 /**
3062 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3063 * refreshes the widgets and shortcuts associated with the given package/user
3064 */
3065 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003066 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003067 }
3068
Sunny Goyalc99cb172017-10-19 16:15:09 -07003069 public boolean isRotationEnabled () {
3070 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003071 }
3072
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003073 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003074 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003075 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003076 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003077 }
3078
Winson Chung80baf5a2010-08-09 16:03:15 -07003079 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003080 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003081 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003082 @Override
3083 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3084 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003085
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003086 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3087 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003088 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003089 writer.println(prefix + " Homescreen " + i);
3090
3091 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3092 for (int j = 0; j < layout.getChildCount(); j++) {
3093 Object tag = layout.getChildAt(j).getTag();
3094 if (tag != null) {
3095 writer.println(prefix + " " + tag.toString());
3096 }
3097 }
3098 }
3099
3100 writer.println(prefix + " Hotseat");
3101 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003102 for (int j = 0; j < layout.getChildCount(); j++) {
3103 Object tag = layout.getChildAt(j).getTag();
3104 if (tag != null) {
3105 writer.println(prefix + " " + tag.toString());
3106 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003107 }
Sunny Goyala1365452015-10-01 15:46:24 -07003108
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003109 try {
3110 FileLog.flushAll(writer);
3111 } catch (Exception e) {
3112 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003113 }
3114 }
3115
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003116 writer.println(prefix + "Misc:");
3117 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3118 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3119 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3120
3121 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003122
Adam Cohen9211d422014-10-07 18:14:53 -07003123 if (mLauncherCallbacks != null) {
3124 mLauncherCallbacks.dump(prefix, fd, writer, args);
3125 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003126 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003127
Sunny Goyal66b24572016-09-21 15:57:55 -07003128 @Override
3129 @TargetApi(Build.VERSION_CODES.N)
3130 public void onProvideKeyboardShortcuts(
3131 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3132
3133 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003134 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003135 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3136 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3137 }
3138 View currentFocus = getCurrentFocus();
3139 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3140 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3141 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3142 }
Tony18c4aa42017-05-10 21:23:57 -05003143 if (currentFocus.getTag() instanceof ItemInfo
3144 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003145 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3146 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3147 }
3148 if (!shortcutInfos.isEmpty()) {
3149 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3150 }
3151
3152 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3153 }
3154
3155 @Override
3156 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3157 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3158 switch (keyCode) {
3159 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003160 if (isInState(NORMAL)) {
3161 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003162 return true;
3163 }
3164 break;
3165 case KeyEvent.KEYCODE_S: {
3166 View focusedView = getCurrentFocus();
3167 if (focusedView instanceof BubbleTextView
3168 && focusedView.getTag() instanceof ItemInfo
3169 && mAccessibilityDelegate.performAction(focusedView,
3170 (ItemInfo) focusedView.getTag(),
3171 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08003172 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003173 return true;
3174 }
3175 break;
3176 }
3177 case KeyEvent.KEYCODE_O:
3178 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3179 return true;
3180 }
3181 break;
3182 }
3183 }
3184 return super.onKeyShortcut(keyCode, event);
3185 }
3186
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003187 public static Launcher getLauncher(Context context) {
3188 if (context instanceof Launcher) {
3189 return (Launcher) context;
3190 }
3191 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3192 }
3193
Sunny Goyal5a81c382017-03-20 15:08:06 -07003194 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003195
3196 @Override
3197 public void onSharedPreferenceChanged(
3198 SharedPreferences sharedPreferences, String key) {
3199 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003200 // Recreate the activity so that it initializes the rotation preference again.
3201 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003202 }
3203 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003204 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003205
3206 /**
3207 * Callback for listening for onResume
3208 */
3209 public interface OnResumeCallback {
3210
3211 void onLauncherResume();
3212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213}