blob: dee44aa684da9d784bc70286bc112ef4d85099b4 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
33import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
34
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070053import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040054import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070056import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070057import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070058import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070059import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070065import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020066import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080069import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070070import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070071import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070072import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070075import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070077import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070078import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.HapticFeedbackConstants;
81import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070082import android.view.KeyboardShortcutGroup;
83import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080084import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080088import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070097import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070098import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700100import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700103import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700108import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700109import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800110import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700111import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700114import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700115import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700116import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700120import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800121import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800122import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700123import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700124import com.android.launcher3.states.AllAppsState;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800125import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530126import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
127import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
128import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700129import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700130import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700131import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700132import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700133import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700134import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800135import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700136import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700137import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700138import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700139import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700140import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700141import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700142import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700143import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800144import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700145import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800146import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700147import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700148import com.android.launcher3.widget.WidgetListRowEntry;
149import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700150import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700151
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700152import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700153import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700155import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700156import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700157import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800158import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700159import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161/**
162 * Default launcher application.
163 */
Sunny Goyal27835952017-01-13 12:15:53 -0800164public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700165 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
166 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700167 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700168 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700169 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chunga2413752012-04-03 14:22:34 -0700171 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700175
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700177 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Michael Jurka8b805b12012-04-18 14:23:14 -0700179 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700180 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
181 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700182
Jon Mirandac476d6e2017-05-04 16:30:01 -0700183 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700184
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700185 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
186
Mathew Inwood876a8462013-06-14 14:12:41 +0100187 /**
188 * IntentStarter uses request codes starting with this. This must be greater than all activity
189 * request codes used internally.
190 */
191 protected static final int REQUEST_LAST = 100;
192
Winson Chung2672ff92012-05-04 16:22:30 -0700193 // The Intent extra that defines whether to ignore the launch animation
194 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400195 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 // Type: int
198 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700199 // Type: int
200 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700201 // Type: PendingRequestArgs
202 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
203 // Type: ActivityResultInfo
204 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700205 // Type: SparseArray<Parcelable>
206 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700208 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Winson Chunga2413752012-04-03 14:22:34 -0700214 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700215 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
216 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
217 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700218
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700223
224 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700225
Sunny Goyalffe83f12014-08-14 17:39:34 -0700226 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700229 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700230
Sunny Goyal316490e2015-06-02 09:38:28 -0700231 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800232 private ViewGroup mOverviewPanel;
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
Jon Miranda6bed3502017-09-19 14:43:17 -0700242 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
243 // that results in widgets being inflated in the wrong orientation.
244 private int mOrientation;
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private SpannableStringBuilder mDefaultKeySsb = null;
247
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400249
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800250 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700251 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700253 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700254 private OnResumeCallback mOnResumeCallback;
255
Sunny Goyal527c7d32015-08-28 15:19:36 -0700256 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700257
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800259 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800260 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700261 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700262 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263
Jon Miranda2d89ea82017-05-04 11:47:53 -0700264 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700265 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700266
Tony Wickham010d2552017-01-20 08:15:28 -0800267 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700268
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700269 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700270
Winson Chung46353de2012-02-16 14:05:10 -0800271 // We only want to get the SharedPreferences once since it does an FS stat each time we get
272 // it from the context.
273 private SharedPreferences mSharedPrefs;
274
Sunny Goyal2100c782016-08-22 16:00:03 -0700275 // Activity result which needs to be processed after workspace has loaded.
276 private ActivityResultInfo mPendingActivityResult;
277 /**
278 * Holds extra information required to handle a result from an external call, like
279 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
280 */
281 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800282
Jon Miranda379198e2016-09-23 08:54:40 -0700283 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700284
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700285 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700286 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700287 private boolean mAppLaunchSuccess;
288
Sunny Goyal745bad92016-05-02 10:54:12 -0700289 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 @Override
292 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700293 if (DEBUG_STRICT_MODE) {
294 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
295 .detectDiskReads()
296 .detectDiskWrites()
297 .detectNetwork() // or .detectAll() for all detectable problems
298 .penaltyLog()
299 .build());
300 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
301 .detectLeakedSqlLiteObjects()
302 .detectLeakedClosableObjects()
303 .penaltyLog()
304 .penaltyDeath()
305 .build());
306 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700307 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700308
Adam Cohen9211d422014-10-07 18:14:53 -0700309 if (mLauncherCallbacks != null) {
310 mLauncherCallbacks.preOnCreate();
311 }
312
Mario Bertschler27288382017-05-24 15:35:09 -0700313 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
314 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700315 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700316
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700318 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400319
Sunny Goyal87f784c2017-01-11 10:48:34 -0800320 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700321
Adam Cohen2e6da152015-05-06 11:42:25 -0700322 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800323 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700324 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700325 Display display = getWindowManager().getDefaultDisplay();
326 Point mwSize = new Point();
327 display.getSize(mwSize);
328 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
329 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700330
Jon Miranda6bed3502017-09-19 14:43:17 -0700331 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700332 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700333 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800334 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800335 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800336 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700337 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700338
Joe Onorato41a12d22009-10-31 18:30:00 -0400339 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700340 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800341 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700342
Sunny Goyalffe83f12014-08-14 17:39:34 -0700343 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700344
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700345 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700346 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700347
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700348 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
349 // this also ensures that any synchronous binding below doesn't re-trigger another
350 // LauncherModel load.
351 mPaused = false;
352
Sunny Goyal60820d72017-05-09 12:40:11 -0700353 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 setupViews();
Winson1f064272016-07-18 17:18:02 -0700356 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357
Tony Wickham010d2552017-01-20 08:15:28 -0800358 mPopupDataProvider = new PopupDataProvider(this);
359
Sunny Goyalfe770c92016-09-27 14:38:58 -0700360 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361
Sunny Goyal2100c782016-08-22 16:00:03 -0700362 // We only load the page synchronously if the user rotates (or triggers a
363 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700364 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
365 if (savedInstanceState != null) {
366 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
367 }
368 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700369 // If we are not binding synchronously, show a fade in animation when
370 // the first page bind completes.
371 mDragLayer.setAlpha(0);
372 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700373 // Pages bound synchronously.
374 mWorkspace.setCurrentPage(currentScreen);
375
Sunny Goyal2100c782016-08-22 16:00:03 -0700376 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
378
379 // For handling default keys
380 mDefaultKeySsb = new SpannableStringBuilder();
381 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800382
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800383 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700384 // In case we are on a device with locked rotation, we should look at preferences to check
385 // if the user has specifically allowed rotation.
386 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700387 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700388 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
389 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700390 }
391
Sunny Goyal5a81c382017-03-20 15:08:06 -0700392 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
393 // Temporarily enable the rotation
394 mRotationEnabled = true;
395 }
396
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700397 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
398 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700399 setRequestedOrientation(mRotationEnabled
400 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700401
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800402 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700403
Jon Miranda7fda2852017-07-19 16:07:01 -0700404 // Listen for broadcasts
405 IntentFilter filter = new IntentFilter();
406 filter.addAction(Intent.ACTION_SCREEN_OFF);
407 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
408 registerReceiver(mReceiver, filter);
409 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700410
Sunny Goyal8392c822017-06-20 10:03:56 -0700411 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
412 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700413
414 if (mLauncherCallbacks != null) {
415 mLauncherCallbacks.onCreate(savedInstanceState);
416 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700417
418 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700419 }
420
Sunny Goyal7779d622015-06-11 16:18:39 -0700421 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700422 public void onThemeChanged() {
423 recreate();
424 }
425
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700426 public LauncherStateManager getStateManager() {
427 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700428 }
429
Mario Bertschlera6936942017-05-31 14:48:19 -0700430 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700431 if (isDark) {
432 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700433 } else if (supportsDarkText) {
434 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700435 }
436 }
437
438 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700439 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800440 return mLauncherView.findViewById(id);
441 }
442
443 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700444 public void onAppWidgetHostReset() {
445 if (mAppWidgetHost != null) {
446 mAppWidgetHost.startListening();
447 }
448 }
449
Adam Cohen9211d422014-10-07 18:14:53 -0700450 private LauncherCallbacks mLauncherCallbacks;
451
Winson1f064272016-07-18 17:18:02 -0700452 public void onInsetsChanged(Rect insets) {
453 mDeviceProfile.updateInsets(insets);
454 mDeviceProfile.layout(this, true /* notifyListeners */);
455 }
456
Sunny Goyal32554d12015-12-03 15:31:25 -0800457 /**
458 * Call this after onCreate to set or clear overlay.
459 */
460 public void setLauncherOverlay(LauncherOverlay overlay) {
461 if (overlay != null) {
462 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
463 }
464 mWorkspace.setLauncherOverlay(overlay);
465 }
466
Adam Cohen9211d422014-10-07 18:14:53 -0700467 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
468 mLauncherCallbacks = callbacks;
469 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700470 }
471
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700472 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700473 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700474 if (mLauncherCallbacks != null) {
475 mLauncherCallbacks.onLauncherProviderChange();
476 }
477 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700478
Hyunyoung Song3f471442015-04-08 19:01:34 -0700479 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700480 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
481 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700482 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700483 }
484
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000485 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700486 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
487 // This cast is safe as long as the id < 0x00FFFFFF
488 // Since we jail all the dynamically generated views, there should be no clashes
489 // with any other views.
490 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000491 }
492
Tony Wickham010d2552017-01-20 08:15:28 -0800493 public PopupDataProvider getPopupDataProvider() {
494 return mPopupDataProvider;
495 }
496
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000497 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700498 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
499 * a configuration step, this allows the proper animations to run after other transitions.
500 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700501 private long completeAdd(
502 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
503 long screenId = info.screenId;
504 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700505 // When the screen id represents an actual screen (as opposed to a rank) we make sure
506 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700507 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700508 }
Adam Cohendb364c32014-05-20 14:23:40 -0700509
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800511 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700512 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700513 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800514 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700515 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700516 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700517 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700518 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700519 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700520 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700521 int widgetId = appWidgetId;
522 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700523 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700525 // Since the view was just bound, also launch the configure activity if needed
526 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
527 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800528 if (provider != null) {
529 new WidgetAddFlowHandler(provider)
530 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700531 }
532 }
533 break;
534 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800535 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700536
Adam Cohendb364c32014-05-20 14:23:40 -0700537 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800538 }
539
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800540 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700541 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 if (isWorkspaceLoading()) {
543 // process the result once the workspace has loaded.
544 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
545 return;
546 }
547 mPendingActivityResult = null;
548
Winson Chunge341d302013-08-16 14:31:00 -0700549 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700550 final PendingRequestArgs requestArgs = mPendingRequestArgs;
551 setWaitingForResult(null);
552 if (requestArgs == null) {
553 return;
554 }
555
556 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700557
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 Runnable exitSpringLoaded = new Runnable() {
559 @Override
560 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700561 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 }
563 };
564
Michael Jurka8b805b12012-04-18 14:23:14 -0700565 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700566 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700567 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
569 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700571 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700572 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 addAppWidgetImpl(
575 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800576 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700577 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700578 }
579 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200580 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700581 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700582 // User could have free-scrolled between pages before picking a wallpaper; make sure
583 // we move to the closest one now.
584 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700585 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200586 }
587 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200589
Adam Cohened66b2b2012-01-23 17:28:51 -0800590 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700591 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700592
Adam Cohened66b2b2012-01-23 17:28:51 -0800593 // We have special handling for widgets
594 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800595 final int appWidgetId;
596 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
597 : -1;
598 if (widgetId < 0) {
599 appWidgetId = pendingAddWidgetId;
600 } else {
601 appWidgetId = widgetId;
602 }
603
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800605 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700606 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
607 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700608 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700610 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700611 @Override
612 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700613 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700614 }
615 };
Adam Cohendb364c32014-05-20 14:23:40 -0700616
Sunny Goyal2100c782016-08-22 16:00:03 -0700617 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
618 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
619 } else {
620 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
621 // When the screen id represents an actual screen (as opposed to a rank)
622 // we make sure that the drop page actually exists.
623 requestArgs.screenId =
624 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700625 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700626 final CellLayout dropLayout =
627 mWorkspace.getScreenWithId(requestArgs.screenId);
628
629 dropLayout.setDropPending(true);
630 final Runnable onComplete = new Runnable() {
631 @Override
632 public void run() {
633 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
634 dropLayout.setDropPending(false);
635 }
636 };
637 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
638 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700639 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 return;
641 }
642
Sunny Goyald478c832016-04-01 12:04:16 -0700643 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
644 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700645 if (resultCode == RESULT_OK) {
646 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700647 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700648 }
649 // Leave the widget in the pending state if the user canceled the configure.
650 return;
651 }
652
Sunny Goyalaad90582015-07-09 14:22:50 -0700653 if (requestCode == REQUEST_CREATE_SHORTCUT) {
654 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
656 completeAdd(requestCode, data, -1, requestArgs);
657 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
658 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
659
Sunny Goyalaad90582015-07-09 14:22:50 -0700660 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700661 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
662 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800665 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800666 }
667
668 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700669 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800670 final int requestCode, final int resultCode, final Intent data) {
671 handleActivityResult(requestCode, resultCode, data);
672 if (mLauncherCallbacks != null) {
673 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
674 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 }
676
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700677 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700678 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600679 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700680 PendingRequestArgs pendingArgs = mPendingRequestArgs;
681 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
682 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
683 setWaitingForResult(null);
684
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700687 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700689 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 Intent intent = pendingArgs.getPendingIntent();
691
Sunny Goyal28c6b962015-10-12 11:42:05 -0700692 if (grantResults.length > 0
693 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700694 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700695 } else {
696 // TODO: Show a snack bar with link to settings
697 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700698 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 }
700 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600701 if (mLauncherCallbacks != null) {
702 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
703 grantResults);
704 }
705 }
706
Adam Cohendb364c32014-05-20 14:23:40 -0700707 /**
708 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
709 *
710 * @param screenId the screen id to check
711 * @return the new screen, or screenId if it exists
712 */
713 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800714 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700715 if (dropLayout == null) {
716 // it's possible that the add screen was removed because it was
717 // empty and a re-bind occurred
718 mWorkspace.addExtraEmptyScreen();
719 return mWorkspace.commitExtraEmptyScreen();
720 } else {
721 return screenId;
722 }
723 }
724
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 @Thunk void completeTwoStageWidgetDrop(
726 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
727 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 Runnable onCompleteRunnable = null;
729 int animationType = 0;
730
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700731 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 if (resultCode == RESULT_OK) {
733 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
734 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800735 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700736 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 onCompleteRunnable = new Runnable() {
738 @Override
739 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700740 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700741 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 }
743 };
744 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800745 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700748 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700749 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700750 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
751 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700753 // The animated view may be null in the case of a rotation during widget configuration
754 onCompleteRunnable.run();
755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
757
758 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700759 protected void onStop() {
760 super.onStop();
761 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700762
763 if (mLauncherCallbacks != null) {
764 mLauncherCallbacks.onStop();
765 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700766
Sunny Goyalf5e37442016-11-02 10:31:24 -0700767 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700768 mAppWidgetHost.stopListening();
769 }
Tony Wickham010d2552017-01-20 08:15:28 -0800770
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700771 if (!mAppLaunchSuccess) {
772 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700773 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700774 }
Tony Wickham010d2552017-01-20 08:15:28 -0800775 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700776 }
777
778 @Override
779 protected void onStart() {
780 super.onStart();
781 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700782
783 if (mLauncherCallbacks != null) {
784 mLauncherCallbacks.onStart();
785 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700786
Sunny Goyalf5e37442016-11-02 10:31:24 -0700787 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700788 mAppWidgetHost.startListening();
789 }
Tony Wickham010d2552017-01-20 08:15:28 -0800790
791 if (!isWorkspaceLoading()) {
792 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
793 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700794
Jon Miranda7fda2852017-07-19 16:07:01 -0700795 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700796 if (mScrimAnimator != null) {
797 mScrimAnimator.cancel();
798 }
799 mDragLayer.getBackground().setAlpha(0);
800 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
801 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
802 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
803 @Override
804 public void onAnimationEnd(Animator animation) {
805 mScrimAnimator = null;
806 }
807 });
808 mScrimAnimator.setDuration(600);
809 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
810 mScrimAnimator.start();
811 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700812 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700813 }
814
815 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700817 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700819 TraceHelper.partitionSection("ON_RESUME", "superCall");
820
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700821 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700822 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800823 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700824 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700825 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700826 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700827 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700829 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200830 // We might have postponed some bind calls until onResume (see waitUntilResume) --
831 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700832 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
833 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200834 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700835 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200836 }
Winson Chunge4e50662012-01-23 14:45:13 -0800837
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700838 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700839 // Process any items that were added while Launcher was away.
840 InstallShortcutReceiver.disableAndFlushInstallQueue(
841 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700842
Sunny Goyala474a9b2017-05-04 16:47:11 -0700843 // Refresh shortcuts if the permission changed.
844 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700845
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700846 if (shouldShowDiscoveryBounce()) {
847 mAllAppsController.showDiscoveryBounce();
848 }
Adam Cohen9211d422014-10-07 18:14:53 -0700849 if (mLauncherCallbacks != null) {
850 mLauncherCallbacks.onResume();
851 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800852
Sunny Goyala502aa32017-10-02 16:04:06 -0700853 clearTypedText();
854
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700855 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700859 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700860 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700861 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700862
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700863 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700864 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800865 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700866 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700867
Adam Cohen9211d422014-10-07 18:14:53 -0700868 if (mLauncherCallbacks != null) {
869 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700870 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700871 }
872
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873 public interface LauncherOverlay {
874
875 /**
876 * Touch interaction leading to overscroll has begun
877 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700878 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700879
880 /**
881 * Touch interaction related to overscroll has ended
882 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700883 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884
885 /**
886 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
887 * screen (or in the case of RTL, the rightmost screen).
888 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700889 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890
891 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800892 * Called when the launcher is ready to use the overlay
893 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700895 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700896 }
897
898 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700899 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900 }
901
902 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
903
Sunny Goyalc86df472016-02-25 09:19:38 -0800904 public void onScrollChanged(float progress) {
905 if (mWorkspace != null) {
906 mWorkspace.onOverlayScrollChanged(progress);
907 }
908 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700909 }
910
Sunny Goyal16764582017-11-06 16:00:34 -0800911 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700912 if (mLauncherCallbacks != null) {
913 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700914 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800915 // On O and above we there is always some setting present settings (add icon to
916 // home screen or icon badging). On earlier APIs we will have the allow rotation
917 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700918 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700919 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800920 }
921
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700922 @Override
923 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700925 if (mModel.isCurrentCallbacks(this)) {
926 mModel.stopLoader();
927 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700928 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
929
Romain Guy13c2e7b2010-03-10 19:45:00 -0800930 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700931 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700932
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800933 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800934 @Override
935 public void onWindowFocusChanged(boolean hasFocus) {
936 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700937 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700938
939 if (mLauncherCallbacks != null) {
940 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
941 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800942 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 private boolean acceptFilter() {
945 final InputMethodManager inputManager = (InputMethodManager)
946 getSystemService(Context.INPUT_METHOD_SERVICE);
947 return !inputManager.isFullscreenMode();
948 }
949
950 @Override
951 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700952 final int uniChar = event.getUnicodeChar();
953 final boolean handled = super.onKeyDown(keyCode, event);
954 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
955 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
957 keyCode, event);
958 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700959 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700960 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 // showSearchDialog()
962 // If there are multiple keystrokes before the search dialog takes focus,
963 // onSearchRequested() will be called for every keystroke,
964 // but it is idempotent, so it's fine.
965 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 }
967 }
968
Joe Onorato8a9625e2010-01-28 15:55:35 -0800969 // Eat the long press event so the keyboard doesn't come up.
970 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
971 return true;
972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 return handled;
975 }
976
Karl Rosaen138a0412009-04-23 19:00:21 -0700977 private String getTypedText() {
978 return mDefaultKeySsb.toString();
979 }
980
Sunny Goyal6f28e712016-09-13 14:19:29 -0700981 @Override
982 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700983 mDefaultKeySsb.clear();
984 mDefaultKeySsb.clearSpans();
985 Selection.setSelection(mDefaultKeySsb, 0);
986 }
987
Sunny Goyalf9403d92017-10-18 10:55:56 -0700988 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700989 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700990 }
991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 /**
993 * Restores the previous state, if it exists.
994 *
995 * @param savedState The previous state.
996 */
997 private void restoreState(Bundle savedState) {
998 if (savedState == null) {
999 return;
1000 }
1001
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001002 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001003 LauncherState[] stateValues = LauncherState.values();
1004 LauncherState state = stateValues[stateOrdinal];
1005 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001006 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001007 }
1008
Sunny Goyal2100c782016-08-22 16:00:03 -07001009 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1010 if (requestArgs != null) {
1011 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001013
1014 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001015
1016 SparseArray<Parcelable> widgetsState =
1017 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1018 if (widgetsState != null) {
1019 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
1022
1023 /**
1024 * Finds all the views we need and configure them properly.
1025 */
1026 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001027 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001028 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001029 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001030 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001031 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001032
Sunny Goyal784f9c32016-03-23 16:56:54 -07001033 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1034 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1035 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036
Winson Chung4c98d922011-05-31 16:50:48 -07001037 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001038 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001039
Winson Chung3d503fb2011-07-13 17:25:49 -07001040 // Setup the hotseat
1041 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1042 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001043 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001044 }
1045
Winson Chung4c98d922011-05-31 16:50:48 -07001046 // Setup the workspace
1047 mWorkspace.setHapticFeedbackEnabled(false);
1048 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001049 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001050 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1051 // default state, otherwise we will update to the wrong offsets in RTL
1052 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001053 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001054 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001055
Tony Wickham34d2c912015-09-11 09:27:58 -07001056 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001057 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001058
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001059 // Setup Apps
1060 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001061
Winson Chung3d503fb2011-07-13 17:25:49 -07001062 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001063 mDragController.setMoveTarget(mWorkspace);
1064 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001065 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001066
Hyunyoung Songd725f642017-08-17 22:26:35 -07001067 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 }
1069
1070 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001071 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001072 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001073 */
1074 public void setAllAppsButton(View allAppsButton) {
1075 mAllAppsButton = allAppsButton;
1076 }
1077
Anjali Koppal5ad44842014-03-10 20:34:39 -07001078 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 * Creates a view representing a shortcut.
1080 *
1081 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001083 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001084 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
1086
1087 /**
1088 * Creates a view representing a shortcut inflated from the specified resource.
1089 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 * @param parent The group the shortcut belongs to.
1091 * @param info The data structure describing the shortcut.
1092 *
1093 * @return A View inflated from layoutResId.
1094 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001095 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001096 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1097 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001098 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001100 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 return favorite;
1102 }
1103
1104 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001105 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 *
1107 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001109 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001110 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001111 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1112 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001113 return;
1114 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001115
Jon Mirandac476d6e2017-05-04 16:30:01 -07001116 int[] cellXY = mTmpAddItemCellCoordinates;
1117 CellLayout layout = getCellLayout(container, screenId);
1118
Sunny Goyal782f0c92017-01-19 10:27:54 -08001119 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001120 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001121 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1122 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001123 }
1124
1125 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001126 // Legacy shortcuts are only supported for primary profile.
1127 info = Process.myUserHandle().equals(args.user)
1128 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001129
Sunny Goyalb57645f2017-03-20 13:57:28 -07001130 if (info == null) {
1131 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1132 return;
1133 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001134 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001135 // The app is trying to add a shortcut without sufficient permissions
1136 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1137 return;
1138 }
1139 }
1140
Jon Mirandac476d6e2017-05-04 16:30:01 -07001141 if (container < 0) {
1142 // Adding a shortcut to the Workspace.
1143 final View view = createShortcut(info);
1144 boolean foundCellSpan = false;
1145 // First we check if we already know the exact location where we want to add this item.
1146 if (cellX >= 0 && cellY >= 0) {
1147 cellXY[0] = cellX;
1148 cellXY[1] = cellY;
1149 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001150
Jon Mirandac476d6e2017-05-04 16:30:01 -07001151 // If appropriate, either create a folder or add to an existing folder
1152 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001153 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001154 return;
1155 }
1156 DragObject dragObject = new DragObject();
1157 dragObject.dragInfo = info;
1158 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1159 true)) {
1160 return;
1161 }
1162 } else {
1163 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1164 }
1165
1166 if (!foundCellSpan) {
1167 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001168 return;
1169 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001170
1171 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1172 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001173 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001174 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001175 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001176 if (folderIcon != null) {
1177 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1178 folderInfo.add(info, args.rank, false);
1179 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001180 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001181 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 }
1184
Sunny Goyale29897f52017-07-20 10:09:42 -07001185 public FolderIcon findFolderIcon(final long folderIconId) {
1186 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1187 @Override
1188 public boolean evaluate(ItemInfo info, View view) {
1189 return info != null && info.id == folderIconId;
1190 }
1191 });
1192 }
1193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001197 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001199 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001200 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1201
Adam Cohened66b2b2012-01-23 17:28:51 -08001202 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001203 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001204 }
Romain Guycbb89e42009-06-08 15:52:54 -07001205
Adam Cohen59400422014-03-05 18:07:04 -08001206 LauncherAppWidgetInfo launcherInfo;
1207 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001208 launcherInfo.spanX = itemInfo.spanX;
1209 launcherInfo.spanY = itemInfo.spanY;
1210 launcherInfo.minSpanX = itemInfo.minSpanX;
1211 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001212 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001213
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001214 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001215 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
Sunny Goyal2100c782016-08-22 16:00:03 -07001217 if (hostView == null) {
1218 // Perform actual inflation because we're live
1219 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001221 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301222 prepareAppWidget(hostView, launcherInfo);
1223 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
Romain Guycbb89e42009-06-08 15:52:54 -07001225
Sunny Goyald5462aa2016-11-22 16:52:39 +05301226 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001227 hostView.setTag(item);
1228 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001229 hostView.setFocusable(true);
1230 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001231 }
1232
Adam Cohended9f8d2010-11-03 13:25:16 -07001233 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1234 @Override
1235 public void onReceive(Context context, Intent intent) {
1236 final String action = intent.getAction();
1237 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001238 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001239 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001240 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001241 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001242 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001243 mShouldFadeInScrim = true;
1244 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1245 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1246 // the user unlocked and the Launcher is not in the foreground.
1247 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001248 }
1249 }
1250 };
1251
Tony Wickham010d2552017-01-20 08:15:28 -08001252 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1253 Runnable r = new Runnable() {
1254 @Override
1255 public void run() {
1256 mWorkspace.updateIconBadges(updatedBadges);
1257 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001258
Sunny Goyal00ac9202017-11-09 15:24:06 -08001259 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1260 if (popup != null) {
1261 popup.updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001262 }
Tony Wickham010d2552017-01-20 08:15:28 -08001263 }
1264 };
1265 if (!waitUntilResume(r)) {
1266 r.run();
1267 }
1268 }
1269
Adam Cohended9f8d2010-11-03 13:25:16 -07001270 @Override
1271 public void onAttachedToWindow() {
1272 super.onAttachedToWindow();
1273
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001274 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001275 if (mLauncherCallbacks != null) {
1276 mLauncherCallbacks.onAttachedToWindow();
1277 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001278 }
1279
1280 @Override
1281 public void onDetachedFromWindow() {
1282 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001283
1284 if (mLauncherCallbacks != null) {
1285 mLauncherCallbacks.onDetachedFromWindow();
1286 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001287 }
1288
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001289 public AllAppsTransitionController getAllAppsController() {
1290 return mAllAppsController;
1291 }
1292
Winson Chunga6945242014-01-08 14:04:34 -08001293 public DragLayer getDragLayer() {
1294 return mDragLayer;
1295 }
1296
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001297 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001298 return mAppsView;
1299 }
1300
Winson Chunga6945242014-01-08 14:04:34 -08001301 public Workspace getWorkspace() {
1302 return mWorkspace;
1303 }
1304
1305 public Hotseat getHotseat() {
1306 return mHotseat;
1307 }
1308
Jorim Jaggid017f882014-01-14 17:08:48 -08001309 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001310 return mOverviewPanel;
1311 }
1312
Sunny Goyal47328fd2016-05-25 18:56:41 -07001313 public DropTargetBar getDropTargetBar() {
1314 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001315 }
1316
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001317 public LauncherAppWidgetHost getAppWidgetHost() {
1318 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
Romain Guycbb89e42009-06-08 15:52:54 -07001320
Winson Chunga9abd0e2010-10-27 17:18:37 -07001321 public LauncherModel getModel() {
1322 return mModel;
1323 }
1324
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001325 public ModelWriter getModelWriter() {
1326 return mModelWriter;
1327 }
1328
Adam Cohen79d90c52016-04-22 13:29:20 -07001329 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001330 return mSharedPrefs;
1331 }
1332
Jon Miranda6bed3502017-09-19 14:43:17 -07001333 public int getOrientation() { return mOrientation; }
1334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 @Override
1336 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001337 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 super.onNewIntent(intent);
1339
Winson15f8b172015-08-19 11:02:39 -07001340 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1341 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1342 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001343
1344 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001345 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001346 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001347
Winson15f8b172015-08-19 11:02:39 -07001348 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1349 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001350 if (mWorkspace == null) {
1351 // Can be cases where mWorkspace is null, this prevents a NPE
1352 return;
1353 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001354
1355 // Note: There should be at most one log per method call. This is enforced implicitly
1356 // by using if-else statements.
1357 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001358 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001359 if (topOpenView != null) {
1360 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001361 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001362 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001363 target.pageIndex = mWorkspace.getCurrentPage();
1364 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001365 }
1366
1367 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001368 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001369 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001370
1371 final View v = getWindow().peekDecorView();
1372 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001373 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001374 }
1375
Winson Chungb745afb2015-03-02 11:51:23 -08001376 // Reset the apps view
1377 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001378 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001379 }
1380
Adam Cohen9211d422014-10-07 18:14:53 -07001381 if (mLauncherCallbacks != null) {
1382 mLauncherCallbacks.onHomeIntent();
1383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001385 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001386
Adam Cohen9211d422014-10-07 18:14:53 -07001387 if (mLauncherCallbacks != null) {
1388 mLauncherCallbacks.onNewIntent(intent);
1389 }
Winson15f8b172015-08-19 11:02:39 -07001390
1391 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1392 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1393 // animation.
1394 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001395 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001396
Winson15f8b172015-08-19 11:02:39 -07001397 mWorkspace.post(new Runnable() {
1398 @Override
1399 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001400 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001401 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001402 }
Winson15f8b172015-08-19 11:02:39 -07001403 }
1404 });
1405 }
1406 }
1407
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001408 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001409 }
1410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001412 public void onRestoreInstanceState(Bundle state) {
1413 super.onRestoreInstanceState(state);
1414 for (int page: mSynchronouslyBoundPages) {
1415 mWorkspace.restoreInstanceStateForChild(page);
1416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418
1419 @Override
1420 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001421 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001422 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001423
Adam Cohen21cd0022013-10-09 18:57:02 -07001424 }
Sunny Goyalea609262017-10-25 15:47:38 -07001425 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001426
1427
1428 AbstractFloatingView widgets = AbstractFloatingView
1429 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1430 if (widgets != null) {
1431 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1432 widgets.saveHierarchyState(widgetsState);
1433 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1434 } else {
1435 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1436 }
1437
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001438 // We close any open folders and shortcut containers since they will not be re-opened,
1439 // and we need to make sure this state is reflected.
1440 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001441
Sunny Goyal2100c782016-08-22 16:00:03 -07001442 if (mPendingRequestArgs != null) {
1443 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1444 }
1445 if (mPendingActivityResult != null) {
1446 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001449 super.onSaveInstanceState(outState);
1450
Adam Cohen9211d422014-10-07 18:14:53 -07001451 if (mLauncherCallbacks != null) {
1452 mLauncherCallbacks.onSaveInstanceState(outState);
1453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 }
1455
1456 @Override
1457 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001460 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001461 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001462
Winson Chungcd2b0142011-06-08 16:02:26 -07001463 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001464 // It's possible to receive onDestroy after a new Launcher activity has
1465 // been created. In this case, don't interfere with the new Launcher.
1466 if (mModel.isCurrentCallbacks(this)) {
1467 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001468 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001469 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001470
Sunny Goyal745bad92016-05-02 10:54:12 -07001471 if (mRotationPrefChangeHandler != null) {
1472 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1473 }
1474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001476 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001478 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001480 mAppWidgetHost = null;
1481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001483 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1484
Michael Jurka2ecf9952012-06-18 12:52:28 -07001485 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001486
Tony2917a8b2017-07-31 23:29:42 -07001487 clearPendingBinds();
1488
Adam Cohen9211d422014-10-07 18:14:53 -07001489 if (mLauncherCallbacks != null) {
1490 mLauncherCallbacks.onDestroy();
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
Sunny Goyalae502842016-06-17 08:43:56 -07001494 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1495 return mAccessibilityDelegate;
1496 }
1497
Adam Cohena9cf38f2011-05-02 15:36:58 -07001498 public DragController getDragController() {
1499 return mDragController;
1500 }
1501
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001503 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001504 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001505 }
1506
1507 @Override
1508 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1509 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001510 try {
1511 super.startIntentSenderForResult(intent, requestCode,
1512 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1513 } catch (IntentSender.SendIntentException e) {
1514 throw new ActivityNotFoundException();
1515 }
1516 }
1517
Winson Chung70d72102011-08-12 11:24:35 -07001518 /**
1519 * Indicates that we want global search for this activity by setting the globalSearch
1520 * argument for {@link #startSearch} to true.
1521 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001523 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001525
Karl Rosaen138a0412009-04-23 19:00:21 -07001526 if (initialQuery == null) {
1527 // Use any text typed in the launcher as the initial query
1528 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 if (appSearchData == null) {
1531 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001532 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001534
Sunny Goyal6f28e712016-09-13 14:19:29 -07001535 if (mLauncherCallbacks == null ||
1536 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1537 // Starting search from the callbacks failed. Start the default global search.
1538 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001539 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001540
1541 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001542 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001543 }
1544
Ian Parkinson047036e2014-09-01 15:40:53 +01001545 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001546 * Starts the global search activity. This code is a copied from SearchManager
1547 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001548 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001549 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001550 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001551 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1552 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1553 if (globalSearchActivity == null) {
1554 Log.w(TAG, "No global search activity found.");
1555 return;
1556 }
1557 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1558 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1559 intent.setComponent(globalSearchActivity);
1560 // Make sure that we have a Bundle to put source in
1561 if (appSearchData == null) {
1562 appSearchData = new Bundle();
1563 } else {
1564 appSearchData = new Bundle(appSearchData);
1565 }
Adam Cohen9211d422014-10-07 18:14:53 -07001566 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001567 if (!appSearchData.containsKey("source")) {
1568 appSearchData.putString("source", getPackageName());
1569 }
1570 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1571 if (!TextUtils.isEmpty(initialQuery)) {
1572 intent.putExtra(SearchManager.QUERY, initialQuery);
1573 }
1574 if (selectInitialQuery) {
1575 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1576 }
1577 intent.setSourceBounds(sourceBounds);
1578 try {
1579 startActivity(intent);
1580 } catch (ActivityNotFoundException ex) {
1581 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584
Winson Chung70d72102011-08-12 11:24:35 -07001585 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001586 public boolean onPrepareOptionsMenu(Menu menu) {
1587 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001588 if (mLauncherCallbacks != null) {
1589 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1590 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001591 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001594 @Override
1595 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001596 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001597 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001598 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001599 }
1600
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001602 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603 }
1604
Adam Cohen517a7f52014-03-01 12:12:59 -08001605 public boolean isWorkspaceLoading() {
1606 return mWorkspaceLoading;
1607 }
1608
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001609 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001610 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001611 }
1612
Sunny Goyal04a324a2017-01-20 21:08:59 -08001613 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001614 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001615 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001616
Sunny Goyal2100c782016-08-22 16:00:03 -07001617 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001618 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001619 if (LOGD) {
1620 Log.d(TAG, "Adding widget from drop");
1621 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001622 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001623 }
1624
Sunny Goyal2100c782016-08-22 16:00:03 -07001625 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001626 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1627 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1628 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001629
Adam Cohenad4e15c2013-10-17 16:21:35 -07001630 Runnable onComplete = new Runnable() {
1631 @Override
1632 public void run() {
1633 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001634 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001635 }
1636 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001637 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001638 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
1640 }
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001642 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1643 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001644 info.container = container;
1645 info.screenId = screenId;
1646 if (cell != null) {
1647 info.cellX = cell[0];
1648 info.cellY = cell[1];
1649 }
1650 info.spanX = spanX;
1651 info.spanY = spanY;
1652
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001653 switch (info.itemType) {
1654 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1655 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001656 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001657 break;
1658 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001659 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001660 break;
1661 default:
1662 throw new IllegalStateException("Unknown item type: " + info.itemType);
1663 }
1664 }
1665
Adam Cohenfbba09b2011-07-18 21:43:05 -07001666 /**
1667 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001668 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001669 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001670 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1671 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001672 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1673 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1674 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001675 }
1676
Adam Cohenfbba09b2011-07-18 21:43:05 -07001677 /**
1678 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001679 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001680 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001681 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001682 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001683 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001684 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001685 // In the case where we've prebound the widget, we remove it from the DragLayer
1686 if (LOGD) {
1687 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1688 }
1689 getDragLayer().removeView(hostView);
1690
Adam Cohened66b2b2012-01-23 17:28:51 -08001691 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001692 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001693
1694 // Clear the boundWidget so that it doesn't get destroyed.
1695 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001696 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001697 // In this case, we either need to start an activity to get permission to bind
1698 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001699 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1700 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1701 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1702 this, info.componentName);
1703 } else {
1704 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1705 }
Adam Cohendd70d662012-10-04 16:53:44 -07001706 Bundle options = info.bindOptions;
1707
Sunny Goyalffe83f12014-08-14 17:39:34 -07001708 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1709 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001710 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001711 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001712 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001713 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001714 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001715 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001716 }
1717
Adam Cohendcd297f2013-06-18 13:13:40 -07001718 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001719 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001720 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 folderInfo.title = getText(R.string.folder_name);
1722
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001724 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725
1726 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001727 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301728 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001729 // Force measure the new folder icon
1730 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1731 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001732 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 }
Romain Guycbb89e42009-06-08 15:52:54 -07001734
Winsonc0b52fe2015-09-09 16:38:15 -07001735 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001736 * Unbinds the view for the specified item, and removes the item and all its children.
1737 *
1738 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001739 * @param itemInfo the {@link ItemInfo} for this view.
1740 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001741 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001742 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001743 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001744 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001745 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1746 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001747 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001748 } else {
1749 mWorkspace.removeWorkspaceItem(v);
1750 }
Winsonc0b52fe2015-09-09 16:38:15 -07001751 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001752 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001753 }
1754 } else if (itemInfo instanceof FolderInfo) {
1755 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001756 if (v instanceof FolderIcon) {
1757 ((FolderIcon) v).removeListeners();
1758 }
Winsonc0b52fe2015-09-09 16:38:15 -07001759 mWorkspace.removeWorkspaceItem(v);
1760 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001761 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001762 }
1763 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1764 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001765 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001766 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001767 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001768 }
1769 } else {
1770 return false;
1771 }
1772 return true;
1773 }
1774
1775 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001776 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001777 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001778 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001779 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001780 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001781 // Deleting an app widget ID is a void call but writes to disk before returning
1782 // to the caller...
1783 new AsyncTask<Void, Void, Void>() {
1784 public Void doInBackground(Void ... args) {
1785 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1786 return null;
1787 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001788 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001789 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001790 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001791 }
1792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 @Override
1794 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001795 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797
Joe Onorato88ec0992009-11-19 13:16:06 -08001798 @Override
1799 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001800 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1801 return;
1802 }
1803
Sunny Goyal45478022015-06-08 16:52:41 -07001804 if (mDragController.isDragging()) {
1805 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001806 return;
1807 }
1808
Jon Mirandafeba90f2016-10-06 10:53:29 -07001809 // Note: There should be at most one log per method call. This is enforced implicitly
1810 // by using if-else statements.
1811 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001812 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1813 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001814 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001815 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001816 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001817 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001818 } else {
1819 // Back button is a no-op here, but give at least some feedback for the button press
1820 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001821 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001822 }
1823
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 * Launches the intent referred by the clicked shortcut.
1826 *
1827 * @param v The view representing the clicked shortcut.
1828 */
1829 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001830 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1831 // view has detached (it's possible for this to happen if the view is removed mid touch).
1832 if (v.getWindowToken() == null) {
1833 return;
1834 }
1835
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001836 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001837 return;
1838 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001839
Adam Cohen1697b792013-09-17 19:08:21 -07001840 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001841 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001842 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1843 LauncherLogProto.Action.Direction.NONE,
1844 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001845 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001846 }
1847 return;
1848 }
1849
1850 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001851 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001852 int page = mWorkspace.indexOfChild(v);
1853 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1854 LauncherLogProto.Action.Direction.NONE,
1855 LauncherLogProto.ContainerType.OVERVIEW, page);
1856 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001857 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001858 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001859 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001860 }
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001863 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001864 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001866 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001867 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001868 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001869 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001870 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001871 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001872 } else if (tag instanceof AppInfo) {
1873 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001874 } else if (tag instanceof LauncherAppWidgetInfo) {
1875 if (v instanceof PendingAppWidgetHostView) {
1876 onClickPendingWidget((PendingAppWidgetHostView) v);
1877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
1879 }
1880
Sunny Goyal70660032015-05-14 00:07:08 -07001881 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001882 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001883 return false;
1884 }
1885
Michael Jurkaaf442092010-06-10 17:01:57 -07001886 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001887 * Event handler for the app widget view which has not fully restored.
1888 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001889 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001890 if (mIsSafeModeEnabled) {
1891 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1892 return;
1893 }
1894
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001895 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001896 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001897 LauncherAppWidgetProviderInfo appWidgetInfo =
1898 mAppWidgetManager.findProvider(info.providerName, info.user);
1899 if (appWidgetInfo == null) {
1900 return;
1901 }
1902 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1903
Sunny Goyald478c832016-04-01 12:04:16 -07001904 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1905 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1906 // This should not happen, as we make sure that an Id is allocated during bind.
1907 return;
1908 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001909 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1910 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001911 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001912 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001913 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001914 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001915 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001916 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001917 }
1918 }
1919
1920 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001921 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1922 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1923 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001924 *
1925 * @param v The view that was clicked.
1926 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001927 protected void onClickAllAppsButton(View v) {
1928 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001929 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301930 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1931 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001932 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001933 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001934 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001935 }
1936 }
1937
Sunny Goyale6e72002017-01-12 16:55:36 -08001938 private void onClickPendingAppItem(final View v, final String packageName,
1939 boolean downloadStarted) {
1940 if (downloadStarted) {
1941 // If the download has started, simply direct to the market app.
1942 startMarketIntentForPackage(v, packageName);
1943 return;
1944 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001945 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001946 .setTitle(R.string.abandoned_promises_title)
1947 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001948 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1949 @Override
1950 public void onClick(DialogInterface dialogInterface, int i) {
1951 startMarketIntentForPackage(v, packageName);
1952 }
1953 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001954 .setNeutralButton(R.string.abandoned_clean_this,
1955 new DialogInterface.OnClickListener() {
1956 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001957 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001958 mWorkspace.removeAbandonedPromise(packageName, user);
1959 }
1960 })
1961 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001962 }
1963
1964 private void startMarketIntentForPackage(View v, String packageName) {
1965 ItemInfo item = (ItemInfo) v.getTag();
1966 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001967 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001968 }
1969
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001970 /**
1971 * Event handler for an app shortcut click.
1972 *
1973 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1974 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001975 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001976 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1977 Object tag = v.getTag();
1978 if (!(tag instanceof ShortcutInfo)) {
1979 throw new IllegalArgumentException("Input must be a Shortcut");
1980 }
1981
1982 // Open shortcut
1983 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001984
Sunny Goyal076839c2017-10-30 13:52:20 -07001985 if (shortcut.isDisabled()) {
1986 if ((shortcut.runtimeStatusFlags &
1987 ~FLAG_DISABLED_SUSPENDED &
1988 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001989 // If the app is only disabled because of the above flags, launch activity anyway.
1990 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001991 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001992 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1993 // Use a message specific to this shortcut, if it has one.
1994 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1995 return;
1996 }
1997 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001998 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001999 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002000 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07002001 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2002 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002003 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002004 }
2005 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2006 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002007 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002008 }
2009
Chris Wren40c5ed32014-06-24 18:24:23 -04002010 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002011 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002012 String packageName = shortcut.intent.getComponent() != null ?
2013 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2014 if (!TextUtils.isEmpty(packageName)) {
2015 onClickPendingAppItem(v, packageName,
2016 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2017 return;
2018 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002019 }
2020
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002021 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002022 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002023 }
2024
Sunny Goyala7ce1662016-05-31 15:01:35 -07002025 private void startAppShortcutOrInfoActivity(View v) {
2026 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002027 Intent intent;
2028 if (item instanceof PromiseAppInfo) {
2029 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2030 intent = promiseAppInfo.getMarketIntent();
2031 } else {
2032 intent = item.getIntent();
2033 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002034 if (intent == null) {
2035 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002036 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002037 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002038 }
2039
2040 /**
2041 * Event handler for a folder icon click.
2042 *
2043 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2044 */
2045 protected void onClickFolderIcon(View v) {
2046 if (LOGD) Log.d(TAG, "onClickFolder");
2047 if (!(v instanceof FolderIcon)){
2048 throw new IllegalArgumentException("Input must be a FolderIcon");
2049 }
2050
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002051 Folder folder = ((FolderIcon) v).getFolder();
2052 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002053 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002054 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002055 }
Michael Jurka5130e402011-10-13 04:55:35 -07002056 }
2057
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002058 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002059 * Event handler for the wallpaper picker button that appears after a long press
2060 * on the home screen.
2061 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002062 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002063 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002064 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2065 return;
2066 }
2067
Tony Wickham785f7a52015-08-31 17:28:32 -07002068 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2069 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002070 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002071 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002072 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002073
2074 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002075 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2076 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002077 intent.setPackage(pickerPackage);
2078 }
2079
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002080 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002081 try {
2082 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2083 // If there is no target package, use the default intent chooser animation
2084 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2085 } catch (ActivityNotFoundException e) {
2086 setWaitingForResult(null);
2087 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2088 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002089 }
2090
Sunny Goyala7ce1662016-05-31 15:01:35 -07002091 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002093 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2094 try {
2095 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2096 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2097 // is enabled by default on NYC.
2098 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2099 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002100
2101 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2102 String id = ((ShortcutInfo) info).getDeepShortcutId();
2103 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302104 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002105 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002106 } else {
2107 // Could be launching some bookkeeping activity
2108 startActivity(intent, optsBundle);
2109 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002110 } finally {
2111 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002114 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2115 // corresponding permission. Show the appropriate permission prompt if that
2116 // is the case.
2117 if (intent.getComponent() == null
2118 && Intent.ACTION_CALL.equals(intent.getAction())
2119 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2120 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002121
2122 setWaitingForResult(PendingRequestArgs
2123 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002124 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2125 REQUEST_PERMISSION_CALL_PHONE);
2126 } else {
2127 // No idea why this was thrown.
2128 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 }
2131 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002132
Sunny Goyalfe770c92016-09-27 14:38:58 -07002133 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002134 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002135 if (Utilities.ATLEAST_MARSHMALLOW) {
2136 int left = 0, top = 0;
2137 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002138 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002139 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002140 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002141 if (icon != null) {
2142 Rect bounds = icon.getBounds();
2143 left = (width - bounds.width()) / 2;
2144 top = v.getPaddingTop();
2145 width = bounds.width();
2146 height = bounds.height();
2147 }
2148 }
2149 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2150 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2151 // On L devices, we use the device default slide-up transition.
2152 // On L MR1 devices, we use a custom version of the slide-up transition which
2153 // doesn't have the delay present in the device default.
2154 return ActivityOptions.makeCustomAnimation(
2155 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2156 }
2157 return null;
2158 }
2159
Tonye3c59252017-05-02 21:32:27 -07002160 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002161 int[] pos = new int[2];
2162 v.getLocationOnScreen(pos);
2163 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2164 }
2165
Sunny Goyala7ce1662016-05-31 15:01:35 -07002166 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002167 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002168 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2169 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002170 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002171 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002172 // Only launch using the new animation if the shortcut has not opted out (this is a
2173 // private contract between launcher and may be ignored in the future).
2174 boolean useLaunchAnimation = (v != null) &&
2175 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2176 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2177
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002178 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002179
2180 // Prepare intent
2181 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2182 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002183 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002184 }
2185 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002186 if (Utilities.ATLEAST_MARSHMALLOW
2187 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002188 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002189 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002190 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002191 // Shortcuts need some special checks due to legacy reasons.
2192 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002193 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002194 // Could be launching some bookkeeping activity
2195 startActivity(intent, optsBundle);
2196 } else {
2197 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2198 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2199 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002200
2201 if (v instanceof BubbleTextView) {
2202 // This is set to the view that launched the activity that navigated the user away
2203 // from launcher. Since there is no callback for when the activity has finished
2204 // launching, enable the press state and keep this reference to reset the press
2205 // state when we return to launcher.
2206 BubbleTextView btv = (BubbleTextView) v;
2207 btv.setStayPressed(true);
2208 setOnResumeCallback(btv);
2209 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002210 mAppLaunchSuccess = true;
2211 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002212 } catch (ActivityNotFoundException|SecurityException e) {
2213 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2214 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2215 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002216 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002217 }
2218
Tony Wickhamdadb3042016-02-24 11:07:00 -08002219 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002220 public boolean dispatchTouchEvent(MotionEvent ev) {
2221 mLastDispatchTouchEventX = ev.getX();
2222 return super.dispatchTouchEvent(ev);
2223 }
2224
2225 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002227 if (!isDraggingEnabled()) return false;
2228 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002229 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230
Jon Miranda51f037d2016-11-07 08:37:20 -08002231 boolean ignoreLongPressToOverview =
2232 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002233
Adam Cohen1697b792013-09-17 19:08:21 -07002234 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002235 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002236 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302237 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2238 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002239 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002240 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002241 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2242 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2243 return true;
2244 } else {
2245 return false;
2246 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002247 } else {
2248 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002249 }
Adam Cohen1697b792013-09-17 19:08:21 -07002250 }
2251
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002252 CellLayout.CellInfo longClickCellInfo = null;
2253 View itemUnderLongClick = null;
2254 if (v.getTag() instanceof ItemInfo) {
2255 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002256 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002257 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002258 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 }
2260
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 // The hotseat touch handling does not go through Workspace, and we always allow long press
2262 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002263 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002265 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002266 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002267 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302268 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2269 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002270 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002271 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002272 return false;
2273 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302274 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2275 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002276 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002277 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002278 }
Jon Miranda379198e2016-09-23 08:54:40 -07002279 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2280 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002282 final boolean isAllAppsButton =
2283 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2284 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2285 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002286 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002288 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 }
2290 }
2291 }
2292 return true;
2293 }
2294
Winson Chung3d503fb2011-07-13 17:25:49 -07002295 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002296 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 return mHotseat != null && layout != null &&
2298 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2299 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002300
Winson Chung3d503fb2011-07-13 17:25:49 -07002301 /**
2302 * Returns the CellLayout of the specified container at the specified screen.
2303 */
Sunny Goyal92820592015-03-02 11:31:35 -08002304 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002305 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2306 if (mHotseat != null) {
2307 return mHotseat.getLayout();
2308 } else {
2309 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002310 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002312 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002313 }
2314 }
2315
Michael Jurkae326f182011-11-21 14:05:46 -08002316 @Override
2317 public void onTrimMemory(int level) {
2318 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002319 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2320 // The widget preview db can result in holding onto over
2321 // 3MB of memory for caching which isn't necessary.
2322 SQLiteDatabase.releaseMemory();
2323
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002324 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002325 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002326 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002327 if (mLauncherCallbacks != null) {
2328 mLauncherCallbacks.onTrimMemory(level);
2329 }
Michael Jurkae326f182011-11-21 14:05:46 -08002330 }
2331
Michael Jurkad7c28052012-04-27 15:43:36 -07002332 @Override
2333 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002334 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002335 final List<CharSequence> text = event.getText();
2336 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002337 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002338 // TODO: When can workspace be null?
2339 text.add(mWorkspace == null
2340 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002341 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002342 return result;
2343 }
2344
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002345 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002346 * If the activity is currently paused, signal that we need to run the passed Runnable
2347 * in onResume.
2348 *
2349 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002350 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2351 * wrong when we're not running, and if the activity comes back to what the configuration was
2352 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002353 *
2354 * Implementation of the method from LauncherModel.Callbacks.
2355 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002356 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002357 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002358 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002359 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002360 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002361 if (run instanceof RunnableWithId) {
2362 // Remove any runnables which have the same id
2363 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002364 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002365 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002366 return true;
2367 } else {
2368 return false;
2369 }
2370 }
2371
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002372 public void setOnResumeCallback(OnResumeCallback callback) {
2373 if (mOnResumeCallback != null) {
2374 mOnResumeCallback.onLauncherResume();
2375 }
2376 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002377 }
2378
Michael Jurka7607c2f2013-04-03 14:33:19 -07002379 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002380 * If the activity is currently paused, signal that we need to re-run the loader
2381 * in onResume.
2382 *
2383 * This needs to be called from incoming places where resources might have been loaded
2384 * while we are paused. That is becaues the Configuration might be wrong
2385 * when we're not running, and if it comes back to what it was when we
2386 * were paused, we are not restarted.
2387 *
2388 * Implementation of the method from LauncherModel.Callbacks.
2389 *
2390 * @return true if we are currently paused. The caller might be able to
2391 * skip some work in that case since we will come back again.
2392 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002393 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002394 public boolean setLoadOnResume() {
2395 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002396 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002397 mOnResumeNeedsLoad = true;
2398 return true;
2399 } else {
2400 return false;
2401 }
2402 }
2403
2404 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002405 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002407 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002409 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002410 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002411 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002412 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002413 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002414 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002415
Joe Onorato9c1289c2009-08-17 11:03:03 -04002416 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002417 * Clear any pending bind callbacks. This is called when is loader is planning to
2418 * perform a full rebind from scratch.
2419 */
2420 @Override
2421 public void clearPendingBinds() {
2422 mBindOnResumeCallbacks.clear();
2423 if (mPendingExecutor != null) {
2424 mPendingExecutor.markCompleted();
2425 mPendingExecutor = null;
2426 }
2427 }
2428
2429 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002430 * Refreshes the shortcuts shown on the workspace.
2431 *
2432 * Implementation of the method from LauncherModel.Callbacks.
2433 */
2434 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002435 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002436 // Floating panels (except the full widget sheet) are associated with individual icons. If
2437 // we are starting a fresh bind, close all such panels as all the icons are about
2438 // to go away.
2439 AbstractFloatingView.closeOpenViews(this, true,
2440 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002441
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002442 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002443
Winson Chungd64d1762013-08-20 14:37:16 -07002444 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002445 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002446 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002447
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 if (mHotseat != null) {
2449 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002451 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 }
2453
Adam Cohendcd297f2013-06-18 13:13:40 -07002454 @Override
2455 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002456 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002457 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2458 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002459 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2460 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002461 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002462 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2463 // If there are no screens, we need to have an empty screen
2464 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002465 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002466 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002467
Winsonc7d2e832016-07-28 12:24:55 -07002468 // After we have added all the screens, if the wallpaper was locked to the default state,
2469 // then notify to indicate that it can be released and a proper wallpaper offset can be
2470 // computed before the next layout
2471 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002472 }
2473
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002474 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 int count = orderedScreenIds.size();
2476 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002477 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002478 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002479 // No need to bind the first screen, as its always bound.
2480 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2481 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002482 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002483 }
2484
Sunny Goyalb23980c2017-08-17 07:45:25 -07002485 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002486 public void bindAppsAdded(final ArrayList<Long> newScreens,
2487 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002488 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002489 Runnable r = new Runnable() {
2490 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002491 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002492 }
2493 };
2494 if (waitUntilResume(r)) {
2495 return;
2496 }
2497
Winson Chungd64d1762013-08-20 14:37:16 -07002498 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002499 if (newScreens != null) {
2500 bindAddScreens(newScreens);
2501 }
Winson Chungd64d1762013-08-20 14:37:16 -07002502
2503 // We add the items without animation on non-visible pages, and with
2504 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002505 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002506 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002507 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002508 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002509 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002510 }
Winson Chungc58497e2013-09-03 17:48:37 -07002511
Winson Chung87412982013-10-03 18:34:14 -07002512 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002513 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002514 }
2515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002517 * Bind the items start-end from the list.
2518 *
2519 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002521 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002522 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002523 Runnable r = new Runnable() {
2524 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002525 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002526 }
2527 };
2528 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002529 return;
2530 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002531
Sunny Goyal3be633b2016-12-08 09:59:25 -08002532 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002533 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002534 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002535 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002536 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002537 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002538 int end = items.size();
2539 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002540 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002541
2542 // Short circuit if we are loading dock items for a configuration which has no dock
2543 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2544 mHotseat == null) {
2545 continue;
2546 }
2547
Sunny Goyal639e9062015-08-19 19:17:06 -07002548 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 switch (item.itemType) {
2550 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2551 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002552 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002553 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002554 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002555 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002556 }
2557 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002558 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002559 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002560 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002561 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002562 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002563 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2564 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002565 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002566 if (view == null) {
2567 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002568 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002569 break;
2570 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002571 default:
2572 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002573 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002574
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002575 /*
2576 * Remove colliding items.
2577 */
2578 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2579 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2580 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2581 View v = cl.getChildAt(item.cellX, item.cellY);
2582 Object tag = v.getTag();
2583 String desc = "Collision while binding workspace item: " + item
2584 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002585 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002586 throw (new RuntimeException(desc));
2587 } else {
2588 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002589 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002590 continue;
2591 }
2592 }
2593 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302594 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002595 if (animateIcons) {
2596 // Animate all the applications up now
2597 view.setAlpha(0f);
2598 view.setScaleX(0f);
2599 view.setScaleY(0f);
2600 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002601 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002604
Winson Chung997a9232013-07-24 15:33:46 -07002605 if (animateIcons) {
2606 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002607 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002608 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002609 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002610 final Runnable startBounceAnimRunnable = new Runnable() {
2611 public void run() {
2612 anim.playTogether(bounceAnims);
2613 anim.start();
2614 }
2615 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002616 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002617 // We post the animation slightly delayed to prevent slowdowns
2618 // when we are loading right after we return to launcher.
2619 mWorkspace.postDelayed(new Runnable() {
2620 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002621 if (mWorkspace != null) {
2622 mWorkspace.snapToPage(newScreenIndex);
2623 mWorkspace.postDelayed(startBounceAnimRunnable,
2624 NEW_APPS_ANIMATION_DELAY);
2625 }
Winson Chung94d67682013-09-25 16:29:40 -07002626 }
2627 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002628 } else {
2629 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002630 }
2631 }
Winson Chung64359a52013-07-08 17:17:08 -07002632 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002633 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 }
2635
Joe Onorato9c1289c2009-08-17 11:03:03 -04002636 /**
2637 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002638 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002639 public void bindAppWidget(LauncherAppWidgetInfo item) {
2640 View view = inflateAppWidget(item);
2641 if (view != null) {
2642 mWorkspace.addInScreen(view, item);
2643 mWorkspace.requestLayout();
2644 }
2645 }
2646
2647 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002648 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302649 PendingAppWidgetHostView view =
2650 new PendingAppWidgetHostView(this, item, mIconCache, true);
2651 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002652 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002653 }
2654
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002655 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002656
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002657 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002658
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002659 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2660 // If the provider is not ready, bind as a pending widget.
2661 appWidgetInfo = null;
2662 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2663 // The widget id is not valid. Try to find the widget based on the provider info.
2664 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2665 } else {
2666 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2667 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002668
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002669 // If the provider is ready, but the width is not yet restored, try to restore it.
2670 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2671 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002672 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002673 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2674 + " belongs to component " + item.providerName
2675 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002676 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002677 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002678 }
Sunny Goyalff572272014-07-23 13:58:07 -07002679
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002680 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002681 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002682 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2683 // Id has not been allocated yet. Allocate a new id.
2684 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2685 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002686
Sunny Goyald478c832016-04-01 12:04:16 -07002687 // Also try to bind the widget. If the bind fails, the user will be shown
2688 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002689 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002690 pendingInfo.spanX = item.spanX;
2691 pendingInfo.spanY = item.spanY;
2692 pendingInfo.minSpanX = item.minSpanX;
2693 pendingInfo.minSpanY = item.minSpanY;
2694 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002695
2696 boolean isDirectConfig =
2697 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2698 if (isDirectConfig && item.bindOptions != null) {
2699 Bundle newOptions = item.bindOptions.getExtras();
2700 if (options != null) {
2701 newOptions.putAll(options);
2702 }
2703 options = newOptions;
2704 }
Sunny Goyald478c832016-04-01 12:04:16 -07002705 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2706 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002707
Sunny Goyal86df1382016-08-10 15:03:22 -07002708 // We tried to bind once. If we were not able to bind, we would need to
2709 // go through the permission dialog, which means we cannot skip the config
2710 // activity.
2711 item.bindOptions = null;
2712 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2713
Sunny Goyald478c832016-04-01 12:04:16 -07002714 // Bind succeeded
2715 if (success) {
2716 // If the widget has a configure activity, it is still needs to set it up,
2717 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002718 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002719 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2720 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002721 }
Sunny Goyald478c832016-04-01 12:04:16 -07002722
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002723 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002724 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002725 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002726 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002727 // The widget was marked as UI not ready, but there is no configure activity to
2728 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002729 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002730 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002731 }
Sunny Goyalff572272014-07-23 13:58:07 -07002732 }
2733
Sunny Goyald5462aa2016-11-22 16:52:39 +05302734 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002735 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002736 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002737 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002738 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002739 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002740 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002741 }
2742
Sunny Goyal233ee962015-08-03 13:05:01 -07002743 item.minSpanX = appWidgetInfo.minSpanX;
2744 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302745 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002746 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302747 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002748 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302749 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002750
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002751 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002752 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 }
2754
Sunny Goyalff572272014-07-23 13:58:07 -07002755 /**
2756 * Restores a pending widget.
2757 *
2758 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002759 */
Sunny Goyald478c832016-04-01 12:04:16 -07002760 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002761 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2762 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2763 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002764 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002765 }
2766
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002767 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002768 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002769 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2770 info.pendingItemInfo = null;
2771 }
Sunny Goyalff572272014-07-23 13:58:07 -07002772
Sunny Goyalba47faa2017-10-04 16:50:57 -07002773 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2774 view.reinflate();
2775 }
2776
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002777 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002778 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002779 }
2780
Adam Cohen1462de32012-07-24 22:34:36 -07002781 public void onPageBoundSynchronously(int page) {
2782 mSynchronouslyBoundPages.add(page);
2783 }
2784
Sunny Goyal527c7d32015-08-28 15:19:36 -07002785 @Override
2786 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2787 if (mPendingExecutor != null) {
2788 mPendingExecutor.markCompleted();
2789 }
2790 mPendingExecutor = executor;
2791 executor.attachTo(this);
2792 }
2793
2794 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2795 if (mPendingExecutor == executor) {
2796 mPendingExecutor = null;
2797 }
2798 }
2799
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002800 @Override
2801 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2802 Runnable r = new Runnable() {
2803 public void run() {
2804 finishFirstPageBind(executor);
2805 }
2806 };
2807 if (waitUntilResume(r)) {
2808 return;
2809 }
2810
2811 Runnable onComplete = new Runnable() {
2812 @Override
2813 public void run() {
2814 if (executor != null) {
2815 executor.onLoadAnimationCompleted();
2816 }
2817 }
2818 };
2819 if (mDragLayer.getAlpha() < 1) {
2820 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2821 } else {
2822 onComplete.run();
2823 }
2824 }
2825
Joe Onorato9c1289c2009-08-17 11:03:03 -04002826 /**
2827 * Callback saying that there aren't any more items to bind.
2828 *
2829 * Implementation of the method from LauncherModel.Callbacks.
2830 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002831 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002832 Runnable r = new Runnable() {
2833 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002834 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002835 }
2836 };
2837 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002838 return;
2839 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002840 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002841 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002843 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002844
Sunny Goyal2100c782016-08-22 16:00:03 -07002845 if (mPendingActivityResult != null) {
2846 handleActivityResult(mPendingActivityResult.requestCode,
2847 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2848 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002849 }
2850
Sunny Goyala474a9b2017-05-04 16:47:11 -07002851 InstallShortcutReceiver.disableAndFlushInstallQueue(
2852 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002853
Tony Wickham010d2552017-01-20 08:15:28 -08002854 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002855 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002856 }
2857
Winson Chunga2413752012-04-03 14:22:34 -07002858 private boolean canRunNewAppsAnimation() {
2859 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002860 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002861 }
2862
Winson Chungc9168342013-06-26 14:54:55 -07002863 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002864 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002865 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2866 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002867 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002868 return bounceAnim;
2869 }
2870
Adam Cohen27772732013-11-11 14:00:56 +00002871 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002872 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002873 }
2874
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002875 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 * Add the icons for all apps.
2877 *
2878 * Implementation of the method from LauncherModel.Callbacks.
2879 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002880 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002881 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2882 public void run() {
2883 bindAllApplications(apps);
2884 }
2885 };
2886 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002887 return;
2888 }
2889
Winson Chungb745afb2015-03-02 11:51:23 -08002890 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002891 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002892 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002893 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002894 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002895 return;
2896 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002897
Winson Chungb745afb2015-03-02 11:51:23 -08002898 mAppsView.setApps(apps);
2899 }
Adam Cohen9211d422014-10-07 18:14:53 -07002900 if (mLauncherCallbacks != null) {
2901 mLauncherCallbacks.bindAllApplications(apps);
2902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002903 }
2904
2905 /**
Tony2917a8b2017-07-31 23:29:42 -07002906 * Returns an Executor that will run after the launcher is first drawn (including after the
2907 * initial fade in animation). Returns null if the first draw has already occurred.
2908 */
2909 public @Nullable Executor getPendingExecutor() {
2910 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2911 }
2912
2913 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002914 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2915 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2916 */
2917 @Override
2918 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002919 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002920 }
2921
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002922 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 * A package was updated.
2924 *
2925 * Implementation of the method from LauncherModel.Callbacks.
2926 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002927 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002928 Runnable r = new Runnable() {
2929 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002930 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002931 }
2932 };
2933 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002934 return;
2935 }
2936
Winson Chungb745afb2015-03-02 11:51:23 -08002937 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002938 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002939 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002940 }
2941
Sunny Goyal4390ace2014-10-13 11:33:11 -07002942 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002943 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2944 Runnable r = new Runnable() {
2945 public void run() {
2946 bindPromiseAppProgressUpdated(app);
2947 }
2948 };
2949 if (waitUntilResume(r)) {
2950 return;
2951 }
2952
2953 if (mAppsView != null) {
2954 mAppsView.updatePromiseAppProgress(app);
2955 }
2956 }
2957
2958 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002959 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2960 Runnable r = new Runnable() {
2961 public void run() {
2962 bindWidgetsRestored(widgets);
2963 }
2964 };
2965 if (waitUntilResume(r)) {
2966 return;
2967 }
2968 mWorkspace.widgetsRestored(widgets);
2969 }
2970
Joe Onorato9c1289c2009-08-17 11:03:03 -04002971 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002972 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002973 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002974 *
2975 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002976 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002977 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002978 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002979 Runnable r = new Runnable() {
2980 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002981 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002982 }
2983 };
2984 if (waitUntilResume(r)) {
2985 return;
2986 }
2987
Sunny Goyal4390ace2014-10-13 11:33:11 -07002988 if (!updated.isEmpty()) {
2989 mWorkspace.updateShortcuts(updated);
2990 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002991 }
2992
2993 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002994 * Update the state of a package, typically related to install state.
2995 *
2996 * Implementation of the method from LauncherModel.Callbacks.
2997 */
Sunny Goyale755d462014-07-22 13:48:29 -07002998 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002999 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3000 Runnable r = new Runnable() {
3001 public void run() {
3002 bindRestoreItemsChange(updates);
3003 }
3004 };
3005 if (waitUntilResume(r)) {
3006 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003007 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003008
Sunny Goyal756adbc2015-04-16 15:20:51 -07003009 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003010 }
3011
3012 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003013 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003014 * in addition to specific applications to remove, the reason being that
3015 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003016 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003017 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003018 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003019 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003020 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003021 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003022 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003023 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003024 }
Winson Chungd64d1762013-08-20 14:37:16 -07003025 };
3026 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003027 return;
3028 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003029 mWorkspace.removeItemsByMatcher(matcher);
3030 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003031 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003032
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003033 @Override
3034 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3035 Runnable r = new Runnable() {
3036 public void run() {
3037 bindAppInfosRemoved(appInfos);
3038 }
3039 };
3040 if (waitUntilResume(r)) {
3041 return;
Joe Onorato36115782010-06-17 13:28:48 -04003042 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043
Winson Chungdf95eb12013-10-16 14:57:07 -07003044 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003045 if (mAppsView != null) {
3046 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 }
Joe Onoratobe386092009-11-17 17:32:16 -08003049
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003050 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003051 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3052 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003053 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3054 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003055 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003056 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003057 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003058 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003059 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003060 return;
3061 }
3062
Tony Wickham26b17462017-03-20 17:12:24 -07003063 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3064 if (topView != null) {
3065 topView.onWidgetsBound();
3066 }
3067 }
3068
Tony Wickham86222d22017-03-29 15:30:43 -07003069 /**
3070 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3071 * refreshes the widgets and shortcuts associated with the given package/user
3072 */
3073 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003074 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003075 }
3076
Sunny Goyalc99cb172017-10-19 16:15:09 -07003077 public boolean isRotationEnabled () {
3078 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003079 }
3080
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003081 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003082 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003083 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003084 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003085 }
3086
Winson Chung80baf5a2010-08-09 16:03:15 -07003087 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003088 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003089 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003090 @Override
3091 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3092 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003093
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003094 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3095 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003096 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003097 writer.println(prefix + " Homescreen " + i);
3098
3099 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3100 for (int j = 0; j < layout.getChildCount(); j++) {
3101 Object tag = layout.getChildAt(j).getTag();
3102 if (tag != null) {
3103 writer.println(prefix + " " + tag.toString());
3104 }
3105 }
3106 }
3107
3108 writer.println(prefix + " Hotseat");
3109 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003110 for (int j = 0; j < layout.getChildCount(); j++) {
3111 Object tag = layout.getChildAt(j).getTag();
3112 if (tag != null) {
3113 writer.println(prefix + " " + tag.toString());
3114 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003115 }
Sunny Goyala1365452015-10-01 15:46:24 -07003116
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003117 try {
3118 FileLog.flushAll(writer);
3119 } catch (Exception e) {
3120 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003121 }
3122 }
3123
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003124 writer.println(prefix + "Misc:");
3125 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3126 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3127 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3128
3129 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003130
Adam Cohen9211d422014-10-07 18:14:53 -07003131 if (mLauncherCallbacks != null) {
3132 mLauncherCallbacks.dump(prefix, fd, writer, args);
3133 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003134 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003135
Sunny Goyal66b24572016-09-21 15:57:55 -07003136 @Override
3137 @TargetApi(Build.VERSION_CODES.N)
3138 public void onProvideKeyboardShortcuts(
3139 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3140
3141 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003142 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003143 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3144 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3145 }
3146 View currentFocus = getCurrentFocus();
3147 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3148 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3149 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3150 }
Tony18c4aa42017-05-10 21:23:57 -05003151 if (currentFocus.getTag() instanceof ItemInfo
3152 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003153 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3154 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3155 }
3156 if (!shortcutInfos.isEmpty()) {
3157 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3158 }
3159
3160 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3161 }
3162
3163 @Override
3164 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3165 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3166 switch (keyCode) {
3167 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003168 if (isInState(NORMAL)) {
3169 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003170 return true;
3171 }
3172 break;
3173 case KeyEvent.KEYCODE_S: {
3174 View focusedView = getCurrentFocus();
3175 if (focusedView instanceof BubbleTextView
3176 && focusedView.getTag() instanceof ItemInfo
3177 && mAccessibilityDelegate.performAction(focusedView,
3178 (ItemInfo) focusedView.getTag(),
3179 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08003180 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003181 return true;
3182 }
3183 break;
3184 }
3185 case KeyEvent.KEYCODE_O:
3186 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3187 return true;
3188 }
3189 break;
3190 }
3191 }
3192 return super.onKeyShortcut(keyCode, event);
3193 }
3194
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003195 public static Launcher getLauncher(Context context) {
3196 if (context instanceof Launcher) {
3197 return (Launcher) context;
3198 }
3199 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3200 }
3201
Sunny Goyal5a81c382017-03-20 15:08:06 -07003202 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003203
3204 @Override
3205 public void onSharedPreferenceChanged(
3206 SharedPreferences sharedPreferences, String key) {
3207 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003208 // Recreate the activity so that it initializes the rotation preference again.
3209 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003210 }
3211 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003212 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003213
3214 /**
3215 * Callback for listening for onResume
3216 */
3217 public interface OnResumeCallback {
3218
3219 void onLauncherResume();
3220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221}