blob: 1542703c52b203183521cb1b52358321d59c594b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
33import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
34
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070053import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040054import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070056import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070057import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070058import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070059import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070065import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020066import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080069import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070070import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070071import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070072import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070075import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070077import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070078import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.HapticFeedbackConstants;
81import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070082import android.view.KeyboardShortcutGroup;
83import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080084import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080088import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070091import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070092import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070097import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070098import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700100import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700101import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700104import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700105import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800106import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700109import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700110import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800111import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700112import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000113import com.android.launcher3.folder.Folder;
114import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700115import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700116import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700117import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700118import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800120import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700121import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700122import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800123import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800124import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700125import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700126import com.android.launcher3.states.AllAppsState;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800127import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530128import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
129import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
130import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700131import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700132import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700133import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700134import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700135import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700136import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800137import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700138import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700139import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700140import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700141import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700142import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700143import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700144import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700145import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700146import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800147import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700148import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800149import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700150import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700151import com.android.launcher3.widget.WidgetListRowEntry;
152import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700153import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700154
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700155import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700156import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700157import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700158import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700159import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700160import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800161import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700162import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164/**
165 * Default launcher application.
166 */
Sunny Goyal27835952017-01-13 12:15:53 -0800167public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700168 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
169 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700170 AccessibilityManager.AccessibilityStateChangeListener,
171 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700172 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700173 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Winson Chunga2413752012-04-03 14:22:34 -0700175 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700179
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700181 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Michael Jurka8b805b12012-04-18 14:23:14 -0700183 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700184 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
185 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700186
Jon Mirandac476d6e2017-05-04 16:30:01 -0700187 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700188
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700189 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
190
Mathew Inwood876a8462013-06-14 14:12:41 +0100191 /**
192 * IntentStarter uses request codes starting with this. This must be greater than all activity
193 * request codes used internally.
194 */
195 protected static final int REQUEST_LAST = 100;
196
Winson Chung2672ff92012-05-04 16:22:30 -0700197 // The Intent extra that defines whether to ignore the launch animation
198 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400199 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 // Type: int
202 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700203 // Type: int
204 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700205 // Type: PendingRequestArgs
206 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
207 // Type: ActivityResultInfo
208 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700209 // Type: SparseArray<Parcelable>
210 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700212 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700213
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700214 private boolean mIsSafeModeEnabled;
215
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Winson Chunga2413752012-04-03 14:22:34 -0700218 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700219 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
220 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
221 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700222
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700224 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800226 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700227
228 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700229
Sunny Goyalffe83f12014-08-14 17:39:34 -0700230 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700231 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700233 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234
Sunny Goyal316490e2015-06-02 09:38:28 -0700235 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800236 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700237
Michael Jurka838a4ca2011-02-07 13:33:06 -0800238 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700239
Sunny Goyal47328fd2016-05-25 18:56:41 -0700240 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700241
242 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700243 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700244 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700245
Jon Miranda6bed3502017-09-19 14:43:17 -0700246 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
247 // that results in widgets being inflated in the wrong orientation.
248 private int mOrientation;
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private SpannableStringBuilder mDefaultKeySsb = null;
251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800254 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700255 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700257 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700258 private OnResumeCallback mOnResumeCallback;
259
Sunny Goyal527c7d32015-08-28 15:19:36 -0700260 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700261
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800263 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800264 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700265 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700266 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267
Jon Miranda2d89ea82017-05-04 11:47:53 -0700268 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700269 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700270
Tony Wickham010d2552017-01-20 08:15:28 -0800271 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700272
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700273 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700274
Winson Chung46353de2012-02-16 14:05:10 -0800275 // We only want to get the SharedPreferences once since it does an FS stat each time we get
276 // it from the context.
277 private SharedPreferences mSharedPrefs;
278
Sunny Goyal2100c782016-08-22 16:00:03 -0700279 // Activity result which needs to be processed after workspace has loaded.
280 private ActivityResultInfo mPendingActivityResult;
281 /**
282 * Holds extra information required to handle a result from an external call, like
283 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
284 */
285 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800286
Jon Miranda379198e2016-09-23 08:54:40 -0700287 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700288
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700289 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700290 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700291 private boolean mAppLaunchSuccess;
292
Sunny Goyal745bad92016-05-02 10:54:12 -0700293 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 @Override
296 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700297 if (DEBUG_STRICT_MODE) {
298 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
299 .detectDiskReads()
300 .detectDiskWrites()
301 .detectNetwork() // or .detectAll() for all detectable problems
302 .penaltyLog()
303 .build());
304 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
305 .detectLeakedSqlLiteObjects()
306 .detectLeakedClosableObjects()
307 .penaltyLog()
308 .penaltyDeath()
309 .build());
310 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700311 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700312
Adam Cohen9211d422014-10-07 18:14:53 -0700313 if (mLauncherCallbacks != null) {
314 mLauncherCallbacks.preOnCreate();
315 }
316
Mario Bertschler27288382017-05-24 15:35:09 -0700317 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
318 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700319 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700320
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700322 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400323
Sunny Goyal87f784c2017-01-11 10:48:34 -0800324 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700325
Adam Cohen2e6da152015-05-06 11:42:25 -0700326 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800327 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700328 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700329 Display display = getWindowManager().getDefaultDisplay();
330 Point mwSize = new Point();
331 display.getSize(mwSize);
332 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
333 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700334
Jon Miranda6bed3502017-09-19 14:43:17 -0700335 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700336 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700337 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800338 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800339 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800340 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700341 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700342
Joe Onorato41a12d22009-10-31 18:30:00 -0400343 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700344 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700345 mStateManager = new LauncherStateManager(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700346
Sunny Goyalffe83f12014-08-14 17:39:34 -0700347 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700348
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700349 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700350 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700351
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700352 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
353 // this also ensures that any synchronous binding below doesn't re-trigger another
354 // LauncherModel load.
355 mPaused = false;
356
Sunny Goyal60820d72017-05-09 12:40:11 -0700357 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 setupViews();
Winson1f064272016-07-18 17:18:02 -0700360 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361
Tony Wickham010d2552017-01-20 08:15:28 -0800362 mPopupDataProvider = new PopupDataProvider(this);
363
Tony Wickhame2217252016-03-22 16:34:23 -0700364 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
365 .addAccessibilityStateChangeListener(this);
366
Sunny Goyalfe770c92016-09-27 14:38:58 -0700367 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368
Sunny Goyal2100c782016-08-22 16:00:03 -0700369 // We only load the page synchronously if the user rotates (or triggers a
370 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700371 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
372 if (savedInstanceState != null) {
373 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
374 }
375 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700376 // If we are not binding synchronously, show a fade in animation when
377 // the first page bind completes.
378 mDragLayer.setAlpha(0);
379 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700380 // Pages bound synchronously.
381 mWorkspace.setCurrentPage(currentScreen);
382
Sunny Goyal2100c782016-08-22 16:00:03 -0700383 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385
386 // For handling default keys
387 mDefaultKeySsb = new SpannableStringBuilder();
388 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800389
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800390 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700391 // In case we are on a device with locked rotation, we should look at preferences to check
392 // if the user has specifically allowed rotation.
393 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700394 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700395 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
396 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700397 }
398
Sunny Goyal5a81c382017-03-20 15:08:06 -0700399 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
400 // Temporarily enable the rotation
401 mRotationEnabled = true;
402 }
403
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700404 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
405 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700406 setRequestedOrientation(mRotationEnabled
407 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700408
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800409 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700410
Jon Miranda7fda2852017-07-19 16:07:01 -0700411 // Listen for broadcasts
412 IntentFilter filter = new IntentFilter();
413 filter.addAction(Intent.ACTION_SCREEN_OFF);
414 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
415 registerReceiver(mReceiver, filter);
416 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700417
Sunny Goyal8392c822017-06-20 10:03:56 -0700418 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
419 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700420
421 if (mLauncherCallbacks != null) {
422 mLauncherCallbacks.onCreate(savedInstanceState);
423 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700424
425 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700426 }
427
Sunny Goyal7779d622015-06-11 16:18:39 -0700428 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700429 public void onThemeChanged() {
430 recreate();
431 }
432
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700433 public LauncherStateManager getStateManager() {
434 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700435 }
436
Mario Bertschlera6936942017-05-31 14:48:19 -0700437 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700438 if (isDark) {
439 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700440 } else if (supportsDarkText) {
441 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700442 }
443 }
444
445 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700446 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800447 return mLauncherView.findViewById(id);
448 }
449
450 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700451 public void onAppWidgetHostReset() {
452 if (mAppWidgetHost != null) {
453 mAppWidgetHost.startListening();
454 }
455 }
456
Adam Cohen9211d422014-10-07 18:14:53 -0700457 private LauncherCallbacks mLauncherCallbacks;
458
Winson1f064272016-07-18 17:18:02 -0700459 public void onInsetsChanged(Rect insets) {
460 mDeviceProfile.updateInsets(insets);
461 mDeviceProfile.layout(this, true /* notifyListeners */);
462 }
463
Sunny Goyal32554d12015-12-03 15:31:25 -0800464 /**
465 * Call this after onCreate to set or clear overlay.
466 */
467 public void setLauncherOverlay(LauncherOverlay overlay) {
468 if (overlay != null) {
469 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
470 }
471 mWorkspace.setLauncherOverlay(overlay);
472 }
473
Adam Cohen9211d422014-10-07 18:14:53 -0700474 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
475 mLauncherCallbacks = callbacks;
476 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700477 }
478
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700479 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700480 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700481 if (mLauncherCallbacks != null) {
482 mLauncherCallbacks.onLauncherProviderChange();
483 }
484 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700485
Hyunyoung Song3f471442015-04-08 19:01:34 -0700486 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700487 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
488 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700489 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700490 }
491
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000492 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700493 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
494 // This cast is safe as long as the id < 0x00FFFFFF
495 // Since we jail all the dynamically generated views, there should be no clashes
496 // with any other views.
497 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000498 }
499
Tony Wickham010d2552017-01-20 08:15:28 -0800500 public PopupDataProvider getPopupDataProvider() {
501 return mPopupDataProvider;
502 }
503
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000504 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700505 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
506 * a configuration step, this allows the proper animations to run after other transitions.
507 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 private long completeAdd(
509 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
510 long screenId = info.screenId;
511 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700512 // When the screen id represents an actual screen (as opposed to a rank) we make sure
513 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700515 }
Adam Cohendb364c32014-05-20 14:23:40 -0700516
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700519 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700520 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700523 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700524 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700526 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700527 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 int widgetId = appWidgetId;
529 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700530 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700531 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700532 // Since the view was just bound, also launch the configure activity if needed
533 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
534 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800535 if (provider != null) {
536 new WidgetAddFlowHandler(provider)
537 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700538 }
539 }
540 break;
541 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800542 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700543
Adam Cohendb364c32014-05-20 14:23:40 -0700544 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800545 }
546
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800547 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700548 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700549 if (isWorkspaceLoading()) {
550 // process the result once the workspace has loaded.
551 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
552 return;
553 }
554 mPendingActivityResult = null;
555
Winson Chunge341d302013-08-16 14:31:00 -0700556 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700557 final PendingRequestArgs requestArgs = mPendingRequestArgs;
558 setWaitingForResult(null);
559 if (requestArgs == null) {
560 return;
561 }
562
563 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700564
Adam Cohenad4e15c2013-10-17 16:21:35 -0700565 Runnable exitSpringLoaded = new Runnable() {
566 @Override
567 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700568 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700569 }
570 };
571
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700573 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
576 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700577 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700578 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700579 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700581 addAppWidgetImpl(
582 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800583 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700584 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 }
586 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200587 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700588 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700589 // User could have free-scrolled between pages before picking a wallpaper; make sure
590 // we move to the closest one now.
591 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700592 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200593 }
594 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700595 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200596
Adam Cohened66b2b2012-01-23 17:28:51 -0800597 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700598 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700599
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 // We have special handling for widgets
601 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800602 final int appWidgetId;
603 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
604 : -1;
605 if (widgetId < 0) {
606 appWidgetId = pendingAddWidgetId;
607 } else {
608 appWidgetId = widgetId;
609 }
610
Adam Cohenad4e15c2013-10-17 16:21:35 -0700611 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800612 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700613 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
614 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700616 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700617 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700618 @Override
619 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700620 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700621 }
622 };
Adam Cohendb364c32014-05-20 14:23:40 -0700623
Sunny Goyal2100c782016-08-22 16:00:03 -0700624 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
625 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
626 } else {
627 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
628 // When the screen id represents an actual screen (as opposed to a rank)
629 // we make sure that the drop page actually exists.
630 requestArgs.screenId =
631 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700632 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700633 final CellLayout dropLayout =
634 mWorkspace.getScreenWithId(requestArgs.screenId);
635
636 dropLayout.setDropPending(true);
637 final Runnable onComplete = new Runnable() {
638 @Override
639 public void run() {
640 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
641 dropLayout.setDropPending(false);
642 }
643 };
644 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
645 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700646 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 return;
648 }
649
Sunny Goyald478c832016-04-01 12:04:16 -0700650 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
651 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700652 if (resultCode == RESULT_OK) {
653 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700655 }
656 // Leave the widget in the pending state if the user canceled the configure.
657 return;
658 }
659
Sunny Goyalaad90582015-07-09 14:22:50 -0700660 if (requestCode == REQUEST_CREATE_SHORTCUT) {
661 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
663 completeAdd(requestCode, data, -1, requestArgs);
664 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
665 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
666
Sunny Goyalaad90582015-07-09 14:22:50 -0700667 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700668 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
669 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800673 }
674
675 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700676 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800677 final int requestCode, final int resultCode, final Intent data) {
678 handleActivityResult(requestCode, resultCode, data);
679 if (mLauncherCallbacks != null) {
680 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
681 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 }
683
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700684 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700685 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600686 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 PendingRequestArgs pendingArgs = mPendingRequestArgs;
688 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
689 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
690 setWaitingForResult(null);
691
Sunny Goyal28c6b962015-10-12 11:42:05 -0700692 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700693 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700694 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 Intent intent = pendingArgs.getPendingIntent();
698
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 if (grantResults.length > 0
700 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700701 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700702 } else {
703 // TODO: Show a snack bar with link to settings
704 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700705 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700706 }
707 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600708 if (mLauncherCallbacks != null) {
709 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
710 grantResults);
711 }
712 }
713
Adam Cohendb364c32014-05-20 14:23:40 -0700714 /**
715 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
716 *
717 * @param screenId the screen id to check
718 * @return the new screen, or screenId if it exists
719 */
720 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800721 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700722 if (dropLayout == null) {
723 // it's possible that the add screen was removed because it was
724 // empty and a re-bind occurred
725 mWorkspace.addExtraEmptyScreen();
726 return mWorkspace.commitExtraEmptyScreen();
727 } else {
728 return screenId;
729 }
730 }
731
Sunny Goyal2100c782016-08-22 16:00:03 -0700732 @Thunk void completeTwoStageWidgetDrop(
733 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
734 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 Runnable onCompleteRunnable = null;
736 int animationType = 0;
737
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700738 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 if (resultCode == RESULT_OK) {
740 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
741 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800742 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 onCompleteRunnable = new Runnable() {
745 @Override
746 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700747 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700748 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 }
750 };
751 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800752 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700755 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700756 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700757 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
758 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700760 // The animated view may be null in the case of a rotation during widget configuration
761 onCompleteRunnable.run();
762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
764
765 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700766 protected void onStop() {
767 super.onStop();
768 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700769
770 if (mLauncherCallbacks != null) {
771 mLauncherCallbacks.onStop();
772 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700773
Sunny Goyalf5e37442016-11-02 10:31:24 -0700774 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700775 mAppWidgetHost.stopListening();
776 }
Tony Wickham010d2552017-01-20 08:15:28 -0800777
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700778 if (!mAppLaunchSuccess) {
779 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700780 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700781 }
Tony Wickham010d2552017-01-20 08:15:28 -0800782 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700783 }
784
785 @Override
786 protected void onStart() {
787 super.onStart();
788 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700789
790 if (mLauncherCallbacks != null) {
791 mLauncherCallbacks.onStart();
792 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700793
Sunny Goyalf5e37442016-11-02 10:31:24 -0700794 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700795 mAppWidgetHost.startListening();
796 }
Tony Wickham010d2552017-01-20 08:15:28 -0800797
798 if (!isWorkspaceLoading()) {
799 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
800 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700801
Jon Miranda7fda2852017-07-19 16:07:01 -0700802 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700803 if (mScrimAnimator != null) {
804 mScrimAnimator.cancel();
805 }
806 mDragLayer.getBackground().setAlpha(0);
807 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
808 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
809 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
810 @Override
811 public void onAnimationEnd(Animator animation) {
812 mScrimAnimator = null;
813 }
814 });
815 mScrimAnimator.setDuration(600);
816 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
817 mScrimAnimator.start();
818 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700819 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700820 }
821
822 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700824 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700826 TraceHelper.partitionSection("ON_RESUME", "superCall");
827
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700828 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700829 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800830 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700831 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700832 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700833 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700834 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700836 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200837 // We might have postponed some bind calls until onResume (see waitUntilResume) --
838 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700839 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
840 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200841 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700842 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200843 }
Winson Chunge4e50662012-01-23 14:45:13 -0800844
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700845 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700846 // Process any items that were added while Launcher was away.
847 InstallShortcutReceiver.disableAndFlushInstallQueue(
848 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700849
Sunny Goyala474a9b2017-05-04 16:47:11 -0700850 // Refresh shortcuts if the permission changed.
851 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700852
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700853 if (shouldShowDiscoveryBounce()) {
854 mAllAppsController.showDiscoveryBounce();
855 }
Adam Cohen9211d422014-10-07 18:14:53 -0700856 if (mLauncherCallbacks != null) {
857 mLauncherCallbacks.onResume();
858 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800859
Sunny Goyala502aa32017-10-02 16:04:06 -0700860 clearTypedText();
861
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700862 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700863 }
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700866 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700867 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700868 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700869
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700870 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700871 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800872 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700873 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700874
Adam Cohen9211d422014-10-07 18:14:53 -0700875 if (mLauncherCallbacks != null) {
876 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700877 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700878 }
879
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880 public interface LauncherOverlay {
881
882 /**
883 * Touch interaction leading to overscroll has begun
884 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700885 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700886
887 /**
888 * Touch interaction related to overscroll has ended
889 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700890 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700891
892 /**
893 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
894 * screen (or in the case of RTL, the rightmost screen).
895 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700896 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700897
898 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800899 * Called when the launcher is ready to use the overlay
900 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700901 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700902 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700903 }
904
905 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700906 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700907 }
908
909 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
910
Sunny Goyalc86df472016-02-25 09:19:38 -0800911 public void onScrollChanged(float progress) {
912 if (mWorkspace != null) {
913 mWorkspace.onOverlayScrollChanged(progress);
914 }
915 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700916 }
917
Jorim Jaggid017f882014-01-14 17:08:48 -0800918 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700919 if (mLauncherCallbacks != null) {
920 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700921 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800922 // On O and above we there is always some setting present settings (add icon to
923 // home screen or icon badging). On earlier APIs we will have the allow rotation
924 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700925 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700926 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800927 }
928
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700929 @Override
930 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700932 if (mModel.isCurrentCallbacks(this)) {
933 mModel.stopLoader();
934 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700935 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
936
Romain Guy13c2e7b2010-03-10 19:45:00 -0800937 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700938 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700939
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800940 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800941 @Override
942 public void onWindowFocusChanged(boolean hasFocus) {
943 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700944 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700945
946 if (mLauncherCallbacks != null) {
947 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
948 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800949 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 private boolean acceptFilter() {
952 final InputMethodManager inputManager = (InputMethodManager)
953 getSystemService(Context.INPUT_METHOD_SERVICE);
954 return !inputManager.isFullscreenMode();
955 }
956
957 @Override
958 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700959 final int uniChar = event.getUnicodeChar();
960 final boolean handled = super.onKeyDown(keyCode, event);
961 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
962 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
964 keyCode, event);
965 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700967 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 // showSearchDialog()
969 // If there are multiple keystrokes before the search dialog takes focus,
970 // onSearchRequested() will be called for every keystroke,
971 // but it is idempotent, so it's fine.
972 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974 }
975
Joe Onorato8a9625e2010-01-28 15:55:35 -0800976 // Eat the long press event so the keyboard doesn't come up.
977 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
978 return true;
979 }
980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 return handled;
982 }
983
Winson46163472015-09-22 17:31:56 -0700984 @Override
985 public boolean onKeyUp(int keyCode, KeyEvent event) {
986 if (keyCode == KeyEvent.KEYCODE_MENU) {
987 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700988 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700989 // Close any open floating view
990 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -0700991
Winson46163472015-09-22 17:31:56 -0700992 // Show the overview mode if we are on the workspace
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700993 if (isInState(NORMAL) && !mWorkspace.isSwitchingState()) {
994 mStateManager.goToState(OVERVIEW, true /* animate */, new Runnable() {
995 @Override
996 public void run() {
997 // Hitting the menu button when in touch mode does not trigger touch
998 // mode to be disabled, so if requested, force focus on one of the
999 // overview panel buttons.
1000 mOverviewPanel.requestFocusFromTouch();
1001 }
1002 });
Winson46163472015-09-22 17:31:56 -07001003 }
1004 }
1005 return true;
1006 }
1007 return super.onKeyUp(keyCode, event);
1008 }
1009
Karl Rosaen138a0412009-04-23 19:00:21 -07001010 private String getTypedText() {
1011 return mDefaultKeySsb.toString();
1012 }
1013
Sunny Goyal6f28e712016-09-13 14:19:29 -07001014 @Override
1015 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001016 mDefaultKeySsb.clear();
1017 mDefaultKeySsb.clearSpans();
1018 Selection.setSelection(mDefaultKeySsb, 0);
1019 }
1020
Sunny Goyalf9403d92017-10-18 10:55:56 -07001021 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001022 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001023 }
1024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 /**
1026 * Restores the previous state, if it exists.
1027 *
1028 * @param savedState The previous state.
1029 */
1030 private void restoreState(Bundle savedState) {
1031 if (savedState == null) {
1032 return;
1033 }
1034
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001035 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001036 LauncherState[] stateValues = LauncherState.values();
1037 LauncherState state = stateValues[stateOrdinal];
1038 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001039 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001040 }
1041
Sunny Goyal2100c782016-08-22 16:00:03 -07001042 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1043 if (requestArgs != null) {
1044 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001046
1047 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001048
1049 SparseArray<Parcelable> widgetsState =
1050 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1051 if (widgetsState != null) {
1052 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 }
1055
1056 /**
1057 * Finds all the views we need and configure them properly.
1058 */
1059 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001060 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001061 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001062 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001063 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001064
Sunny Goyal784f9c32016-03-23 16:56:54 -07001065 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1066 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1067 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068
Winson Chung4c98d922011-05-31 16:50:48 -07001069 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001070 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001071
Winson Chung3d503fb2011-07-13 17:25:49 -07001072 // Setup the hotseat
1073 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1074 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001075 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001076 }
1077
Winsone9f27272015-10-13 10:47:51 -07001078 // Setup the overview panel
1079 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001080
Winson Chung4c98d922011-05-31 16:50:48 -07001081 // Setup the workspace
1082 mWorkspace.setHapticFeedbackEnabled(false);
1083 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001084 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001085 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1086 // default state, otherwise we will update to the wrong offsets in RTL
1087 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001088 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001089 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001090
Tony Wickham34d2c912015-09-11 09:27:58 -07001091 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001092 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001093
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001094 // Setup Apps
1095 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001096
Winson Chung3d503fb2011-07-13 17:25:49 -07001097 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001098 mDragController.setMoveTarget(mWorkspace);
1099 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001100 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001101
Hyunyoung Songd725f642017-08-17 22:26:35 -07001102 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001103
Sunny Goyal322d5562015-06-25 19:35:49 -07001104 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1105 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
1108
Winsone9f27272015-10-13 10:47:51 -07001109 private void setupOverviewPanel() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001110 mOverviewPanel = findViewById(R.id.overview_panel);
Winsone9f27272015-10-13 10:47:51 -07001111
Winsone9f27272015-10-13 10:47:51 -07001112 // Bind wallpaper button actions
1113 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301114 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001115 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001116 public void handleViewClick(View view) {
1117 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001118 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001119 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001120
1121 // Bind widget button actions
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301122 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001123 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001124 public void handleViewClick(View view) {
1125 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001126 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001127 }.attachTo(findViewById(R.id.widget_button));
Winsone9f27272015-10-13 10:47:51 -07001128
1129 // Bind settings actions
1130 View settingsButton = findViewById(R.id.settings_button);
1131 boolean hasSettings = hasSettings();
1132 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301133 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001134 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001135 public void handleViewClick(View view) {
1136 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001137 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001138 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001139 } else {
1140 settingsButton.setVisibility(View.GONE);
1141 }
1142
1143 mOverviewPanel.setAlpha(0f);
1144 }
1145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001147 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001148 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001149 */
1150 public void setAllAppsButton(View allAppsButton) {
1151 mAllAppsButton = allAppsButton;
1152 }
1153
Anjali Koppal5ad44842014-03-10 20:34:39 -07001154 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 * Creates a view representing a shortcut.
1156 *
1157 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001159 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001160 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 }
1162
1163 /**
1164 * Creates a view representing a shortcut inflated from the specified resource.
1165 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 * @param parent The group the shortcut belongs to.
1167 * @param info The data structure describing the shortcut.
1168 *
1169 * @return A View inflated from layoutResId.
1170 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001171 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001172 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1173 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001174 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001176 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 return favorite;
1178 }
1179
1180 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001181 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 *
1183 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001186 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001187 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1188 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001189 return;
1190 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001191
Jon Mirandac476d6e2017-05-04 16:30:01 -07001192 int[] cellXY = mTmpAddItemCellCoordinates;
1193 CellLayout layout = getCellLayout(container, screenId);
1194
Sunny Goyal782f0c92017-01-19 10:27:54 -08001195 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001196 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001197 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1198 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001199 }
1200
1201 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001202 // Legacy shortcuts are only supported for primary profile.
1203 info = Process.myUserHandle().equals(args.user)
1204 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001205
Sunny Goyalb57645f2017-03-20 13:57:28 -07001206 if (info == null) {
1207 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1208 return;
1209 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001210 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001211 // The app is trying to add a shortcut without sufficient permissions
1212 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1213 return;
1214 }
1215 }
1216
Jon Mirandac476d6e2017-05-04 16:30:01 -07001217 if (container < 0) {
1218 // Adding a shortcut to the Workspace.
1219 final View view = createShortcut(info);
1220 boolean foundCellSpan = false;
1221 // First we check if we already know the exact location where we want to add this item.
1222 if (cellX >= 0 && cellY >= 0) {
1223 cellXY[0] = cellX;
1224 cellXY[1] = cellY;
1225 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001226
Jon Mirandac476d6e2017-05-04 16:30:01 -07001227 // If appropriate, either create a folder or add to an existing folder
1228 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001229 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001230 return;
1231 }
1232 DragObject dragObject = new DragObject();
1233 dragObject.dragInfo = info;
1234 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1235 true)) {
1236 return;
1237 }
1238 } else {
1239 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1240 }
1241
1242 if (!foundCellSpan) {
1243 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001244 return;
1245 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001246
1247 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1248 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001249 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001250 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001251 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001252 if (folderIcon != null) {
1253 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1254 folderInfo.add(info, args.rank, false);
1255 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001256 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001257 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
Sunny Goyale29897f52017-07-20 10:09:42 -07001261 public FolderIcon findFolderIcon(final long folderIconId) {
1262 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1263 @Override
1264 public boolean evaluate(ItemInfo info, View view) {
1265 return info != null && info.id == folderIconId;
1266 }
1267 });
1268 }
1269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001271 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001273 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001275 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001276 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1277
Adam Cohened66b2b2012-01-23 17:28:51 -08001278 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001279 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001280 }
Romain Guycbb89e42009-06-08 15:52:54 -07001281
Adam Cohen59400422014-03-05 18:07:04 -08001282 LauncherAppWidgetInfo launcherInfo;
1283 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001284 launcherInfo.spanX = itemInfo.spanX;
1285 launcherInfo.spanY = itemInfo.spanY;
1286 launcherInfo.minSpanX = itemInfo.minSpanX;
1287 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001288 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001289
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001290 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001291 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292
Sunny Goyal2100c782016-08-22 16:00:03 -07001293 if (hostView == null) {
1294 // Perform actual inflation because we're live
1295 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001297 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301298 prepareAppWidget(hostView, launcherInfo);
1299 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
Romain Guycbb89e42009-06-08 15:52:54 -07001301
Sunny Goyald5462aa2016-11-22 16:52:39 +05301302 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001303 hostView.setTag(item);
1304 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001305 hostView.setFocusable(true);
1306 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001307 }
1308
Adam Cohended9f8d2010-11-03 13:25:16 -07001309 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1310 @Override
1311 public void onReceive(Context context, Intent intent) {
1312 final String action = intent.getAction();
1313 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001314 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001315 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001316 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001317 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001318 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001319 mShouldFadeInScrim = true;
1320 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1321 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1322 // the user unlocked and the Launcher is not in the foreground.
1323 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001324 }
1325 }
1326 };
1327
Tony Wickham010d2552017-01-20 08:15:28 -08001328 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1329 Runnable r = new Runnable() {
1330 @Override
1331 public void run() {
1332 mWorkspace.updateIconBadges(updatedBadges);
1333 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001334
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001335 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1336 if (popup instanceof PopupContainerWithArrow) {
1337 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001338 }
Tony Wickham010d2552017-01-20 08:15:28 -08001339 }
1340 };
1341 if (!waitUntilResume(r)) {
1342 r.run();
1343 }
1344 }
1345
Adam Cohended9f8d2010-11-03 13:25:16 -07001346 @Override
1347 public void onAttachedToWindow() {
1348 super.onAttachedToWindow();
1349
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001350 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001351 if (mLauncherCallbacks != null) {
1352 mLauncherCallbacks.onAttachedToWindow();
1353 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 }
1355
1356 @Override
1357 public void onDetachedFromWindow() {
1358 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001359
1360 if (mLauncherCallbacks != null) {
1361 mLauncherCallbacks.onDetachedFromWindow();
1362 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 }
1364
Winson Chunga6945242014-01-08 14:04:34 -08001365 public DragLayer getDragLayer() {
1366 return mDragLayer;
1367 }
1368
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001369 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001370 return mAppsView;
1371 }
1372
Winson Chunga6945242014-01-08 14:04:34 -08001373 public Workspace getWorkspace() {
1374 return mWorkspace;
1375 }
1376
1377 public Hotseat getHotseat() {
1378 return mHotseat;
1379 }
1380
Jorim Jaggid017f882014-01-14 17:08:48 -08001381 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001382 return mOverviewPanel;
1383 }
1384
Sunny Goyal47328fd2016-05-25 18:56:41 -07001385 public DropTargetBar getDropTargetBar() {
1386 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001387 }
1388
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001389 public LauncherAppWidgetHost getAppWidgetHost() {
1390 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
Romain Guycbb89e42009-06-08 15:52:54 -07001392
Winson Chunga9abd0e2010-10-27 17:18:37 -07001393 public LauncherModel getModel() {
1394 return mModel;
1395 }
1396
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001397 public ModelWriter getModelWriter() {
1398 return mModelWriter;
1399 }
1400
Adam Cohen79d90c52016-04-22 13:29:20 -07001401 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001402 return mSharedPrefs;
1403 }
1404
Jon Miranda6bed3502017-09-19 14:43:17 -07001405 public int getOrientation() { return mOrientation; }
1406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 @Override
1408 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001409 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 super.onNewIntent(intent);
1411
Winson15f8b172015-08-19 11:02:39 -07001412 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1413 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1414 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001415
1416 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001417 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001418 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001419
Winson15f8b172015-08-19 11:02:39 -07001420 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1421 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001422 if (mWorkspace == null) {
1423 // Can be cases where mWorkspace is null, this prevents a NPE
1424 return;
1425 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001426
1427 // Note: There should be at most one log per method call. This is enforced implicitly
1428 // by using if-else statements.
1429 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001430 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001431 if (topOpenView != null) {
1432 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001433 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001434 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001435 target.pageIndex = mWorkspace.getCurrentPage();
1436 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001437 }
1438
1439 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001440 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001441 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001442
1443 final View v = getWindow().peekDecorView();
1444 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001445 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001446 }
1447
Winson Chungb745afb2015-03-02 11:51:23 -08001448 // Reset the apps view
1449 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001450 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001451 }
1452
Adam Cohen9211d422014-10-07 18:14:53 -07001453 if (mLauncherCallbacks != null) {
1454 mLauncherCallbacks.onHomeIntent();
1455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001457 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001458
Adam Cohen9211d422014-10-07 18:14:53 -07001459 if (mLauncherCallbacks != null) {
1460 mLauncherCallbacks.onNewIntent(intent);
1461 }
Winson15f8b172015-08-19 11:02:39 -07001462
1463 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1464 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1465 // animation.
1466 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001467 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001468
Winson15f8b172015-08-19 11:02:39 -07001469 mWorkspace.post(new Runnable() {
1470 @Override
1471 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001472 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001473 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001474 }
Winson15f8b172015-08-19 11:02:39 -07001475 }
1476 });
1477 }
1478 }
1479
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001480 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001481 }
1482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001484 public void onRestoreInstanceState(Bundle state) {
1485 super.onRestoreInstanceState(state);
1486 for (int page: mSynchronouslyBoundPages) {
1487 mWorkspace.restoreInstanceStateForChild(page);
1488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 @Override
1492 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001493 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001494 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001495
Adam Cohen21cd0022013-10-09 18:57:02 -07001496 }
Sunny Goyalea609262017-10-25 15:47:38 -07001497 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001498
1499
1500 AbstractFloatingView widgets = AbstractFloatingView
1501 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1502 if (widgets != null) {
1503 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1504 widgets.saveHierarchyState(widgetsState);
1505 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1506 } else {
1507 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1508 }
1509
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001510 // We close any open folders and shortcut containers since they will not be re-opened,
1511 // and we need to make sure this state is reflected.
1512 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001513
Sunny Goyal2100c782016-08-22 16:00:03 -07001514 if (mPendingRequestArgs != null) {
1515 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1516 }
1517 if (mPendingActivityResult != null) {
1518 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001521 super.onSaveInstanceState(outState);
1522
Adam Cohen9211d422014-10-07 18:14:53 -07001523 if (mLauncherCallbacks != null) {
1524 mLauncherCallbacks.onSaveInstanceState(outState);
1525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527
1528 @Override
1529 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001532 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001533 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001534
Winson Chungcd2b0142011-06-08 16:02:26 -07001535 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001536 // It's possible to receive onDestroy after a new Launcher activity has
1537 // been created. In this case, don't interfere with the new Launcher.
1538 if (mModel.isCurrentCallbacks(this)) {
1539 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001540 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001541 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001542
Sunny Goyal745bad92016-05-02 10:54:12 -07001543 if (mRotationPrefChangeHandler != null) {
1544 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001548 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001550 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001552 mAppWidgetHost = null;
1553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 TextKeyListener.getInstance().release();
1555
Tony Wickhame2217252016-03-22 16:34:23 -07001556 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1557 .removeAccessibilityStateChangeListener(this);
1558
Mario Bertschler27288382017-05-24 15:35:09 -07001559 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1560
Michael Jurka2ecf9952012-06-18 12:52:28 -07001561 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001562
Tony2917a8b2017-07-31 23:29:42 -07001563 clearPendingBinds();
1564
Adam Cohen9211d422014-10-07 18:14:53 -07001565 if (mLauncherCallbacks != null) {
1566 mLauncherCallbacks.onDestroy();
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569
Sunny Goyalae502842016-06-17 08:43:56 -07001570 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1571 return mAccessibilityDelegate;
1572 }
1573
Adam Cohena9cf38f2011-05-02 15:36:58 -07001574 public DragController getDragController() {
1575 return mDragController;
1576 }
1577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001579 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001580 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001581 }
1582
1583 @Override
1584 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1585 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001586 try {
1587 super.startIntentSenderForResult(intent, requestCode,
1588 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1589 } catch (IntentSender.SendIntentException e) {
1590 throw new ActivityNotFoundException();
1591 }
1592 }
1593
Winson Chung70d72102011-08-12 11:24:35 -07001594 /**
1595 * Indicates that we want global search for this activity by setting the globalSearch
1596 * argument for {@link #startSearch} to true.
1597 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001599 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001601
Karl Rosaen138a0412009-04-23 19:00:21 -07001602 if (initialQuery == null) {
1603 // Use any text typed in the launcher as the initial query
1604 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 if (appSearchData == null) {
1607 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001608 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001610
Sunny Goyal6f28e712016-09-13 14:19:29 -07001611 if (mLauncherCallbacks == null ||
1612 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1613 // Starting search from the callbacks failed. Start the default global search.
1614 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001615 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001616
1617 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001618 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001619 }
1620
Ian Parkinson047036e2014-09-01 15:40:53 +01001621 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001622 * Starts the global search activity. This code is a copied from SearchManager
1623 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001624 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001625 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001626 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001627 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1628 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1629 if (globalSearchActivity == null) {
1630 Log.w(TAG, "No global search activity found.");
1631 return;
1632 }
1633 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1634 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1635 intent.setComponent(globalSearchActivity);
1636 // Make sure that we have a Bundle to put source in
1637 if (appSearchData == null) {
1638 appSearchData = new Bundle();
1639 } else {
1640 appSearchData = new Bundle(appSearchData);
1641 }
Adam Cohen9211d422014-10-07 18:14:53 -07001642 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001643 if (!appSearchData.containsKey("source")) {
1644 appSearchData.putString("source", getPackageName());
1645 }
1646 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1647 if (!TextUtils.isEmpty(initialQuery)) {
1648 intent.putExtra(SearchManager.QUERY, initialQuery);
1649 }
1650 if (selectInitialQuery) {
1651 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1652 }
1653 intent.setSourceBounds(sourceBounds);
1654 try {
1655 startActivity(intent);
1656 } catch (ActivityNotFoundException ex) {
1657 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 }
1660
Winson Chung70d72102011-08-12 11:24:35 -07001661 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001662 public boolean onPrepareOptionsMenu(Menu menu) {
1663 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001664 if (mLauncherCallbacks != null) {
1665 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1666 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001667 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001670 @Override
1671 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001672 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001673 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001674 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001675 }
1676
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001678 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679 }
1680
Adam Cohen517a7f52014-03-01 12:12:59 -08001681 public boolean isWorkspaceLoading() {
1682 return mWorkspaceLoading;
1683 }
1684
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001685 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001686 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001687 }
1688
Sunny Goyal04a324a2017-01-20 21:08:59 -08001689 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001690 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001691 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001692
Sunny Goyal2100c782016-08-22 16:00:03 -07001693 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001694 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001695 if (LOGD) {
1696 Log.d(TAG, "Adding widget from drop");
1697 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001698 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001699 }
1700
Sunny Goyal2100c782016-08-22 16:00:03 -07001701 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001702 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1703 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1704 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001705
Adam Cohenad4e15c2013-10-17 16:21:35 -07001706 Runnable onComplete = new Runnable() {
1707 @Override
1708 public void run() {
1709 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001710 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001711 }
1712 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001713 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001714 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 }
1716 }
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001718 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1719 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001720 info.container = container;
1721 info.screenId = screenId;
1722 if (cell != null) {
1723 info.cellX = cell[0];
1724 info.cellY = cell[1];
1725 }
1726 info.spanX = spanX;
1727 info.spanY = spanY;
1728
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001729 switch (info.itemType) {
1730 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1731 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001732 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001733 break;
1734 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001735 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001736 break;
1737 default:
1738 throw new IllegalStateException("Unknown item type: " + info.itemType);
1739 }
1740 }
1741
Adam Cohenfbba09b2011-07-18 21:43:05 -07001742 /**
1743 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001744 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001745 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001746 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1747 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001748 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1749 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1750 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001751 }
1752
Adam Cohenfbba09b2011-07-18 21:43:05 -07001753 /**
1754 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001755 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001756 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001757 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001758 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001759 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001760 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001761 // In the case where we've prebound the widget, we remove it from the DragLayer
1762 if (LOGD) {
1763 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1764 }
1765 getDragLayer().removeView(hostView);
1766
Adam Cohened66b2b2012-01-23 17:28:51 -08001767 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001768 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001769
1770 // Clear the boundWidget so that it doesn't get destroyed.
1771 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001772 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001773 // In this case, we either need to start an activity to get permission to bind
1774 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001775 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1776 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1777 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1778 this, info.componentName);
1779 } else {
1780 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1781 }
Adam Cohendd70d662012-10-04 16:53:44 -07001782 Bundle options = info.bindOptions;
1783
Sunny Goyalffe83f12014-08-14 17:39:34 -07001784 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1785 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001786 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001787 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001788 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001789 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001790 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001791 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001792 }
1793
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001795 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001796 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 folderInfo.title = getText(R.string.folder_name);
1798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001800 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801
1802 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001803 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301804 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001805 // Force measure the new folder icon
1806 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1807 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001808 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Winsonc0b52fe2015-09-09 16:38:15 -07001811 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001812 * Unbinds the view for the specified item, and removes the item and all its children.
1813 *
1814 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001815 * @param itemInfo the {@link ItemInfo} for this view.
1816 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001817 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001818 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001819 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001820 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001821 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1822 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001823 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001824 } else {
1825 mWorkspace.removeWorkspaceItem(v);
1826 }
Winsonc0b52fe2015-09-09 16:38:15 -07001827 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001828 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001829 }
1830 } else if (itemInfo instanceof FolderInfo) {
1831 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001832 if (v instanceof FolderIcon) {
1833 ((FolderIcon) v).removeListeners();
1834 }
Winsonc0b52fe2015-09-09 16:38:15 -07001835 mWorkspace.removeWorkspaceItem(v);
1836 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001837 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001838 }
1839 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1840 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001841 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001842 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001843 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001844 }
1845 } else {
1846 return false;
1847 }
1848 return true;
1849 }
1850
1851 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001852 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001853 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001854 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001855 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001856 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001857 // Deleting an app widget ID is a void call but writes to disk before returning
1858 // to the caller...
1859 new AsyncTask<Void, Void, Void>() {
1860 public Void doInBackground(Void ... args) {
1861 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1862 return null;
1863 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001864 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001865 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001866 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 @Override
1870 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001871 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873
Joe Onorato88ec0992009-11-19 13:16:06 -08001874 @Override
1875 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001876 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1877 return;
1878 }
1879
Sunny Goyal45478022015-06-08 16:52:41 -07001880 if (mDragController.isDragging()) {
1881 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001882 return;
1883 }
1884
Jon Mirandafeba90f2016-10-06 10:53:29 -07001885 // Note: There should be at most one log per method call. This is enforced implicitly
1886 // by using if-else statements.
1887 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001888 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1889 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001890 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001891 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001892 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001893 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001894 } else {
1895 // Back button is a no-op here, but give at least some feedback for the button press
1896 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001897 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 * Launches the intent referred by the clicked shortcut.
1902 *
1903 * @param v The view representing the clicked shortcut.
1904 */
1905 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001906 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1907 // view has detached (it's possible for this to happen if the view is removed mid touch).
1908 if (v.getWindowToken() == null) {
1909 return;
1910 }
1911
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001912 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001913 return;
1914 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001915
Adam Cohen1697b792013-09-17 19:08:21 -07001916 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001917 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001918 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1919 LauncherLogProto.Action.Direction.NONE,
1920 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001921 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001922 }
1923 return;
1924 }
1925
1926 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001927 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001928 int page = mWorkspace.indexOfChild(v);
1929 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1930 LauncherLogProto.Action.Direction.NONE,
1931 LauncherLogProto.ContainerType.OVERVIEW, page);
1932 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001933 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001934 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001935 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001939 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001940 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001942 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001943 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001944 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001945 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001946 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001947 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001948 } else if (tag instanceof AppInfo) {
1949 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001950 } else if (tag instanceof LauncherAppWidgetInfo) {
1951 if (v instanceof PendingAppWidgetHostView) {
1952 onClickPendingWidget((PendingAppWidgetHostView) v);
1953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955 }
1956
Sunny Goyal70660032015-05-14 00:07:08 -07001957 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001958 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001959 return false;
1960 }
1961
Michael Jurkaaf442092010-06-10 17:01:57 -07001962 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001963 * Event handler for the app widget view which has not fully restored.
1964 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001965 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001966 if (mIsSafeModeEnabled) {
1967 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1968 return;
1969 }
1970
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001971 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001972 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001973 LauncherAppWidgetProviderInfo appWidgetInfo =
1974 mAppWidgetManager.findProvider(info.providerName, info.user);
1975 if (appWidgetInfo == null) {
1976 return;
1977 }
1978 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1979
Sunny Goyald478c832016-04-01 12:04:16 -07001980 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1981 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1982 // This should not happen, as we make sure that an Id is allocated during bind.
1983 return;
1984 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001985 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1986 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001987 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001988 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001989 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001990 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001991 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001992 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001993 }
1994 }
1995
1996 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001997 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1998 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1999 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002000 *
2001 * @param v The view that was clicked.
2002 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002003 protected void onClickAllAppsButton(View v) {
2004 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002005 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302006 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2007 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002008 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002009 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002010 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002011 }
2012 }
2013
Sunny Goyale6e72002017-01-12 16:55:36 -08002014 private void onClickPendingAppItem(final View v, final String packageName,
2015 boolean downloadStarted) {
2016 if (downloadStarted) {
2017 // If the download has started, simply direct to the market app.
2018 startMarketIntentForPackage(v, packageName);
2019 return;
2020 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002021 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002022 .setTitle(R.string.abandoned_promises_title)
2023 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002024 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2025 @Override
2026 public void onClick(DialogInterface dialogInterface, int i) {
2027 startMarketIntentForPackage(v, packageName);
2028 }
2029 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002030 .setNeutralButton(R.string.abandoned_clean_this,
2031 new DialogInterface.OnClickListener() {
2032 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002033 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002034 mWorkspace.removeAbandonedPromise(packageName, user);
2035 }
2036 })
2037 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002038 }
2039
2040 private void startMarketIntentForPackage(View v, String packageName) {
2041 ItemInfo item = (ItemInfo) v.getTag();
2042 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002043 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002044 }
2045
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002046 /**
2047 * Event handler for an app shortcut click.
2048 *
2049 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2050 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002051 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002052 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2053 Object tag = v.getTag();
2054 if (!(tag instanceof ShortcutInfo)) {
2055 throw new IllegalArgumentException("Input must be a Shortcut");
2056 }
2057
2058 // Open shortcut
2059 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002060
Sunny Goyal076839c2017-10-30 13:52:20 -07002061 if (shortcut.isDisabled()) {
2062 if ((shortcut.runtimeStatusFlags &
2063 ~FLAG_DISABLED_SUSPENDED &
2064 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002065 // If the app is only disabled because of the above flags, launch activity anyway.
2066 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002067 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002068 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2069 // Use a message specific to this shortcut, if it has one.
2070 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2071 return;
2072 }
2073 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002074 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07002075 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002076 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07002077 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2078 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002079 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002080 }
2081 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2082 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002083 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002084 }
2085
Chris Wren40c5ed32014-06-24 18:24:23 -04002086 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002087 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002088 String packageName = shortcut.intent.getComponent() != null ?
2089 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2090 if (!TextUtils.isEmpty(packageName)) {
2091 onClickPendingAppItem(v, packageName,
2092 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2093 return;
2094 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002095 }
2096
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002097 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002098 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002099 }
2100
Sunny Goyala7ce1662016-05-31 15:01:35 -07002101 private void startAppShortcutOrInfoActivity(View v) {
2102 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002103 Intent intent;
2104 if (item instanceof PromiseAppInfo) {
2105 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2106 intent = promiseAppInfo.getMarketIntent();
2107 } else {
2108 intent = item.getIntent();
2109 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002110 if (intent == null) {
2111 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002112 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002113 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002114 }
2115
2116 /**
2117 * Event handler for a folder icon click.
2118 *
2119 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2120 */
2121 protected void onClickFolderIcon(View v) {
2122 if (LOGD) Log.d(TAG, "onClickFolder");
2123 if (!(v instanceof FolderIcon)){
2124 throw new IllegalArgumentException("Input must be a FolderIcon");
2125 }
2126
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002127 Folder folder = ((FolderIcon) v).getFolder();
2128 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002129 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002130 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002131 }
Michael Jurka5130e402011-10-13 04:55:35 -07002132 }
2133
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002134 /**
2135 * Event handler for the (Add) Widgets button that appears after a long press
2136 * on the home screen.
2137 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002138 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002139 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002140 if (mIsSafeModeEnabled) {
2141 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2142 } else {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002143 WidgetsFullSheet.show(this, true /* animated */);
Adam Cohen9211d422014-10-07 18:14:53 -07002144 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002145 }
2146
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002147 /**
2148 * Event handler for the wallpaper picker button that appears after a long press
2149 * on the home screen.
2150 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002151 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002152 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002153 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2154 return;
2155 }
2156
Tony Wickham785f7a52015-08-31 17:28:32 -07002157 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2158 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002159 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002160 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002161 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002162
2163 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002164 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2165 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002166 intent.setPackage(pickerPackage);
2167 }
2168
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002169 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002170 try {
2171 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2172 // If there is no target package, use the default intent chooser animation
2173 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2174 } catch (ActivityNotFoundException e) {
2175 setWaitingForResult(null);
2176 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2177 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002178 }
2179
2180 /**
2181 * Event handler for a click on the settings button that appears after a long press
2182 * on the home screen.
2183 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002184 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002185 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002186 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2187 .setPackage(getPackageName());
2188 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002189 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002190 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002191 }
2192
Tony Wickhame2217252016-03-22 16:34:23 -07002193 @Override
2194 public void onAccessibilityStateChanged(boolean enabled) {
2195 mDragLayer.onAccessibilityStateChanged(enabled);
2196 }
2197
Sunny Goyala7ce1662016-05-31 15:01:35 -07002198 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002200 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2201 try {
2202 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2203 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2204 // is enabled by default on NYC.
2205 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2206 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002207
2208 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2209 String id = ((ShortcutInfo) info).getDeepShortcutId();
2210 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302211 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002212 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002213 } else {
2214 // Could be launching some bookkeeping activity
2215 startActivity(intent, optsBundle);
2216 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002217 } finally {
2218 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002221 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2222 // corresponding permission. Show the appropriate permission prompt if that
2223 // is the case.
2224 if (intent.getComponent() == null
2225 && Intent.ACTION_CALL.equals(intent.getAction())
2226 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2227 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002228
2229 setWaitingForResult(PendingRequestArgs
2230 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002231 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2232 REQUEST_PERMISSION_CALL_PHONE);
2233 } else {
2234 // No idea why this was thrown.
2235 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 }
2238 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002239
Sunny Goyalfe770c92016-09-27 14:38:58 -07002240 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002241 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002242 if (Utilities.ATLEAST_MARSHMALLOW) {
2243 int left = 0, top = 0;
2244 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002245 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002246 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002247 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002248 if (icon != null) {
2249 Rect bounds = icon.getBounds();
2250 left = (width - bounds.width()) / 2;
2251 top = v.getPaddingTop();
2252 width = bounds.width();
2253 height = bounds.height();
2254 }
2255 }
2256 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2257 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2258 // On L devices, we use the device default slide-up transition.
2259 // On L MR1 devices, we use a custom version of the slide-up transition which
2260 // doesn't have the delay present in the device default.
2261 return ActivityOptions.makeCustomAnimation(
2262 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2263 }
2264 return null;
2265 }
2266
Tonye3c59252017-05-02 21:32:27 -07002267 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002268 int[] pos = new int[2];
2269 v.getLocationOnScreen(pos);
2270 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2271 }
2272
Sunny Goyala7ce1662016-05-31 15:01:35 -07002273 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002274 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002275 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2276 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002277 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002278 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002279 // Only launch using the new animation if the shortcut has not opted out (this is a
2280 // private contract between launcher and may be ignored in the future).
2281 boolean useLaunchAnimation = (v != null) &&
2282 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2283 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2284
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002285 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002286
2287 // Prepare intent
2288 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2289 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002290 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002291 }
2292 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002293 if (Utilities.ATLEAST_MARSHMALLOW
2294 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002295 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002296 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002297 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002298 // Shortcuts need some special checks due to legacy reasons.
2299 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002300 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002301 // Could be launching some bookkeeping activity
2302 startActivity(intent, optsBundle);
2303 } else {
2304 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2305 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2306 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002307
2308 if (v instanceof BubbleTextView) {
2309 // This is set to the view that launched the activity that navigated the user away
2310 // from launcher. Since there is no callback for when the activity has finished
2311 // launching, enable the press state and keep this reference to reset the press
2312 // state when we return to launcher.
2313 BubbleTextView btv = (BubbleTextView) v;
2314 btv.setStayPressed(true);
2315 setOnResumeCallback(btv);
2316 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002317 mAppLaunchSuccess = true;
2318 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002319 } catch (ActivityNotFoundException|SecurityException e) {
2320 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2321 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2322 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002323 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002324 }
2325
Tony Wickhamdadb3042016-02-24 11:07:00 -08002326 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002327 public boolean dispatchTouchEvent(MotionEvent ev) {
2328 mLastDispatchTouchEventX = ev.getX();
2329 return super.dispatchTouchEvent(ev);
2330 }
2331
2332 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002334 if (!isDraggingEnabled()) return false;
2335 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002336 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337
Jon Miranda51f037d2016-11-07 08:37:20 -08002338 boolean ignoreLongPressToOverview =
2339 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002340
Adam Cohen1697b792013-09-17 19:08:21 -07002341 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002342 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002343 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302344 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2345 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002346 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002347 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002348 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2349 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2350 return true;
2351 } else {
2352 return false;
2353 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002354 } else {
2355 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002356 }
Adam Cohen1697b792013-09-17 19:08:21 -07002357 }
2358
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002359 CellLayout.CellInfo longClickCellInfo = null;
2360 View itemUnderLongClick = null;
2361 if (v.getTag() instanceof ItemInfo) {
2362 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002363 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002364 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002365 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
2367
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 // The hotseat touch handling does not go through Workspace, and we always allow long press
2369 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002370 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002371 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002372 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002373 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002374 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302375 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2376 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002377 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002378 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002379 return false;
2380 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302381 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2382 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002383 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002384 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002385 }
Jon Miranda379198e2016-09-23 08:54:40 -07002386 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2387 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002389 final boolean isAllAppsButton =
2390 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2391 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2392 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002393 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002395 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 }
2397 }
2398 }
2399 return true;
2400 }
2401
Winson Chung3d503fb2011-07-13 17:25:49 -07002402 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002403 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002404 return mHotseat != null && layout != null &&
2405 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2406 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002407
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 /**
2409 * Returns the CellLayout of the specified container at the specified screen.
2410 */
Sunny Goyal92820592015-03-02 11:31:35 -08002411 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002412 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2413 if (mHotseat != null) {
2414 return mHotseat.getLayout();
2415 } else {
2416 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002417 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002419 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002420 }
2421 }
2422
Michael Jurkae326f182011-11-21 14:05:46 -08002423 @Override
2424 public void onTrimMemory(int level) {
2425 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002426 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2427 // The widget preview db can result in holding onto over
2428 // 3MB of memory for caching which isn't necessary.
2429 SQLiteDatabase.releaseMemory();
2430
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002431 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002432 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002433 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002434 if (mLauncherCallbacks != null) {
2435 mLauncherCallbacks.onTrimMemory(level);
2436 }
Michael Jurkae326f182011-11-21 14:05:46 -08002437 }
2438
Michael Jurkad7c28052012-04-27 15:43:36 -07002439 @Override
2440 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002441 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002442 final List<CharSequence> text = event.getText();
2443 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002444 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002445 // TODO: When can workspace be null?
2446 text.add(mWorkspace == null
2447 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002448 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002449 return result;
2450 }
2451
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002452 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002453 * If the activity is currently paused, signal that we need to run the passed Runnable
2454 * in onResume.
2455 *
2456 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002457 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2458 * wrong when we're not running, and if the activity comes back to what the configuration was
2459 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002460 *
2461 * Implementation of the method from LauncherModel.Callbacks.
2462 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002463 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002464 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002465 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002466 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002467 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002468 if (run instanceof RunnableWithId) {
2469 // Remove any runnables which have the same id
2470 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002471 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002472 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002473 return true;
2474 } else {
2475 return false;
2476 }
2477 }
2478
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002479 public void setOnResumeCallback(OnResumeCallback callback) {
2480 if (mOnResumeCallback != null) {
2481 mOnResumeCallback.onLauncherResume();
2482 }
2483 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002484 }
2485
Michael Jurka7607c2f2013-04-03 14:33:19 -07002486 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002487 * If the activity is currently paused, signal that we need to re-run the loader
2488 * in onResume.
2489 *
2490 * This needs to be called from incoming places where resources might have been loaded
2491 * while we are paused. That is becaues the Configuration might be wrong
2492 * when we're not running, and if it comes back to what it was when we
2493 * were paused, we are not restarted.
2494 *
2495 * Implementation of the method from LauncherModel.Callbacks.
2496 *
2497 * @return true if we are currently paused. The caller might be able to
2498 * skip some work in that case since we will come back again.
2499 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002500 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002501 public boolean setLoadOnResume() {
2502 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002503 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002504 mOnResumeNeedsLoad = true;
2505 return true;
2506 } else {
2507 return false;
2508 }
2509 }
2510
2511 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002512 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002514 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002515 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002516 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002517 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002518 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002519 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002520 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002522
Joe Onorato9c1289c2009-08-17 11:03:03 -04002523 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002524 * Clear any pending bind callbacks. This is called when is loader is planning to
2525 * perform a full rebind from scratch.
2526 */
2527 @Override
2528 public void clearPendingBinds() {
2529 mBindOnResumeCallbacks.clear();
2530 if (mPendingExecutor != null) {
2531 mPendingExecutor.markCompleted();
2532 mPendingExecutor = null;
2533 }
2534 }
2535
2536 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002537 * Refreshes the shortcuts shown on the workspace.
2538 *
2539 * Implementation of the method from LauncherModel.Callbacks.
2540 */
2541 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002542 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002543 // Floating panels (except the full widget sheet) are associated with individual icons. If
2544 // we are starting a fresh bind, close all such panels as all the icons are about
2545 // to go away.
2546 AbstractFloatingView.closeOpenViews(this, true,
2547 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002548
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002549 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002550
Winson Chungd64d1762013-08-20 14:37:16 -07002551 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002552 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002553 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002554
Winson Chung3d503fb2011-07-13 17:25:49 -07002555 if (mHotseat != null) {
2556 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002558 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 }
2560
Adam Cohendcd297f2013-06-18 13:13:40 -07002561 @Override
2562 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002563 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002564 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2565 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002566 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2567 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002568 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002569 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2570 // If there are no screens, we need to have an empty screen
2571 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002572 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002573 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002574
Winsonc7d2e832016-07-28 12:24:55 -07002575 // After we have added all the screens, if the wallpaper was locked to the default state,
2576 // then notify to indicate that it can be released and a proper wallpaper offset can be
2577 // computed before the next layout
2578 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002579 }
2580
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002581 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002582 int count = orderedScreenIds.size();
2583 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002584 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002585 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002586 // No need to bind the first screen, as its always bound.
2587 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2588 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002589 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002590 }
2591
Sunny Goyalb23980c2017-08-17 07:45:25 -07002592 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002593 public void bindAppsAdded(final ArrayList<Long> newScreens,
2594 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002595 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002596 Runnable r = new Runnable() {
2597 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002598 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002599 }
2600 };
2601 if (waitUntilResume(r)) {
2602 return;
2603 }
2604
Winson Chungd64d1762013-08-20 14:37:16 -07002605 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002606 if (newScreens != null) {
2607 bindAddScreens(newScreens);
2608 }
Winson Chungd64d1762013-08-20 14:37:16 -07002609
2610 // We add the items without animation on non-visible pages, and with
2611 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002612 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002613 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002614 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002615 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002616 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002617 }
Winson Chungc58497e2013-09-03 17:48:37 -07002618
Winson Chung87412982013-10-03 18:34:14 -07002619 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002620 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002621 }
2622
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002623 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002624 * Bind the items start-end from the list.
2625 *
2626 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002628 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002629 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002630 Runnable r = new Runnable() {
2631 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002632 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002633 }
2634 };
2635 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002636 return;
2637 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002638
Sunny Goyal3be633b2016-12-08 09:59:25 -08002639 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002640 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002641 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002642 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002643 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002644 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002645 int end = items.size();
2646 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002647 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002648
2649 // Short circuit if we are loading dock items for a configuration which has no dock
2650 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2651 mHotseat == null) {
2652 continue;
2653 }
2654
Sunny Goyal639e9062015-08-19 19:17:06 -07002655 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002656 switch (item.itemType) {
2657 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2658 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002659 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002660 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002661 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002662 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002663 }
2664 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002665 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002666 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002667 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002668 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002669 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002670 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2671 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002672 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002673 if (view == null) {
2674 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002675 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002676 break;
2677 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002678 default:
2679 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002680 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002681
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002682 /*
2683 * Remove colliding items.
2684 */
2685 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2686 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2687 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2688 View v = cl.getChildAt(item.cellX, item.cellY);
2689 Object tag = v.getTag();
2690 String desc = "Collision while binding workspace item: " + item
2691 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002692 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002693 throw (new RuntimeException(desc));
2694 } else {
2695 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002696 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002697 continue;
2698 }
2699 }
2700 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302701 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002702 if (animateIcons) {
2703 // Animate all the applications up now
2704 view.setAlpha(0f);
2705 view.setScaleX(0f);
2706 view.setScaleY(0f);
2707 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002708 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002711
Winson Chung997a9232013-07-24 15:33:46 -07002712 if (animateIcons) {
2713 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002714 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002715 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002716 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002717 final Runnable startBounceAnimRunnable = new Runnable() {
2718 public void run() {
2719 anim.playTogether(bounceAnims);
2720 anim.start();
2721 }
2722 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002723 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002724 // We post the animation slightly delayed to prevent slowdowns
2725 // when we are loading right after we return to launcher.
2726 mWorkspace.postDelayed(new Runnable() {
2727 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002728 if (mWorkspace != null) {
2729 mWorkspace.snapToPage(newScreenIndex);
2730 mWorkspace.postDelayed(startBounceAnimRunnable,
2731 NEW_APPS_ANIMATION_DELAY);
2732 }
Winson Chung94d67682013-09-25 16:29:40 -07002733 }
2734 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002735 } else {
2736 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002737 }
2738 }
Winson Chung64359a52013-07-08 17:17:08 -07002739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002740 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 }
2742
Joe Onorato9c1289c2009-08-17 11:03:03 -04002743 /**
2744 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002745 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002746 public void bindAppWidget(LauncherAppWidgetInfo item) {
2747 View view = inflateAppWidget(item);
2748 if (view != null) {
2749 mWorkspace.addInScreen(view, item);
2750 mWorkspace.requestLayout();
2751 }
2752 }
2753
2754 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002755 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302756 PendingAppWidgetHostView view =
2757 new PendingAppWidgetHostView(this, item, mIconCache, true);
2758 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002759 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002760 }
2761
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002762 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002763
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002764 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002765
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002766 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2767 // If the provider is not ready, bind as a pending widget.
2768 appWidgetInfo = null;
2769 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2770 // The widget id is not valid. Try to find the widget based on the provider info.
2771 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2772 } else {
2773 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2774 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002775
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002776 // If the provider is ready, but the width is not yet restored, try to restore it.
2777 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2778 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002779 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002780 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2781 + " belongs to component " + item.providerName
2782 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002783 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002784 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002785 }
Sunny Goyalff572272014-07-23 13:58:07 -07002786
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002787 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002788 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002789 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2790 // Id has not been allocated yet. Allocate a new id.
2791 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2792 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002793
Sunny Goyald478c832016-04-01 12:04:16 -07002794 // Also try to bind the widget. If the bind fails, the user will be shown
2795 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002796 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002797 pendingInfo.spanX = item.spanX;
2798 pendingInfo.spanY = item.spanY;
2799 pendingInfo.minSpanX = item.minSpanX;
2800 pendingInfo.minSpanY = item.minSpanY;
2801 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002802
2803 boolean isDirectConfig =
2804 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2805 if (isDirectConfig && item.bindOptions != null) {
2806 Bundle newOptions = item.bindOptions.getExtras();
2807 if (options != null) {
2808 newOptions.putAll(options);
2809 }
2810 options = newOptions;
2811 }
Sunny Goyald478c832016-04-01 12:04:16 -07002812 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2813 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002814
Sunny Goyal86df1382016-08-10 15:03:22 -07002815 // We tried to bind once. If we were not able to bind, we would need to
2816 // go through the permission dialog, which means we cannot skip the config
2817 // activity.
2818 item.bindOptions = null;
2819 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2820
Sunny Goyald478c832016-04-01 12:04:16 -07002821 // Bind succeeded
2822 if (success) {
2823 // If the widget has a configure activity, it is still needs to set it up,
2824 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002825 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002826 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2827 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002828 }
Sunny Goyald478c832016-04-01 12:04:16 -07002829
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002830 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002831 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002832 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002833 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002834 // The widget was marked as UI not ready, but there is no configure activity to
2835 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002836 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002837 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002838 }
Sunny Goyalff572272014-07-23 13:58:07 -07002839 }
2840
Sunny Goyald5462aa2016-11-22 16:52:39 +05302841 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002842 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002843 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002844 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002845 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002846 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002847 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002848 }
2849
Sunny Goyal233ee962015-08-03 13:05:01 -07002850 item.minSpanX = appWidgetInfo.minSpanX;
2851 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302852 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002853 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302854 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002855 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302856 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002858 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002859 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002860 }
2861
Sunny Goyalff572272014-07-23 13:58:07 -07002862 /**
2863 * Restores a pending widget.
2864 *
2865 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002866 */
Sunny Goyald478c832016-04-01 12:04:16 -07002867 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002868 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2869 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2870 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002871 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002872 }
2873
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002874 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002875 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002876 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2877 info.pendingItemInfo = null;
2878 }
Sunny Goyalff572272014-07-23 13:58:07 -07002879
Sunny Goyalba47faa2017-10-04 16:50:57 -07002880 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2881 view.reinflate();
2882 }
2883
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002884 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002885 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002886 }
2887
Adam Cohen1462de32012-07-24 22:34:36 -07002888 public void onPageBoundSynchronously(int page) {
2889 mSynchronouslyBoundPages.add(page);
2890 }
2891
Sunny Goyal527c7d32015-08-28 15:19:36 -07002892 @Override
2893 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2894 if (mPendingExecutor != null) {
2895 mPendingExecutor.markCompleted();
2896 }
2897 mPendingExecutor = executor;
2898 executor.attachTo(this);
2899 }
2900
2901 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2902 if (mPendingExecutor == executor) {
2903 mPendingExecutor = null;
2904 }
2905 }
2906
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002907 @Override
2908 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2909 Runnable r = new Runnable() {
2910 public void run() {
2911 finishFirstPageBind(executor);
2912 }
2913 };
2914 if (waitUntilResume(r)) {
2915 return;
2916 }
2917
2918 Runnable onComplete = new Runnable() {
2919 @Override
2920 public void run() {
2921 if (executor != null) {
2922 executor.onLoadAnimationCompleted();
2923 }
2924 }
2925 };
2926 if (mDragLayer.getAlpha() < 1) {
2927 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2928 } else {
2929 onComplete.run();
2930 }
2931 }
2932
Joe Onorato9c1289c2009-08-17 11:03:03 -04002933 /**
2934 * Callback saying that there aren't any more items to bind.
2935 *
2936 * Implementation of the method from LauncherModel.Callbacks.
2937 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002938 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002939 Runnable r = new Runnable() {
2940 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002941 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002942 }
2943 };
2944 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002945 return;
2946 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002947 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002948 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002950 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002951
Sunny Goyal2100c782016-08-22 16:00:03 -07002952 if (mPendingActivityResult != null) {
2953 handleActivityResult(mPendingActivityResult.requestCode,
2954 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2955 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002956 }
2957
Sunny Goyala474a9b2017-05-04 16:47:11 -07002958 InstallShortcutReceiver.disableAndFlushInstallQueue(
2959 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002960
Tony Wickham010d2552017-01-20 08:15:28 -08002961 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002962 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002963 }
2964
Winson Chunga2413752012-04-03 14:22:34 -07002965 private boolean canRunNewAppsAnimation() {
2966 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002967 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002968 }
2969
Winson Chungc9168342013-06-26 14:54:55 -07002970 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002971 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002972 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2973 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002974 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002975 return bounceAnim;
2976 }
2977
Adam Cohen27772732013-11-11 14:00:56 +00002978 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002979 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002980 }
2981
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002982 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 * Add the icons for all apps.
2984 *
2985 * Implementation of the method from LauncherModel.Callbacks.
2986 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002987 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002988 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2989 public void run() {
2990 bindAllApplications(apps);
2991 }
2992 };
2993 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002994 return;
2995 }
2996
Winson Chungb745afb2015-03-02 11:51:23 -08002997 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002998 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002999 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07003000 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003001 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003002 return;
3003 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003004
Winson Chungb745afb2015-03-02 11:51:23 -08003005 mAppsView.setApps(apps);
3006 }
Adam Cohen9211d422014-10-07 18:14:53 -07003007 if (mLauncherCallbacks != null) {
3008 mLauncherCallbacks.bindAllApplications(apps);
3009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003010 }
3011
3012 /**
Tony2917a8b2017-07-31 23:29:42 -07003013 * Returns an Executor that will run after the launcher is first drawn (including after the
3014 * initial fade in animation). Returns null if the first draw has already occurred.
3015 */
3016 public @Nullable Executor getPendingExecutor() {
3017 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3018 }
3019
3020 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003021 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3022 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3023 */
3024 @Override
3025 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003026 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003027 }
3028
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003029 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 * A package was updated.
3031 *
3032 * Implementation of the method from LauncherModel.Callbacks.
3033 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003034 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003035 Runnable r = new Runnable() {
3036 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003037 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003038 }
3039 };
3040 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003041 return;
3042 }
3043
Winson Chungb745afb2015-03-02 11:51:23 -08003044 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003045 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 }
3048
Sunny Goyal4390ace2014-10-13 11:33:11 -07003049 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003050 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3051 Runnable r = new Runnable() {
3052 public void run() {
3053 bindPromiseAppProgressUpdated(app);
3054 }
3055 };
3056 if (waitUntilResume(r)) {
3057 return;
3058 }
3059
3060 if (mAppsView != null) {
3061 mAppsView.updatePromiseAppProgress(app);
3062 }
3063 }
3064
3065 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003066 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3067 Runnable r = new Runnable() {
3068 public void run() {
3069 bindWidgetsRestored(widgets);
3070 }
3071 };
3072 if (waitUntilResume(r)) {
3073 return;
3074 }
3075 mWorkspace.widgetsRestored(widgets);
3076 }
3077
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003079 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003080 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003081 *
3082 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003083 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003084 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003085 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003086 Runnable r = new Runnable() {
3087 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003088 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003089 }
3090 };
3091 if (waitUntilResume(r)) {
3092 return;
3093 }
3094
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095 if (!updated.isEmpty()) {
3096 mWorkspace.updateShortcuts(updated);
3097 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003098 }
3099
3100 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003101 * Update the state of a package, typically related to install state.
3102 *
3103 * Implementation of the method from LauncherModel.Callbacks.
3104 */
Sunny Goyale755d462014-07-22 13:48:29 -07003105 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003106 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3107 Runnable r = new Runnable() {
3108 public void run() {
3109 bindRestoreItemsChange(updates);
3110 }
3111 };
3112 if (waitUntilResume(r)) {
3113 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003114 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003115
Sunny Goyal756adbc2015-04-16 15:20:51 -07003116 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003117 }
3118
3119 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003120 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003121 * in addition to specific applications to remove, the reason being that
3122 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003123 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003124 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003125 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003126 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003127 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003128 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003129 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003130 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003131 }
Winson Chungd64d1762013-08-20 14:37:16 -07003132 };
3133 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003134 return;
3135 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003136 mWorkspace.removeItemsByMatcher(matcher);
3137 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003138 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003140 @Override
3141 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3142 Runnable r = new Runnable() {
3143 public void run() {
3144 bindAppInfosRemoved(appInfos);
3145 }
3146 };
3147 if (waitUntilResume(r)) {
3148 return;
Joe Onorato36115782010-06-17 13:28:48 -04003149 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003150
Winson Chungdf95eb12013-10-16 14:57:07 -07003151 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003152 if (mAppsView != null) {
3153 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 }
Joe Onoratobe386092009-11-17 17:32:16 -08003156
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003157 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003158 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3159 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003160 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3161 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003162 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003163 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003164 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003165 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003166 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003167 return;
3168 }
3169
Tony Wickham26b17462017-03-20 17:12:24 -07003170 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3171 if (topView != null) {
3172 topView.onWidgetsBound();
3173 }
3174 }
3175
Tony Wickham86222d22017-03-29 15:30:43 -07003176 /**
3177 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3178 * refreshes the widgets and shortcuts associated with the given package/user
3179 */
3180 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003181 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003182 }
3183
Sunny Goyalc99cb172017-10-19 16:15:09 -07003184 public boolean isRotationEnabled () {
3185 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003186 }
3187
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003188 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003189 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003190 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003191 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003192 }
3193
Winson Chung80baf5a2010-08-09 16:03:15 -07003194 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003195 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003196 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003197 @Override
3198 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3199 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003200
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003201 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3202 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003203 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003204 writer.println(prefix + " Homescreen " + i);
3205
3206 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3207 for (int j = 0; j < layout.getChildCount(); j++) {
3208 Object tag = layout.getChildAt(j).getTag();
3209 if (tag != null) {
3210 writer.println(prefix + " " + tag.toString());
3211 }
3212 }
3213 }
3214
3215 writer.println(prefix + " Hotseat");
3216 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003217 for (int j = 0; j < layout.getChildCount(); j++) {
3218 Object tag = layout.getChildAt(j).getTag();
3219 if (tag != null) {
3220 writer.println(prefix + " " + tag.toString());
3221 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003222 }
Sunny Goyala1365452015-10-01 15:46:24 -07003223
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003224 try {
3225 FileLog.flushAll(writer);
3226 } catch (Exception e) {
3227 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003228 }
3229 }
3230
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003231 writer.println(prefix + "Misc:");
3232 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3233 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3234 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3235
3236 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003237
Adam Cohen9211d422014-10-07 18:14:53 -07003238 if (mLauncherCallbacks != null) {
3239 mLauncherCallbacks.dump(prefix, fd, writer, args);
3240 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003241 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003242
Sunny Goyal66b24572016-09-21 15:57:55 -07003243 @Override
3244 @TargetApi(Build.VERSION_CODES.N)
3245 public void onProvideKeyboardShortcuts(
3246 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3247
3248 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003249 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003250 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3251 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3252 }
3253 View currentFocus = getCurrentFocus();
3254 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3255 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3256 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3257 }
Tony18c4aa42017-05-10 21:23:57 -05003258 if (currentFocus.getTag() instanceof ItemInfo
3259 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003260 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3261 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3262 }
3263 if (!shortcutInfos.isEmpty()) {
3264 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3265 }
3266
3267 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3268 }
3269
3270 @Override
3271 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3272 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3273 switch (keyCode) {
3274 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003275 if (isInState(NORMAL)) {
3276 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003277 return true;
3278 }
3279 break;
3280 case KeyEvent.KEYCODE_S: {
3281 View focusedView = getCurrentFocus();
3282 if (focusedView instanceof BubbleTextView
3283 && focusedView.getTag() instanceof ItemInfo
3284 && mAccessibilityDelegate.performAction(focusedView,
3285 (ItemInfo) focusedView.getTag(),
3286 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003287 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003288 return true;
3289 }
3290 break;
3291 }
3292 case KeyEvent.KEYCODE_O:
3293 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3294 return true;
3295 }
3296 break;
3297 }
3298 }
3299 return super.onKeyShortcut(keyCode, event);
3300 }
3301
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003302 public static Launcher getLauncher(Context context) {
3303 if (context instanceof Launcher) {
3304 return (Launcher) context;
3305 }
3306 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3307 }
3308
Sunny Goyal5a81c382017-03-20 15:08:06 -07003309 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003310
3311 @Override
3312 public void onSharedPreferenceChanged(
3313 SharedPreferences sharedPreferences, String key) {
3314 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003315 // Recreate the activity so that it initializes the rotation preference again.
3316 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003317 }
3318 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003319 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003320
3321 /**
3322 * Callback for listening for onResume
3323 */
3324 public interface OnResumeCallback {
3325
3326 void onLauncherResume();
3327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003328}