blob: 5c635ca513430fa6b5478e874233a499b8c3b1af [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 Goyal326403e2017-10-02 12:45:10 -070019import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
20import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
21
Sunny Goyal28c6b962015-10-12 11:42:05 -070022import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070024import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070026import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070028import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000029import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070040import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070050import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070053import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020054import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080058import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070060import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070061import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.Selection;
63import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070064import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070066import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070067import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.HapticFeedbackConstants;
70import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070071import android.view.KeyboardShortcutGroup;
72import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080073import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.ViewGroup;
Mario Bertschleracbf5702017-03-03 10:58:06 -080079import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070081import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070082import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070085
Sunny Goyal651077b2014-06-30 14:15:31 -070086import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070087import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070088import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070089import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070090import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070091import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070092import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010093import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070094import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -070095import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080096import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070097import com.android.launcher3.dragndrop.DragController;
98import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070099import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700100import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800101import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700102import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000103import com.android.launcher3.folder.Folder;
104import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700105import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700106import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700107import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700108import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800110import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700111import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700112import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800116import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530117import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
118import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
119import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700120import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700122import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700123import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700124import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800125import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700126import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700127import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700128import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700129import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700132import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700134import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800135import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800137import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700138import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139import com.android.launcher3.widget.WidgetListRowEntry;
140import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700141import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700142
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700143import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700145import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700146import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700147import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700148import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800149import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700150import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152/**
153 * Default launcher application.
154 */
Sunny Goyal27835952017-01-13 12:15:53 -0800155public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700156 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
157 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700158 AccessibilityManager.AccessibilityStateChangeListener,
159 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700160 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700161 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Winson Chunga2413752012-04-03 14:22:34 -0700163 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700167
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700169 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Michael Jurka8b805b12012-04-18 14:23:14 -0700171 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700172 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
173 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700174
Jon Mirandac476d6e2017-05-04 16:30:01 -0700175 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700176
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700177 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
178
Mathew Inwood876a8462013-06-14 14:12:41 +0100179 /**
180 * IntentStarter uses request codes starting with this. This must be greater than all activity
181 * request codes used internally.
182 */
183 protected static final int REQUEST_LAST = 100;
184
Mario Bertschleracbf5702017-03-03 10:58:06 -0800185 private static final int SOFT_INPUT_MODE_DEFAULT =
Jon Mirandafd516322017-03-09 13:14:50 -0800186 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800187 private static final int SOFT_INPUT_MODE_ALL_APPS =
Jon Mirandafd516322017-03-09 13:14:50 -0800188 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800189
Winson Chung2672ff92012-05-04 16:22:30 -0700190 // The Intent extra that defines whether to ignore the launch animation
191 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400192 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 // Type: int
195 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700196 // Type: int
197 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700198 // Type: PendingRequestArgs
199 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
200 // Type: ActivityResultInfo
201 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700202 // Type: SparseArray<Parcelable>
203 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700205 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800206
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207 /** The different states that Launcher can be in. */
Sunny Goyal53b99362017-10-05 14:58:56 -0700208 enum State { WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
Tony Wickham94e0d372015-09-11 12:17:48 -0700209 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700210
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700213
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700214 private boolean mIsSafeModeEnabled;
215
Adam Cohen4f8071b2016-02-13 16:06:05 -0800216 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700220 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
222 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700226 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800227 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700228
229 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Sunny Goyalffe83f12014-08-14 17:39:34 -0700231 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700232 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700234 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700235
Sunny Goyal316490e2015-06-02 09:38:28 -0700236 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800237 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700238
Michael Jurka838a4ca2011-02-07 13:33:06 -0800239 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700240
Sunny Goyal47328fd2016-05-25 18:56:41 -0700241 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700242
243 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700244 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700245 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700246
Jon Miranda6bed3502017-09-19 14:43:17 -0700247 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
248 // that results in widgets being inflated in the wrong orientation.
249 private int mOrientation;
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private SpannableStringBuilder mDefaultKeySsb = null;
252
Adam Cohen091440a2015-03-18 14:16:05 -0700253 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800255 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700256 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700258 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700259 private OnResumeCallback mOnResumeCallback;
260
Sunny Goyal527c7d32015-08-28 15:19:36 -0700261 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800264 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800265 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700266 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700267 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700268 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Jon Miranda2d89ea82017-05-04 11:47:53 -0700270 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700271 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700272
Tony Wickham010d2552017-01-20 08:15:28 -0800273 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700274
Winson Chung400438b2011-01-16 17:53:48 -0800275 // Determines how long to wait after a rotation before restoring the screen orientation to
276 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700277 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800278
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700279 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700280
Winson Chung46353de2012-02-16 14:05:10 -0800281 // We only want to get the SharedPreferences once since it does an FS stat each time we get
282 // it from the context.
283 private SharedPreferences mSharedPrefs;
284
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700285 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
286 // state transition. If another state transition happened during this delay,
287 // simply unregister this runnable.
288 private Runnable mExitSpringLoadedModeRunnable;
289
Sunny Goyal2100c782016-08-22 16:00:03 -0700290 // Activity result which needs to be processed after workspace has loaded.
291 private ActivityResultInfo mPendingActivityResult;
292 /**
293 * Holds extra information required to handle a result from an external call, like
294 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
295 */
296 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800297
Jon Miranda379198e2016-09-23 08:54:40 -0700298 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700299
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700300 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700301 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400302
303 @Thunk void setOrientation() {
304 if (mRotationEnabled) {
305 unlockScreenOrientation(true);
306 } else {
307 setRequestedOrientation(
308 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700309 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400310 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700311
Sunny Goyal745bad92016-05-02 10:54:12 -0700312 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700313
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 @Override
315 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700316 if (DEBUG_STRICT_MODE) {
317 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
318 .detectDiskReads()
319 .detectDiskWrites()
320 .detectNetwork() // or .detectAll() for all detectable problems
321 .penaltyLog()
322 .build());
323 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
324 .detectLeakedSqlLiteObjects()
325 .detectLeakedClosableObjects()
326 .penaltyLog()
327 .penaltyDeath()
328 .build());
329 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700330 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700331
Adam Cohen9211d422014-10-07 18:14:53 -0700332 if (mLauncherCallbacks != null) {
333 mLauncherCallbacks.preOnCreate();
334 }
335
Mario Bertschler27288382017-05-24 15:35:09 -0700336 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
337 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700338 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700341 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400342
Sunny Goyal87f784c2017-01-11 10:48:34 -0800343 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700344
Adam Cohen2e6da152015-05-06 11:42:25 -0700345 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800346 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700347 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700348 Display display = getWindowManager().getDefaultDisplay();
349 Point mwSize = new Point();
350 display.getSize(mwSize);
351 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
352 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700353
Jon Miranda6bed3502017-09-19 14:43:17 -0700354 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700355 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700356 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800358 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800359 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700360 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700361
Joe Onorato41a12d22009-10-31 18:30:00 -0400362 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700363 mAllAppsController = new AllAppsTransitionController(this);
364 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Sunny Goyalffe83f12014-08-14 17:39:34 -0700366 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700367
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700368 mAppWidgetHost = new LauncherAppWidgetHost(this);
369 if (Utilities.ATLEAST_MARSHMALLOW) {
370 mAppWidgetHost.addProviderChangeListener(this);
371 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700372 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700373
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700374 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
375 // this also ensures that any synchronous binding below doesn't re-trigger another
376 // LauncherModel load.
377 mPaused = false;
378
Sunny Goyal60820d72017-05-09 12:40:11 -0700379 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 setupViews();
Winson1f064272016-07-18 17:18:02 -0700382 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383
Tony Wickham010d2552017-01-20 08:15:28 -0800384 mPopupDataProvider = new PopupDataProvider(this);
385
Tony Wickhame2217252016-03-22 16:34:23 -0700386 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
387 .addAccessibilityStateChangeListener(this);
388
Sunny Goyalfe770c92016-09-27 14:38:58 -0700389 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390
Sunny Goyal2100c782016-08-22 16:00:03 -0700391 // We only load the page synchronously if the user rotates (or triggers a
392 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700393 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
394 if (savedInstanceState != null) {
395 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
396 }
397 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700398 // If we are not binding synchronously, show a fade in animation when
399 // the first page bind completes.
400 mDragLayer.setAlpha(0);
401 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700402 // Pages bound synchronously.
403 mWorkspace.setCurrentPage(currentScreen);
404
Sunny Goyal2100c782016-08-22 16:00:03 -0700405 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407
408 // For handling default keys
409 mDefaultKeySsb = new SpannableStringBuilder();
410 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800411
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800412 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700413 // In case we are on a device with locked rotation, we should look at preferences to check
414 // if the user has specifically allowed rotation.
415 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700416 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700417 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
418 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700419 }
420
Sunny Goyal5a81c382017-03-20 15:08:06 -0700421 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
422 // Temporarily enable the rotation
423 mRotationEnabled = true;
424 }
425
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700426 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
427 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400428 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700429
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800430 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700431
Jon Miranda7fda2852017-07-19 16:07:01 -0700432 // Listen for broadcasts
433 IntentFilter filter = new IntentFilter();
434 filter.addAction(Intent.ACTION_SCREEN_OFF);
435 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
436 registerReceiver(mReceiver, filter);
437 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700438
Sunny Goyal8392c822017-06-20 10:03:56 -0700439 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
440 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700441
442 if (mLauncherCallbacks != null) {
443 mLauncherCallbacks.onCreate(savedInstanceState);
444 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700445
446 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700447 }
448
Sunny Goyal7779d622015-06-11 16:18:39 -0700449 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700450 public void onThemeChanged() {
451 recreate();
452 }
453
Mario Bertschlera6936942017-05-31 14:48:19 -0700454 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700455 if (isDark) {
456 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700457 } else if (supportsDarkText) {
458 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700459 }
460 }
461
462 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700463 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800464 return mLauncherView.findViewById(id);
465 }
466
467 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700468 public void onAppWidgetHostReset() {
469 if (mAppWidgetHost != null) {
470 mAppWidgetHost.startListening();
471 }
472 }
473
Adam Cohen9211d422014-10-07 18:14:53 -0700474 private LauncherCallbacks mLauncherCallbacks;
475
Winson1f064272016-07-18 17:18:02 -0700476 public void onInsetsChanged(Rect insets) {
477 mDeviceProfile.updateInsets(insets);
478 mDeviceProfile.layout(this, true /* notifyListeners */);
479 }
480
Sunny Goyal32554d12015-12-03 15:31:25 -0800481 /**
482 * Call this after onCreate to set or clear overlay.
483 */
484 public void setLauncherOverlay(LauncherOverlay overlay) {
485 if (overlay != null) {
486 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
487 }
488 mWorkspace.setLauncherOverlay(overlay);
489 }
490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
492 mLauncherCallbacks = callbacks;
493 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700494 }
495
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700496 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700497 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700498 if (mLauncherCallbacks != null) {
499 mLauncherCallbacks.onLauncherProviderChange();
500 }
501 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700502
Hyunyoung Song3f471442015-04-08 19:01:34 -0700503 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700504 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
505 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700506 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700507 }
508
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000509 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700510 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
511 // This cast is safe as long as the id < 0x00FFFFFF
512 // Since we jail all the dynamically generated views, there should be no clashes
513 // with any other views.
514 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000515 }
516
Tony Wickham010d2552017-01-20 08:15:28 -0800517 public PopupDataProvider getPopupDataProvider() {
518 return mPopupDataProvider;
519 }
520
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000521 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700522 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
523 * a configuration step, this allows the proper animations to run after other transitions.
524 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 private long completeAdd(
526 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
527 long screenId = info.screenId;
528 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700529 // When the screen id represents an actual screen (as opposed to a rank) we make sure
530 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700531 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700532 }
Adam Cohendb364c32014-05-20 14:23:40 -0700533
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800535 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700536 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700537 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800538 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700539 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700540 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700541 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700543 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700544 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700545 int widgetId = appWidgetId;
546 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700547 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700548 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700549 // Since the view was just bound, also launch the configure activity if needed
550 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
551 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800552 if (provider != null) {
553 new WidgetAddFlowHandler(provider)
554 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700555 }
556 }
557 break;
558 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800559 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700560
Adam Cohendb364c32014-05-20 14:23:40 -0700561 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800562 }
563
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800564 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700565 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 if (isWorkspaceLoading()) {
567 // process the result once the workspace has loaded.
568 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
569 return;
570 }
571 mPendingActivityResult = null;
572
Winson Chunge341d302013-08-16 14:31:00 -0700573 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 final PendingRequestArgs requestArgs = mPendingRequestArgs;
575 setWaitingForResult(null);
576 if (requestArgs == null) {
577 return;
578 }
579
580 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700581
Adam Cohenad4e15c2013-10-17 16:21:35 -0700582 Runnable exitSpringLoaded = new Runnable() {
583 @Override
584 public void run() {
585 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
586 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
587 }
588 };
589
Michael Jurka8b805b12012-04-18 14:23:14 -0700590 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700591 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700592 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700593 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
594 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700595 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700596 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700597 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700598 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700599 addAppWidgetImpl(
600 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800601 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700602 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700603 }
604 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200605 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
606 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700607 // User could have free-scrolled between pages before picking a wallpaper; make sure
608 // we move to the closest one now.
609 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700610 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200611 }
612 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700613 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200614
Adam Cohened66b2b2012-01-23 17:28:51 -0800615 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700616 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700617
Adam Cohened66b2b2012-01-23 17:28:51 -0800618 // We have special handling for widgets
619 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800620 final int appWidgetId;
621 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
622 : -1;
623 if (widgetId < 0) {
624 appWidgetId = pendingAddWidgetId;
625 } else {
626 appWidgetId = widgetId;
627 }
628
Adam Cohenad4e15c2013-10-17 16:21:35 -0700629 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800630 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700631 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
632 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700633 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700634 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700635 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700636 @Override
637 public void run() {
638 exitSpringLoadedDragModeDelayed(false, 0, null);
639 }
640 };
Adam Cohendb364c32014-05-20 14:23:40 -0700641
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
643 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
644 } else {
645 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
646 // When the screen id represents an actual screen (as opposed to a rank)
647 // we make sure that the drop page actually exists.
648 requestArgs.screenId =
649 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700650 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 final CellLayout dropLayout =
652 mWorkspace.getScreenWithId(requestArgs.screenId);
653
654 dropLayout.setDropPending(true);
655 final Runnable onComplete = new Runnable() {
656 @Override
657 public void run() {
658 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
659 dropLayout.setDropPending(false);
660 }
661 };
662 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
663 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700664 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800665 return;
666 }
667
Sunny Goyald478c832016-04-01 12:04:16 -0700668 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
669 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700670 if (resultCode == RESULT_OK) {
671 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700673 }
674 // Leave the widget in the pending state if the user canceled the configure.
675 return;
676 }
677
Sunny Goyalaad90582015-07-09 14:22:50 -0700678 if (requestCode == REQUEST_CREATE_SHORTCUT) {
679 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700680 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
681 completeAdd(requestCode, data, -1, requestArgs);
682 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
683 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
684
Sunny Goyalaad90582015-07-09 14:22:50 -0700685 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700686 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
687 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800691 }
692
693 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700694 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800695 final int requestCode, final int resultCode, final Intent data) {
696 handleActivityResult(requestCode, resultCode, data);
697 if (mLauncherCallbacks != null) {
698 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
699 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800700 }
701
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700702 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700703 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600704 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700705 PendingRequestArgs pendingArgs = mPendingRequestArgs;
706 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
707 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
708 setWaitingForResult(null);
709
Sunny Goyal28c6b962015-10-12 11:42:05 -0700710 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700711 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700712 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700713 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700714 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700715 Intent intent = pendingArgs.getPendingIntent();
716
Sunny Goyal28c6b962015-10-12 11:42:05 -0700717 if (grantResults.length > 0
718 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700719 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700720 } else {
721 // TODO: Show a snack bar with link to settings
722 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700723 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700724 }
725 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600726 if (mLauncherCallbacks != null) {
727 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
728 grantResults);
729 }
730 }
731
Adam Cohendb364c32014-05-20 14:23:40 -0700732 /**
733 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
734 *
735 * @param screenId the screen id to check
736 * @return the new screen, or screenId if it exists
737 */
738 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800739 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700740 if (dropLayout == null) {
741 // it's possible that the add screen was removed because it was
742 // empty and a re-bind occurred
743 mWorkspace.addExtraEmptyScreen();
744 return mWorkspace.commitExtraEmptyScreen();
745 } else {
746 return screenId;
747 }
748 }
749
Sunny Goyal2100c782016-08-22 16:00:03 -0700750 @Thunk void completeTwoStageWidgetDrop(
751 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
752 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 Runnable onCompleteRunnable = null;
754 int animationType = 0;
755
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700756 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 if (resultCode == RESULT_OK) {
758 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
759 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800760 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700761 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 onCompleteRunnable = new Runnable() {
763 @Override
764 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700765 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
767 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 }
769 };
770 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800771 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800772 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700774 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700776 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
777 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700779 // The animated view may be null in the case of a rotation during widget configuration
780 onCompleteRunnable.run();
781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
783
784 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700785 protected void onStop() {
786 super.onStop();
787 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700788
789 if (mLauncherCallbacks != null) {
790 mLauncherCallbacks.onStop();
791 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700792
Sunny Goyalf5e37442016-11-02 10:31:24 -0700793 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700794 mAppWidgetHost.stopListening();
795 }
Tony Wickham010d2552017-01-20 08:15:28 -0800796
797 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700798 }
799
800 @Override
801 protected void onStart() {
802 super.onStart();
803 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700804
805 if (mLauncherCallbacks != null) {
806 mLauncherCallbacks.onStart();
807 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700808
Sunny Goyalf5e37442016-11-02 10:31:24 -0700809 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700810 mAppWidgetHost.startListening();
811 }
Tony Wickham010d2552017-01-20 08:15:28 -0800812
813 if (!isWorkspaceLoading()) {
814 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
815 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700816
Jon Miranda7fda2852017-07-19 16:07:01 -0700817 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700818 if (mScrimAnimator != null) {
819 mScrimAnimator.cancel();
820 }
821 mDragLayer.getBackground().setAlpha(0);
822 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
823 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
824 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
825 @Override
826 public void onAnimationEnd(Animator animation) {
827 mScrimAnimator = null;
828 }
829 });
830 mScrimAnimator.setDuration(600);
831 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
832 mScrimAnimator.start();
833 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700834 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700835 }
836
837 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700839 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700841 TraceHelper.partitionSection("ON_RESUME", "superCall");
842
Hyunyoung Songaa953652016-04-19 18:30:24 -0700843 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800844 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700845 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700846 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700847 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700848 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700850 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200851 // We might have postponed some bind calls until onResume (see waitUntilResume) --
852 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700853 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
854 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200855 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700856 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200857 }
Winson Chunge4e50662012-01-23 14:45:13 -0800858
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700859 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700860 // Process any items that were added while Launcher was away.
861 InstallShortcutReceiver.disableAndFlushInstallQueue(
862 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700863
Sunny Goyala474a9b2017-05-04 16:47:11 -0700864 // Refresh shortcuts if the permission changed.
865 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700866
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700867 if (shouldShowDiscoveryBounce()) {
868 mAllAppsController.showDiscoveryBounce();
869 }
Adam Cohen9211d422014-10-07 18:14:53 -0700870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onResume();
872 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800873
Sunny Goyala502aa32017-10-02 16:04:06 -0700874 clearTypedText();
875
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700876 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700877 }
878
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700880 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700881 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700882 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700883
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700884 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700885 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800886 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700887 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700888
Adam Cohen9211d422014-10-07 18:14:53 -0700889 if (mLauncherCallbacks != null) {
890 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700891 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700892 }
893
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894 public interface LauncherOverlay {
895
896 /**
897 * Touch interaction leading to overscroll has begun
898 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700899 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900
901 /**
902 * Touch interaction related to overscroll has ended
903 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700904 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700905
906 /**
907 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
908 * screen (or in the case of RTL, the rightmost screen).
909 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700910 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700911
912 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800913 * Called when the launcher is ready to use the overlay
914 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700915 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700916 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700917 }
918
919 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700920 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700921 }
922
923 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
924
Sunny Goyalc86df472016-02-25 09:19:38 -0800925 public void onScrollChanged(float progress) {
926 if (mWorkspace != null) {
927 mWorkspace.onOverlayScrollChanged(progress);
928 }
929 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700930 }
931
Jorim Jaggid017f882014-01-14 17:08:48 -0800932 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700933 if (mLauncherCallbacks != null) {
934 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700935 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800936 // On O and above we there is always some setting present settings (add icon to
937 // home screen or icon badging). On earlier APIs we will have the allow rotation
938 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700939 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700940 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800941 }
942
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700943 @Override
944 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700946 if (mModel.isCurrentCallbacks(this)) {
947 mModel.stopLoader();
948 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700949 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
950
Romain Guy13c2e7b2010-03-10 19:45:00 -0800951 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700952 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700953
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800954 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800955 @Override
956 public void onWindowFocusChanged(boolean hasFocus) {
957 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700958 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
962 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800963 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 private boolean acceptFilter() {
966 final InputMethodManager inputManager = (InputMethodManager)
967 getSystemService(Context.INPUT_METHOD_SERVICE);
968 return !inputManager.isFullscreenMode();
969 }
970
971 @Override
972 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700973 final int uniChar = event.getUnicodeChar();
974 final boolean handled = super.onKeyDown(keyCode, event);
975 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
976 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
978 keyCode, event);
979 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700981 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700982 // showSearchDialog()
983 // If there are multiple keystrokes before the search dialog takes focus,
984 // onSearchRequested() will be called for every keystroke,
985 // but it is idempotent, so it's fine.
986 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988 }
989
Joe Onorato8a9625e2010-01-28 15:55:35 -0800990 // Eat the long press event so the keyboard doesn't come up.
991 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
992 return true;
993 }
994
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 return handled;
996 }
997
Winson46163472015-09-22 17:31:56 -0700998 @Override
999 public boolean onKeyUp(int keyCode, KeyEvent event) {
1000 if (keyCode == KeyEvent.KEYCODE_MENU) {
1001 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001002 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001003 // Close any open floating view
1004 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001005
Winson46163472015-09-22 17:31:56 -07001006 // Show the overview mode if we are on the workspace
1007 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1008 !mWorkspace.isSwitchingState()) {
1009 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001010 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001011 }
1012 }
1013 return true;
1014 }
1015 return super.onKeyUp(keyCode, event);
1016 }
1017
Karl Rosaen138a0412009-04-23 19:00:21 -07001018 private String getTypedText() {
1019 return mDefaultKeySsb.toString();
1020 }
1021
Sunny Goyal6f28e712016-09-13 14:19:29 -07001022 @Override
1023 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001024 mDefaultKeySsb.clear();
1025 mDefaultKeySsb.clearSpans();
1026 Selection.setSelection(mDefaultKeySsb, 0);
1027 }
1028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 /**
1030 * Restores the previous state, if it exists.
1031 *
1032 * @param savedState The previous state.
1033 */
1034 private void restoreState(Bundle savedState) {
1035 if (savedState == null) {
1036 return;
1037 }
1038
Sunny Goyalfe770c92016-09-27 14:38:58 -07001039 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1040 State[] stateValues = State.values();
1041 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1042 ? stateValues[stateOrdinal] : State.WORKSPACE;
Sunny Goyal53b99362017-10-05 14:58:56 -07001043 if (state == State.APPS) {
1044 showAppsView(false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001045 }
1046
Sunny Goyal2100c782016-08-22 16:00:03 -07001047 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1048 if (requestArgs != null) {
1049 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001051
1052 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001053
1054 SparseArray<Parcelable> widgetsState =
1055 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1056 if (widgetsState != null) {
1057 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
1061 /**
1062 * Finds all the views we need and configure them properly.
1063 */
1064 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001065 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001066 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001067 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001068 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001069
Sunny Goyal784f9c32016-03-23 16:56:54 -07001070 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1071 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1072 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073
Winson Chung4c98d922011-05-31 16:50:48 -07001074 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001075 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001076
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 // Setup the hotseat
1078 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1079 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001080 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001081 }
1082
Winsone9f27272015-10-13 10:47:51 -07001083 // Setup the overview panel
1084 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001085
Winson Chung4c98d922011-05-31 16:50:48 -07001086 // Setup the workspace
1087 mWorkspace.setHapticFeedbackEnabled(false);
1088 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001089 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001090 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1091 // default state, otherwise we will update to the wrong offsets in RTL
1092 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001093 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001094 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001095
Tony Wickham34d2c912015-09-11 09:27:58 -07001096 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001097 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001098
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001099 // Setup Apps
1100 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001101
Winson Chung3d503fb2011-07-13 17:25:49 -07001102 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001103 mDragController.setMoveTarget(mWorkspace);
1104 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001105 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001106
Hyunyoung Songd725f642017-08-17 22:26:35 -07001107 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001108
Sunny Goyal322d5562015-06-25 19:35:49 -07001109 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1110 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113
Winsone9f27272015-10-13 10:47:51 -07001114 private void setupOverviewPanel() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001115 mOverviewPanel = findViewById(R.id.overview_panel);
Winsone9f27272015-10-13 10:47:51 -07001116
Winsone9f27272015-10-13 10:47:51 -07001117 // Bind wallpaper button actions
1118 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301119 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001120 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001121 public void handleViewClick(View view) {
1122 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001123 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001124 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001125
1126 // Bind widget button actions
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301127 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001128 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001129 public void handleViewClick(View view) {
1130 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001131 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001132 }.attachTo(findViewById(R.id.widget_button));
Winsone9f27272015-10-13 10:47:51 -07001133
1134 // Bind settings actions
1135 View settingsButton = findViewById(R.id.settings_button);
1136 boolean hasSettings = hasSettings();
1137 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301138 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001139 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001140 public void handleViewClick(View view) {
1141 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001142 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001143 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001144 } else {
1145 settingsButton.setVisibility(View.GONE);
1146 }
1147
1148 mOverviewPanel.setAlpha(0f);
1149 }
1150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001152 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001153 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001154 */
1155 public void setAllAppsButton(View allAppsButton) {
1156 mAllAppsButton = allAppsButton;
1157 }
1158
Anjali Koppal5ad44842014-03-10 20:34:39 -07001159 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 * Creates a view representing a shortcut.
1161 *
1162 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001164 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001165 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167
1168 /**
1169 * Creates a view representing a shortcut inflated from the specified resource.
1170 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 * @param parent The group the shortcut belongs to.
1172 * @param info The data structure describing the shortcut.
1173 *
1174 * @return A View inflated from layoutResId.
1175 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001176 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001177 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1178 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001179 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001181 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 return favorite;
1183 }
1184
1185 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001186 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 *
1188 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001190 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001191 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001192 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1193 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001194 return;
1195 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001196
Jon Mirandac476d6e2017-05-04 16:30:01 -07001197 int[] cellXY = mTmpAddItemCellCoordinates;
1198 CellLayout layout = getCellLayout(container, screenId);
1199
Sunny Goyal782f0c92017-01-19 10:27:54 -08001200 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001201 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001202 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1203 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001204 }
1205
1206 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001207 // Legacy shortcuts are only supported for primary profile.
1208 info = Process.myUserHandle().equals(args.user)
1209 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001210
Sunny Goyalb57645f2017-03-20 13:57:28 -07001211 if (info == null) {
1212 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1213 return;
1214 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001215 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001216 // The app is trying to add a shortcut without sufficient permissions
1217 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1218 return;
1219 }
1220 }
1221
Jon Mirandac476d6e2017-05-04 16:30:01 -07001222 if (container < 0) {
1223 // Adding a shortcut to the Workspace.
1224 final View view = createShortcut(info);
1225 boolean foundCellSpan = false;
1226 // First we check if we already know the exact location where we want to add this item.
1227 if (cellX >= 0 && cellY >= 0) {
1228 cellXY[0] = cellX;
1229 cellXY[1] = cellY;
1230 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001231
Jon Mirandac476d6e2017-05-04 16:30:01 -07001232 // If appropriate, either create a folder or add to an existing folder
1233 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1234 true, null, null)) {
1235 return;
1236 }
1237 DragObject dragObject = new DragObject();
1238 dragObject.dragInfo = info;
1239 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1240 true)) {
1241 return;
1242 }
1243 } else {
1244 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1245 }
1246
1247 if (!foundCellSpan) {
1248 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001249 return;
1250 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001251
1252 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1253 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001254 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001255 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001256 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001257 if (folderIcon != null) {
1258 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1259 folderInfo.add(info, args.rank, false);
1260 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001261 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001262 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265
Sunny Goyale29897f2017-07-20 10:09:42 -07001266 public FolderIcon findFolderIcon(final long folderIconId) {
1267 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1268 @Override
1269 public boolean evaluate(ItemInfo info, View view) {
1270 return info != null && info.id == folderIconId;
1271 }
1272 });
1273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001278 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001280 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001281 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1282
Adam Cohened66b2b2012-01-23 17:28:51 -08001283 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001284 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001285 }
Romain Guycbb89e42009-06-08 15:52:54 -07001286
Adam Cohen59400422014-03-05 18:07:04 -08001287 LauncherAppWidgetInfo launcherInfo;
1288 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001289 launcherInfo.spanX = itemInfo.spanX;
1290 launcherInfo.spanY = itemInfo.spanY;
1291 launcherInfo.minSpanX = itemInfo.minSpanX;
1292 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001293 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001294
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001295 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001296 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297
Sunny Goyal2100c782016-08-22 16:00:03 -07001298 if (hostView == null) {
1299 // Perform actual inflation because we're live
1300 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001302 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301303 prepareAppWidget(hostView, launcherInfo);
1304 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
Romain Guycbb89e42009-06-08 15:52:54 -07001306
Sunny Goyald5462aa2016-11-22 16:52:39 +05301307 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001308 hostView.setTag(item);
1309 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001310 hostView.setFocusable(true);
1311 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001312 }
1313
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1315 @Override
1316 public void onReceive(Context context, Intent intent) {
1317 final String action = intent.getAction();
1318 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001319 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001320 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001321 if (mAppsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001322 if (!showWorkspace(false)) {
1323 // If we are already on the workspace, then manually reset all apps
1324 mAppsView.reset();
1325 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001326 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001327 mShouldFadeInScrim = true;
1328 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1329 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1330 // the user unlocked and the Launcher is not in the foreground.
1331 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 }
1333 }
1334 };
1335
Tony Wickham010d2552017-01-20 08:15:28 -08001336 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1337 Runnable r = new Runnable() {
1338 @Override
1339 public void run() {
1340 mWorkspace.updateIconBadges(updatedBadges);
1341 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001342
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001343 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1344 if (popup instanceof PopupContainerWithArrow) {
1345 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001346 }
Tony Wickham010d2552017-01-20 08:15:28 -08001347 }
1348 };
1349 if (!waitUntilResume(r)) {
1350 r.run();
1351 }
1352 }
1353
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 @Override
1355 public void onAttachedToWindow() {
1356 super.onAttachedToWindow();
1357
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001358 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001359 if (mLauncherCallbacks != null) {
1360 mLauncherCallbacks.onAttachedToWindow();
1361 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001362 }
1363
1364 @Override
1365 public void onDetachedFromWindow() {
1366 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001367
1368 if (mLauncherCallbacks != null) {
1369 mLauncherCallbacks.onDetachedFromWindow();
1370 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001371 }
1372
Winson Chunga6945242014-01-08 14:04:34 -08001373 public DragLayer getDragLayer() {
1374 return mDragLayer;
1375 }
1376
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001377 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001378 return mAppsView;
1379 }
1380
Winson Chunga6945242014-01-08 14:04:34 -08001381 public Workspace getWorkspace() {
1382 return mWorkspace;
1383 }
1384
1385 public Hotseat getHotseat() {
1386 return mHotseat;
1387 }
1388
Jorim Jaggid017f882014-01-14 17:08:48 -08001389 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001390 return mOverviewPanel;
1391 }
1392
Sunny Goyal47328fd2016-05-25 18:56:41 -07001393 public DropTargetBar getDropTargetBar() {
1394 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001395 }
1396
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001397 public LauncherAppWidgetHost getAppWidgetHost() {
1398 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
Romain Guycbb89e42009-06-08 15:52:54 -07001400
Winson Chunga9abd0e2010-10-27 17:18:37 -07001401 public LauncherModel getModel() {
1402 return mModel;
1403 }
1404
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001405 public ModelWriter getModelWriter() {
1406 return mModelWriter;
1407 }
1408
Adam Cohen79d90c52016-04-22 13:29:20 -07001409 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001410 return mSharedPrefs;
1411 }
1412
Jon Miranda6bed3502017-09-19 14:43:17 -07001413 public int getOrientation() { return mOrientation; }
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 @Override
1416 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001417 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 super.onNewIntent(intent);
1419
Winson15f8b172015-08-19 11:02:39 -07001420 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1421 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1422 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001423
1424 // Check this condition before handling isActionMain, as this will get reset.
1425 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001426 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001427
Winson15f8b172015-08-19 11:02:39 -07001428 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1429 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001430 if (mWorkspace == null) {
1431 // Can be cases where mWorkspace is null, this prevents a NPE
1432 return;
1433 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001434
1435 // Note: There should be at most one log per method call. This is enforced implicitly
1436 // by using if-else statements.
1437 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001438 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001439 if (topOpenView != null) {
1440 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001441 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301442 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001443 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1444 }
1445
1446 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001447 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal53b99362017-10-05 14:58:56 -07001448 showWorkspace(alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001449
1450 final View v = getWindow().peekDecorView();
1451 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001452 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001453 }
1454
Winson Chungb745afb2015-03-02 11:51:23 -08001455 // Reset the apps view
1456 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001457 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001458 }
1459
Adam Cohen9211d422014-10-07 18:14:53 -07001460 if (mLauncherCallbacks != null) {
1461 mLauncherCallbacks.onHomeIntent();
1462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001464 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001465
Adam Cohen9211d422014-10-07 18:14:53 -07001466 if (mLauncherCallbacks != null) {
1467 mLauncherCallbacks.onNewIntent(intent);
1468 }
Winson15f8b172015-08-19 11:02:39 -07001469
1470 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1471 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1472 // animation.
1473 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001474 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001475
Winson15f8b172015-08-19 11:02:39 -07001476 mWorkspace.post(new Runnable() {
1477 @Override
1478 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001479 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001480 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001481 }
Winson15f8b172015-08-19 11:02:39 -07001482 }
1483 });
1484 }
1485 }
1486
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001487 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001488 }
1489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001491 public void onRestoreInstanceState(Bundle state) {
1492 super.onRestoreInstanceState(state);
1493 for (int page: mSynchronouslyBoundPages) {
1494 mWorkspace.restoreInstanceStateForChild(page);
1495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
1498 @Override
1499 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001500 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001501 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001502
Adam Cohen21cd0022013-10-09 18:57:02 -07001503 }
Michael Jurka883f55b2010-10-21 15:47:14 -07001504 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001505
1506
1507 AbstractFloatingView widgets = AbstractFloatingView
1508 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1509 if (widgets != null) {
1510 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1511 widgets.saveHierarchyState(widgetsState);
1512 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1513 } else {
1514 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1515 }
1516
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001517 // We close any open folders and shortcut containers since they will not be re-opened,
1518 // and we need to make sure this state is reflected.
1519 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001520
Sunny Goyal2100c782016-08-22 16:00:03 -07001521 if (mPendingRequestArgs != null) {
1522 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1523 }
1524 if (mPendingActivityResult != null) {
1525 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001528 super.onSaveInstanceState(outState);
1529
Adam Cohen9211d422014-10-07 18:14:53 -07001530 if (mLauncherCallbacks != null) {
1531 mLauncherCallbacks.onSaveInstanceState(outState);
1532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
1534
1535 @Override
1536 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001539 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001540 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001541
Winson Chungcd2b0142011-06-08 16:02:26 -07001542 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001543 // It's possible to receive onDestroy after a new Launcher activity has
1544 // been created. In this case, don't interfere with the new Launcher.
1545 if (mModel.isCurrentCallbacks(this)) {
1546 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001547 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001548 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001549
Sunny Goyal745bad92016-05-02 10:54:12 -07001550 if (mRotationPrefChangeHandler != null) {
1551 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1552 }
1553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001555 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001557 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001559 mAppWidgetHost = null;
1560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 TextKeyListener.getInstance().release();
1562
Tony Wickhame2217252016-03-22 16:34:23 -07001563 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1564 .removeAccessibilityStateChangeListener(this);
1565
Mario Bertschler27288382017-05-24 15:35:09 -07001566 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1567
Michael Jurka2ecf9952012-06-18 12:52:28 -07001568 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001569
Tony2917a8b2017-07-31 23:29:42 -07001570 clearPendingBinds();
1571
Adam Cohen9211d422014-10-07 18:14:53 -07001572 if (mLauncherCallbacks != null) {
1573 mLauncherCallbacks.onDestroy();
1574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
1576
Sunny Goyalae502842016-06-17 08:43:56 -07001577 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1578 return mAccessibilityDelegate;
1579 }
1580
Adam Cohena9cf38f2011-05-02 15:36:58 -07001581 public DragController getDragController() {
1582 return mDragController;
1583 }
1584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001586 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001587 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001588 }
1589
1590 @Override
1591 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1592 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001593 try {
1594 super.startIntentSenderForResult(intent, requestCode,
1595 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1596 } catch (IntentSender.SendIntentException e) {
1597 throw new ActivityNotFoundException();
1598 }
1599 }
1600
Winson Chung70d72102011-08-12 11:24:35 -07001601 /**
1602 * Indicates that we want global search for this activity by setting the globalSearch
1603 * argument for {@link #startSearch} to true.
1604 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001606 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001608
Karl Rosaen138a0412009-04-23 19:00:21 -07001609 if (initialQuery == null) {
1610 // Use any text typed in the launcher as the initial query
1611 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 if (appSearchData == null) {
1614 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001615 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001617
Sunny Goyal6f28e712016-09-13 14:19:29 -07001618 if (mLauncherCallbacks == null ||
1619 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1620 // Starting search from the callbacks failed. Start the default global search.
1621 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001622 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001623
1624 // We need to show the workspace after starting the search
1625 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001626 }
1627
Ian Parkinson047036e2014-09-01 15:40:53 +01001628 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001629 * Starts the global search activity. This code is a copied from SearchManager
1630 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001631 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001632 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001633 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001634 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1635 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1636 if (globalSearchActivity == null) {
1637 Log.w(TAG, "No global search activity found.");
1638 return;
1639 }
1640 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1641 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1642 intent.setComponent(globalSearchActivity);
1643 // Make sure that we have a Bundle to put source in
1644 if (appSearchData == null) {
1645 appSearchData = new Bundle();
1646 } else {
1647 appSearchData = new Bundle(appSearchData);
1648 }
Adam Cohen9211d422014-10-07 18:14:53 -07001649 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001650 if (!appSearchData.containsKey("source")) {
1651 appSearchData.putString("source", getPackageName());
1652 }
1653 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1654 if (!TextUtils.isEmpty(initialQuery)) {
1655 intent.putExtra(SearchManager.QUERY, initialQuery);
1656 }
1657 if (selectInitialQuery) {
1658 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1659 }
1660 intent.setSourceBounds(sourceBounds);
1661 try {
1662 startActivity(intent);
1663 } catch (ActivityNotFoundException ex) {
1664 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
1667
Winson Chung70d72102011-08-12 11:24:35 -07001668 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001669 public boolean onPrepareOptionsMenu(Menu menu) {
1670 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001671 if (mLauncherCallbacks != null) {
1672 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1673 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001674 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001677 @Override
1678 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001679 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001680 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001681 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001682 }
1683
Joe Onorato9c1289c2009-08-17 11:03:03 -04001684 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001685 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686 }
1687
Adam Cohen517a7f52014-03-01 12:12:59 -08001688 public boolean isWorkspaceLoading() {
1689 return mWorkspaceLoading;
1690 }
1691
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001692 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001693 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001694 }
1695
Sunny Goyal04a324a2017-01-20 21:08:59 -08001696 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001697 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001698 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001699
Sunny Goyal2100c782016-08-22 16:00:03 -07001700 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001701 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001702 if (LOGD) {
1703 Log.d(TAG, "Adding widget from drop");
1704 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001705 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001706 }
1707
Sunny Goyal2100c782016-08-22 16:00:03 -07001708 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001709 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1710 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1711 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001712
Adam Cohenad4e15c2013-10-17 16:21:35 -07001713 Runnable onComplete = new Runnable() {
1714 @Override
1715 public void run() {
1716 // Exit spring loaded mode if necessary after adding the widget
1717 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1718 null);
1719 }
1720 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001721 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001722 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724 }
Romain Guycbb89e42009-06-08 15:52:54 -07001725
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001726 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1727 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001728 info.container = container;
1729 info.screenId = screenId;
1730 if (cell != null) {
1731 info.cellX = cell[0];
1732 info.cellY = cell[1];
1733 }
1734 info.spanX = spanX;
1735 info.spanY = spanY;
1736
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001737 switch (info.itemType) {
1738 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1739 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001740 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001741 break;
1742 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001743 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001744 break;
1745 default:
1746 throw new IllegalStateException("Unknown item type: " + info.itemType);
1747 }
1748 }
1749
Adam Cohenfbba09b2011-07-18 21:43:05 -07001750 /**
1751 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001752 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001753 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001754 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1755 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001756 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1757 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1758 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001759 }
1760
Adam Cohenfbba09b2011-07-18 21:43:05 -07001761 /**
1762 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001763 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001764 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001765 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001766 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001767 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001768 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001769 // In the case where we've prebound the widget, we remove it from the DragLayer
1770 if (LOGD) {
1771 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1772 }
1773 getDragLayer().removeView(hostView);
1774
Adam Cohened66b2b2012-01-23 17:28:51 -08001775 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001776 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001777
1778 // Clear the boundWidget so that it doesn't get destroyed.
1779 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001780 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001781 // In this case, we either need to start an activity to get permission to bind
1782 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001783 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1784 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1785 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1786 this, info.componentName);
1787 } else {
1788 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1789 }
Adam Cohendd70d662012-10-04 16:53:44 -07001790 Bundle options = info.bindOptions;
1791
Sunny Goyalffe83f12014-08-14 17:39:34 -07001792 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1793 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001794 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001795 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001796 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001797 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001798 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001799 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001800 }
1801
Adam Cohendcd297f2013-06-18 13:13:40 -07001802 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001803 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001804 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 folderInfo.title = getText(R.string.folder_name);
1806
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001808 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809
1810 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001811 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301812 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001813 // Force measure the new folder icon
1814 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1815 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001816 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Winsonc0b52fe2015-09-09 16:38:15 -07001819 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001820 * Unbinds the view for the specified item, and removes the item and all its children.
1821 *
1822 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001823 * @param itemInfo the {@link ItemInfo} for this view.
1824 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001825 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001826 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001827 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001828 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001829 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1830 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001831 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001832 } else {
1833 mWorkspace.removeWorkspaceItem(v);
1834 }
Winsonc0b52fe2015-09-09 16:38:15 -07001835 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001836 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001837 }
1838 } else if (itemInfo instanceof FolderInfo) {
1839 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001840 if (v instanceof FolderIcon) {
1841 ((FolderIcon) v).removeListeners();
1842 }
Winsonc0b52fe2015-09-09 16:38:15 -07001843 mWorkspace.removeWorkspaceItem(v);
1844 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001845 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001846 }
1847 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1848 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001849 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001850 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001851 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001852 }
1853 } else {
1854 return false;
1855 }
1856 return true;
1857 }
1858
1859 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001860 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001861 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001862 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001863 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001864 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001865 // Deleting an app widget ID is a void call but writes to disk before returning
1866 // to the caller...
1867 new AsyncTask<Void, Void, Void>() {
1868 public Void doInBackground(Void ... args) {
1869 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1870 return null;
1871 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001872 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001873 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001874 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001875 }
1876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 @Override
1878 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001879 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
1881
Joe Onorato88ec0992009-11-19 13:16:06 -08001882 @Override
1883 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001884 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1885 return;
1886 }
1887
Sunny Goyal45478022015-06-08 16:52:41 -07001888 if (mDragController.isDragging()) {
1889 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001890 return;
1891 }
1892
Jon Mirandafeba90f2016-10-06 10:53:29 -07001893 // Note: There should be at most one log per method call. This is enforced implicitly
1894 // by using if-else statements.
1895 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001896 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1897 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001898 topView.onBackPressed();
Tony Wickham49c8d292016-07-01 11:44:34 -07001899 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301900 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001901 showWorkspace(true);
1902 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301903 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08001904 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001905 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301906 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001907 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001908 } else {
1909 // Back button is a no-op here, but give at least some feedback for the button press
1910 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001911 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001912 }
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 * Launches the intent referred by the clicked shortcut.
1916 *
1917 * @param v The view representing the clicked shortcut.
1918 */
1919 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001920 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1921 // view has detached (it's possible for this to happen if the view is removed mid touch).
1922 if (v.getWindowToken() == null) {
1923 return;
1924 }
1925
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001926 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001927 return;
1928 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001929
Adam Cohen1697b792013-09-17 19:08:21 -07001930 if (v instanceof Workspace) {
1931 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001932 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1933 LauncherLogProto.Action.Direction.NONE,
1934 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07001935 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001936 }
1937 return;
1938 }
1939
1940 if (v instanceof CellLayout) {
1941 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001942 int page = mWorkspace.indexOfChild(v);
1943 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1944 LauncherLogProto.Action.Direction.NONE,
1945 LauncherLogProto.ContainerType.OVERVIEW, page);
1946 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001947 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001948 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001949 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001950 }
1951
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001953 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001954 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001956 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001957 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001958 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001959 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001960 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001961 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001962 } else if (tag instanceof AppInfo) {
1963 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001964 } else if (tag instanceof LauncherAppWidgetInfo) {
1965 if (v instanceof PendingAppWidgetHostView) {
1966 onClickPendingWidget((PendingAppWidgetHostView) v);
1967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969 }
1970
Sunny Goyal70660032015-05-14 00:07:08 -07001971 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001972 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001973 return false;
1974 }
1975
Michael Jurkaaf442092010-06-10 17:01:57 -07001976 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001977 * Event handler for the app widget view which has not fully restored.
1978 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001979 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001980 if (mIsSafeModeEnabled) {
1981 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1982 return;
1983 }
1984
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001985 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001986 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001987 LauncherAppWidgetProviderInfo appWidgetInfo =
1988 mAppWidgetManager.findProvider(info.providerName, info.user);
1989 if (appWidgetInfo == null) {
1990 return;
1991 }
1992 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1993
Sunny Goyald478c832016-04-01 12:04:16 -07001994 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1995 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1996 // This should not happen, as we make sure that an Id is allocated during bind.
1997 return;
1998 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001999 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2000 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002001 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002002 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002003 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002004 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002005 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002006 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002007 }
2008 }
2009
2010 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002011 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2012 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2013 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002014 *
2015 * @param v The view that was clicked.
2016 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002017 protected void onClickAllAppsButton(View v) {
2018 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002019 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302020 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2021 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002022 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002023 } else {
2024 showWorkspace(true);
2025 }
2026 }
2027
Sunny Goyale6e72002017-01-12 16:55:36 -08002028 private void onClickPendingAppItem(final View v, final String packageName,
2029 boolean downloadStarted) {
2030 if (downloadStarted) {
2031 // If the download has started, simply direct to the market app.
2032 startMarketIntentForPackage(v, packageName);
2033 return;
2034 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002035 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002036 .setTitle(R.string.abandoned_promises_title)
2037 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002038 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2039 @Override
2040 public void onClick(DialogInterface dialogInterface, int i) {
2041 startMarketIntentForPackage(v, packageName);
2042 }
2043 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002044 .setNeutralButton(R.string.abandoned_clean_this,
2045 new DialogInterface.OnClickListener() {
2046 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002047 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002048 mWorkspace.removeAbandonedPromise(packageName, user);
2049 }
2050 })
2051 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002052 }
2053
2054 private void startMarketIntentForPackage(View v, String packageName) {
2055 ItemInfo item = (ItemInfo) v.getTag();
2056 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002057 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002058 }
2059
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002060 /**
2061 * Event handler for an app shortcut click.
2062 *
2063 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2064 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002065 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002066 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2067 Object tag = v.getTag();
2068 if (!(tag instanceof ShortcutInfo)) {
2069 throw new IllegalArgumentException("Input must be a Shortcut");
2070 }
2071
2072 // Open shortcut
2073 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002074
2075 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002076 if ((shortcut.isDisabled &
2077 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2078 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2079 // If the app is only disabled because of the above flags, launch activity anyway.
2080 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002081 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002082 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2083 // Use a message specific to this shortcut, if it has one.
2084 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2085 return;
2086 }
2087 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002088 int error = R.string.activity_not_available;
2089 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2090 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002091 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2092 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002093 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002094 }
2095 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2096 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002097 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002098 }
2099
Chris Wren40c5ed32014-06-24 18:24:23 -04002100 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002101 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002102 String packageName = shortcut.intent.getComponent() != null ?
2103 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2104 if (!TextUtils.isEmpty(packageName)) {
2105 onClickPendingAppItem(v, packageName,
2106 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2107 return;
2108 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002109 }
2110
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002111 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002112 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002113 }
2114
Sunny Goyala7ce1662016-05-31 15:01:35 -07002115 private void startAppShortcutOrInfoActivity(View v) {
2116 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002117 Intent intent;
2118 if (item instanceof PromiseAppInfo) {
2119 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2120 intent = promiseAppInfo.getMarketIntent();
2121 } else {
2122 intent = item.getIntent();
2123 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002124 if (intent == null) {
2125 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002126 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002127 startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002128 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002129 }
2130
2131 /**
2132 * Event handler for a folder icon click.
2133 *
2134 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2135 */
2136 protected void onClickFolderIcon(View v) {
2137 if (LOGD) Log.d(TAG, "onClickFolder");
2138 if (!(v instanceof FolderIcon)){
2139 throw new IllegalArgumentException("Input must be a FolderIcon");
2140 }
2141
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002142 Folder folder = ((FolderIcon) v).getFolder();
2143 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002144 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002145 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002146 }
Michael Jurka5130e402011-10-13 04:55:35 -07002147 }
2148
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002149 /**
2150 * Event handler for the (Add) Widgets button that appears after a long press
2151 * on the home screen.
2152 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002153 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002154 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002155 if (mIsSafeModeEnabled) {
2156 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2157 } else {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002158 WidgetsFullSheet.show(this, true /* animated */);
Adam Cohen9211d422014-10-07 18:14:53 -07002159 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002160 }
2161
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002162 /**
2163 * Event handler for the wallpaper picker button that appears after a long press
2164 * on the home screen.
2165 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002166 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002167 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002168 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2169 return;
2170 }
2171
Tony Wickham785f7a52015-08-31 17:28:32 -07002172 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2173 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002174 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002175 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002176 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002177
2178 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002179 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2180 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002181 intent.setPackage(pickerPackage);
2182 }
2183
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002184 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002185 try {
2186 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2187 // If there is no target package, use the default intent chooser animation
2188 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2189 } catch (ActivityNotFoundException e) {
2190 setWaitingForResult(null);
2191 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2192 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002193 }
2194
2195 /**
2196 * Event handler for a click on the settings button that appears after a long press
2197 * on the home screen.
2198 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002199 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002200 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002201 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2202 .setPackage(getPackageName());
2203 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002204 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002205 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002206 }
2207
Tony Wickhame2217252016-03-22 16:34:23 -07002208 @Override
2209 public void onAccessibilityStateChanged(boolean enabled) {
2210 mDragLayer.onAccessibilityStateChanged(enabled);
2211 }
2212
Sunny Goyala7ce1662016-05-31 15:01:35 -07002213 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002215 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2216 try {
2217 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2218 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2219 // is enabled by default on NYC.
2220 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2221 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002222
2223 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2224 String id = ((ShortcutInfo) info).getDeepShortcutId();
2225 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302226 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002227 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002228 } else {
2229 // Could be launching some bookkeeping activity
2230 startActivity(intent, optsBundle);
2231 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002232 } finally {
2233 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002236 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2237 // corresponding permission. Show the appropriate permission prompt if that
2238 // is the case.
2239 if (intent.getComponent() == null
2240 && Intent.ACTION_CALL.equals(intent.getAction())
2241 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2242 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002243
2244 setWaitingForResult(PendingRequestArgs
2245 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002246 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2247 REQUEST_PERMISSION_CALL_PHONE);
2248 } else {
2249 // No idea why this was thrown.
2250 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 }
2253 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002254
Sunny Goyalfe770c92016-09-27 14:38:58 -07002255 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002256 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002257 if (Utilities.ATLEAST_MARSHMALLOW) {
2258 int left = 0, top = 0;
2259 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002260 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002261 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002262 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002263 if (icon != null) {
2264 Rect bounds = icon.getBounds();
2265 left = (width - bounds.width()) / 2;
2266 top = v.getPaddingTop();
2267 width = bounds.width();
2268 height = bounds.height();
2269 }
2270 }
2271 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2272 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2273 // On L devices, we use the device default slide-up transition.
2274 // On L MR1 devices, we use a custom version of the slide-up transition which
2275 // doesn't have the delay present in the device default.
2276 return ActivityOptions.makeCustomAnimation(
2277 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2278 }
2279 return null;
2280 }
2281
Tonye3c59252017-05-02 21:32:27 -07002282 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002283 int[] pos = new int[2];
2284 v.getLocationOnScreen(pos);
2285 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2286 }
2287
Sunny Goyala7ce1662016-05-31 15:01:35 -07002288 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002289 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2290 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2291 return false;
2292 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002293 // Only launch using the new animation if the shortcut has not opted out (this is a
2294 // private contract between launcher and may be ignored in the future).
2295 boolean useLaunchAnimation = (v != null) &&
2296 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2297 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2298
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002299 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002300
2301 // Prepare intent
2302 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2303 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002304 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002305 }
2306 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002307 if (Utilities.ATLEAST_MARSHMALLOW
2308 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002309 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002310 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002311 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002312 // Shortcuts need some special checks due to legacy reasons.
2313 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002314 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002315 // Could be launching some bookkeeping activity
2316 startActivity(intent, optsBundle);
2317 } else {
2318 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2319 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2320 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002321
2322 if (v instanceof BubbleTextView) {
2323 // This is set to the view that launched the activity that navigated the user away
2324 // from launcher. Since there is no callback for when the activity has finished
2325 // launching, enable the press state and keep this reference to reset the press
2326 // state when we return to launcher.
2327 BubbleTextView btv = (BubbleTextView) v;
2328 btv.setStayPressed(true);
2329 setOnResumeCallback(btv);
2330 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002331 return true;
2332 } catch (ActivityNotFoundException|SecurityException e) {
2333 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2334 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2335 }
2336 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002337 }
2338
Tony Wickhamdadb3042016-02-24 11:07:00 -08002339 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002340 public boolean dispatchTouchEvent(MotionEvent ev) {
2341 mLastDispatchTouchEventX = ev.getX();
2342 return super.dispatchTouchEvent(ev);
2343 }
2344
2345 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002347 if (!isDraggingEnabled()) return false;
2348 if (isWorkspaceLocked()) return false;
2349 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350
Jon Miranda51f037d2016-11-07 08:37:20 -08002351 boolean ignoreLongPressToOverview =
2352 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002353
Adam Cohen1697b792013-09-17 19:08:21 -07002354 if (v instanceof Workspace) {
2355 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002356 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302357 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2358 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002359 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002360 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002361 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2362 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2363 return true;
2364 } else {
2365 return false;
2366 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002367 } else {
2368 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002369 }
Adam Cohen1697b792013-09-17 19:08:21 -07002370 }
2371
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002372 CellLayout.CellInfo longClickCellInfo = null;
2373 View itemUnderLongClick = null;
2374 if (v.getTag() instanceof ItemInfo) {
2375 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002376 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002377 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002378 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 }
2380
Winson Chung3d503fb2011-07-13 17:25:49 -07002381 // The hotseat touch handling does not go through Workspace, and we always allow long press
2382 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002383 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002384 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002385 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002386 if (mWorkspace.isInOverviewMode()) {
2387 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302388 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2389 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002390 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002391 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002392 return false;
2393 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302394 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2395 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002396 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002397 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002398 }
Jon Miranda379198e2016-09-23 08:54:40 -07002399 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2400 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002402 final boolean isAllAppsButton =
2403 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2404 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2405 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002406 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002408 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410 }
2411 }
2412 return true;
2413 }
2414
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002416 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002417 return mHotseat != null && layout != null &&
2418 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2419 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002420
Winson Chung3d503fb2011-07-13 17:25:49 -07002421 /**
2422 * Returns the CellLayout of the specified container at the specified screen.
2423 */
Sunny Goyal92820592015-03-02 11:31:35 -08002424 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2426 if (mHotseat != null) {
2427 return mHotseat.getLayout();
2428 } else {
2429 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002430 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002431 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002432 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002433 }
2434 }
2435
Winson Chungb745afb2015-03-02 11:51:23 -08002436 /**
2437 * For overridden classes.
2438 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002439 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002440 return isAppsViewVisible();
2441 }
2442
2443 public boolean isAppsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002444 return mState == State.APPS;
Winson Chungb745afb2015-03-02 11:51:23 -08002445 }
2446
2447 public boolean isWidgetsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002448 return mState == State.WIDGETS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002449 }
2450
Michael Jurkae326f182011-11-21 14:05:46 -08002451 @Override
2452 public void onTrimMemory(int level) {
2453 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002454 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2455 // The widget preview db can result in holding onto over
2456 // 3MB of memory for caching which isn't necessary.
2457 SQLiteDatabase.releaseMemory();
2458
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002459 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002460 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002461 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002462 if (mLauncherCallbacks != null) {
2463 mLauncherCallbacks.onTrimMemory(level);
2464 }
Michael Jurkae326f182011-11-21 14:05:46 -08002465 }
2466
Winson5c6bdbb2015-09-03 11:36:19 -07002467 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002468 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002469 }
2470
Winson5c6bdbb2015-09-03 11:36:19 -07002471 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002472 boolean changed = mState != State.WORKSPACE ||
2473 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002474 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002475 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002476 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002477 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002478
Michael Jurkab3e22d92011-10-31 15:58:33 -07002479 // Set focus to the AppsCustomize button
2480 if (mAllAppsButton != null) {
2481 mAllAppsButton.requestFocus();
2482 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002483 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002484
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002485 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002486 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002487
Winson Chung0f785722015-04-08 10:27:49 -07002488 if (changed) {
2489 // Send an accessibility event to announce the context change
2490 getWindow().getDecorView()
2491 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002492 }
Winson5c6bdbb2015-09-03 11:36:19 -07002493 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002494 }
2495
Winsone9f27272015-10-13 10:47:51 -07002496 /**
2497 * Shows the overview button.
2498 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002499 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002500 showOverviewMode(animated, false);
2501 }
2502
2503 /**
2504 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2505 * onto one of the overview panel buttons.
2506 */
2507 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2508 Runnable postAnimRunnable = null;
2509 if (requestButtonFocus) {
2510 postAnimRunnable = new Runnable() {
2511 @Override
2512 public void run() {
2513 // Hitting the menu button when in touch mode does not trigger touch mode to
2514 // be disabled, so if requested, force focus on one of the overview panel
2515 // buttons.
2516 mOverviewPanel.requestFocusFromTouch();
2517 }
2518 };
2519 }
Adam Cohened307df2013-10-02 09:37:31 -07002520 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002521 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002522 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002523 setState(State.WORKSPACE);
2524
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002525 // If animated from long press, then don't allow any of the controller in the drag
2526 // layer to intercept any remaining touch.
2527 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002528 }
2529
Mario Bertschleracbf5702017-03-03 10:58:06 -08002530 private void setState(State state) {
2531 this.mState = state;
2532 updateSoftInputMode();
2533 }
2534
2535 private void updateSoftInputMode() {
2536 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2537 final int mode;
2538 if (isAppsViewVisible()) {
2539 mode = SOFT_INPUT_MODE_ALL_APPS;
2540 } else {
2541 mode = SOFT_INPUT_MODE_DEFAULT;
2542 }
2543 getWindow().setSoftInputMode(mode);
2544 }
2545 }
2546
Winson Chungb745afb2015-03-02 11:51:23 -08002547 /**
2548 * Shows the apps view.
2549 */
Sunny Goyald0d07032017-10-04 10:40:28 -07002550 public void showAppsView(boolean animated) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002551 markAppsViewShown();
Sunny Goyal5606e072017-06-15 14:24:21 -07002552 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002553 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002554
Winson Chungb745afb2015-03-02 11:51:23 -08002555 /**
Winson Chungb745afb2015-03-02 11:51:23 -08002556 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002557 *
2558 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002559 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002560 // TODO: calling method should use the return value so that when {@code false} is returned
2561 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002562 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002563 if (!(mState == State.WORKSPACE ||
2564 mState == State.APPS_SPRING_LOADED ||
2565 mState == State.WIDGETS_SPRING_LOADED ||
2566 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002567 return false;
2568 }
2569 if (toState != State.APPS && toState != State.WIDGETS) {
2570 return false;
2571 }
Winson Chungb745afb2015-03-02 11:51:23 -08002572
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002573 // This is a safe and supported transition to bypass spring_loaded mode.
2574 if (mExitSpringLoadedModeRunnable != null) {
2575 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2576 mExitSpringLoadedModeRunnable = null;
2577 }
2578
Winson Chungb745afb2015-03-02 11:51:23 -08002579 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002580 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002581 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002582 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002583 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002584
Michael Jurkab3e22d92011-10-31 15:58:33 -07002585 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002586 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002587 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002588
2589 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002590 getWindow().getDecorView()
2591 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002592 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002593 }
2594
Hyunyoung Song3f471442015-04-08 19:01:34 -07002595 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002596 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002597 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002598 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002599 }
Winson Chungb745afb2015-03-02 11:51:23 -08002600
Winson Chung006ee262015-08-03 14:40:11 -07002601 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002602 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002603 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002604 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002605 }
Adam Cohen7777d962011-08-18 18:58:38 -07002606
Hyunyoung Song3f471442015-04-08 19:01:34 -07002607 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002608 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002609 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002610
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002611 if (mExitSpringLoadedModeRunnable != null) {
2612 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2613 }
2614 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002615 @Override
2616 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002617 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002618 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2619 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002620 // Before we show workspace, hide all apps again because
2621 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2622 // clean up our state transition functions
Adam Cohened66b2b2012-01-23 17:28:51 -08002623 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002624 } else {
2625 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002626 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002627 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002628 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002629 };
2630 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002631 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002632
Tony Wickhame0c33232016-02-08 11:37:04 -08002633 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002634 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2635 || mState == State.WIDGETS_SPRING_LOADED;
2636 }
2637
Sunny Goyal0f76b562016-12-13 19:37:10 -08002638 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002639 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyald0d07032017-10-04 10:40:28 -07002640 showAppsView(true /* animated */);
Tony Wickham94e0d372015-09-11 12:17:48 -07002641 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2642 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002643 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002644 }
2645
Michael Jurkad7c28052012-04-27 15:43:36 -07002646 @Override
2647 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002648 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002649 final List<CharSequence> text = event.getText();
2650 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002651 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002652 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002653 text.add(getString(R.string.all_apps_button_label));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002654 } else if (mWorkspace != null) {
2655 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002656 } else {
2657 text.add(getString(R.string.all_apps_home_button_label));
2658 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002659 return result;
2660 }
2661
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002662 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002663 * If the activity is currently paused, signal that we need to run the passed Runnable
2664 * in onResume.
2665 *
2666 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002667 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2668 * wrong when we're not running, and if the activity comes back to what the configuration was
2669 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002670 *
2671 * Implementation of the method from LauncherModel.Callbacks.
2672 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002673 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002674 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002675 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002676 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002677 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002678 if (run instanceof RunnableWithId) {
2679 // Remove any runnables which have the same id
2680 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002681 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002682 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002683 return true;
2684 } else {
2685 return false;
2686 }
2687 }
2688
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002689 public void setOnResumeCallback(OnResumeCallback callback) {
2690 if (mOnResumeCallback != null) {
2691 mOnResumeCallback.onLauncherResume();
2692 }
2693 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002694 }
2695
Michael Jurka7607c2f2013-04-03 14:33:19 -07002696 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002697 * If the activity is currently paused, signal that we need to re-run the loader
2698 * in onResume.
2699 *
2700 * This needs to be called from incoming places where resources might have been loaded
2701 * while we are paused. That is becaues the Configuration might be wrong
2702 * when we're not running, and if it comes back to what it was when we
2703 * were paused, we are not restarted.
2704 *
2705 * Implementation of the method from LauncherModel.Callbacks.
2706 *
2707 * @return true if we are currently paused. The caller might be able to
2708 * skip some work in that case since we will come back again.
2709 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002710 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002711 public boolean setLoadOnResume() {
2712 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002713 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002714 mOnResumeNeedsLoad = true;
2715 return true;
2716 } else {
2717 return false;
2718 }
2719 }
2720
2721 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002722 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002724 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002725 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002726 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002727 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002728 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002729 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002731 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002732
Joe Onorato9c1289c2009-08-17 11:03:03 -04002733 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002734 * Clear any pending bind callbacks. This is called when is loader is planning to
2735 * perform a full rebind from scratch.
2736 */
2737 @Override
2738 public void clearPendingBinds() {
2739 mBindOnResumeCallbacks.clear();
2740 if (mPendingExecutor != null) {
2741 mPendingExecutor.markCompleted();
2742 mPendingExecutor = null;
2743 }
2744 }
2745
2746 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002747 * Refreshes the shortcuts shown on the workspace.
2748 *
2749 * Implementation of the method from LauncherModel.Callbacks.
2750 */
2751 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002752 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002753 // Floating panels (except the full widget sheet) are associated with individual icons. If
2754 // we are starting a fresh bind, close all such panels as all the icons are about
2755 // to go away.
2756 AbstractFloatingView.closeOpenViews(this, true,
2757 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002758
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002759 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002760
Winson Chungd64d1762013-08-20 14:37:16 -07002761 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002762 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002763 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002764
Winson Chung3d503fb2011-07-13 17:25:49 -07002765 if (mHotseat != null) {
2766 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002768 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 }
2770
Adam Cohendcd297f2013-06-18 13:13:40 -07002771 @Override
2772 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002773 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002774 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2775 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002776 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2777 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002778 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002779 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2780 // If there are no screens, we need to have an empty screen
2781 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002782 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002783 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002784
Winsonc7d2e832016-07-28 12:24:55 -07002785 // After we have added all the screens, if the wallpaper was locked to the default state,
2786 // then notify to indicate that it can be released and a proper wallpaper offset can be
2787 // computed before the next layout
2788 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002789 }
2790
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002791 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002792 int count = orderedScreenIds.size();
2793 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002794 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002795 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002796 // No need to bind the first screen, as its always bound.
2797 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2798 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002799 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002800 }
2801
Sunny Goyalb23980c2017-08-17 07:45:25 -07002802 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002803 public void bindAppsAdded(final ArrayList<Long> newScreens,
2804 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002805 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002806 Runnable r = new Runnable() {
2807 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002808 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002809 }
2810 };
2811 if (waitUntilResume(r)) {
2812 return;
2813 }
2814
Winson Chungd64d1762013-08-20 14:37:16 -07002815 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002816 if (newScreens != null) {
2817 bindAddScreens(newScreens);
2818 }
Winson Chungd64d1762013-08-20 14:37:16 -07002819
2820 // We add the items without animation on non-visible pages, and with
2821 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002822 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002823 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002824 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002825 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002826 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002827 }
Winson Chungc58497e2013-09-03 17:48:37 -07002828
Winson Chung87412982013-10-03 18:34:14 -07002829 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002830 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002831 }
2832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 * Bind the items start-end from the list.
2835 *
2836 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002838 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002839 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002840 Runnable r = new Runnable() {
2841 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002842 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002843 }
2844 };
2845 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002846 return;
2847 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002848
Sunny Goyal3be633b2016-12-08 09:59:25 -08002849 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002850 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002851 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002852 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002853 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002854 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002855 int end = items.size();
2856 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002857 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002858
2859 // Short circuit if we are loading dock items for a configuration which has no dock
2860 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2861 mHotseat == null) {
2862 continue;
2863 }
2864
Sunny Goyal639e9062015-08-19 19:17:06 -07002865 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 switch (item.itemType) {
2867 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2868 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002869 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002870 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002871 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002873 }
2874 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002875 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002876 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002877 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002879 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002880 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2881 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002882 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002883 if (view == null) {
2884 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002885 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002886 break;
2887 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002888 default:
2889 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002891
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002892 /*
2893 * Remove colliding items.
2894 */
2895 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2896 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2897 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2898 View v = cl.getChildAt(item.cellX, item.cellY);
2899 Object tag = v.getTag();
2900 String desc = "Collision while binding workspace item: " + item
2901 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002902 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002903 throw (new RuntimeException(desc));
2904 } else {
2905 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002906 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002907 continue;
2908 }
2909 }
2910 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302911 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002912 if (animateIcons) {
2913 // Animate all the applications up now
2914 view.setAlpha(0f);
2915 view.setScaleX(0f);
2916 view.setScaleY(0f);
2917 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002918 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002921
Winson Chung997a9232013-07-24 15:33:46 -07002922 if (animateIcons) {
2923 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002924 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002925 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002926 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002927 final Runnable startBounceAnimRunnable = new Runnable() {
2928 public void run() {
2929 anim.playTogether(bounceAnims);
2930 anim.start();
2931 }
2932 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002933 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002934 // We post the animation slightly delayed to prevent slowdowns
2935 // when we are loading right after we return to launcher.
2936 mWorkspace.postDelayed(new Runnable() {
2937 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002938 if (mWorkspace != null) {
2939 mWorkspace.snapToPage(newScreenIndex);
2940 mWorkspace.postDelayed(startBounceAnimRunnable,
2941 NEW_APPS_ANIMATION_DELAY);
2942 }
Winson Chung94d67682013-09-25 16:29:40 -07002943 }
2944 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002945 } else {
2946 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002947 }
2948 }
Winson Chung64359a52013-07-08 17:17:08 -07002949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 }
2952
Joe Onorato9c1289c2009-08-17 11:03:03 -04002953 /**
2954 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002955 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002956 public void bindAppWidget(LauncherAppWidgetInfo item) {
2957 View view = inflateAppWidget(item);
2958 if (view != null) {
2959 mWorkspace.addInScreen(view, item);
2960 mWorkspace.requestLayout();
2961 }
2962 }
2963
2964 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002965 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302966 PendingAppWidgetHostView view =
2967 new PendingAppWidgetHostView(this, item, mIconCache, true);
2968 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002969 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002970 }
2971
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002972 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002974 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002975
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002976 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2977 // If the provider is not ready, bind as a pending widget.
2978 appWidgetInfo = null;
2979 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2980 // The widget id is not valid. Try to find the widget based on the provider info.
2981 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2982 } else {
2983 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2984 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002985
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002986 // If the provider is ready, but the width is not yet restored, try to restore it.
2987 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2988 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002989 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002990 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2991 + " belongs to component " + item.providerName
2992 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002993 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002994 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002995 }
Sunny Goyalff572272014-07-23 13:58:07 -07002996
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002997 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002998 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002999 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3000 // Id has not been allocated yet. Allocate a new id.
3001 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3002 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003003
Sunny Goyald478c832016-04-01 12:04:16 -07003004 // Also try to bind the widget. If the bind fails, the user will be shown
3005 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003006 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003007 pendingInfo.spanX = item.spanX;
3008 pendingInfo.spanY = item.spanY;
3009 pendingInfo.minSpanX = item.minSpanX;
3010 pendingInfo.minSpanY = item.minSpanY;
3011 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003012
3013 boolean isDirectConfig =
3014 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3015 if (isDirectConfig && item.bindOptions != null) {
3016 Bundle newOptions = item.bindOptions.getExtras();
3017 if (options != null) {
3018 newOptions.putAll(options);
3019 }
3020 options = newOptions;
3021 }
Sunny Goyald478c832016-04-01 12:04:16 -07003022 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3023 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003024
Sunny Goyal86df1382016-08-10 15:03:22 -07003025 // We tried to bind once. If we were not able to bind, we would need to
3026 // go through the permission dialog, which means we cannot skip the config
3027 // activity.
3028 item.bindOptions = null;
3029 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3030
Sunny Goyald478c832016-04-01 12:04:16 -07003031 // Bind succeeded
3032 if (success) {
3033 // If the widget has a configure activity, it is still needs to set it up,
3034 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003035 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003036 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3037 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003038 }
Sunny Goyald478c832016-04-01 12:04:16 -07003039
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003040 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003041 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003042 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003043 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003044 // The widget was marked as UI not ready, but there is no configure activity to
3045 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003046 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003047 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003048 }
Sunny Goyalff572272014-07-23 13:58:07 -07003049 }
3050
Sunny Goyald5462aa2016-11-22 16:52:39 +05303051 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003052 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003053 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003054 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003055 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003056 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003057 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003058 }
3059
Sunny Goyal233ee962015-08-03 13:05:01 -07003060 item.minSpanX = appWidgetInfo.minSpanX;
3061 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303062 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003063 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303064 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003065 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303066 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003068 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003069 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 }
3071
Sunny Goyalff572272014-07-23 13:58:07 -07003072 /**
3073 * Restores a pending widget.
3074 *
3075 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003076 */
Sunny Goyald478c832016-04-01 12:04:16 -07003077 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003078 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3079 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3080 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003081 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003082 }
3083
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003084 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003085 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003086 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3087 info.pendingItemInfo = null;
3088 }
Sunny Goyalff572272014-07-23 13:58:07 -07003089
Sunny Goyalba47faa2017-10-04 16:50:57 -07003090 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3091 view.reinflate();
3092 }
3093
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003094 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003095 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003096 }
3097
Adam Cohen1462de32012-07-24 22:34:36 -07003098 public void onPageBoundSynchronously(int page) {
3099 mSynchronouslyBoundPages.add(page);
3100 }
3101
Sunny Goyal527c7d32015-08-28 15:19:36 -07003102 @Override
3103 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3104 if (mPendingExecutor != null) {
3105 mPendingExecutor.markCompleted();
3106 }
3107 mPendingExecutor = executor;
3108 executor.attachTo(this);
3109 }
3110
3111 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3112 if (mPendingExecutor == executor) {
3113 mPendingExecutor = null;
3114 }
3115 }
3116
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003117 @Override
3118 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3119 Runnable r = new Runnable() {
3120 public void run() {
3121 finishFirstPageBind(executor);
3122 }
3123 };
3124 if (waitUntilResume(r)) {
3125 return;
3126 }
3127
3128 Runnable onComplete = new Runnable() {
3129 @Override
3130 public void run() {
3131 if (executor != null) {
3132 executor.onLoadAnimationCompleted();
3133 }
3134 }
3135 };
3136 if (mDragLayer.getAlpha() < 1) {
3137 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3138 } else {
3139 onComplete.run();
3140 }
3141 }
3142
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143 /**
3144 * Callback saying that there aren't any more items to bind.
3145 *
3146 * Implementation of the method from LauncherModel.Callbacks.
3147 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003148 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003149 Runnable r = new Runnable() {
3150 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003151 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003152 }
3153 };
3154 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003155 return;
3156 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003157 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003158 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003159
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003160 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003161
Sunny Goyal2100c782016-08-22 16:00:03 -07003162 if (mPendingActivityResult != null) {
3163 handleActivityResult(mPendingActivityResult.requestCode,
3164 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3165 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003166 }
3167
Sunny Goyala474a9b2017-05-04 16:47:11 -07003168 InstallShortcutReceiver.disableAndFlushInstallQueue(
3169 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003170
Tony Wickham010d2552017-01-20 08:15:28 -08003171 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003172 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003173 }
3174
Winson Chunga2413752012-04-03 14:22:34 -07003175 private boolean canRunNewAppsAnimation() {
3176 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003177 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003178 }
3179
Winson Chungc9168342013-06-26 14:54:55 -07003180 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003181 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003182 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3183 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003184 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003185 return bounceAnim;
3186 }
3187
Adam Cohen27772732013-11-11 14:00:56 +00003188 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003189 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003190 }
3191
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003192 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 * Add the icons for all apps.
3194 *
3195 * Implementation of the method from LauncherModel.Callbacks.
3196 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003197 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003198 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3199 public void run() {
3200 bindAllApplications(apps);
3201 }
3202 };
3203 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003204 return;
3205 }
3206
Winson Chungb745afb2015-03-02 11:51:23 -08003207 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003208 Executor pendingExecutor = getPendingExecutor();
3209 if (pendingExecutor != null && mState != State.APPS) {
3210 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003211 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003212 return;
3213 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003214
Winson Chungb745afb2015-03-02 11:51:23 -08003215 mAppsView.setApps(apps);
3216 }
Adam Cohen9211d422014-10-07 18:14:53 -07003217 if (mLauncherCallbacks != null) {
3218 mLauncherCallbacks.bindAllApplications(apps);
3219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003220 }
3221
3222 /**
Tony2917a8b2017-07-31 23:29:42 -07003223 * Returns an Executor that will run after the launcher is first drawn (including after the
3224 * initial fade in animation). Returns null if the first draw has already occurred.
3225 */
3226 public @Nullable Executor getPendingExecutor() {
3227 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3228 }
3229
3230 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003231 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3232 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3233 */
3234 @Override
3235 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003236 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003237 }
3238
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003239 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003240 * A package was updated.
3241 *
3242 * Implementation of the method from LauncherModel.Callbacks.
3243 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003244 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003245 Runnable r = new Runnable() {
3246 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003247 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003248 }
3249 };
3250 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003251 return;
3252 }
3253
Winson Chungb745afb2015-03-02 11:51:23 -08003254 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003255 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003256 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003257 }
3258
Sunny Goyal4390ace2014-10-13 11:33:11 -07003259 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003260 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3261 Runnable r = new Runnable() {
3262 public void run() {
3263 bindPromiseAppProgressUpdated(app);
3264 }
3265 };
3266 if (waitUntilResume(r)) {
3267 return;
3268 }
3269
3270 if (mAppsView != null) {
3271 mAppsView.updatePromiseAppProgress(app);
3272 }
3273 }
3274
3275 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003276 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3277 Runnable r = new Runnable() {
3278 public void run() {
3279 bindWidgetsRestored(widgets);
3280 }
3281 };
3282 if (waitUntilResume(r)) {
3283 return;
3284 }
3285 mWorkspace.widgetsRestored(widgets);
3286 }
3287
Joe Onorato9c1289c2009-08-17 11:03:03 -04003288 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003289 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003290 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003291 *
3292 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003293 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003294 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003295 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003296 Runnable r = new Runnable() {
3297 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003298 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003299 }
3300 };
3301 if (waitUntilResume(r)) {
3302 return;
3303 }
3304
Sunny Goyal4390ace2014-10-13 11:33:11 -07003305 if (!updated.isEmpty()) {
3306 mWorkspace.updateShortcuts(updated);
3307 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003308 }
3309
3310 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003311 * Update the state of a package, typically related to install state.
3312 *
3313 * Implementation of the method from LauncherModel.Callbacks.
3314 */
Sunny Goyale755d462014-07-22 13:48:29 -07003315 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003316 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3317 Runnable r = new Runnable() {
3318 public void run() {
3319 bindRestoreItemsChange(updates);
3320 }
3321 };
3322 if (waitUntilResume(r)) {
3323 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003324 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003325
Sunny Goyal756adbc2015-04-16 15:20:51 -07003326 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003327 }
3328
3329 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003330 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003331 * in addition to specific applications to remove, the reason being that
3332 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003333 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003334 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003336 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003337 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003338 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003339 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003340 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003341 }
Winson Chungd64d1762013-08-20 14:37:16 -07003342 };
3343 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003344 return;
3345 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003346 mWorkspace.removeItemsByMatcher(matcher);
3347 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003348 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003349
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003350 @Override
3351 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3352 Runnable r = new Runnable() {
3353 public void run() {
3354 bindAppInfosRemoved(appInfos);
3355 }
3356 };
3357 if (waitUntilResume(r)) {
3358 return;
Joe Onorato36115782010-06-17 13:28:48 -04003359 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003360
Winson Chungdf95eb12013-10-16 14:57:07 -07003361 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003362 if (mAppsView != null) {
3363 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003364 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 }
Joe Onoratobe386092009-11-17 17:32:16 -08003366
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003367 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003368 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3369 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003370 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3371 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003372 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003373 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003374 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003375 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003376 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003377 return;
3378 }
3379
Tony Wickham26b17462017-03-20 17:12:24 -07003380 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3381 if (topView != null) {
3382 topView.onWidgetsBound();
3383 }
3384 }
3385
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003386 @Override
3387 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003388 if (mWorkspace.getState().shouldUpdateWidget) {
3389 refreshAndBindWidgetsForPackageUser(null);
3390 }
Tony Wickham26b17462017-03-20 17:12:24 -07003391 }
3392
Tony Wickham86222d22017-03-29 15:30:43 -07003393 /**
3394 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3395 * refreshes the widgets and shortcuts associated with the given package/user
3396 */
3397 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003398 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003399 }
3400
Winson Chung4b919f82012-05-01 10:44:08 -07003401 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003402 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003403 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003404 }
3405 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003406
Winson Chung4b919f82012-05-01 10:44:08 -07003407 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003408 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003409 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003410 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003411 } else {
3412 mHandler.postDelayed(new Runnable() {
3413 public void run() {
3414 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3415 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003416 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003417 }
Winson Chung4b919f82012-05-01 10:44:08 -07003418 }
Winson Chung400438b2011-01-16 17:53:48 -08003419 }
3420
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003421 private void markAppsViewShown() {
3422 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3423 return;
3424 }
3425 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3426 }
3427
3428 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003429 UserManagerCompat um = UserManagerCompat.getInstance(this);
3430 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003431 }
3432
Winson Chung80baf5a2010-08-09 16:03:15 -07003433 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003434 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003435 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003436 @Override
3437 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3438 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003439
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003440 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3441 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003442 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003443 writer.println(prefix + " Homescreen " + i);
3444
3445 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3446 for (int j = 0; j < layout.getChildCount(); j++) {
3447 Object tag = layout.getChildAt(j).getTag();
3448 if (tag != null) {
3449 writer.println(prefix + " " + tag.toString());
3450 }
3451 }
3452 }
3453
3454 writer.println(prefix + " Hotseat");
3455 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003456 for (int j = 0; j < layout.getChildCount(); j++) {
3457 Object tag = layout.getChildAt(j).getTag();
3458 if (tag != null) {
3459 writer.println(prefix + " " + tag.toString());
3460 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003461 }
Sunny Goyala1365452015-10-01 15:46:24 -07003462
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003463 try {
3464 FileLog.flushAll(writer);
3465 } catch (Exception e) {
3466 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003467 }
3468 }
3469
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003470 writer.println(prefix + "Misc:");
3471 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3472 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3473 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3474
3475 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003476
Adam Cohen9211d422014-10-07 18:14:53 -07003477 if (mLauncherCallbacks != null) {
3478 mLauncherCallbacks.dump(prefix, fd, writer, args);
3479 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003480 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003481
Sunny Goyal66b24572016-09-21 15:57:55 -07003482 @Override
3483 @TargetApi(Build.VERSION_CODES.N)
3484 public void onProvideKeyboardShortcuts(
3485 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3486
3487 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3488 if (mState == State.WORKSPACE) {
3489 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3490 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3491 }
3492 View currentFocus = getCurrentFocus();
3493 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3494 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3495 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3496 }
Tony18c4aa42017-05-10 21:23:57 -05003497 if (currentFocus.getTag() instanceof ItemInfo
3498 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003499 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3500 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3501 }
3502 if (!shortcutInfos.isEmpty()) {
3503 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3504 }
3505
3506 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3507 }
3508
3509 @Override
3510 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3511 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3512 switch (keyCode) {
3513 case KeyEvent.KEYCODE_A:
3514 if (mState == State.WORKSPACE) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003515 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003516 return true;
3517 }
3518 break;
3519 case KeyEvent.KEYCODE_S: {
3520 View focusedView = getCurrentFocus();
3521 if (focusedView instanceof BubbleTextView
3522 && focusedView.getTag() instanceof ItemInfo
3523 && mAccessibilityDelegate.performAction(focusedView,
3524 (ItemInfo) focusedView.getTag(),
3525 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003526 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003527 return true;
3528 }
3529 break;
3530 }
3531 case KeyEvent.KEYCODE_O:
3532 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3533 return true;
3534 }
3535 break;
3536 }
3537 }
3538 return super.onKeyShortcut(keyCode, event);
3539 }
3540
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003541 public static Launcher getLauncher(Context context) {
3542 if (context instanceof Launcher) {
3543 return (Launcher) context;
3544 }
3545 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3546 }
3547
Sunny Goyal5a81c382017-03-20 15:08:06 -07003548 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003549
3550 @Override
3551 public void onSharedPreferenceChanged(
3552 SharedPreferences sharedPreferences, String key) {
3553 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003554 // Recreate the activity so that it initializes the rotation preference again.
3555 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003556 }
3557 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003558 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003559
3560 /**
3561 * Callback for listening for onResume
3562 */
3563 public interface OnResumeCallback {
3564
3565 void onLauncherResume();
3566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003567}