blob: a9b1619cde9f5fdab7b2c1c0c9156bba157f5b5a [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 Goyal7ede6112017-12-05 15:11:21 -0800101import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700104import com.android.launcher3.compat.LauncherAppsCompatVO;
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;
Mario Bertschler27288382017-05-24 15:35:09 -0700110import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700113import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700114import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700115import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700116import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800117import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700119import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800120import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800121import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700122import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800123import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800124import com.android.launcher3.uioverrides.UiFactory;
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
Sunny Goyalffe83f12014-08-14 17:39:34 -0700224 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700227 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700228
Sunny Goyal316490e2015-06-02 09:38:28 -0700229 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700230
Michael Jurka838a4ca2011-02-07 13:33:06 -0800231 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700232
Sunny Goyal47328fd2016-05-25 18:56:41 -0700233 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
235 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700236 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700237 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700238
Winson Chung8ae41982017-11-10 11:42:13 -0800239 // UI and state for the overview panel
240 private ViewGroup mOverviewPanel;
241
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 Goyald3864fa2017-11-14 15:06:36 -0800360 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
361 // In case we are on a device with locked rotation, we should look at preferences to check
362 // if the user has specifically allowed rotation.
363 if (!mRotationEnabled) {
364 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
365 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
366 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368
Sunny Goyald3864fa2017-11-14 15:06:36 -0800369 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
370 if (internalStateHandled) {
371 // Temporarily enable the rotation
372 mRotationEnabled = true;
373
374 if (savedInstanceState != null) {
375 // InternalStateHandler has already set the appropriate state.
376 // We dont need to do anything.
377 savedInstanceState.remove(RUNTIME_STATE);
378 }
379 }
380 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800381
Sunny Goyal2100c782016-08-22 16:00:03 -0700382 // We only load the page synchronously if the user rotates (or triggers a
383 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700384 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
385 if (savedInstanceState != null) {
386 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
387 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800388
Sunny Goyalfe770c92016-09-27 14:38:58 -0700389 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800390 if (!internalStateHandled) {
391 // If we are not binding synchronously, show a fade in animation when
392 // the first page bind completes.
393 mDragLayer.setAlpha(0);
394 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700395 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700396 // Pages bound synchronously.
397 mWorkspace.setCurrentPage(currentScreen);
398
Sunny Goyal2100c782016-08-22 16:00:03 -0700399 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
402 // For handling default keys
403 mDefaultKeySsb = new SpannableStringBuilder();
404 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800405
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700406 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
407 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700408 setRequestedOrientation(mRotationEnabled
409 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700410
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800411 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700412
Jon Miranda7fda2852017-07-19 16:07:01 -0700413 // Listen for broadcasts
414 IntentFilter filter = new IntentFilter();
415 filter.addAction(Intent.ACTION_SCREEN_OFF);
416 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
417 registerReceiver(mReceiver, filter);
418 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700419
Sunny Goyal8392c822017-06-20 10:03:56 -0700420 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
421 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700422
423 if (mLauncherCallbacks != null) {
424 mLauncherCallbacks.onCreate(savedInstanceState);
425 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700426
427 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700428 }
429
Sunny Goyal7779d622015-06-11 16:18:39 -0700430 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700431 public void onThemeChanged() {
432 recreate();
433 }
434
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700435 public LauncherStateManager getStateManager() {
436 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700437 }
438
Mario Bertschlera6936942017-05-31 14:48:19 -0700439 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700440 if (isDark) {
441 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700442 } else if (supportsDarkText) {
443 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700444 }
445 }
446
447 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700448 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800449 return mLauncherView.findViewById(id);
450 }
451
452 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700453 public void onAppWidgetHostReset() {
454 if (mAppWidgetHost != null) {
455 mAppWidgetHost.startListening();
456 }
457 }
458
Adam Cohen9211d422014-10-07 18:14:53 -0700459 private LauncherCallbacks mLauncherCallbacks;
460
Winson1f064272016-07-18 17:18:02 -0700461 public void onInsetsChanged(Rect insets) {
462 mDeviceProfile.updateInsets(insets);
463 mDeviceProfile.layout(this, true /* notifyListeners */);
464 }
465
Sunny Goyal32554d12015-12-03 15:31:25 -0800466 /**
467 * Call this after onCreate to set or clear overlay.
468 */
469 public void setLauncherOverlay(LauncherOverlay overlay) {
470 if (overlay != null) {
471 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
472 }
473 mWorkspace.setLauncherOverlay(overlay);
474 }
475
Adam Cohen9211d422014-10-07 18:14:53 -0700476 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
477 mLauncherCallbacks = callbacks;
478 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700479 }
480
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700481 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700482 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700483 if (mLauncherCallbacks != null) {
484 mLauncherCallbacks.onLauncherProviderChange();
485 }
486 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700487
Hyunyoung Song3f471442015-04-08 19:01:34 -0700488 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700489 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
490 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700491 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700492 }
493
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000494 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700495 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
496 // This cast is safe as long as the id < 0x00FFFFFF
497 // Since we jail all the dynamically generated views, there should be no clashes
498 // with any other views.
499 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000500 }
501
Tony Wickham010d2552017-01-20 08:15:28 -0800502 public PopupDataProvider getPopupDataProvider() {
503 return mPopupDataProvider;
504 }
505
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000506 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700507 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
508 * a configuration step, this allows the proper animations to run after other transitions.
509 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 private long completeAdd(
511 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
512 long screenId = info.screenId;
513 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700514 // When the screen id represents an actual screen (as opposed to a rank) we make sure
515 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700517 }
Adam Cohendb364c32014-05-20 14:23:40 -0700518
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800520 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700521 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700522 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800523 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700524 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700525 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700526 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700527 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700528 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700529 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 int widgetId = appWidgetId;
531 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700532 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700534 // Since the view was just bound, also launch the configure activity if needed
535 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
536 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800537 if (provider != null) {
538 new WidgetAddFlowHandler(provider)
539 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700540 }
541 }
542 break;
543 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800544 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700545
Adam Cohendb364c32014-05-20 14:23:40 -0700546 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800547 }
548
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800549 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700550 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700551 if (isWorkspaceLoading()) {
552 // process the result once the workspace has loaded.
553 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
554 return;
555 }
556 mPendingActivityResult = null;
557
Winson Chunge341d302013-08-16 14:31:00 -0700558 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700559 final PendingRequestArgs requestArgs = mPendingRequestArgs;
560 setWaitingForResult(null);
561 if (requestArgs == null) {
562 return;
563 }
564
565 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700566
Adam Cohenad4e15c2013-10-17 16:21:35 -0700567 Runnable exitSpringLoaded = new Runnable() {
568 @Override
569 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700570 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 }
572 };
573
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700575 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
578 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700580 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700581 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700582 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700583 addAppWidgetImpl(
584 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800585 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700586 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700587 }
588 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200589 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700590 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700591 // User could have free-scrolled between pages before picking a wallpaper; make sure
592 // we move to the closest one now.
593 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700594 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200595 }
596 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700597 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200598
Adam Cohened66b2b2012-01-23 17:28:51 -0800599 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700600 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700601
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 // We have special handling for widgets
603 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800604 final int appWidgetId;
605 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
606 : -1;
607 if (widgetId < 0) {
608 appWidgetId = pendingAddWidgetId;
609 } else {
610 appWidgetId = widgetId;
611 }
612
Adam Cohenad4e15c2013-10-17 16:21:35 -0700613 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800614 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700615 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
616 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700617 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700618 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700619 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700620 @Override
621 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700622 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700623 }
624 };
Adam Cohendb364c32014-05-20 14:23:40 -0700625
Sunny Goyal2100c782016-08-22 16:00:03 -0700626 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
627 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
628 } else {
629 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
630 // When the screen id represents an actual screen (as opposed to a rank)
631 // we make sure that the drop page actually exists.
632 requestArgs.screenId =
633 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700634 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700635 final CellLayout dropLayout =
636 mWorkspace.getScreenWithId(requestArgs.screenId);
637
638 dropLayout.setDropPending(true);
639 final Runnable onComplete = new Runnable() {
640 @Override
641 public void run() {
642 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
643 dropLayout.setDropPending(false);
644 }
645 };
646 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
647 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700648 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 return;
650 }
651
Sunny Goyald478c832016-04-01 12:04:16 -0700652 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
653 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700654 if (resultCode == RESULT_OK) {
655 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700656 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700657 }
658 // Leave the widget in the pending state if the user canceled the configure.
659 return;
660 }
661
Sunny Goyalaad90582015-07-09 14:22:50 -0700662 if (requestCode == REQUEST_CREATE_SHORTCUT) {
663 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
665 completeAdd(requestCode, data, -1, requestArgs);
666 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
667 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
668
Sunny Goyalaad90582015-07-09 14:22:50 -0700669 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700670 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
671 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800675 }
676
677 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700678 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800679 final int requestCode, final int resultCode, final Intent data) {
680 handleActivityResult(requestCode, resultCode, data);
681 if (mLauncherCallbacks != null) {
682 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
683 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 }
685
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700686 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700687 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600688 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 PendingRequestArgs pendingArgs = mPendingRequestArgs;
690 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
691 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
692 setWaitingForResult(null);
693
Sunny Goyal28c6b962015-10-12 11:42:05 -0700694 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700698 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700699 Intent intent = pendingArgs.getPendingIntent();
700
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 if (grantResults.length > 0
702 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700703 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700704 } else {
705 // TODO: Show a snack bar with link to settings
706 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700707 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700708 }
709 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600710 if (mLauncherCallbacks != null) {
711 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
712 grantResults);
713 }
714 }
715
Adam Cohendb364c32014-05-20 14:23:40 -0700716 /**
717 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
718 *
719 * @param screenId the screen id to check
720 * @return the new screen, or screenId if it exists
721 */
722 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800723 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700724 if (dropLayout == null) {
725 // it's possible that the add screen was removed because it was
726 // empty and a re-bind occurred
727 mWorkspace.addExtraEmptyScreen();
728 return mWorkspace.commitExtraEmptyScreen();
729 } else {
730 return screenId;
731 }
732 }
733
Sunny Goyal2100c782016-08-22 16:00:03 -0700734 @Thunk void completeTwoStageWidgetDrop(
735 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
736 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 Runnable onCompleteRunnable = null;
738 int animationType = 0;
739
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 if (resultCode == RESULT_OK) {
742 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
743 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800744 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700745 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 onCompleteRunnable = new Runnable() {
747 @Override
748 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700749 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700750 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 }
752 };
753 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800754 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700757 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700758 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700759 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
760 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700762 // The animated view may be null in the case of a rotation during widget configuration
763 onCompleteRunnable.run();
764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
766
767 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700768 protected void onStop() {
769 super.onStop();
770 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700771
772 if (mLauncherCallbacks != null) {
773 mLauncherCallbacks.onStop();
774 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800775 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800776
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700777 if (!mAppLaunchSuccess) {
778 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700779 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700780 }
Tony Wickham010d2552017-01-20 08:15:28 -0800781 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700782 }
783
784 @Override
785 protected void onStart() {
786 super.onStart();
787 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700788
789 if (mLauncherCallbacks != null) {
790 mLauncherCallbacks.onStart();
791 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800792 mAppWidgetHost.setListenIfResumed(true);
Tony Wickham010d2552017-01-20 08:15:28 -0800793
794 if (!isWorkspaceLoading()) {
795 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
796 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700797
Jon Miranda7fda2852017-07-19 16:07:01 -0700798 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700799 if (mScrimAnimator != null) {
800 mScrimAnimator.cancel();
801 }
802 mDragLayer.getBackground().setAlpha(0);
803 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
804 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
805 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
806 @Override
807 public void onAnimationEnd(Animator animation) {
808 mScrimAnimator = null;
809 }
810 });
811 mScrimAnimator.setDuration(600);
812 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
813 mScrimAnimator.start();
814 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700815 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700816 }
817
818 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700820 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700822 TraceHelper.partitionSection("ON_RESUME", "superCall");
823
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700824 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700825 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800826 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700827 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700828 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700829 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700830 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700832 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200833 // We might have postponed some bind calls until onResume (see waitUntilResume) --
834 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700835 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
836 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200837 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700838 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200839 }
Winson Chunge4e50662012-01-23 14:45:13 -0800840
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700841 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700842 // Process any items that were added while Launcher was away.
843 InstallShortcutReceiver.disableAndFlushInstallQueue(
844 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700845
Sunny Goyala474a9b2017-05-04 16:47:11 -0700846 // Refresh shortcuts if the permission changed.
847 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700848
Sunny Goyal7ede6112017-12-05 15:11:21 -0800849 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onResume();
852 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800853
Sunny Goyala502aa32017-10-02 16:04:06 -0700854 clearTypedText();
855
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700856 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700860 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700861 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700862 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700863
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700864 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700865 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800866 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700867 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700868
Adam Cohen9211d422014-10-07 18:14:53 -0700869 if (mLauncherCallbacks != null) {
870 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700871 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700872 }
873
Adam Cohenc2d6e892014-10-16 09:49:24 -0700874 public interface LauncherOverlay {
875
876 /**
877 * Touch interaction leading to overscroll has begun
878 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700879 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880
881 /**
882 * Touch interaction related to overscroll has ended
883 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700884 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885
886 /**
887 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
888 * screen (or in the case of RTL, the rightmost screen).
889 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700890 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700891
892 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800893 * Called when the launcher is ready to use the overlay
894 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700895 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700896 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700897 }
898
899 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700900 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700901 }
902
903 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
904
Sunny Goyalc86df472016-02-25 09:19:38 -0800905 public void onScrollChanged(float progress) {
906 if (mWorkspace != null) {
907 mWorkspace.onOverlayScrollChanged(progress);
908 }
909 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700910 }
911
Sunny Goyal16764582017-11-06 16:00:34 -0800912 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700913 if (mLauncherCallbacks != null) {
914 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700915 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800916 // On O and above we there is always some setting present settings (add icon to
917 // home screen or icon badging). On earlier APIs we will have the allow rotation
918 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700919 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700920 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800921 }
922
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700923 @Override
924 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700926 if (mModel.isCurrentCallbacks(this)) {
927 mModel.stopLoader();
928 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700929 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
930
Romain Guy13c2e7b2010-03-10 19:45:00 -0800931 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700932 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700933
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800934 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800935 @Override
936 public void onWindowFocusChanged(boolean hasFocus) {
937 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700938 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700939
940 if (mLauncherCallbacks != null) {
941 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
942 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800943 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 private boolean acceptFilter() {
946 final InputMethodManager inputManager = (InputMethodManager)
947 getSystemService(Context.INPUT_METHOD_SERVICE);
948 return !inputManager.isFullscreenMode();
949 }
950
951 @Override
952 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700953 final int uniChar = event.getUnicodeChar();
954 final boolean handled = super.onKeyDown(keyCode, event);
955 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
956 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
958 keyCode, event);
959 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700961 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 // showSearchDialog()
963 // If there are multiple keystrokes before the search dialog takes focus,
964 // onSearchRequested() will be called for every keystroke,
965 // but it is idempotent, so it's fine.
966 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968 }
969
Joe Onorato8a9625e2010-01-28 15:55:35 -0800970 // Eat the long press event so the keyboard doesn't come up.
971 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
972 return true;
973 }
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 return handled;
976 }
977
Karl Rosaen138a0412009-04-23 19:00:21 -0700978 private String getTypedText() {
979 return mDefaultKeySsb.toString();
980 }
981
Sunny Goyal6f28e712016-09-13 14:19:29 -0700982 @Override
983 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700984 mDefaultKeySsb.clear();
985 mDefaultKeySsb.clearSpans();
986 Selection.setSelection(mDefaultKeySsb, 0);
987 }
988
Sunny Goyalf9403d92017-10-18 10:55:56 -0700989 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700990 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 /**
994 * Restores the previous state, if it exists.
995 *
996 * @param savedState The previous state.
997 */
998 private void restoreState(Bundle savedState) {
999 if (savedState == null) {
1000 return;
1001 }
1002
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001003 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001004 LauncherState[] stateValues = LauncherState.values();
1005 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -08001006 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001007 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001008 }
1009
Sunny Goyal2100c782016-08-22 16:00:03 -07001010 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1011 if (requestArgs != null) {
1012 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001014
1015 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001016
1017 SparseArray<Parcelable> widgetsState =
1018 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1019 if (widgetsState != null) {
1020 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023
1024 /**
1025 * Finds all the views we need and configure them properly.
1026 */
1027 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001028 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001029 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001030 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001031 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001032 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001033
Sunny Goyal784f9c32016-03-23 16:56:54 -07001034 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1035 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1036 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
Winson Chung4c98d922011-05-31 16:50:48 -07001038 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -08001039 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -07001040
Winson Chung3d503fb2011-07-13 17:25:49 -07001041 // Setup the hotseat
1042 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1043 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001044 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 }
1046
Winson Chung4c98d922011-05-31 16:50:48 -07001047 // Setup the workspace
1048 mWorkspace.setHapticFeedbackEnabled(false);
1049 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001050 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001051 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1052 // default state, otherwise we will update to the wrong offsets in RTL
1053 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001054 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001055 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001056
Tony Wickham34d2c912015-09-11 09:27:58 -07001057 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001058 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001059
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001060 // Setup Apps
1061 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001062
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001064 mDragController.setMoveTarget(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 Goyale29897f2017-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 Goyale29897f2017-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 Goyale29897f2017-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
Winson Chung8ae41982017-11-10 11:42:13 -08001309 public <T extends ViewGroup> T getOverviewPanel() {
1310 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001311 }
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;
Winson15f8b172015-08-19 11:02:39 -07001347 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001348 boolean internalStateHandled = InternalStateHandler
1349 .handleNewIntent(this, intent, alreadyOnHome);
1350
Winson15f8b172015-08-19 11:02:39 -07001351 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001352 if (!internalStateHandled) {
1353 // Note: There should be at most one log per method call. This is enforced
1354 // implicitly by using if-else statements.
1355 UserEventDispatcher ued = getUserEventDispatcher();
1356 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1357 if (topOpenView != null) {
1358 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1359 } else if (alreadyOnHome) {
1360 Target target = newContainerTarget(mStateManager.getState().containerType);
1361 target.pageIndex = mWorkspace.getCurrentPage();
1362 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1363 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001364
Sunny Goyald3864fa2017-11-14 15:06:36 -08001365 // In all these cases, only animate if we're already on home
1366 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001367
Sunny Goyald3864fa2017-11-14 15:06:36 -08001368 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1369
1370 // Reset the apps view
1371 if (!alreadyOnHome && mAppsView != null) {
1372 mAppsView.reset();
1373 }
1374
1375 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1376 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1377 }
1378 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001379
1380 final View v = getWindow().peekDecorView();
1381 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001382 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001383 }
1384
Adam Cohen9211d422014-10-07 18:14:53 -07001385 if (mLauncherCallbacks != null) {
1386 mLauncherCallbacks.onHomeIntent();
1387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
Winson15f8b172015-08-19 11:02:39 -07001389
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001390 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001391 }
1392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001394 public void onRestoreInstanceState(Bundle state) {
1395 super.onRestoreInstanceState(state);
1396 for (int page: mSynchronouslyBoundPages) {
1397 mWorkspace.restoreInstanceStateForChild(page);
1398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400
1401 @Override
1402 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001403 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001404 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001405
Adam Cohen21cd0022013-10-09 18:57:02 -07001406 }
Sunny Goyalea609262017-10-25 15:47:38 -07001407 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001408
1409
1410 AbstractFloatingView widgets = AbstractFloatingView
1411 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1412 if (widgets != null) {
1413 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1414 widgets.saveHierarchyState(widgetsState);
1415 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1416 } else {
1417 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1418 }
1419
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001420 // We close any open folders and shortcut containers since they will not be re-opened,
1421 // and we need to make sure this state is reflected.
1422 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001423
Sunny Goyal2100c782016-08-22 16:00:03 -07001424 if (mPendingRequestArgs != null) {
1425 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1426 }
1427 if (mPendingActivityResult != null) {
1428 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001431 super.onSaveInstanceState(outState);
1432
Adam Cohen9211d422014-10-07 18:14:53 -07001433 if (mLauncherCallbacks != null) {
1434 mLauncherCallbacks.onSaveInstanceState(outState);
1435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
1437
1438 @Override
1439 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001441
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001442 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001443 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001444
Winson Chungcd2b0142011-06-08 16:02:26 -07001445 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001446 // It's possible to receive onDestroy after a new Launcher activity has
1447 // been created. In this case, don't interfere with the new Launcher.
1448 if (mModel.isCurrentCallbacks(this)) {
1449 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001450 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001451 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001452
Sunny Goyal745bad92016-05-02 10:54:12 -07001453 if (mRotationPrefChangeHandler != null) {
1454 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1455 }
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001458 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001460 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001462 mAppWidgetHost = null;
1463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001465 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1466
Michael Jurka2ecf9952012-06-18 12:52:28 -07001467 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001468
Tony2917a8b2017-07-31 23:29:42 -07001469 clearPendingBinds();
1470
Adam Cohen9211d422014-10-07 18:14:53 -07001471 if (mLauncherCallbacks != null) {
1472 mLauncherCallbacks.onDestroy();
1473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
Sunny Goyalae502842016-06-17 08:43:56 -07001476 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1477 return mAccessibilityDelegate;
1478 }
1479
Adam Cohena9cf38f2011-05-02 15:36:58 -07001480 public DragController getDragController() {
1481 return mDragController;
1482 }
1483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001485 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001486 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001487 }
1488
1489 @Override
1490 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1491 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001492 try {
1493 super.startIntentSenderForResult(intent, requestCode,
1494 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1495 } catch (IntentSender.SendIntentException e) {
1496 throw new ActivityNotFoundException();
1497 }
1498 }
1499
Winson Chung70d72102011-08-12 11:24:35 -07001500 /**
1501 * Indicates that we want global search for this activity by setting the globalSearch
1502 * argument for {@link #startSearch} to true.
1503 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001505 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001507
Karl Rosaen138a0412009-04-23 19:00:21 -07001508 if (initialQuery == null) {
1509 // Use any text typed in the launcher as the initial query
1510 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 if (appSearchData == null) {
1513 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001514 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001516
Sunny Goyal6f28e712016-09-13 14:19:29 -07001517 if (mLauncherCallbacks == null ||
1518 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1519 // Starting search from the callbacks failed. Start the default global search.
1520 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001521 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001522
1523 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001524 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001525 }
1526
Ian Parkinson047036e2014-09-01 15:40:53 +01001527 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001528 * Starts the global search activity. This code is a copied from SearchManager
1529 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001530 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001531 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001533 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1534 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1535 if (globalSearchActivity == null) {
1536 Log.w(TAG, "No global search activity found.");
1537 return;
1538 }
1539 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1540 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1541 intent.setComponent(globalSearchActivity);
1542 // Make sure that we have a Bundle to put source in
1543 if (appSearchData == null) {
1544 appSearchData = new Bundle();
1545 } else {
1546 appSearchData = new Bundle(appSearchData);
1547 }
Adam Cohen9211d422014-10-07 18:14:53 -07001548 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001549 if (!appSearchData.containsKey("source")) {
1550 appSearchData.putString("source", getPackageName());
1551 }
1552 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1553 if (!TextUtils.isEmpty(initialQuery)) {
1554 intent.putExtra(SearchManager.QUERY, initialQuery);
1555 }
1556 if (selectInitialQuery) {
1557 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1558 }
1559 intent.setSourceBounds(sourceBounds);
1560 try {
1561 startActivity(intent);
1562 } catch (ActivityNotFoundException ex) {
1563 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566
Winson Chung70d72102011-08-12 11:24:35 -07001567 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001568 public boolean onPrepareOptionsMenu(Menu menu) {
1569 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001570 if (mLauncherCallbacks != null) {
1571 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1572 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001573 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001576 @Override
1577 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001578 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001579 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001580 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001581 }
1582
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001584 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 }
1586
Adam Cohen517a7f52014-03-01 12:12:59 -08001587 public boolean isWorkspaceLoading() {
1588 return mWorkspaceLoading;
1589 }
1590
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001591 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001592 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001593 }
1594
Sunny Goyal04a324a2017-01-20 21:08:59 -08001595 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001596 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001597 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001598
Sunny Goyal2100c782016-08-22 16:00:03 -07001599 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001600 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001601 if (LOGD) {
1602 Log.d(TAG, "Adding widget from drop");
1603 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001604 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001605 }
1606
Sunny Goyal2100c782016-08-22 16:00:03 -07001607 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001608 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1609 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1610 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001611
Adam Cohenad4e15c2013-10-17 16:21:35 -07001612 Runnable onComplete = new Runnable() {
1613 @Override
1614 public void run() {
1615 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001616 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001617 }
1618 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001619 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001620 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
1622 }
Romain Guycbb89e42009-06-08 15:52:54 -07001623
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001624 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1625 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001626 info.container = container;
1627 info.screenId = screenId;
1628 if (cell != null) {
1629 info.cellX = cell[0];
1630 info.cellY = cell[1];
1631 }
1632 info.spanX = spanX;
1633 info.spanY = spanY;
1634
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001635 switch (info.itemType) {
1636 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1637 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001638 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001639 break;
1640 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001641 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001642 break;
1643 default:
1644 throw new IllegalStateException("Unknown item type: " + info.itemType);
1645 }
1646 }
1647
Adam Cohenfbba09b2011-07-18 21:43:05 -07001648 /**
1649 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001650 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001651 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001652 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1653 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001654 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1655 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1656 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001657 }
1658
Adam Cohenfbba09b2011-07-18 21:43:05 -07001659 /**
1660 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001661 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001662 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001663 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001664 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001665 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001666 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001667 // In the case where we've prebound the widget, we remove it from the DragLayer
1668 if (LOGD) {
1669 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1670 }
1671 getDragLayer().removeView(hostView);
1672
Adam Cohened66b2b2012-01-23 17:28:51 -08001673 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001674 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001675
1676 // Clear the boundWidget so that it doesn't get destroyed.
1677 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001678 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001679 // In this case, we either need to start an activity to get permission to bind
1680 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001681 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1682 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1683 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1684 this, info.componentName);
1685 } else {
1686 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1687 }
Adam Cohendd70d662012-10-04 16:53:44 -07001688 Bundle options = info.bindOptions;
1689
Sunny Goyalffe83f12014-08-14 17:39:34 -07001690 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1691 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001692 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001693 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001694 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001695 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001696 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001697 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001698 }
1699
Adam Cohendcd297f2013-06-18 13:13:40 -07001700 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001701 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001702 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 folderInfo.title = getText(R.string.folder_name);
1704
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001706 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707
1708 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001709 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301710 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001711 // Force measure the new folder icon
1712 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1713 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001714 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 }
Romain Guycbb89e42009-06-08 15:52:54 -07001716
Winsonc0b52fe2015-09-09 16:38:15 -07001717 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001718 * Unbinds the view for the specified item, and removes the item and all its children.
1719 *
1720 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001721 * @param itemInfo the {@link ItemInfo} for this view.
1722 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001723 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001724 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001725 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001726 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001727 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1728 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001729 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001730 } else {
1731 mWorkspace.removeWorkspaceItem(v);
1732 }
Winsonc0b52fe2015-09-09 16:38:15 -07001733 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001734 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001735 }
1736 } else if (itemInfo instanceof FolderInfo) {
1737 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001738 if (v instanceof FolderIcon) {
1739 ((FolderIcon) v).removeListeners();
1740 }
Winsonc0b52fe2015-09-09 16:38:15 -07001741 mWorkspace.removeWorkspaceItem(v);
1742 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001743 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001744 }
1745 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1746 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001747 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001748 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001749 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001750 }
1751 } else {
1752 return false;
1753 }
1754 return true;
1755 }
1756
1757 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001758 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001759 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001760 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001761 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001762 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001763 // Deleting an app widget ID is a void call but writes to disk before returning
1764 // to the caller...
1765 new AsyncTask<Void, Void, Void>() {
1766 public Void doInBackground(Void ... args) {
1767 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1768 return null;
1769 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001770 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001771 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001772 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001773 }
1774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 @Override
1776 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001777 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
1779
Joe Onorato88ec0992009-11-19 13:16:06 -08001780 @Override
1781 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001782 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1783 return;
1784 }
1785
Sunny Goyal45478022015-06-08 16:52:41 -07001786 if (mDragController.isDragging()) {
1787 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001788 return;
1789 }
1790
Jon Mirandafeba90f2016-10-06 10:53:29 -07001791 // Note: There should be at most one log per method call. This is enforced implicitly
1792 // by using if-else statements.
1793 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001794 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1795 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001796 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001797 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001798 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001799 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001800 } else {
1801 // Back button is a no-op here, but give at least some feedback for the button press
1802 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001803 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001804 }
1805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 * Launches the intent referred by the clicked shortcut.
1808 *
1809 * @param v The view representing the clicked shortcut.
1810 */
1811 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001812 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1813 // view has detached (it's possible for this to happen if the view is removed mid touch).
1814 if (v.getWindowToken() == null) {
1815 return;
1816 }
1817
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001818 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001819 return;
1820 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001821
Adam Cohen1697b792013-09-17 19:08:21 -07001822 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001823 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001824 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1825 LauncherLogProto.Action.Direction.NONE,
1826 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001827 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001828 }
1829 return;
1830 }
1831
1832 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001833 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001834 int page = mWorkspace.indexOfChild(v);
1835 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1836 LauncherLogProto.Action.Direction.NONE,
1837 LauncherLogProto.ContainerType.OVERVIEW, page);
1838 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001839 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001840 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001841 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001842 }
1843
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001845 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001846 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001848 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001849 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001850 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001851 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001852 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001853 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001854 } else if (tag instanceof AppInfo) {
1855 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001856 } else if (tag instanceof LauncherAppWidgetInfo) {
1857 if (v instanceof PendingAppWidgetHostView) {
1858 onClickPendingWidget((PendingAppWidgetHostView) v);
1859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 }
1861 }
1862
Sunny Goyal70660032015-05-14 00:07:08 -07001863 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001864 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001865 return false;
1866 }
1867
Michael Jurkaaf442092010-06-10 17:01:57 -07001868 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001869 * Event handler for the app widget view which has not fully restored.
1870 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001871 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001872 if (mIsSafeModeEnabled) {
1873 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1874 return;
1875 }
1876
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001877 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001878 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001879 LauncherAppWidgetProviderInfo appWidgetInfo =
1880 mAppWidgetManager.findProvider(info.providerName, info.user);
1881 if (appWidgetInfo == null) {
1882 return;
1883 }
1884 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1885
Sunny Goyald478c832016-04-01 12:04:16 -07001886 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1887 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1888 // This should not happen, as we make sure that an Id is allocated during bind.
1889 return;
1890 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001891 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1892 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001893 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001894 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001895 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001896 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001897 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001898 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001899 }
1900 }
1901
1902 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001903 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1904 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1905 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001906 *
1907 * @param v The view that was clicked.
1908 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001909 protected void onClickAllAppsButton(View v) {
1910 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001911 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301912 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1913 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001914 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001915 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001916 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001917 }
1918 }
1919
Sunny Goyale6e72002017-01-12 16:55:36 -08001920 private void onClickPendingAppItem(final View v, final String packageName,
1921 boolean downloadStarted) {
1922 if (downloadStarted) {
1923 // If the download has started, simply direct to the market app.
1924 startMarketIntentForPackage(v, packageName);
1925 return;
1926 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001927 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001928 .setTitle(R.string.abandoned_promises_title)
1929 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001930 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1931 @Override
1932 public void onClick(DialogInterface dialogInterface, int i) {
1933 startMarketIntentForPackage(v, packageName);
1934 }
1935 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001936 .setNeutralButton(R.string.abandoned_clean_this,
1937 new DialogInterface.OnClickListener() {
1938 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001939 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001940 mWorkspace.removeAbandonedPromise(packageName, user);
1941 }
1942 })
1943 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001944 }
1945
1946 private void startMarketIntentForPackage(View v, String packageName) {
1947 ItemInfo item = (ItemInfo) v.getTag();
1948 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001949 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001950 }
1951
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001952 /**
1953 * Event handler for an app shortcut click.
1954 *
1955 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1956 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001957 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001958 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1959 Object tag = v.getTag();
1960 if (!(tag instanceof ShortcutInfo)) {
1961 throw new IllegalArgumentException("Input must be a Shortcut");
1962 }
1963
1964 // Open shortcut
1965 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001966
Sunny Goyal076839c2017-10-30 13:52:20 -07001967 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001968 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1969 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001970 ~FLAG_DISABLED_SUSPENDED &
1971 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001972 // If the app is only disabled because of the above flags, launch activity anyway.
1973 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001974 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001975 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1976 // Use a message specific to this shortcut, if it has one.
1977 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1978 return;
1979 }
1980 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001981 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001982 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001983 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001984 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1985 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001986 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001987 }
1988 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1989 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001990 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001991 }
1992
Chris Wren40c5ed32014-06-24 18:24:23 -04001993 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001994 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001995 String packageName = shortcut.intent.getComponent() != null ?
1996 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1997 if (!TextUtils.isEmpty(packageName)) {
1998 onClickPendingAppItem(v, packageName,
1999 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2000 return;
2001 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002002 }
2003
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002004 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002005 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002006 }
2007
Sunny Goyala7ce1662016-05-31 15:01:35 -07002008 private void startAppShortcutOrInfoActivity(View v) {
2009 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002010 Intent intent;
2011 if (item instanceof PromiseAppInfo) {
2012 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2013 intent = promiseAppInfo.getMarketIntent();
2014 } else {
2015 intent = item.getIntent();
2016 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002017 if (intent == null) {
2018 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002019 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002020 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002021 }
2022
2023 /**
2024 * Event handler for a folder icon click.
2025 *
2026 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2027 */
2028 protected void onClickFolderIcon(View v) {
2029 if (LOGD) Log.d(TAG, "onClickFolder");
2030 if (!(v instanceof FolderIcon)){
2031 throw new IllegalArgumentException("Input must be a FolderIcon");
2032 }
2033
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002034 Folder folder = ((FolderIcon) v).getFolder();
2035 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002036 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002037 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002038 }
Michael Jurka5130e402011-10-13 04:55:35 -07002039 }
2040
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002041 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002042 * Event handler for the wallpaper picker button that appears after a long press
2043 * on the home screen.
2044 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002045 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002046 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002047 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2048 return;
2049 }
2050
Tony Wickham785f7a52015-08-31 17:28:32 -07002051 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2052 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002053 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002054 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002055 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002056
2057 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002058 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2059 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002060 intent.setPackage(pickerPackage);
2061 }
2062
Sunny Goyala5ace712017-11-15 17:12:51 -08002063 final Bundle launchOptions;
2064 if (v != null) {
2065 intent.setSourceBounds(getViewBounds(v));
2066 // If there is no target package, use the default intent chooser animation
2067 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
2068 } else {
2069 launchOptions = null;
2070 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002071 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08002072 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002073 } catch (ActivityNotFoundException e) {
2074 setWaitingForResult(null);
2075 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2076 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002077 }
2078
Sunny Goyala7ce1662016-05-31 15:01:35 -07002079 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002081 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2082 try {
2083 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2084 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2085 // is enabled by default on NYC.
2086 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2087 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002088
2089 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2090 String id = ((ShortcutInfo) info).getDeepShortcutId();
2091 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302092 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002093 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002094 } else {
2095 // Could be launching some bookkeeping activity
2096 startActivity(intent, optsBundle);
2097 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002098 } finally {
2099 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002102 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2103 // corresponding permission. Show the appropriate permission prompt if that
2104 // is the case.
2105 if (intent.getComponent() == null
2106 && Intent.ACTION_CALL.equals(intent.getAction())
2107 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2108 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002109
2110 setWaitingForResult(PendingRequestArgs
2111 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002112 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2113 REQUEST_PERMISSION_CALL_PHONE);
2114 } else {
2115 // No idea why this was thrown.
2116 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
2119 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002120
Sunny Goyalfe770c92016-09-27 14:38:58 -07002121 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002122 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002123 if (Utilities.ATLEAST_MARSHMALLOW) {
2124 int left = 0, top = 0;
2125 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002126 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002127 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002128 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002129 if (icon != null) {
2130 Rect bounds = icon.getBounds();
2131 left = (width - bounds.width()) / 2;
2132 top = v.getPaddingTop();
2133 width = bounds.width();
2134 height = bounds.height();
2135 }
2136 }
2137 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2138 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2139 // On L devices, we use the device default slide-up transition.
2140 // On L MR1 devices, we use a custom version of the slide-up transition which
2141 // doesn't have the delay present in the device default.
2142 return ActivityOptions.makeCustomAnimation(
2143 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2144 }
2145 return null;
2146 }
2147
Tonye3c59252017-05-02 21:32:27 -07002148 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002149 int[] pos = new int[2];
2150 v.getLocationOnScreen(pos);
2151 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2152 }
2153
Sunny Goyala7ce1662016-05-31 15:01:35 -07002154 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002155 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002156 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2157 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002158 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002159 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002160 // Only launch using the new animation if the shortcut has not opted out (this is a
2161 // private contract between launcher and may be ignored in the future).
2162 boolean useLaunchAnimation = (v != null) &&
2163 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2164 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2165
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002166 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002167
2168 // Prepare intent
2169 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2170 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002171 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002172 }
2173 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002174 if (Utilities.ATLEAST_MARSHMALLOW
2175 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002176 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002177 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002178 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002179 // Shortcuts need some special checks due to legacy reasons.
2180 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002181 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002182 // Could be launching some bookkeeping activity
2183 startActivity(intent, optsBundle);
2184 } else {
2185 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2186 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2187 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002188
2189 if (v instanceof BubbleTextView) {
2190 // This is set to the view that launched the activity that navigated the user away
2191 // from launcher. Since there is no callback for when the activity has finished
2192 // launching, enable the press state and keep this reference to reset the press
2193 // state when we return to launcher.
2194 BubbleTextView btv = (BubbleTextView) v;
2195 btv.setStayPressed(true);
2196 setOnResumeCallback(btv);
2197 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002198 mAppLaunchSuccess = true;
2199 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002200 } catch (ActivityNotFoundException|SecurityException e) {
2201 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2202 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2203 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002204 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002205 }
2206
Tony Wickhamdadb3042016-02-24 11:07:00 -08002207 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002208 public boolean dispatchTouchEvent(MotionEvent ev) {
2209 mLastDispatchTouchEventX = ev.getX();
2210 return super.dispatchTouchEvent(ev);
2211 }
2212
2213 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002215 if (!isDraggingEnabled()) return false;
2216 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002217 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218
Jon Miranda51f037d2016-11-07 08:37:20 -08002219 boolean ignoreLongPressToOverview =
2220 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002221
Adam Cohen1697b792013-09-17 19:08:21 -07002222 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002223 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002224 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302225 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2226 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002227 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002228 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002229 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2230 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2231 return true;
2232 } else {
2233 return false;
2234 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002235 } else {
2236 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002237 }
Adam Cohen1697b792013-09-17 19:08:21 -07002238 }
2239
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002240 CellLayout.CellInfo longClickCellInfo = null;
2241 View itemUnderLongClick = null;
2242 if (v.getTag() instanceof ItemInfo) {
2243 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002244 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002245 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002246 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 }
2248
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 // The hotseat touch handling does not go through Workspace, and we always allow long press
2250 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002251 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002253 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002254 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002255 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302256 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2257 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002258 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002259 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002260 return false;
2261 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302262 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2263 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002264 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002265 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002266 }
Jon Miranda379198e2016-09-23 08:54:40 -07002267 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2268 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002270 final boolean isAllAppsButton =
2271 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2272 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2273 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002274 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002276 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 }
2278 }
2279 }
2280 return true;
2281 }
2282
Winson Chung3d503fb2011-07-13 17:25:49 -07002283 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002284 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 return mHotseat != null && layout != null &&
2286 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2287 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002288
Winson Chung3d503fb2011-07-13 17:25:49 -07002289 /**
2290 * Returns the CellLayout of the specified container at the specified screen.
2291 */
Sunny Goyal92820592015-03-02 11:31:35 -08002292 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2294 if (mHotseat != null) {
2295 return mHotseat.getLayout();
2296 } else {
2297 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002298 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002299 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002300 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002301 }
2302 }
2303
Michael Jurkae326f182011-11-21 14:05:46 -08002304 @Override
2305 public void onTrimMemory(int level) {
2306 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002307 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2308 // The widget preview db can result in holding onto over
2309 // 3MB of memory for caching which isn't necessary.
2310 SQLiteDatabase.releaseMemory();
2311
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002312 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002313 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002314 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002315 if (mLauncherCallbacks != null) {
2316 mLauncherCallbacks.onTrimMemory(level);
2317 }
Michael Jurkae326f182011-11-21 14:05:46 -08002318 }
2319
Michael Jurkad7c28052012-04-27 15:43:36 -07002320 @Override
2321 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002322 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002323 final List<CharSequence> text = event.getText();
2324 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002325 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002326 // TODO: When can workspace be null?
2327 text.add(mWorkspace == null
2328 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002329 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002330 return result;
2331 }
2332
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002333 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002334 * If the activity is currently paused, signal that we need to run the passed Runnable
2335 * in onResume.
2336 *
2337 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002338 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2339 * wrong when we're not running, and if the activity comes back to what the configuration was
2340 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002341 *
2342 * Implementation of the method from LauncherModel.Callbacks.
2343 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002344 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002345 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002346 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002347 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002348 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002349 if (run instanceof RunnableWithId) {
2350 // Remove any runnables which have the same id
2351 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002352 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002353 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002354 return true;
2355 } else {
2356 return false;
2357 }
2358 }
2359
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002360 public void setOnResumeCallback(OnResumeCallback callback) {
2361 if (mOnResumeCallback != null) {
2362 mOnResumeCallback.onLauncherResume();
2363 }
2364 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002365 }
2366
Michael Jurka7607c2f2013-04-03 14:33:19 -07002367 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002368 * If the activity is currently paused, signal that we need to re-run the loader
2369 * in onResume.
2370 *
2371 * This needs to be called from incoming places where resources might have been loaded
2372 * while we are paused. That is becaues the Configuration might be wrong
2373 * when we're not running, and if it comes back to what it was when we
2374 * were paused, we are not restarted.
2375 *
2376 * Implementation of the method from LauncherModel.Callbacks.
2377 *
2378 * @return true if we are currently paused. The caller might be able to
2379 * skip some work in that case since we will come back again.
2380 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002381 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002382 public boolean setLoadOnResume() {
2383 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002384 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002385 mOnResumeNeedsLoad = true;
2386 return true;
2387 } else {
2388 return false;
2389 }
2390 }
2391
2392 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002395 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002397 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002398 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002399 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002400 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002401 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002402 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002403
Joe Onorato9c1289c2009-08-17 11:03:03 -04002404 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002405 * Clear any pending bind callbacks. This is called when is loader is planning to
2406 * perform a full rebind from scratch.
2407 */
2408 @Override
2409 public void clearPendingBinds() {
2410 mBindOnResumeCallbacks.clear();
2411 if (mPendingExecutor != null) {
2412 mPendingExecutor.markCompleted();
2413 mPendingExecutor = null;
2414 }
2415 }
2416
2417 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 * Refreshes the shortcuts shown on the workspace.
2419 *
2420 * Implementation of the method from LauncherModel.Callbacks.
2421 */
2422 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002423 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002424 // Floating panels (except the full widget sheet) are associated with individual icons. If
2425 // we are starting a fresh bind, close all such panels as all the icons are about
2426 // to go away.
2427 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002428 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002429
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002430 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002431
Winson Chungd64d1762013-08-20 14:37:16 -07002432 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002433 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002434 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002435
Winson Chung3d503fb2011-07-13 17:25:49 -07002436 if (mHotseat != null) {
2437 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002439 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 }
2441
Adam Cohendcd297f2013-06-18 13:13:40 -07002442 @Override
2443 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002444 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002445 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2446 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002447 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2448 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002449 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002450 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2451 // If there are no screens, we need to have an empty screen
2452 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002453 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002454 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002455
Winsonc7d2e832016-07-28 12:24:55 -07002456 // After we have added all the screens, if the wallpaper was locked to the default state,
2457 // then notify to indicate that it can be released and a proper wallpaper offset can be
2458 // computed before the next layout
2459 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002460 }
2461
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002462 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002463 int count = orderedScreenIds.size();
2464 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002465 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002466 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002467 // No need to bind the first screen, as its always bound.
2468 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2469 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002470 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002471 }
2472
Sunny Goyalb23980c2017-08-17 07:45:25 -07002473 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002474 public void bindAppsAdded(final ArrayList<Long> newScreens,
2475 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002476 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002477 Runnable r = new Runnable() {
2478 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002479 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002480 }
2481 };
2482 if (waitUntilResume(r)) {
2483 return;
2484 }
2485
Winson Chungd64d1762013-08-20 14:37:16 -07002486 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002487 if (newScreens != null) {
2488 bindAddScreens(newScreens);
2489 }
Winson Chungd64d1762013-08-20 14:37:16 -07002490
2491 // We add the items without animation on non-visible pages, and with
2492 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002493 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002494 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002495 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002496 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002497 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002498 }
Winson Chungc58497e2013-09-03 17:48:37 -07002499
Winson Chung87412982013-10-03 18:34:14 -07002500 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002501 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002502 }
2503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 * Bind the items start-end from the list.
2506 *
2507 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002509 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002510 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002511 Runnable r = new Runnable() {
2512 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002513 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002514 }
2515 };
2516 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002517 return;
2518 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002519
Sunny Goyal3be633b2016-12-08 09:59:25 -08002520 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002521 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002522 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002523 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002524 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002525 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002526 int end = items.size();
2527 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002528 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002529
2530 // Short circuit if we are loading dock items for a configuration which has no dock
2531 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2532 mHotseat == null) {
2533 continue;
2534 }
2535
Sunny Goyal639e9062015-08-19 19:17:06 -07002536 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002537 switch (item.itemType) {
2538 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2539 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002540 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002541 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002542 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002543 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002544 }
2545 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002546 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002547 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002548 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002550 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002551 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2552 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002553 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002554 if (view == null) {
2555 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002556 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002557 break;
2558 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002559 default:
2560 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002561 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002562
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002563 /*
2564 * Remove colliding items.
2565 */
2566 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2567 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2568 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2569 View v = cl.getChildAt(item.cellX, item.cellY);
2570 Object tag = v.getTag();
2571 String desc = "Collision while binding workspace item: " + item
2572 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002573 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002574 throw (new RuntimeException(desc));
2575 } else {
2576 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002577 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002578 continue;
2579 }
2580 }
2581 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302582 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002583 if (animateIcons) {
2584 // Animate all the applications up now
2585 view.setAlpha(0f);
2586 view.setScaleX(0f);
2587 view.setScaleY(0f);
2588 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002589 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002592
Winson Chung997a9232013-07-24 15:33:46 -07002593 if (animateIcons) {
2594 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002595 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002596 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002597 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002598 final Runnable startBounceAnimRunnable = new Runnable() {
2599 public void run() {
2600 anim.playTogether(bounceAnims);
2601 anim.start();
2602 }
2603 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002604 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002605 // We post the animation slightly delayed to prevent slowdowns
2606 // when we are loading right after we return to launcher.
2607 mWorkspace.postDelayed(new Runnable() {
2608 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002609 if (mWorkspace != null) {
2610 mWorkspace.snapToPage(newScreenIndex);
2611 mWorkspace.postDelayed(startBounceAnimRunnable,
2612 NEW_APPS_ANIMATION_DELAY);
2613 }
Winson Chung94d67682013-09-25 16:29:40 -07002614 }
2615 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002616 } else {
2617 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002618 }
2619 }
Winson Chung64359a52013-07-08 17:17:08 -07002620 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002621 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 }
2623
Joe Onorato9c1289c2009-08-17 11:03:03 -04002624 /**
2625 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002626 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002627 public void bindAppWidget(LauncherAppWidgetInfo item) {
2628 View view = inflateAppWidget(item);
2629 if (view != null) {
2630 mWorkspace.addInScreen(view, item);
2631 mWorkspace.requestLayout();
2632 }
2633 }
2634
2635 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002636 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302637 PendingAppWidgetHostView view =
2638 new PendingAppWidgetHostView(this, item, mIconCache, true);
2639 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002640 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002641 }
2642
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002643 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002644
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002645 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002646
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002647 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2648 // If the provider is not ready, bind as a pending widget.
2649 appWidgetInfo = null;
2650 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2651 // The widget id is not valid. Try to find the widget based on the provider info.
2652 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2653 } else {
2654 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2655 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002656
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002657 // If the provider is ready, but the width is not yet restored, try to restore it.
2658 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2659 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002660 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002661 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2662 + " belongs to component " + item.providerName
2663 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002664 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002665 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002666 }
Sunny Goyalff572272014-07-23 13:58:07 -07002667
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002668 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002669 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002670 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2671 // Id has not been allocated yet. Allocate a new id.
2672 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2673 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002674
Sunny Goyald478c832016-04-01 12:04:16 -07002675 // Also try to bind the widget. If the bind fails, the user will be shown
2676 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002677 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002678 pendingInfo.spanX = item.spanX;
2679 pendingInfo.spanY = item.spanY;
2680 pendingInfo.minSpanX = item.minSpanX;
2681 pendingInfo.minSpanY = item.minSpanY;
2682 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002683
2684 boolean isDirectConfig =
2685 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2686 if (isDirectConfig && item.bindOptions != null) {
2687 Bundle newOptions = item.bindOptions.getExtras();
2688 if (options != null) {
2689 newOptions.putAll(options);
2690 }
2691 options = newOptions;
2692 }
Sunny Goyald478c832016-04-01 12:04:16 -07002693 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2694 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002695
Sunny Goyal86df1382016-08-10 15:03:22 -07002696 // We tried to bind once. If we were not able to bind, we would need to
2697 // go through the permission dialog, which means we cannot skip the config
2698 // activity.
2699 item.bindOptions = null;
2700 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2701
Sunny Goyald478c832016-04-01 12:04:16 -07002702 // Bind succeeded
2703 if (success) {
2704 // If the widget has a configure activity, it is still needs to set it up,
2705 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002706 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002707 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2708 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002709 }
Sunny Goyald478c832016-04-01 12:04:16 -07002710
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002711 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002712 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002713 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002714 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002715 // The widget was marked as UI not ready, but there is no configure activity to
2716 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002717 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002718 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002719 }
Sunny Goyalff572272014-07-23 13:58:07 -07002720 }
2721
Sunny Goyald5462aa2016-11-22 16:52:39 +05302722 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002723 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002724 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002725 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002726 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002727 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002728 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002729 }
2730
Sunny Goyal233ee962015-08-03 13:05:01 -07002731 item.minSpanX = appWidgetInfo.minSpanX;
2732 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302733 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002734 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302735 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002736 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302737 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002739 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002740 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 }
2742
Sunny Goyalff572272014-07-23 13:58:07 -07002743 /**
2744 * Restores a pending widget.
2745 *
2746 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002747 */
Sunny Goyald478c832016-04-01 12:04:16 -07002748 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002749 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2750 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2751 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002752 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002753 }
2754
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002755 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002756 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002757 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2758 info.pendingItemInfo = null;
2759 }
Sunny Goyalff572272014-07-23 13:58:07 -07002760
Sunny Goyalba47faa2017-10-04 16:50:57 -07002761 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2762 view.reinflate();
2763 }
2764
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002765 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002766 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002767 }
2768
Adam Cohen1462de32012-07-24 22:34:36 -07002769 public void onPageBoundSynchronously(int page) {
2770 mSynchronouslyBoundPages.add(page);
2771 }
2772
Sunny Goyal527c7d32015-08-28 15:19:36 -07002773 @Override
2774 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2775 if (mPendingExecutor != null) {
2776 mPendingExecutor.markCompleted();
2777 }
2778 mPendingExecutor = executor;
2779 executor.attachTo(this);
2780 }
2781
2782 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2783 if (mPendingExecutor == executor) {
2784 mPendingExecutor = null;
2785 }
2786 }
2787
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002788 @Override
2789 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2790 Runnable r = new Runnable() {
2791 public void run() {
2792 finishFirstPageBind(executor);
2793 }
2794 };
2795 if (waitUntilResume(r)) {
2796 return;
2797 }
2798
2799 Runnable onComplete = new Runnable() {
2800 @Override
2801 public void run() {
2802 if (executor != null) {
2803 executor.onLoadAnimationCompleted();
2804 }
2805 }
2806 };
2807 if (mDragLayer.getAlpha() < 1) {
2808 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2809 } else {
2810 onComplete.run();
2811 }
2812 }
2813
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 /**
2815 * Callback saying that there aren't any more items to bind.
2816 *
2817 * Implementation of the method from LauncherModel.Callbacks.
2818 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002819 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002820 Runnable r = new Runnable() {
2821 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002822 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002823 }
2824 };
2825 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002826 return;
2827 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002828 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002829 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002831 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002832
Sunny Goyal2100c782016-08-22 16:00:03 -07002833 if (mPendingActivityResult != null) {
2834 handleActivityResult(mPendingActivityResult.requestCode,
2835 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2836 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002837 }
2838
Sunny Goyala474a9b2017-05-04 16:47:11 -07002839 InstallShortcutReceiver.disableAndFlushInstallQueue(
2840 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002841
Tony Wickham010d2552017-01-20 08:15:28 -08002842 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002843 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002844 }
2845
Winson Chunga2413752012-04-03 14:22:34 -07002846 private boolean canRunNewAppsAnimation() {
2847 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002848 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002849 }
2850
Winson Chungc9168342013-06-26 14:54:55 -07002851 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002852 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002853 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2854 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002855 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002856 return bounceAnim;
2857 }
2858
Adam Cohen27772732013-11-11 14:00:56 +00002859 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002860 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002861 }
2862
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002863 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 * Add the icons for all apps.
2865 *
2866 * Implementation of the method from LauncherModel.Callbacks.
2867 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002868 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002869 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2870 public void run() {
2871 bindAllApplications(apps);
2872 }
2873 };
2874 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002875 return;
2876 }
2877
Winson Chungb745afb2015-03-02 11:51:23 -08002878 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002879 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002880 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002881 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002882 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002883 return;
2884 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002885
Winson Chungb745afb2015-03-02 11:51:23 -08002886 mAppsView.setApps(apps);
2887 }
Adam Cohen9211d422014-10-07 18:14:53 -07002888 if (mLauncherCallbacks != null) {
2889 mLauncherCallbacks.bindAllApplications(apps);
2890 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 }
2892
2893 /**
Tony2917a8b2017-07-31 23:29:42 -07002894 * Returns an Executor that will run after the launcher is first drawn (including after the
2895 * initial fade in animation). Returns null if the first draw has already occurred.
2896 */
2897 public @Nullable Executor getPendingExecutor() {
2898 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2899 }
2900
2901 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002902 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2903 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2904 */
2905 @Override
2906 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002907 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002908 }
2909
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002910 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911 * A package was updated.
2912 *
2913 * Implementation of the method from LauncherModel.Callbacks.
2914 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002915 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002916 Runnable r = new Runnable() {
2917 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002918 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002919 }
2920 };
2921 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002922 return;
2923 }
2924
Winson Chungb745afb2015-03-02 11:51:23 -08002925 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002926 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002927 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 }
2929
Sunny Goyal4390ace2014-10-13 11:33:11 -07002930 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002931 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2932 Runnable r = new Runnable() {
2933 public void run() {
2934 bindPromiseAppProgressUpdated(app);
2935 }
2936 };
2937 if (waitUntilResume(r)) {
2938 return;
2939 }
2940
2941 if (mAppsView != null) {
2942 mAppsView.updatePromiseAppProgress(app);
2943 }
2944 }
2945
2946 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002947 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2948 Runnable r = new Runnable() {
2949 public void run() {
2950 bindWidgetsRestored(widgets);
2951 }
2952 };
2953 if (waitUntilResume(r)) {
2954 return;
2955 }
2956 mWorkspace.widgetsRestored(widgets);
2957 }
2958
Joe Onorato9c1289c2009-08-17 11:03:03 -04002959 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002960 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002961 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002962 *
2963 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002964 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002965 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002966 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002967 Runnable r = new Runnable() {
2968 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002969 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002970 }
2971 };
2972 if (waitUntilResume(r)) {
2973 return;
2974 }
2975
Sunny Goyal4390ace2014-10-13 11:33:11 -07002976 if (!updated.isEmpty()) {
2977 mWorkspace.updateShortcuts(updated);
2978 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002979 }
2980
2981 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002982 * Update the state of a package, typically related to install state.
2983 *
2984 * Implementation of the method from LauncherModel.Callbacks.
2985 */
Sunny Goyale755d462014-07-22 13:48:29 -07002986 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002987 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
2988 Runnable r = new Runnable() {
2989 public void run() {
2990 bindRestoreItemsChange(updates);
2991 }
2992 };
2993 if (waitUntilResume(r)) {
2994 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002995 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002996
Sunny Goyal756adbc2015-04-16 15:20:51 -07002997 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002998 }
2999
3000 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003001 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003002 * in addition to specific applications to remove, the reason being that
3003 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003004 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003005 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003006 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003007 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003008 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003009 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003010 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003011 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003012 }
Winson Chungd64d1762013-08-20 14:37:16 -07003013 };
3014 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003015 return;
3016 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003017 mWorkspace.removeItemsByMatcher(matcher);
3018 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003019 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003020
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003021 @Override
3022 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3023 Runnable r = new Runnable() {
3024 public void run() {
3025 bindAppInfosRemoved(appInfos);
3026 }
3027 };
3028 if (waitUntilResume(r)) {
3029 return;
Joe Onorato36115782010-06-17 13:28:48 -04003030 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003031
Winson Chungdf95eb12013-10-16 14:57:07 -07003032 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003033 if (mAppsView != null) {
3034 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003035 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003036 }
Joe Onoratobe386092009-11-17 17:32:16 -08003037
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003038 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003039 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3040 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003041 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3042 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003043 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003044 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003045 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003046 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003047 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003048 return;
3049 }
3050
Tony Wickham26b17462017-03-20 17:12:24 -07003051 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3052 if (topView != null) {
3053 topView.onWidgetsBound();
3054 }
3055 }
3056
Tony Wickham86222d22017-03-29 15:30:43 -07003057 /**
3058 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3059 * refreshes the widgets and shortcuts associated with the given package/user
3060 */
3061 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003062 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003063 }
3064
Sunny Goyalc99cb172017-10-19 16:15:09 -07003065 public boolean isRotationEnabled () {
3066 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003067 }
3068
Winson Chung80baf5a2010-08-09 16:03:15 -07003069 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003070 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003071 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003072 @Override
3073 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3074 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003075
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003076 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3077 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003078 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003079 writer.println(prefix + " Homescreen " + i);
3080
3081 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3082 for (int j = 0; j < layout.getChildCount(); j++) {
3083 Object tag = layout.getChildAt(j).getTag();
3084 if (tag != null) {
3085 writer.println(prefix + " " + tag.toString());
3086 }
3087 }
3088 }
3089
3090 writer.println(prefix + " Hotseat");
3091 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003092 for (int j = 0; j < layout.getChildCount(); j++) {
3093 Object tag = layout.getChildAt(j).getTag();
3094 if (tag != null) {
3095 writer.println(prefix + " " + tag.toString());
3096 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003097 }
Sunny Goyala1365452015-10-01 15:46:24 -07003098
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003099 try {
3100 FileLog.flushAll(writer);
3101 } catch (Exception e) {
3102 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003103 }
3104 }
3105
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003106 writer.println(prefix + "Misc:");
3107 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3108 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3109 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3110
3111 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003112
Adam Cohen9211d422014-10-07 18:14:53 -07003113 if (mLauncherCallbacks != null) {
3114 mLauncherCallbacks.dump(prefix, fd, writer, args);
3115 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003116 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003117
Sunny Goyal66b24572016-09-21 15:57:55 -07003118 @Override
3119 @TargetApi(Build.VERSION_CODES.N)
3120 public void onProvideKeyboardShortcuts(
3121 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3122
3123 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003124 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003125 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3126 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3127 }
3128 View currentFocus = getCurrentFocus();
3129 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3130 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3131 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3132 }
Tony18c4aa42017-05-10 21:23:57 -05003133 if (currentFocus.getTag() instanceof ItemInfo
3134 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003135 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3136 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3137 }
3138 if (!shortcutInfos.isEmpty()) {
3139 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3140 }
3141
3142 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3143 }
3144
3145 @Override
3146 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3147 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3148 switch (keyCode) {
3149 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003150 if (isInState(NORMAL)) {
3151 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003152 return true;
3153 }
3154 break;
3155 case KeyEvent.KEYCODE_S: {
3156 View focusedView = getCurrentFocus();
3157 if (focusedView instanceof BubbleTextView
3158 && focusedView.getTag() instanceof ItemInfo
3159 && mAccessibilityDelegate.performAction(focusedView,
3160 (ItemInfo) focusedView.getTag(),
3161 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08003162 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003163 return true;
3164 }
3165 break;
3166 }
3167 case KeyEvent.KEYCODE_O:
3168 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3169 return true;
3170 }
3171 break;
3172 }
3173 }
3174 return super.onKeyShortcut(keyCode, event);
3175 }
3176
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003177 public static Launcher getLauncher(Context context) {
3178 if (context instanceof Launcher) {
3179 return (Launcher) context;
3180 }
3181 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3182 }
3183
Sunny Goyal5a81c382017-03-20 15:08:06 -07003184 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003185
3186 @Override
3187 public void onSharedPreferenceChanged(
3188 SharedPreferences sharedPreferences, String key) {
3189 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003190 // Recreate the activity so that it initializes the rotation preference again.
3191 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003192 }
3193 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003194 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003195
3196 /**
3197 * Callback for listening for onResume
3198 */
3199 public interface OnResumeCallback {
3200
3201 void onLauncherResume();
3202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203}