blob: b1b3452e4b2e554736bd27d521db558f4587bce4 [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;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080066import android.os.Binder;
Adam Cohen0f668f32014-09-08 19:54:17 +020067import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070069import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080070import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070071import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070072import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070073import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070076import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070078import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070079import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.HapticFeedbackConstants;
82import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070083import android.view.KeyboardShortcutGroup;
84import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080085import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080089import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070092import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070097import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070098import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700100import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700101import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700104import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700105import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800106import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700109import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700110import com.android.launcher3.dragndrop.DragView;
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;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800127import com.android.launcher3.states.InternalStateHandler;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800128import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530129import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
130import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
131import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700132import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700133import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700134import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700135import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700136import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700137import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800138import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700139import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700140import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700141import com.android.launcher3.util.SystemUiController;
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 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700171 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700172 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chunga2413752012-04-03 14:22:34 -0700174 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700178
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700180 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Michael Jurka8b805b12012-04-18 14:23:14 -0700182 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700183 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
184 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700185
Jon Mirandac476d6e2017-05-04 16:30:01 -0700186 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700187
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700188 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
189
Mathew Inwood876a8462013-06-14 14:12:41 +0100190 /**
191 * IntentStarter uses request codes starting with this. This must be greater than all activity
192 * request codes used internally.
193 */
194 protected static final int REQUEST_LAST = 100;
195
Winson Chung2672ff92012-05-04 16:22:30 -0700196 // The Intent extra that defines whether to ignore the launch animation
197 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400198 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 // Type: int
201 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700202 // Type: int
203 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700204 // Type: PendingRequestArgs
205 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
206 // Type: ActivityResultInfo
207 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700208 // Type: SparseArray<Parcelable>
209 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700211 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700218 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
220 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700223 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800225 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700226
Sunny Goyalffe83f12014-08-14 17:39:34 -0700227 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700229
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700230 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231
Sunny Goyal316490e2015-06-02 09:38:28 -0700232 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700233
Michael Jurka838a4ca2011-02-07 13:33:06 -0800234 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700235
Sunny Goyal47328fd2016-05-25 18:56:41 -0700236 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700237
238 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700239 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700240 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700241
Winson Chung8ae41982017-11-10 11:42:13 -0800242 // UI and state for the overview panel
243 private ViewGroup mOverviewPanel;
244
Jon Miranda6bed3502017-09-19 14:43:17 -0700245 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
246 // that results in widgets being inflated in the wrong orientation.
247 private int mOrientation;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private SpannableStringBuilder mDefaultKeySsb = null;
250
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800253 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700254 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700256 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700257 private OnResumeCallback mOnResumeCallback;
258
Sunny Goyal527c7d32015-08-28 15:19:36 -0700259 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700260
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800262 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800263 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700264 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700265 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266
Jon Miranda2d89ea82017-05-04 11:47:53 -0700267 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700268 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700269
Tony Wickham010d2552017-01-20 08:15:28 -0800270 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700271
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700272 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700273
Winson Chung46353de2012-02-16 14:05:10 -0800274 // We only want to get the SharedPreferences once since it does an FS stat each time we get
275 // it from the context.
276 private SharedPreferences mSharedPrefs;
277
Sunny Goyal2100c782016-08-22 16:00:03 -0700278 // Activity result which needs to be processed after workspace has loaded.
279 private ActivityResultInfo mPendingActivityResult;
280 /**
281 * Holds extra information required to handle a result from an external call, like
282 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
283 */
284 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800285
Jon Miranda379198e2016-09-23 08:54:40 -0700286 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700287
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700288 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700289 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700290 private boolean mAppLaunchSuccess;
291
Sunny Goyal745bad92016-05-02 10:54:12 -0700292 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 @Override
295 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700296 if (DEBUG_STRICT_MODE) {
297 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
298 .detectDiskReads()
299 .detectDiskWrites()
300 .detectNetwork() // or .detectAll() for all detectable problems
301 .penaltyLog()
302 .build());
303 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
304 .detectLeakedSqlLiteObjects()
305 .detectLeakedClosableObjects()
306 .penaltyLog()
307 .penaltyDeath()
308 .build());
309 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700310 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700311
Adam Cohen9211d422014-10-07 18:14:53 -0700312 if (mLauncherCallbacks != null) {
313 mLauncherCallbacks.preOnCreate();
314 }
315
Mario Bertschler27288382017-05-24 15:35:09 -0700316 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
317 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700318 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700321 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400322
Sunny Goyal87f784c2017-01-11 10:48:34 -0800323 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700324
Adam Cohen2e6da152015-05-06 11:42:25 -0700325 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800326 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700327 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700328 Display display = getWindowManager().getDefaultDisplay();
329 Point mwSize = new Point();
330 display.getSize(mwSize);
331 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
332 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700333
Jon Miranda6bed3502017-09-19 14:43:17 -0700334 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700335 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700336 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800337 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800338 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800339 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700340 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700341
Joe Onorato41a12d22009-10-31 18:30:00 -0400342 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700343 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800344 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700345
Sunny Goyalffe83f12014-08-14 17:39:34 -0700346 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700347
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700348 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700349 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700350
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700351 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
352 // this also ensures that any synchronous binding below doesn't re-trigger another
353 // LauncherModel load.
354 mPaused = false;
355
Sunny Goyal60820d72017-05-09 12:40:11 -0700356 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 setupViews();
Winson1f064272016-07-18 17:18:02 -0700359 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360
Tony Wickham010d2552017-01-20 08:15:28 -0800361 mPopupDataProvider = new PopupDataProvider(this);
362
Sunny Goyalfe770c92016-09-27 14:38:58 -0700363 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364
Sunny Goyal2100c782016-08-22 16:00:03 -0700365 // We only load the page synchronously if the user rotates (or triggers a
366 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700367 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
368 if (savedInstanceState != null) {
369 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
370 }
371 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700372 // If we are not binding synchronously, show a fade in animation when
373 // the first page bind completes.
374 mDragLayer.setAlpha(0);
375 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700376 // Pages bound synchronously.
377 mWorkspace.setCurrentPage(currentScreen);
378
Sunny Goyal2100c782016-08-22 16:00:03 -0700379 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 }
381
382 // For handling default keys
383 mDefaultKeySsb = new SpannableStringBuilder();
384 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800385
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800386 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700387 // In case we are on a device with locked rotation, we should look at preferences to check
388 // if the user has specifically allowed rotation.
389 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700390 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700391 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
392 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700393 }
394
Sunny Goyal5a81c382017-03-20 15:08:06 -0700395 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
396 // Temporarily enable the rotation
397 mRotationEnabled = true;
398 }
399
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700400 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
401 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700402 setRequestedOrientation(mRotationEnabled
403 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700404
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800405 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700406
Jon Miranda7fda2852017-07-19 16:07:01 -0700407 // Listen for broadcasts
408 IntentFilter filter = new IntentFilter();
409 filter.addAction(Intent.ACTION_SCREEN_OFF);
410 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
411 registerReceiver(mReceiver, filter);
412 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700413
Sunny Goyal8392c822017-06-20 10:03:56 -0700414 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
415 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700416
417 if (mLauncherCallbacks != null) {
418 mLauncherCallbacks.onCreate(savedInstanceState);
419 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700420
421 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700422 }
423
Sunny Goyal7779d622015-06-11 16:18:39 -0700424 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700425 public void onThemeChanged() {
426 recreate();
427 }
428
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700429 public LauncherStateManager getStateManager() {
430 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700431 }
432
Mario Bertschlera6936942017-05-31 14:48:19 -0700433 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700434 if (isDark) {
435 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700436 } else if (supportsDarkText) {
437 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700438 }
439 }
440
441 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700442 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800443 return mLauncherView.findViewById(id);
444 }
445
446 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700447 public void onAppWidgetHostReset() {
448 if (mAppWidgetHost != null) {
449 mAppWidgetHost.startListening();
450 }
451 }
452
Adam Cohen9211d422014-10-07 18:14:53 -0700453 private LauncherCallbacks mLauncherCallbacks;
454
Winson1f064272016-07-18 17:18:02 -0700455 public void onInsetsChanged(Rect insets) {
456 mDeviceProfile.updateInsets(insets);
457 mDeviceProfile.layout(this, true /* notifyListeners */);
458 }
459
Sunny Goyal32554d12015-12-03 15:31:25 -0800460 /**
461 * Call this after onCreate to set or clear overlay.
462 */
463 public void setLauncherOverlay(LauncherOverlay overlay) {
464 if (overlay != null) {
465 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
466 }
467 mWorkspace.setLauncherOverlay(overlay);
468 }
469
Adam Cohen9211d422014-10-07 18:14:53 -0700470 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
471 mLauncherCallbacks = callbacks;
472 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700473 }
474
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700475 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700476 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700477 if (mLauncherCallbacks != null) {
478 mLauncherCallbacks.onLauncherProviderChange();
479 }
480 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700481
Hyunyoung Song3f471442015-04-08 19:01:34 -0700482 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700483 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
484 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700485 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700486 }
487
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000488 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700489 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
490 // This cast is safe as long as the id < 0x00FFFFFF
491 // Since we jail all the dynamically generated views, there should be no clashes
492 // with any other views.
493 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000494 }
495
Tony Wickham010d2552017-01-20 08:15:28 -0800496 public PopupDataProvider getPopupDataProvider() {
497 return mPopupDataProvider;
498 }
499
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000500 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700501 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
502 * a configuration step, this allows the proper animations to run after other transitions.
503 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700504 private long completeAdd(
505 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
506 long screenId = info.screenId;
507 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700508 // When the screen id represents an actual screen (as opposed to a rank) we make sure
509 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700511 }
Adam Cohendb364c32014-05-20 14:23:40 -0700512
Sunny Goyal2100c782016-08-22 16:00:03 -0700513 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800514 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700515 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700516 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700518 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700519 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700520 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700521 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700522 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700523 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 int widgetId = appWidgetId;
525 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700526 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700527 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700528 // Since the view was just bound, also launch the configure activity if needed
529 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
530 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800531 if (provider != null) {
532 new WidgetAddFlowHandler(provider)
533 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700534 }
535 }
536 break;
537 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800538 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700539
Adam Cohendb364c32014-05-20 14:23:40 -0700540 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800541 }
542
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800543 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700544 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700545 if (isWorkspaceLoading()) {
546 // process the result once the workspace has loaded.
547 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
548 return;
549 }
550 mPendingActivityResult = null;
551
Winson Chunge341d302013-08-16 14:31:00 -0700552 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700553 final PendingRequestArgs requestArgs = mPendingRequestArgs;
554 setWaitingForResult(null);
555 if (requestArgs == null) {
556 return;
557 }
558
559 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700560
Adam Cohenad4e15c2013-10-17 16:21:35 -0700561 Runnable exitSpringLoaded = new Runnable() {
562 @Override
563 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700564 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700565 }
566 };
567
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700569 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700570 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700571 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
572 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700574 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700575 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700576 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700577 addAppWidgetImpl(
578 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800579 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700580 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700581 }
582 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200583 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700584 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700585 // User could have free-scrolled between pages before picking a wallpaper; make sure
586 // we move to the closest one now.
587 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700588 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200589 }
590 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700591 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200592
Adam Cohened66b2b2012-01-23 17:28:51 -0800593 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700594 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700595
Adam Cohened66b2b2012-01-23 17:28:51 -0800596 // We have special handling for widgets
597 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800598 final int appWidgetId;
599 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
600 : -1;
601 if (widgetId < 0) {
602 appWidgetId = pendingAddWidgetId;
603 } else {
604 appWidgetId = widgetId;
605 }
606
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800608 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700609 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
610 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700611 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700612 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700613 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700614 @Override
615 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700616 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700617 }
618 };
Adam Cohendb364c32014-05-20 14:23:40 -0700619
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
621 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
622 } else {
623 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
624 // When the screen id represents an actual screen (as opposed to a rank)
625 // we make sure that the drop page actually exists.
626 requestArgs.screenId =
627 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700628 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700629 final CellLayout dropLayout =
630 mWorkspace.getScreenWithId(requestArgs.screenId);
631
632 dropLayout.setDropPending(true);
633 final Runnable onComplete = new Runnable() {
634 @Override
635 public void run() {
636 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
637 dropLayout.setDropPending(false);
638 }
639 };
640 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
641 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700642 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800643 return;
644 }
645
Sunny Goyald478c832016-04-01 12:04:16 -0700646 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
647 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700648 if (resultCode == RESULT_OK) {
649 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700651 }
652 // Leave the widget in the pending state if the user canceled the configure.
653 return;
654 }
655
Sunny Goyalaad90582015-07-09 14:22:50 -0700656 if (requestCode == REQUEST_CREATE_SHORTCUT) {
657 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700658 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
659 completeAdd(requestCode, data, -1, requestArgs);
660 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
661 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
662
Sunny Goyalaad90582015-07-09 14:22:50 -0700663 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700664 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
665 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800669 }
670
671 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700672 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800673 final int requestCode, final int resultCode, final Intent data) {
674 handleActivityResult(requestCode, resultCode, data);
675 if (mLauncherCallbacks != null) {
676 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
677 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 }
679
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700680 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700681 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600682 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 PendingRequestArgs pendingArgs = mPendingRequestArgs;
684 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
685 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
686 setWaitingForResult(null);
687
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700690 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700691 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700692 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700693 Intent intent = pendingArgs.getPendingIntent();
694
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 if (grantResults.length > 0
696 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700697 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700698 } else {
699 // TODO: Show a snack bar with link to settings
700 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700701 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700702 }
703 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600704 if (mLauncherCallbacks != null) {
705 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
706 grantResults);
707 }
708 }
709
Adam Cohendb364c32014-05-20 14:23:40 -0700710 /**
711 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
712 *
713 * @param screenId the screen id to check
714 * @return the new screen, or screenId if it exists
715 */
716 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800717 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700718 if (dropLayout == null) {
719 // it's possible that the add screen was removed because it was
720 // empty and a re-bind occurred
721 mWorkspace.addExtraEmptyScreen();
722 return mWorkspace.commitExtraEmptyScreen();
723 } else {
724 return screenId;
725 }
726 }
727
Sunny Goyal2100c782016-08-22 16:00:03 -0700728 @Thunk void completeTwoStageWidgetDrop(
729 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
730 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 Runnable onCompleteRunnable = null;
732 int animationType = 0;
733
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700734 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 if (resultCode == RESULT_OK) {
736 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
737 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800738 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700739 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 onCompleteRunnable = new Runnable() {
741 @Override
742 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700743 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700744 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 }
746 };
747 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800748 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700751 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700753 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
754 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700756 // The animated view may be null in the case of a rotation during widget configuration
757 onCompleteRunnable.run();
758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
760
761 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700762 protected void onStop() {
763 super.onStop();
764 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700765
766 if (mLauncherCallbacks != null) {
767 mLauncherCallbacks.onStop();
768 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700769
Sunny Goyalf5e37442016-11-02 10:31:24 -0700770 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700771 mAppWidgetHost.stopListening();
772 }
Tony Wickham010d2552017-01-20 08:15:28 -0800773
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700774 if (!mAppLaunchSuccess) {
775 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700776 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700777 }
Tony Wickham010d2552017-01-20 08:15:28 -0800778 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700779 }
780
781 @Override
782 protected void onStart() {
783 super.onStart();
784 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700785
786 if (mLauncherCallbacks != null) {
787 mLauncherCallbacks.onStart();
788 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700789
Sunny Goyalf5e37442016-11-02 10:31:24 -0700790 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700791 mAppWidgetHost.startListening();
792 }
Tony Wickham010d2552017-01-20 08:15:28 -0800793
794 if (!isWorkspaceLoading()) {
795 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
796 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700797
Jon Miranda7fda2852017-07-19 16:07:01 -0700798 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700799 if (mScrimAnimator != null) {
800 mScrimAnimator.cancel();
801 }
802 mDragLayer.getBackground().setAlpha(0);
803 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
804 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
805 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
806 @Override
807 public void onAnimationEnd(Animator animation) {
808 mScrimAnimator = null;
809 }
810 });
811 mScrimAnimator.setDuration(600);
812 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
813 mScrimAnimator.start();
814 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700815 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700816 }
817
818 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700820 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700822 TraceHelper.partitionSection("ON_RESUME", "superCall");
823
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700824 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700825 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800826 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700827 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700828 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700829 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700830 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700832 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200833 // We might have postponed some bind calls until onResume (see waitUntilResume) --
834 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700835 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
836 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200837 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700838 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200839 }
Winson Chunge4e50662012-01-23 14:45:13 -0800840
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700841 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700842 // Process any items that were added while Launcher was away.
843 InstallShortcutReceiver.disableAndFlushInstallQueue(
844 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700845
Sunny Goyala474a9b2017-05-04 16:47:11 -0700846 // Refresh shortcuts if the permission changed.
847 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700848
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700849 if (shouldShowDiscoveryBounce()) {
850 mAllAppsController.showDiscoveryBounce();
851 }
Adam Cohen9211d422014-10-07 18:14:53 -0700852 if (mLauncherCallbacks != null) {
853 mLauncherCallbacks.onResume();
854 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800855
Sunny Goyala502aa32017-10-02 16:04:06 -0700856 clearTypedText();
857
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700858 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700859 }
860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700862 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700863 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700864 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700865
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700866 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700867 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800868 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700869 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700870
Adam Cohen9211d422014-10-07 18:14:53 -0700871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700873 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700874 }
875
Adam Cohenc2d6e892014-10-16 09:49:24 -0700876 public interface LauncherOverlay {
877
878 /**
879 * Touch interaction leading to overscroll has begun
880 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700881 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700882
883 /**
884 * Touch interaction related to overscroll has ended
885 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700886 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700887
888 /**
889 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
890 * screen (or in the case of RTL, the rightmost screen).
891 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700892 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700893
894 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800895 * Called when the launcher is ready to use the overlay
896 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700897 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700898 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700899 }
900
901 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700902 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700903 }
904
905 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
906
Sunny Goyalc86df472016-02-25 09:19:38 -0800907 public void onScrollChanged(float progress) {
908 if (mWorkspace != null) {
909 mWorkspace.onOverlayScrollChanged(progress);
910 }
911 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700912 }
913
Sunny Goyal16764582017-11-06 16:00:34 -0800914 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700915 if (mLauncherCallbacks != null) {
916 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700917 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800918 // On O and above we there is always some setting present settings (add icon to
919 // home screen or icon badging). On earlier APIs we will have the allow rotation
920 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700921 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700922 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800923 }
924
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700925 @Override
926 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700928 if (mModel.isCurrentCallbacks(this)) {
929 mModel.stopLoader();
930 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700931 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
932
Romain Guy13c2e7b2010-03-10 19:45:00 -0800933 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700934 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700935
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800936 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800937 @Override
938 public void onWindowFocusChanged(boolean hasFocus) {
939 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700940 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700941
942 if (mLauncherCallbacks != null) {
943 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
944 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800945 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 private boolean acceptFilter() {
948 final InputMethodManager inputManager = (InputMethodManager)
949 getSystemService(Context.INPUT_METHOD_SERVICE);
950 return !inputManager.isFullscreenMode();
951 }
952
953 @Override
954 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700955 final int uniChar = event.getUnicodeChar();
956 final boolean handled = super.onKeyDown(keyCode, event);
957 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
958 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
960 keyCode, event);
961 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700963 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700964 // showSearchDialog()
965 // If there are multiple keystrokes before the search dialog takes focus,
966 // onSearchRequested() will be called for every keystroke,
967 // but it is idempotent, so it's fine.
968 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
970 }
971
Joe Onorato8a9625e2010-01-28 15:55:35 -0800972 // Eat the long press event so the keyboard doesn't come up.
973 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
974 return true;
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 return handled;
978 }
979
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 private String getTypedText() {
981 return mDefaultKeySsb.toString();
982 }
983
Sunny Goyal6f28e712016-09-13 14:19:29 -0700984 @Override
985 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700986 mDefaultKeySsb.clear();
987 mDefaultKeySsb.clearSpans();
988 Selection.setSelection(mDefaultKeySsb, 0);
989 }
990
Sunny Goyalf9403d92017-10-18 10:55:56 -0700991 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700992 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700993 }
994
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 /**
996 * Restores the previous state, if it exists.
997 *
998 * @param savedState The previous state.
999 */
1000 private void restoreState(Bundle savedState) {
1001 if (savedState == null) {
1002 return;
1003 }
1004
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001005 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001006 LauncherState[] stateValues = LauncherState.values();
1007 LauncherState state = stateValues[stateOrdinal];
1008 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001009 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001010 }
1011
Sunny Goyal2100c782016-08-22 16:00:03 -07001012 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1013 if (requestArgs != null) {
1014 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001016
1017 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001018
1019 SparseArray<Parcelable> widgetsState =
1020 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1021 if (widgetsState != null) {
1022 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025
1026 /**
1027 * Finds all the views we need and configure them properly.
1028 */
1029 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001030 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001031 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001032 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001033 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001034 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001035
Sunny Goyal784f9c32016-03-23 16:56:54 -07001036 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1037 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1038 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039
Winson Chung4c98d922011-05-31 16:50:48 -07001040 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001041 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001042
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 // Setup the hotseat
1044 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1045 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001046 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001047 }
1048
Winson Chung4c98d922011-05-31 16:50:48 -07001049 // Setup the workspace
1050 mWorkspace.setHapticFeedbackEnabled(false);
1051 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001052 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001053 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1054 // default state, otherwise we will update to the wrong offsets in RTL
1055 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001056 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001057 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001058
Tony Wickham34d2c912015-09-11 09:27:58 -07001059 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001060 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001061
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001062 // Setup Apps
1063 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001064
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001066 mDragController.setMoveTarget(mWorkspace);
1067 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001068 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001069
Hyunyoung Songd725f642017-08-17 22:26:35 -07001070 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
1073 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001074 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001075 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001076 */
1077 public void setAllAppsButton(View allAppsButton) {
1078 mAllAppsButton = allAppsButton;
1079 }
1080
Anjali Koppal5ad44842014-03-10 20:34:39 -07001081 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 * Creates a view representing a shortcut.
1083 *
1084 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001086 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001087 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089
1090 /**
1091 * Creates a view representing a shortcut inflated from the specified resource.
1092 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 * @param parent The group the shortcut belongs to.
1094 * @param info The data structure describing the shortcut.
1095 *
1096 * @return A View inflated from layoutResId.
1097 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001098 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001099 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1100 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001101 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001103 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 return favorite;
1105 }
1106
1107 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001108 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 *
1110 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001112 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001113 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001114 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1115 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001116 return;
1117 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001118
Jon Mirandac476d6e2017-05-04 16:30:01 -07001119 int[] cellXY = mTmpAddItemCellCoordinates;
1120 CellLayout layout = getCellLayout(container, screenId);
1121
Sunny Goyal782f0c92017-01-19 10:27:54 -08001122 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001123 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001124 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1125 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001126 }
1127
1128 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001129 // Legacy shortcuts are only supported for primary profile.
1130 info = Process.myUserHandle().equals(args.user)
1131 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001132
Sunny Goyalb57645f2017-03-20 13:57:28 -07001133 if (info == null) {
1134 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1135 return;
1136 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001137 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001138 // The app is trying to add a shortcut without sufficient permissions
1139 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1140 return;
1141 }
1142 }
1143
Jon Mirandac476d6e2017-05-04 16:30:01 -07001144 if (container < 0) {
1145 // Adding a shortcut to the Workspace.
1146 final View view = createShortcut(info);
1147 boolean foundCellSpan = false;
1148 // First we check if we already know the exact location where we want to add this item.
1149 if (cellX >= 0 && cellY >= 0) {
1150 cellXY[0] = cellX;
1151 cellXY[1] = cellY;
1152 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001153
Jon Mirandac476d6e2017-05-04 16:30:01 -07001154 // If appropriate, either create a folder or add to an existing folder
1155 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001156 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001157 return;
1158 }
1159 DragObject dragObject = new DragObject();
1160 dragObject.dragInfo = info;
1161 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1162 true)) {
1163 return;
1164 }
1165 } else {
1166 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1167 }
1168
1169 if (!foundCellSpan) {
1170 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001171 return;
1172 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001173
1174 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1175 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001176 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001177 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001178 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001179 if (folderIcon != null) {
1180 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1181 folderInfo.add(info, args.rank, false);
1182 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001183 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001184 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
1187
Sunny Goyale29897f52017-07-20 10:09:42 -07001188 public FolderIcon findFolderIcon(final long folderIconId) {
1189 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1190 @Override
1191 public boolean evaluate(ItemInfo info, View view) {
1192 return info != null && info.id == folderIconId;
1193 }
1194 });
1195 }
1196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001200 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001202 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001203 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1204
Adam Cohened66b2b2012-01-23 17:28:51 -08001205 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001206 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001207 }
Romain Guycbb89e42009-06-08 15:52:54 -07001208
Adam Cohen59400422014-03-05 18:07:04 -08001209 LauncherAppWidgetInfo launcherInfo;
1210 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001211 launcherInfo.spanX = itemInfo.spanX;
1212 launcherInfo.spanY = itemInfo.spanY;
1213 launcherInfo.minSpanX = itemInfo.minSpanX;
1214 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001215 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001216
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001217 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001218 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
Sunny Goyal2100c782016-08-22 16:00:03 -07001220 if (hostView == null) {
1221 // Perform actual inflation because we're live
1222 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001224 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301225 prepareAppWidget(hostView, launcherInfo);
1226 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
Romain Guycbb89e42009-06-08 15:52:54 -07001228
Sunny Goyald5462aa2016-11-22 16:52:39 +05301229 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001230 hostView.setTag(item);
1231 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001232 hostView.setFocusable(true);
1233 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001234 }
1235
Adam Cohended9f8d2010-11-03 13:25:16 -07001236 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1237 @Override
1238 public void onReceive(Context context, Intent intent) {
1239 final String action = intent.getAction();
1240 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001241 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001242 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001243 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001244 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001245 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001246 mShouldFadeInScrim = true;
1247 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1248 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1249 // the user unlocked and the Launcher is not in the foreground.
1250 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001251 }
1252 }
1253 };
1254
Tony Wickham010d2552017-01-20 08:15:28 -08001255 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1256 Runnable r = new Runnable() {
1257 @Override
1258 public void run() {
1259 mWorkspace.updateIconBadges(updatedBadges);
1260 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001261
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001262 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1263 if (popup instanceof PopupContainerWithArrow) {
1264 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001265 }
Tony Wickham010d2552017-01-20 08:15:28 -08001266 }
1267 };
1268 if (!waitUntilResume(r)) {
1269 r.run();
1270 }
1271 }
1272
Adam Cohended9f8d2010-11-03 13:25:16 -07001273 @Override
1274 public void onAttachedToWindow() {
1275 super.onAttachedToWindow();
1276
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001277 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001278 if (mLauncherCallbacks != null) {
1279 mLauncherCallbacks.onAttachedToWindow();
1280 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 }
1282
1283 @Override
1284 public void onDetachedFromWindow() {
1285 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001286
1287 if (mLauncherCallbacks != null) {
1288 mLauncherCallbacks.onDetachedFromWindow();
1289 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001290 }
1291
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001292 public AllAppsTransitionController getAllAppsController() {
1293 return mAllAppsController;
1294 }
1295
Winson Chunga6945242014-01-08 14:04:34 -08001296 public DragLayer getDragLayer() {
1297 return mDragLayer;
1298 }
1299
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001300 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001301 return mAppsView;
1302 }
1303
Winson Chunga6945242014-01-08 14:04:34 -08001304 public Workspace getWorkspace() {
1305 return mWorkspace;
1306 }
1307
1308 public Hotseat getHotseat() {
1309 return mHotseat;
1310 }
1311
Winson Chung8ae41982017-11-10 11:42:13 -08001312 public <T extends ViewGroup> T getOverviewPanel() {
1313 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001314 }
1315
Sunny Goyal47328fd2016-05-25 18:56:41 -07001316 public DropTargetBar getDropTargetBar() {
1317 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001318 }
1319
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001320 public LauncherAppWidgetHost getAppWidgetHost() {
1321 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Winson Chunga9abd0e2010-10-27 17:18:37 -07001324 public LauncherModel getModel() {
1325 return mModel;
1326 }
1327
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001328 public ModelWriter getModelWriter() {
1329 return mModelWriter;
1330 }
1331
Adam Cohen79d90c52016-04-22 13:29:20 -07001332 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001333 return mSharedPrefs;
1334 }
1335
Jon Miranda6bed3502017-09-19 14:43:17 -07001336 public int getOrientation() { return mOrientation; }
1337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 @Override
1339 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001340 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 super.onNewIntent(intent);
1342
Winson15f8b172015-08-19 11:02:39 -07001343 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1344 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1345 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001346
1347 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001348 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001349 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001350
Winson15f8b172015-08-19 11:02:39 -07001351 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1352 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001353 if (mWorkspace == null) {
1354 // Can be cases where mWorkspace is null, this prevents a NPE
1355 return;
1356 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001357
1358 // Note: There should be at most one log per method call. This is enforced implicitly
1359 // by using if-else statements.
1360 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001361 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001362 if (topOpenView != null) {
1363 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001364 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001365 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001366 target.pageIndex = mWorkspace.getCurrentPage();
1367 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001368 }
1369
1370 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001371 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001372 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001373
1374 final View v = getWindow().peekDecorView();
1375 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001376 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001377 }
1378
Winson Chungb745afb2015-03-02 11:51:23 -08001379 // Reset the apps view
1380 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001381 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001382 }
1383
Adam Cohen9211d422014-10-07 18:14:53 -07001384 if (mLauncherCallbacks != null) {
1385 mLauncherCallbacks.onHomeIntent();
1386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001388 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001389
Adam Cohen9211d422014-10-07 18:14:53 -07001390 if (mLauncherCallbacks != null) {
1391 mLauncherCallbacks.onNewIntent(intent);
1392 }
Winson15f8b172015-08-19 11:02:39 -07001393
1394 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1395 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1396 // animation.
1397 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001398 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001399
Winson15f8b172015-08-19 11:02:39 -07001400 mWorkspace.post(new Runnable() {
1401 @Override
1402 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001403 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001404 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001405 }
Winson15f8b172015-08-19 11:02:39 -07001406 }
1407 });
1408 }
1409 }
Sunny Goyalf8088ee2017-11-10 14:52:00 -08001410 InternalStateHandler.handleIntent(this, intent);
Winson15f8b172015-08-19 11:02:39 -07001411
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001412 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001413 }
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001416 public void onRestoreInstanceState(Bundle state) {
1417 super.onRestoreInstanceState(state);
1418 for (int page: mSynchronouslyBoundPages) {
1419 mWorkspace.restoreInstanceStateForChild(page);
1420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422
1423 @Override
1424 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001425 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001426 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001427
Adam Cohen21cd0022013-10-09 18:57:02 -07001428 }
Sunny Goyalea609262017-10-25 15:47:38 -07001429 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001430
1431
1432 AbstractFloatingView widgets = AbstractFloatingView
1433 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1434 if (widgets != null) {
1435 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1436 widgets.saveHierarchyState(widgetsState);
1437 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1438 } else {
1439 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1440 }
1441
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001442 // We close any open folders and shortcut containers since they will not be re-opened,
1443 // and we need to make sure this state is reflected.
1444 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001445
Sunny Goyal2100c782016-08-22 16:00:03 -07001446 if (mPendingRequestArgs != null) {
1447 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1448 }
1449 if (mPendingActivityResult != null) {
1450 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001453 super.onSaveInstanceState(outState);
1454
Adam Cohen9211d422014-10-07 18:14:53 -07001455 if (mLauncherCallbacks != null) {
1456 mLauncherCallbacks.onSaveInstanceState(outState);
1457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
1459
1460 @Override
1461 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001463
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001464 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001465 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001466
Winson Chungcd2b0142011-06-08 16:02:26 -07001467 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001468 // It's possible to receive onDestroy after a new Launcher activity has
1469 // been created. In this case, don't interfere with the new Launcher.
1470 if (mModel.isCurrentCallbacks(this)) {
1471 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001472 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001473 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001474
Sunny Goyal745bad92016-05-02 10:54:12 -07001475 if (mRotationPrefChangeHandler != null) {
1476 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1477 }
1478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001480 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001482 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001484 mAppWidgetHost = null;
1485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001487 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1488
Michael Jurka2ecf9952012-06-18 12:52:28 -07001489 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001490
Tony2917a8b2017-07-31 23:29:42 -07001491 clearPendingBinds();
1492
Adam Cohen9211d422014-10-07 18:14:53 -07001493 if (mLauncherCallbacks != null) {
1494 mLauncherCallbacks.onDestroy();
1495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
Sunny Goyalae502842016-06-17 08:43:56 -07001498 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1499 return mAccessibilityDelegate;
1500 }
1501
Adam Cohena9cf38f2011-05-02 15:36:58 -07001502 public DragController getDragController() {
1503 return mDragController;
1504 }
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001507 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001508 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001509 }
1510
1511 @Override
1512 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1513 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001514 try {
1515 super.startIntentSenderForResult(intent, requestCode,
1516 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1517 } catch (IntentSender.SendIntentException e) {
1518 throw new ActivityNotFoundException();
1519 }
1520 }
1521
Winson Chung70d72102011-08-12 11:24:35 -07001522 /**
1523 * Indicates that we want global search for this activity by setting the globalSearch
1524 * argument for {@link #startSearch} to true.
1525 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001527 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001529
Karl Rosaen138a0412009-04-23 19:00:21 -07001530 if (initialQuery == null) {
1531 // Use any text typed in the launcher as the initial query
1532 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 if (appSearchData == null) {
1535 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001536 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001538
Sunny Goyal6f28e712016-09-13 14:19:29 -07001539 if (mLauncherCallbacks == null ||
1540 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1541 // Starting search from the callbacks failed. Start the default global search.
1542 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001543 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001544
1545 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001546 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001547 }
1548
Ian Parkinson047036e2014-09-01 15:40:53 +01001549 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001550 * Starts the global search activity. This code is a copied from SearchManager
1551 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001552 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001553 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001554 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001555 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1556 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1557 if (globalSearchActivity == null) {
1558 Log.w(TAG, "No global search activity found.");
1559 return;
1560 }
1561 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1562 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1563 intent.setComponent(globalSearchActivity);
1564 // Make sure that we have a Bundle to put source in
1565 if (appSearchData == null) {
1566 appSearchData = new Bundle();
1567 } else {
1568 appSearchData = new Bundle(appSearchData);
1569 }
Adam Cohen9211d422014-10-07 18:14:53 -07001570 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001571 if (!appSearchData.containsKey("source")) {
1572 appSearchData.putString("source", getPackageName());
1573 }
1574 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1575 if (!TextUtils.isEmpty(initialQuery)) {
1576 intent.putExtra(SearchManager.QUERY, initialQuery);
1577 }
1578 if (selectInitialQuery) {
1579 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1580 }
1581 intent.setSourceBounds(sourceBounds);
1582 try {
1583 startActivity(intent);
1584 } catch (ActivityNotFoundException ex) {
1585 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588
Winson Chung70d72102011-08-12 11:24:35 -07001589 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001590 public boolean onPrepareOptionsMenu(Menu menu) {
1591 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001592 if (mLauncherCallbacks != null) {
1593 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1594 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001595 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001598 @Override
1599 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001600 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001601 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001602 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001603 }
1604
Joe Onorato9c1289c2009-08-17 11:03:03 -04001605 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001606 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 }
1608
Adam Cohen517a7f52014-03-01 12:12:59 -08001609 public boolean isWorkspaceLoading() {
1610 return mWorkspaceLoading;
1611 }
1612
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001613 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001614 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001615 }
1616
Sunny Goyal04a324a2017-01-20 21:08:59 -08001617 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001618 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001619 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001620
Sunny Goyal2100c782016-08-22 16:00:03 -07001621 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001622 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001623 if (LOGD) {
1624 Log.d(TAG, "Adding widget from drop");
1625 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001626 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001627 }
1628
Sunny Goyal2100c782016-08-22 16:00:03 -07001629 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001630 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1631 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1632 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001633
Adam Cohenad4e15c2013-10-17 16:21:35 -07001634 Runnable onComplete = new Runnable() {
1635 @Override
1636 public void run() {
1637 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001638 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001639 }
1640 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001641 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001642 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
1644 }
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001646 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1647 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001648 info.container = container;
1649 info.screenId = screenId;
1650 if (cell != null) {
1651 info.cellX = cell[0];
1652 info.cellY = cell[1];
1653 }
1654 info.spanX = spanX;
1655 info.spanY = spanY;
1656
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001657 switch (info.itemType) {
1658 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1659 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001660 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001661 break;
1662 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001663 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001664 break;
1665 default:
1666 throw new IllegalStateException("Unknown item type: " + info.itemType);
1667 }
1668 }
1669
Adam Cohenfbba09b2011-07-18 21:43:05 -07001670 /**
1671 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001672 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001673 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001674 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1675 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001676 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1677 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1678 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001679 }
1680
Adam Cohenfbba09b2011-07-18 21:43:05 -07001681 /**
1682 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001683 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001684 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001685 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001686 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001687 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001688 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001689 // In the case where we've prebound the widget, we remove it from the DragLayer
1690 if (LOGD) {
1691 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1692 }
1693 getDragLayer().removeView(hostView);
1694
Adam Cohened66b2b2012-01-23 17:28:51 -08001695 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001696 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001697
1698 // Clear the boundWidget so that it doesn't get destroyed.
1699 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001700 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001701 // In this case, we either need to start an activity to get permission to bind
1702 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001703 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1704 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1705 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1706 this, info.componentName);
1707 } else {
1708 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1709 }
Adam Cohendd70d662012-10-04 16:53:44 -07001710 Bundle options = info.bindOptions;
1711
Sunny Goyalffe83f12014-08-14 17:39:34 -07001712 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1713 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001714 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001715 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001716 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001717 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001718 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001719 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001720 }
1721
Adam Cohendcd297f2013-06-18 13:13:40 -07001722 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001723 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001724 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 folderInfo.title = getText(R.string.folder_name);
1726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001728 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729
1730 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001731 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301732 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001733 // Force measure the new folder icon
1734 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1735 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001736 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
Romain Guycbb89e42009-06-08 15:52:54 -07001738
Winsonc0b52fe2015-09-09 16:38:15 -07001739 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001740 * Unbinds the view for the specified item, and removes the item and all its children.
1741 *
1742 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001743 * @param itemInfo the {@link ItemInfo} for this view.
1744 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001745 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001746 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001747 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001748 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001749 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1750 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001751 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001752 } else {
1753 mWorkspace.removeWorkspaceItem(v);
1754 }
Winsonc0b52fe2015-09-09 16:38:15 -07001755 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001756 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001757 }
1758 } else if (itemInfo instanceof FolderInfo) {
1759 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001760 if (v instanceof FolderIcon) {
1761 ((FolderIcon) v).removeListeners();
1762 }
Winsonc0b52fe2015-09-09 16:38:15 -07001763 mWorkspace.removeWorkspaceItem(v);
1764 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001765 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001766 }
1767 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1768 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001769 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001770 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001771 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001772 }
1773 } else {
1774 return false;
1775 }
1776 return true;
1777 }
1778
1779 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001780 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001781 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001782 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001783 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001784 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001785 // Deleting an app widget ID is a void call but writes to disk before returning
1786 // to the caller...
1787 new AsyncTask<Void, Void, Void>() {
1788 public Void doInBackground(Void ... args) {
1789 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1790 return null;
1791 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001792 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001793 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001794 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001795 }
1796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 @Override
1798 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001799 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
1801
Joe Onorato88ec0992009-11-19 13:16:06 -08001802 @Override
1803 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001804 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1805 return;
1806 }
1807
Sunny Goyal45478022015-06-08 16:52:41 -07001808 if (mDragController.isDragging()) {
1809 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001810 return;
1811 }
1812
Jon Mirandafeba90f2016-10-06 10:53:29 -07001813 // Note: There should be at most one log per method call. This is enforced implicitly
1814 // by using if-else statements.
1815 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001816 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1817 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001818 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001819 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001820 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001821 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001822 } else {
1823 // Back button is a no-op here, but give at least some feedback for the button press
1824 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001825 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001826 }
1827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 * Launches the intent referred by the clicked shortcut.
1830 *
1831 * @param v The view representing the clicked shortcut.
1832 */
1833 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001834 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1835 // view has detached (it's possible for this to happen if the view is removed mid touch).
1836 if (v.getWindowToken() == null) {
1837 return;
1838 }
1839
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001840 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001841 return;
1842 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001843
Adam Cohen1697b792013-09-17 19:08:21 -07001844 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001845 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001846 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1847 LauncherLogProto.Action.Direction.NONE,
1848 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001849 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001850 }
1851 return;
1852 }
1853
1854 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001855 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001856 int page = mWorkspace.indexOfChild(v);
1857 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1858 LauncherLogProto.Action.Direction.NONE,
1859 LauncherLogProto.ContainerType.OVERVIEW, page);
1860 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001861 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001862 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001863 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001867 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001868 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001870 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001871 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001872 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001873 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001874 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001875 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001876 } else if (tag instanceof AppInfo) {
1877 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001878 } else if (tag instanceof LauncherAppWidgetInfo) {
1879 if (v instanceof PendingAppWidgetHostView) {
1880 onClickPendingWidget((PendingAppWidgetHostView) v);
1881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
1883 }
1884
Sunny Goyal70660032015-05-14 00:07:08 -07001885 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001886 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001887 return false;
1888 }
1889
Michael Jurkaaf442092010-06-10 17:01:57 -07001890 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001891 * Event handler for the app widget view which has not fully restored.
1892 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001893 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001894 if (mIsSafeModeEnabled) {
1895 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1896 return;
1897 }
1898
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001899 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001900 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001901 LauncherAppWidgetProviderInfo appWidgetInfo =
1902 mAppWidgetManager.findProvider(info.providerName, info.user);
1903 if (appWidgetInfo == null) {
1904 return;
1905 }
1906 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1907
Sunny Goyald478c832016-04-01 12:04:16 -07001908 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1909 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1910 // This should not happen, as we make sure that an Id is allocated during bind.
1911 return;
1912 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001913 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1914 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001915 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001916 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001917 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001918 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001919 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001920 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001921 }
1922 }
1923
1924 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001925 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1926 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1927 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001928 *
1929 * @param v The view that was clicked.
1930 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001931 protected void onClickAllAppsButton(View v) {
1932 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001933 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301934 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1935 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001936 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001937 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001938 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001939 }
1940 }
1941
Sunny Goyale6e72002017-01-12 16:55:36 -08001942 private void onClickPendingAppItem(final View v, final String packageName,
1943 boolean downloadStarted) {
1944 if (downloadStarted) {
1945 // If the download has started, simply direct to the market app.
1946 startMarketIntentForPackage(v, packageName);
1947 return;
1948 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001949 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001950 .setTitle(R.string.abandoned_promises_title)
1951 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001952 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1953 @Override
1954 public void onClick(DialogInterface dialogInterface, int i) {
1955 startMarketIntentForPackage(v, packageName);
1956 }
1957 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001958 .setNeutralButton(R.string.abandoned_clean_this,
1959 new DialogInterface.OnClickListener() {
1960 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001961 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001962 mWorkspace.removeAbandonedPromise(packageName, user);
1963 }
1964 })
1965 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001966 }
1967
1968 private void startMarketIntentForPackage(View v, String packageName) {
1969 ItemInfo item = (ItemInfo) v.getTag();
1970 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001971 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001972 }
1973
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001974 /**
1975 * Event handler for an app shortcut click.
1976 *
1977 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1978 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001979 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001980 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1981 Object tag = v.getTag();
1982 if (!(tag instanceof ShortcutInfo)) {
1983 throw new IllegalArgumentException("Input must be a Shortcut");
1984 }
1985
1986 // Open shortcut
1987 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001988
Sunny Goyal076839c2017-10-30 13:52:20 -07001989 if (shortcut.isDisabled()) {
1990 if ((shortcut.runtimeStatusFlags &
1991 ~FLAG_DISABLED_SUSPENDED &
1992 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001993 // If the app is only disabled because of the above flags, launch activity anyway.
1994 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001995 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001996 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1997 // Use a message specific to this shortcut, if it has one.
1998 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1999 return;
2000 }
2001 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002002 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07002003 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002004 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07002005 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2006 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002007 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002008 }
2009 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2010 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002011 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002012 }
2013
Chris Wren40c5ed32014-06-24 18:24:23 -04002014 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002015 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002016 String packageName = shortcut.intent.getComponent() != null ?
2017 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2018 if (!TextUtils.isEmpty(packageName)) {
2019 onClickPendingAppItem(v, packageName,
2020 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2021 return;
2022 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002023 }
2024
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002025 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002026 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002027 }
2028
Sunny Goyala7ce1662016-05-31 15:01:35 -07002029 private void startAppShortcutOrInfoActivity(View v) {
2030 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002031 Intent intent;
2032 if (item instanceof PromiseAppInfo) {
2033 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2034 intent = promiseAppInfo.getMarketIntent();
2035 } else {
2036 intent = item.getIntent();
2037 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002038 if (intent == null) {
2039 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002040 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002041 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002042 }
2043
2044 /**
2045 * Event handler for a folder icon click.
2046 *
2047 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2048 */
2049 protected void onClickFolderIcon(View v) {
2050 if (LOGD) Log.d(TAG, "onClickFolder");
2051 if (!(v instanceof FolderIcon)){
2052 throw new IllegalArgumentException("Input must be a FolderIcon");
2053 }
2054
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002055 Folder folder = ((FolderIcon) v).getFolder();
2056 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002057 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002058 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002059 }
Michael Jurka5130e402011-10-13 04:55:35 -07002060 }
2061
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002062 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002063 * Event handler for the wallpaper picker button that appears after a long press
2064 * on the home screen.
2065 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002066 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002067 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002068 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2069 return;
2070 }
2071
Tony Wickham785f7a52015-08-31 17:28:32 -07002072 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2073 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002074 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002075 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002076 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002077
2078 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002079 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2080 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002081 intent.setPackage(pickerPackage);
2082 }
2083
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002084 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002085 try {
2086 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2087 // If there is no target package, use the default intent chooser animation
2088 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2089 } catch (ActivityNotFoundException e) {
2090 setWaitingForResult(null);
2091 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2092 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002093 }
2094
Sunny Goyala7ce1662016-05-31 15:01:35 -07002095 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002097 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2098 try {
2099 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2100 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2101 // is enabled by default on NYC.
2102 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2103 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002104
2105 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2106 String id = ((ShortcutInfo) info).getDeepShortcutId();
2107 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302108 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002109 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002110 } else {
2111 // Could be launching some bookkeeping activity
2112 startActivity(intent, optsBundle);
2113 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002114 } finally {
2115 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002118 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2119 // corresponding permission. Show the appropriate permission prompt if that
2120 // is the case.
2121 if (intent.getComponent() == null
2122 && Intent.ACTION_CALL.equals(intent.getAction())
2123 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2124 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002125
2126 setWaitingForResult(PendingRequestArgs
2127 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002128 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2129 REQUEST_PERMISSION_CALL_PHONE);
2130 } else {
2131 // No idea why this was thrown.
2132 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 }
2135 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002136
Sunny Goyalfe770c92016-09-27 14:38:58 -07002137 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002138 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002139 if (Utilities.ATLEAST_MARSHMALLOW) {
2140 int left = 0, top = 0;
2141 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002142 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002143 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002144 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002145 if (icon != null) {
2146 Rect bounds = icon.getBounds();
2147 left = (width - bounds.width()) / 2;
2148 top = v.getPaddingTop();
2149 width = bounds.width();
2150 height = bounds.height();
2151 }
2152 }
2153 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2154 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2155 // On L devices, we use the device default slide-up transition.
2156 // On L MR1 devices, we use a custom version of the slide-up transition which
2157 // doesn't have the delay present in the device default.
2158 return ActivityOptions.makeCustomAnimation(
2159 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2160 }
2161 return null;
2162 }
2163
Tonye3c59252017-05-02 21:32:27 -07002164 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002165 int[] pos = new int[2];
2166 v.getLocationOnScreen(pos);
2167 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2168 }
2169
Sunny Goyala7ce1662016-05-31 15:01:35 -07002170 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002171 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002172 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2173 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002174 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002175 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002176 // Only launch using the new animation if the shortcut has not opted out (this is a
2177 // private contract between launcher and may be ignored in the future).
2178 boolean useLaunchAnimation = (v != null) &&
2179 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2180 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2181
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002182 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002183
2184 // Prepare intent
2185 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2186 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002187 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002188 }
2189 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002190 if (Utilities.ATLEAST_MARSHMALLOW
2191 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002192 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002193 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002194 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002195 // Shortcuts need some special checks due to legacy reasons.
2196 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002197 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002198 // Could be launching some bookkeeping activity
2199 startActivity(intent, optsBundle);
2200 } else {
2201 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2202 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2203 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002204
2205 if (v instanceof BubbleTextView) {
2206 // This is set to the view that launched the activity that navigated the user away
2207 // from launcher. Since there is no callback for when the activity has finished
2208 // launching, enable the press state and keep this reference to reset the press
2209 // state when we return to launcher.
2210 BubbleTextView btv = (BubbleTextView) v;
2211 btv.setStayPressed(true);
2212 setOnResumeCallback(btv);
2213 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002214 mAppLaunchSuccess = true;
2215 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002216 } catch (ActivityNotFoundException|SecurityException e) {
2217 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2218 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2219 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002220 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002221 }
2222
Tony Wickhamdadb3042016-02-24 11:07:00 -08002223 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002224 public boolean dispatchTouchEvent(MotionEvent ev) {
2225 mLastDispatchTouchEventX = ev.getX();
2226 return super.dispatchTouchEvent(ev);
2227 }
2228
2229 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002231 if (!isDraggingEnabled()) return false;
2232 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002233 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234
Jon Miranda51f037d2016-11-07 08:37:20 -08002235 boolean ignoreLongPressToOverview =
2236 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002237
Adam Cohen1697b792013-09-17 19:08:21 -07002238 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002239 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002240 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302241 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2242 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002243 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002244 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002245 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2246 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2247 return true;
2248 } else {
2249 return false;
2250 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002251 } else {
2252 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002253 }
Adam Cohen1697b792013-09-17 19:08:21 -07002254 }
2255
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002256 CellLayout.CellInfo longClickCellInfo = null;
2257 View itemUnderLongClick = null;
2258 if (v.getTag() instanceof ItemInfo) {
2259 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002260 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002261 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002262 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 }
2264
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 // The hotseat touch handling does not go through Workspace, and we always allow long press
2266 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002267 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002269 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002270 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002271 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302272 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2273 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002274 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002275 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002276 return false;
2277 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302278 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2279 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002280 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002281 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002282 }
Jon Miranda379198e2016-09-23 08:54:40 -07002283 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2284 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002286 final boolean isAllAppsButton =
2287 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2288 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2289 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002290 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002292 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 }
2294 }
2295 }
2296 return true;
2297 }
2298
Winson Chung3d503fb2011-07-13 17:25:49 -07002299 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002300 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002301 return mHotseat != null && layout != null &&
2302 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2303 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002304
Winson Chung3d503fb2011-07-13 17:25:49 -07002305 /**
2306 * Returns the CellLayout of the specified container at the specified screen.
2307 */
Sunny Goyal92820592015-03-02 11:31:35 -08002308 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2310 if (mHotseat != null) {
2311 return mHotseat.getLayout();
2312 } else {
2313 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002314 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002316 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002317 }
2318 }
2319
Michael Jurkae326f182011-11-21 14:05:46 -08002320 @Override
2321 public void onTrimMemory(int level) {
2322 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002323 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2324 // The widget preview db can result in holding onto over
2325 // 3MB of memory for caching which isn't necessary.
2326 SQLiteDatabase.releaseMemory();
2327
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002328 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002329 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002330 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002331 if (mLauncherCallbacks != null) {
2332 mLauncherCallbacks.onTrimMemory(level);
2333 }
Michael Jurkae326f182011-11-21 14:05:46 -08002334 }
2335
Michael Jurkad7c28052012-04-27 15:43:36 -07002336 @Override
2337 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002338 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002339 final List<CharSequence> text = event.getText();
2340 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002341 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002342 // TODO: When can workspace be null?
2343 text.add(mWorkspace == null
2344 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002345 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002346 return result;
2347 }
2348
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002349 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002350 * If the activity is currently paused, signal that we need to run the passed Runnable
2351 * in onResume.
2352 *
2353 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002354 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2355 * wrong when we're not running, and if the activity comes back to what the configuration was
2356 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002357 *
2358 * Implementation of the method from LauncherModel.Callbacks.
2359 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002360 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002361 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002362 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002363 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002364 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002365 if (run instanceof RunnableWithId) {
2366 // Remove any runnables which have the same id
2367 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002368 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002369 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002370 return true;
2371 } else {
2372 return false;
2373 }
2374 }
2375
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002376 public void setOnResumeCallback(OnResumeCallback callback) {
2377 if (mOnResumeCallback != null) {
2378 mOnResumeCallback.onLauncherResume();
2379 }
2380 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002381 }
2382
Michael Jurka7607c2f2013-04-03 14:33:19 -07002383 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002384 * If the activity is currently paused, signal that we need to re-run the loader
2385 * in onResume.
2386 *
2387 * This needs to be called from incoming places where resources might have been loaded
2388 * while we are paused. That is becaues the Configuration might be wrong
2389 * when we're not running, and if it comes back to what it was when we
2390 * were paused, we are not restarted.
2391 *
2392 * Implementation of the method from LauncherModel.Callbacks.
2393 *
2394 * @return true if we are currently paused. The caller might be able to
2395 * skip some work in that case since we will come back again.
2396 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002397 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002398 public boolean setLoadOnResume() {
2399 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002400 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002401 mOnResumeNeedsLoad = true;
2402 return true;
2403 } else {
2404 return false;
2405 }
2406 }
2407
2408 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002409 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002411 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002412 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002413 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002414 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002415 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002416 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002419
Joe Onorato9c1289c2009-08-17 11:03:03 -04002420 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002421 * Clear any pending bind callbacks. This is called when is loader is planning to
2422 * perform a full rebind from scratch.
2423 */
2424 @Override
2425 public void clearPendingBinds() {
2426 mBindOnResumeCallbacks.clear();
2427 if (mPendingExecutor != null) {
2428 mPendingExecutor.markCompleted();
2429 mPendingExecutor = null;
2430 }
2431 }
2432
2433 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002434 * Refreshes the shortcuts shown on the workspace.
2435 *
2436 * Implementation of the method from LauncherModel.Callbacks.
2437 */
2438 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002439 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002440 // Floating panels (except the full widget sheet) are associated with individual icons. If
2441 // we are starting a fresh bind, close all such panels as all the icons are about
2442 // to go away.
2443 AbstractFloatingView.closeOpenViews(this, true,
2444 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002445
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002446 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002447
Winson Chungd64d1762013-08-20 14:37:16 -07002448 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002449 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002450 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002451
Winson Chung3d503fb2011-07-13 17:25:49 -07002452 if (mHotseat != null) {
2453 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002455 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 }
2457
Adam Cohendcd297f2013-06-18 13:13:40 -07002458 @Override
2459 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002460 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002461 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2462 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002463 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2464 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002465 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002466 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2467 // If there are no screens, we need to have an empty screen
2468 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002469 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002470 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002471
Winsonc7d2e832016-07-28 12:24:55 -07002472 // After we have added all the screens, if the wallpaper was locked to the default state,
2473 // then notify to indicate that it can be released and a proper wallpaper offset can be
2474 // computed before the next layout
2475 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002476 }
2477
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002478 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002479 int count = orderedScreenIds.size();
2480 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002481 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002482 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002483 // No need to bind the first screen, as its always bound.
2484 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2485 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002487 }
2488
Sunny Goyalb23980c2017-08-17 07:45:25 -07002489 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002490 public void bindAppsAdded(final ArrayList<Long> newScreens,
2491 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002492 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002493 Runnable r = new Runnable() {
2494 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002495 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002496 }
2497 };
2498 if (waitUntilResume(r)) {
2499 return;
2500 }
2501
Winson Chungd64d1762013-08-20 14:37:16 -07002502 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002503 if (newScreens != null) {
2504 bindAddScreens(newScreens);
2505 }
Winson Chungd64d1762013-08-20 14:37:16 -07002506
2507 // We add the items without animation on non-visible pages, and with
2508 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002509 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002510 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002511 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002512 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002513 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002514 }
Winson Chungc58497e2013-09-03 17:48:37 -07002515
Winson Chung87412982013-10-03 18:34:14 -07002516 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002517 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002518 }
2519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521 * Bind the items start-end from the list.
2522 *
2523 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002524 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002525 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002526 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002527 Runnable r = new Runnable() {
2528 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002529 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002530 }
2531 };
2532 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002533 return;
2534 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002535
Sunny Goyal3be633b2016-12-08 09:59:25 -08002536 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002537 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002538 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002539 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002540 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002541 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002542 int end = items.size();
2543 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002544 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002545
2546 // Short circuit if we are loading dock items for a configuration which has no dock
2547 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2548 mHotseat == null) {
2549 continue;
2550 }
2551
Sunny Goyal639e9062015-08-19 19:17:06 -07002552 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002553 switch (item.itemType) {
2554 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2555 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002556 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002557 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002558 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002559 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002560 }
2561 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002562 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002563 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002564 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002566 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002567 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2568 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002569 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002570 if (view == null) {
2571 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002572 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002573 break;
2574 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002575 default:
2576 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002577 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002578
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002579 /*
2580 * Remove colliding items.
2581 */
2582 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2583 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2584 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2585 View v = cl.getChildAt(item.cellX, item.cellY);
2586 Object tag = v.getTag();
2587 String desc = "Collision while binding workspace item: " + item
2588 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002589 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002590 throw (new RuntimeException(desc));
2591 } else {
2592 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002593 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002594 continue;
2595 }
2596 }
2597 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302598 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002599 if (animateIcons) {
2600 // Animate all the applications up now
2601 view.setAlpha(0f);
2602 view.setScaleX(0f);
2603 view.setScaleY(0f);
2604 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002605 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002608
Winson Chung997a9232013-07-24 15:33:46 -07002609 if (animateIcons) {
2610 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002611 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002612 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002613 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002614 final Runnable startBounceAnimRunnable = new Runnable() {
2615 public void run() {
2616 anim.playTogether(bounceAnims);
2617 anim.start();
2618 }
2619 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002620 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002621 // We post the animation slightly delayed to prevent slowdowns
2622 // when we are loading right after we return to launcher.
2623 mWorkspace.postDelayed(new Runnable() {
2624 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002625 if (mWorkspace != null) {
2626 mWorkspace.snapToPage(newScreenIndex);
2627 mWorkspace.postDelayed(startBounceAnimRunnable,
2628 NEW_APPS_ANIMATION_DELAY);
2629 }
Winson Chung94d67682013-09-25 16:29:40 -07002630 }
2631 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002632 } else {
2633 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002634 }
2635 }
Winson Chung64359a52013-07-08 17:17:08 -07002636 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002637 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 }
2639
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 /**
2641 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002642 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002643 public void bindAppWidget(LauncherAppWidgetInfo item) {
2644 View view = inflateAppWidget(item);
2645 if (view != null) {
2646 mWorkspace.addInScreen(view, item);
2647 mWorkspace.requestLayout();
2648 }
2649 }
2650
2651 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002652 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302653 PendingAppWidgetHostView view =
2654 new PendingAppWidgetHostView(this, item, mIconCache, true);
2655 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002656 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002657 }
2658
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002659 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002660
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002661 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002662
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002663 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2664 // If the provider is not ready, bind as a pending widget.
2665 appWidgetInfo = null;
2666 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2667 // The widget id is not valid. Try to find the widget based on the provider info.
2668 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2669 } else {
2670 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2671 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002672
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002673 // If the provider is ready, but the width is not yet restored, try to restore it.
2674 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2675 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002676 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002677 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2678 + " belongs to component " + item.providerName
2679 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002680 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002681 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002682 }
Sunny Goyalff572272014-07-23 13:58:07 -07002683
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002684 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002685 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002686 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2687 // Id has not been allocated yet. Allocate a new id.
2688 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2689 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002690
Sunny Goyald478c832016-04-01 12:04:16 -07002691 // Also try to bind the widget. If the bind fails, the user will be shown
2692 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002693 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002694 pendingInfo.spanX = item.spanX;
2695 pendingInfo.spanY = item.spanY;
2696 pendingInfo.minSpanX = item.minSpanX;
2697 pendingInfo.minSpanY = item.minSpanY;
2698 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002699
2700 boolean isDirectConfig =
2701 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2702 if (isDirectConfig && item.bindOptions != null) {
2703 Bundle newOptions = item.bindOptions.getExtras();
2704 if (options != null) {
2705 newOptions.putAll(options);
2706 }
2707 options = newOptions;
2708 }
Sunny Goyald478c832016-04-01 12:04:16 -07002709 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2710 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002711
Sunny Goyal86df1382016-08-10 15:03:22 -07002712 // We tried to bind once. If we were not able to bind, we would need to
2713 // go through the permission dialog, which means we cannot skip the config
2714 // activity.
2715 item.bindOptions = null;
2716 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2717
Sunny Goyald478c832016-04-01 12:04:16 -07002718 // Bind succeeded
2719 if (success) {
2720 // If the widget has a configure activity, it is still needs to set it up,
2721 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002722 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002723 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2724 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002725 }
Sunny Goyald478c832016-04-01 12:04:16 -07002726
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002727 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002728 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002729 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002730 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002731 // The widget was marked as UI not ready, but there is no configure activity to
2732 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002733 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002734 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002735 }
Sunny Goyalff572272014-07-23 13:58:07 -07002736 }
2737
Sunny Goyald5462aa2016-11-22 16:52:39 +05302738 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002739 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002740 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002741 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002742 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002743 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002744 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002745 }
2746
Sunny Goyal233ee962015-08-03 13:05:01 -07002747 item.minSpanX = appWidgetInfo.minSpanX;
2748 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302749 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002750 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302751 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002752 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302753 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002754
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002755 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002756 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002757 }
2758
Sunny Goyalff572272014-07-23 13:58:07 -07002759 /**
2760 * Restores a pending widget.
2761 *
2762 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002763 */
Sunny Goyald478c832016-04-01 12:04:16 -07002764 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002765 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2766 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2767 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002768 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002769 }
2770
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002771 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002772 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002773 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2774 info.pendingItemInfo = null;
2775 }
Sunny Goyalff572272014-07-23 13:58:07 -07002776
Sunny Goyalba47faa2017-10-04 16:50:57 -07002777 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2778 view.reinflate();
2779 }
2780
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002781 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002782 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002783 }
2784
Adam Cohen1462de32012-07-24 22:34:36 -07002785 public void onPageBoundSynchronously(int page) {
2786 mSynchronouslyBoundPages.add(page);
2787 }
2788
Sunny Goyal527c7d32015-08-28 15:19:36 -07002789 @Override
2790 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2791 if (mPendingExecutor != null) {
2792 mPendingExecutor.markCompleted();
2793 }
2794 mPendingExecutor = executor;
2795 executor.attachTo(this);
2796 }
2797
2798 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2799 if (mPendingExecutor == executor) {
2800 mPendingExecutor = null;
2801 }
2802 }
2803
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002804 @Override
2805 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2806 Runnable r = new Runnable() {
2807 public void run() {
2808 finishFirstPageBind(executor);
2809 }
2810 };
2811 if (waitUntilResume(r)) {
2812 return;
2813 }
2814
2815 Runnable onComplete = new Runnable() {
2816 @Override
2817 public void run() {
2818 if (executor != null) {
2819 executor.onLoadAnimationCompleted();
2820 }
2821 }
2822 };
2823 if (mDragLayer.getAlpha() < 1) {
2824 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2825 } else {
2826 onComplete.run();
2827 }
2828 }
2829
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 /**
2831 * Callback saying that there aren't any more items to bind.
2832 *
2833 * Implementation of the method from LauncherModel.Callbacks.
2834 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002835 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002836 Runnable r = new Runnable() {
2837 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002838 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002839 }
2840 };
2841 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002842 return;
2843 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002844 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002845 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002846
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002847 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002848
Sunny Goyal2100c782016-08-22 16:00:03 -07002849 if (mPendingActivityResult != null) {
2850 handleActivityResult(mPendingActivityResult.requestCode,
2851 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2852 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002853 }
2854
Sunny Goyala474a9b2017-05-04 16:47:11 -07002855 InstallShortcutReceiver.disableAndFlushInstallQueue(
2856 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002857
Tony Wickham010d2552017-01-20 08:15:28 -08002858 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002859 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002860 }
2861
Winson Chunga2413752012-04-03 14:22:34 -07002862 private boolean canRunNewAppsAnimation() {
2863 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002864 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002865 }
2866
Winson Chungc9168342013-06-26 14:54:55 -07002867 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002868 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002869 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2870 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002871 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002872 return bounceAnim;
2873 }
2874
Adam Cohen27772732013-11-11 14:00:56 +00002875 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002876 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002877 }
2878
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002879 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002880 * Add the icons for all apps.
2881 *
2882 * Implementation of the method from LauncherModel.Callbacks.
2883 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002884 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002885 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2886 public void run() {
2887 bindAllApplications(apps);
2888 }
2889 };
2890 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002891 return;
2892 }
2893
Winson Chungb745afb2015-03-02 11:51:23 -08002894 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002895 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002896 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002897 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002898 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002899 return;
2900 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002901
Winson Chungb745afb2015-03-02 11:51:23 -08002902 mAppsView.setApps(apps);
2903 }
Adam Cohen9211d422014-10-07 18:14:53 -07002904 if (mLauncherCallbacks != null) {
2905 mLauncherCallbacks.bindAllApplications(apps);
2906 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 }
2908
2909 /**
Tony2917a8b2017-07-31 23:29:42 -07002910 * Returns an Executor that will run after the launcher is first drawn (including after the
2911 * initial fade in animation). Returns null if the first draw has already occurred.
2912 */
2913 public @Nullable Executor getPendingExecutor() {
2914 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2915 }
2916
2917 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002918 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2919 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2920 */
2921 @Override
2922 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002923 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002924 }
2925
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002926 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 * A package was updated.
2928 *
2929 * Implementation of the method from LauncherModel.Callbacks.
2930 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002931 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002932 Runnable r = new Runnable() {
2933 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002934 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002935 }
2936 };
2937 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002938 return;
2939 }
2940
Winson Chungb745afb2015-03-02 11:51:23 -08002941 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002942 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002944 }
2945
Sunny Goyal4390ace2014-10-13 11:33:11 -07002946 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002947 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2948 Runnable r = new Runnable() {
2949 public void run() {
2950 bindPromiseAppProgressUpdated(app);
2951 }
2952 };
2953 if (waitUntilResume(r)) {
2954 return;
2955 }
2956
2957 if (mAppsView != null) {
2958 mAppsView.updatePromiseAppProgress(app);
2959 }
2960 }
2961
2962 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002963 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2964 Runnable r = new Runnable() {
2965 public void run() {
2966 bindWidgetsRestored(widgets);
2967 }
2968 };
2969 if (waitUntilResume(r)) {
2970 return;
2971 }
2972 mWorkspace.widgetsRestored(widgets);
2973 }
2974
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002976 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002977 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002978 *
2979 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002980 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002981 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002982 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002983 Runnable r = new Runnable() {
2984 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002985 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002986 }
2987 };
2988 if (waitUntilResume(r)) {
2989 return;
2990 }
2991
Sunny Goyal4390ace2014-10-13 11:33:11 -07002992 if (!updated.isEmpty()) {
2993 mWorkspace.updateShortcuts(updated);
2994 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002995 }
2996
2997 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002998 * Update the state of a package, typically related to install state.
2999 *
3000 * Implementation of the method from LauncherModel.Callbacks.
3001 */
Sunny Goyale755d462014-07-22 13:48:29 -07003002 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003003 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3004 Runnable r = new Runnable() {
3005 public void run() {
3006 bindRestoreItemsChange(updates);
3007 }
3008 };
3009 if (waitUntilResume(r)) {
3010 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003011 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003012
Sunny Goyal756adbc2015-04-16 15:20:51 -07003013 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003014 }
3015
3016 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003017 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003018 * in addition to specific applications to remove, the reason being that
3019 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003020 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003021 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003023 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003024 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003025 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003026 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003027 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003028 }
Winson Chungd64d1762013-08-20 14:37:16 -07003029 };
3030 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003031 return;
3032 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003033 mWorkspace.removeItemsByMatcher(matcher);
3034 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003035 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003036
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003037 @Override
3038 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3039 Runnable r = new Runnable() {
3040 public void run() {
3041 bindAppInfosRemoved(appInfos);
3042 }
3043 };
3044 if (waitUntilResume(r)) {
3045 return;
Joe Onorato36115782010-06-17 13:28:48 -04003046 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003047
Winson Chungdf95eb12013-10-16 14:57:07 -07003048 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003049 if (mAppsView != null) {
3050 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003051 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 }
Joe Onoratobe386092009-11-17 17:32:16 -08003053
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003054 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003055 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3056 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003057 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3058 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003059 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003060 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003061 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003062 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003063 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003064 return;
3065 }
3066
Tony Wickham26b17462017-03-20 17:12:24 -07003067 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3068 if (topView != null) {
3069 topView.onWidgetsBound();
3070 }
3071 }
3072
Tony Wickham86222d22017-03-29 15:30:43 -07003073 /**
3074 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3075 * refreshes the widgets and shortcuts associated with the given package/user
3076 */
3077 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003078 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003079 }
3080
Sunny Goyalc99cb172017-10-19 16:15:09 -07003081 public boolean isRotationEnabled () {
3082 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003083 }
3084
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003085 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003086 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003087 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003088 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003089 }
3090
Winson Chung80baf5a2010-08-09 16:03:15 -07003091 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003092 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003093 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003094 @Override
3095 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3096 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003097
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003098 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3099 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003100 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003101 writer.println(prefix + " Homescreen " + i);
3102
3103 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3104 for (int j = 0; j < layout.getChildCount(); j++) {
3105 Object tag = layout.getChildAt(j).getTag();
3106 if (tag != null) {
3107 writer.println(prefix + " " + tag.toString());
3108 }
3109 }
3110 }
3111
3112 writer.println(prefix + " Hotseat");
3113 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003114 for (int j = 0; j < layout.getChildCount(); j++) {
3115 Object tag = layout.getChildAt(j).getTag();
3116 if (tag != null) {
3117 writer.println(prefix + " " + tag.toString());
3118 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003119 }
Sunny Goyala1365452015-10-01 15:46:24 -07003120
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003121 try {
3122 FileLog.flushAll(writer);
3123 } catch (Exception e) {
3124 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003125 }
3126 }
3127
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003128 writer.println(prefix + "Misc:");
3129 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3130 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3131 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3132
3133 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003134
Adam Cohen9211d422014-10-07 18:14:53 -07003135 if (mLauncherCallbacks != null) {
3136 mLauncherCallbacks.dump(prefix, fd, writer, args);
3137 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003138 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003139
Sunny Goyal66b24572016-09-21 15:57:55 -07003140 @Override
3141 @TargetApi(Build.VERSION_CODES.N)
3142 public void onProvideKeyboardShortcuts(
3143 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3144
3145 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003146 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003147 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3148 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3149 }
3150 View currentFocus = getCurrentFocus();
3151 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3152 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3153 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3154 }
Tony18c4aa42017-05-10 21:23:57 -05003155 if (currentFocus.getTag() instanceof ItemInfo
3156 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003157 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3158 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3159 }
3160 if (!shortcutInfos.isEmpty()) {
3161 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3162 }
3163
3164 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3165 }
3166
3167 @Override
3168 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3169 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3170 switch (keyCode) {
3171 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003172 if (isInState(NORMAL)) {
3173 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003174 return true;
3175 }
3176 break;
3177 case KeyEvent.KEYCODE_S: {
3178 View focusedView = getCurrentFocus();
3179 if (focusedView instanceof BubbleTextView
3180 && focusedView.getTag() instanceof ItemInfo
3181 && mAccessibilityDelegate.performAction(focusedView,
3182 (ItemInfo) focusedView.getTag(),
3183 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003184 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003185 return true;
3186 }
3187 break;
3188 }
3189 case KeyEvent.KEYCODE_O:
3190 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3191 return true;
3192 }
3193 break;
3194 }
3195 }
3196 return super.onKeyShortcut(keyCode, event);
3197 }
3198
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003199 public static Launcher getLauncher(Context context) {
3200 if (context instanceof Launcher) {
3201 return (Launcher) context;
3202 }
3203 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3204 }
3205
Sunny Goyal5a81c382017-03-20 15:08:06 -07003206 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003207
3208 @Override
3209 public void onSharedPreferenceChanged(
3210 SharedPreferences sharedPreferences, String key) {
3211 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003212 // Recreate the activity so that it initializes the rotation preference again.
3213 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003214 }
3215 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003216 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003217
3218 /**
3219 * Callback for listening for onResume
3220 */
3221 public interface OnResumeCallback {
3222
3223 void onLauncherResume();
3224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225}