blob: 1461b5c3b0c9f418b27e1f7fcd9d94b3197385d9 [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 -070032
Sunny Goyal28c6b962015-10-12 11:42:05 -070033import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070035import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070039import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070041import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070049import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040050import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070052import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070053import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070054import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070055import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070057import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070058import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070068import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070072import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080073import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070077import android.view.KeyboardShortcutGroup;
78import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080079import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070088
Sunny Goyal651077b2014-06-30 14:15:31 -070089import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070091import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070092import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070093import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070094import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080095import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070096import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010097import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070098import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080099import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700100import com.android.launcher3.dragndrop.DragController;
101import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700102import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700103import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700104import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000105import com.android.launcher3.folder.Folder;
106import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700107import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700108import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700109import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700110import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800116import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800117import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530119import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700121import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700124import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700125import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700129import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700132import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700134import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800135import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800136import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800138import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800139import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700140import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700141import com.android.launcher3.widget.WidgetListRowEntry;
142import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700143import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700144
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700147import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700148import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700149import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700150import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800151import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700152import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154/**
155 * Default launcher application.
156 */
Sunny Goyal27835952017-01-13 12:15:53 -0800157public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700158 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
159 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700170 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
Winson Chung2672ff92012-05-04 16:22:30 -0700186 // The Intent extra that defines whether to ignore the launch animation
187 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400188 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
196 // Type: ActivityResultInfo
197 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700198 // Type: SparseArray<Parcelable>
199 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800201 // When starting an action mode, setting this tag will cause the action mode to be cancelled
202 // automatically when user interacts with the launcher.
203 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
204
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700205 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
213 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
214 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700215
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700217 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800219 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700220
Sunny Goyalffe83f12014-08-14 17:39:34 -0700221 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700222 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700223
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700224 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700225
Sunny Goyal316490e2015-06-02 09:38:28 -0700226 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700227
Sunny Goyal47328fd2016-05-25 18:56:41 -0700228 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700229
230 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700231 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700232 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700233
Winson Chung8ae41982017-11-10 11:42:13 -0800234 // UI and state for the overview panel
235 private ViewGroup mOverviewPanel;
236
Jon Miranda6bed3502017-09-19 14:43:17 -0700237 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
238 // that results in widgets being inflated in the wrong orientation.
239 private int mOrientation;
240
Adam Cohen091440a2015-03-18 14:16:05 -0700241 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800243 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700245 private OnResumeCallback mOnResumeCallback;
246
Sunny Goyal527c7d32015-08-28 15:19:36 -0700247 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700248
Joe Onorato9c1289c2009-08-17 11:03:03 -0400249 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800250 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700252 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253
Jon Miranda2d89ea82017-05-04 11:47:53 -0700254 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700255 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700256
Tony Wickham010d2552017-01-20 08:15:28 -0800257 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700258
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800259 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700260
Winson Chung46353de2012-02-16 14:05:10 -0800261 // We only want to get the SharedPreferences once since it does an FS stat each time we get
262 // it from the context.
263 private SharedPreferences mSharedPrefs;
264
Sunny Goyal2100c782016-08-22 16:00:03 -0700265 // Activity result which needs to be processed after workspace has loaded.
266 private ActivityResultInfo mPendingActivityResult;
267 /**
268 * Holds extra information required to handle a result from an external call, like
269 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
270 */
271 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800272
Jon Miranda379198e2016-09-23 08:54:40 -0700273 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700274
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700275 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700276 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700277 private boolean mAppLaunchSuccess;
278
Sunny Goyal745bad92016-05-02 10:54:12 -0700279 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800280 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 @Override
283 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700284 if (DEBUG_STRICT_MODE) {
285 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
286 .detectDiskReads()
287 .detectDiskWrites()
288 .detectNetwork() // or .detectAll() for all detectable problems
289 .penaltyLog()
290 .build());
291 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
292 .detectLeakedSqlLiteObjects()
293 .detectLeakedClosableObjects()
294 .penaltyLog()
295 .penaltyDeath()
296 .build());
297 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700298 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700299
Adam Cohen9211d422014-10-07 18:14:53 -0700300 if (mLauncherCallbacks != null) {
301 mLauncherCallbacks.preOnCreate();
302 }
303
Mario Bertschler27288382017-05-24 15:35:09 -0700304 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
305 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700306 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700309 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400310
Sunny Goyal87f784c2017-01-11 10:48:34 -0800311 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700312
Adam Cohen2e6da152015-05-06 11:42:25 -0700313 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800314 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700315 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700316 Display display = getWindowManager().getDefaultDisplay();
317 Point mwSize = new Point();
318 display.getSize(mwSize);
319 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
320 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700321
Jon Miranda6bed3502017-09-19 14:43:17 -0700322 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700323 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700324 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800325 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800326 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800327 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700328 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700329
Joe Onorato41a12d22009-10-31 18:30:00 -0400330 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700331 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800332 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700333
Sunny Goyalffe83f12014-08-14 17:39:34 -0700334 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700335
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700336 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700337 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700338
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700339 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
340 // this also ensures that any synchronous binding below doesn't re-trigger another
341 // LauncherModel load.
342 mPaused = false;
343
Sunny Goyal60820d72017-05-09 12:40:11 -0700344 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 setupViews();
Winson1f064272016-07-18 17:18:02 -0700347 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348
Tony Wickham010d2552017-01-20 08:15:28 -0800349 mPopupDataProvider = new PopupDataProvider(this);
350
Sunny Goyald3864fa2017-11-14 15:06:36 -0800351 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
352 // In case we are on a device with locked rotation, we should look at preferences to check
353 // if the user has specifically allowed rotation.
354 if (!mRotationEnabled) {
355 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
356 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
357 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359
Sunny Goyald3864fa2017-11-14 15:06:36 -0800360 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
361 if (internalStateHandled) {
362 // Temporarily enable the rotation
363 mRotationEnabled = true;
364
365 if (savedInstanceState != null) {
366 // InternalStateHandler has already set the appropriate state.
367 // We dont need to do anything.
368 savedInstanceState.remove(RUNTIME_STATE);
369 }
370 }
371 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800372
Sunny Goyal2100c782016-08-22 16:00:03 -0700373 // We only load the page synchronously if the user rotates (or triggers a
374 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700375 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
376 if (savedInstanceState != null) {
377 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
378 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800379
Sunny Goyalfe770c92016-09-27 14:38:58 -0700380 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800381 if (!internalStateHandled) {
382 // If we are not binding synchronously, show a fade in animation when
383 // the first page bind completes.
384 mDragLayer.setAlpha(0);
385 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700386 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700387 // Pages bound synchronously.
388 mWorkspace.setCurrentPage(currentScreen);
389
Sunny Goyal2100c782016-08-22 16:00:03 -0700390 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
392
393 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800394 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800395
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700396 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
397 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700398 setRequestedOrientation(mRotationEnabled
399 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700400
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800401 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700402
Jon Miranda7fda2852017-07-19 16:07:01 -0700403 // Listen for broadcasts
404 IntentFilter filter = new IntentFilter();
405 filter.addAction(Intent.ACTION_SCREEN_OFF);
406 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
407 registerReceiver(mReceiver, filter);
408 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700409
Sunny Goyal8392c822017-06-20 10:03:56 -0700410 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
411 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700412
413 if (mLauncherCallbacks != null) {
414 mLauncherCallbacks.onCreate(savedInstanceState);
415 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700416
417 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700418 }
419
Sunny Goyal7779d622015-06-11 16:18:39 -0700420 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700421 public void onThemeChanged() {
422 recreate();
423 }
424
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700425 public LauncherStateManager getStateManager() {
426 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700427 }
428
Mario Bertschlera6936942017-05-31 14:48:19 -0700429 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700430 if (isDark) {
431 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700432 } else if (supportsDarkText) {
433 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700434 }
435 }
436
437 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700438 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800439 return mLauncherView.findViewById(id);
440 }
441
442 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700443 public void onAppWidgetHostReset() {
444 if (mAppWidgetHost != null) {
445 mAppWidgetHost.startListening();
446 }
447 }
448
Adam Cohen9211d422014-10-07 18:14:53 -0700449 private LauncherCallbacks mLauncherCallbacks;
450
Winson1f064272016-07-18 17:18:02 -0700451 public void onInsetsChanged(Rect insets) {
452 mDeviceProfile.updateInsets(insets);
453 mDeviceProfile.layout(this, true /* notifyListeners */);
454 }
455
Sunny Goyal32554d12015-12-03 15:31:25 -0800456 /**
457 * Call this after onCreate to set or clear overlay.
458 */
459 public void setLauncherOverlay(LauncherOverlay overlay) {
460 if (overlay != null) {
461 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
462 }
463 mWorkspace.setLauncherOverlay(overlay);
464 }
465
Adam Cohen9211d422014-10-07 18:14:53 -0700466 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
467 mLauncherCallbacks = callbacks;
468 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700469 }
470
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700471 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700472 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700473 if (mLauncherCallbacks != null) {
474 mLauncherCallbacks.onLauncherProviderChange();
475 }
476 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700477
Hyunyoung Song3f471442015-04-08 19:01:34 -0700478 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700479 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
480 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700481 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700482 }
483
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000484 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700485 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
486 // This cast is safe as long as the id < 0x00FFFFFF
487 // Since we jail all the dynamically generated views, there should be no clashes
488 // with any other views.
489 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000490 }
491
Tony Wickham010d2552017-01-20 08:15:28 -0800492 public PopupDataProvider getPopupDataProvider() {
493 return mPopupDataProvider;
494 }
495
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000496 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700497 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
498 * a configuration step, this allows the proper animations to run after other transitions.
499 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700500 private long completeAdd(
501 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
502 long screenId = info.screenId;
503 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700504 // When the screen id represents an actual screen (as opposed to a rank) we make sure
505 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700506 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700507 }
Adam Cohendb364c32014-05-20 14:23:40 -0700508
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800510 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700511 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700512 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800513 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700515 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700516 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700518 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700519 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 int widgetId = appWidgetId;
521 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700522 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700524 // Since the view was just bound, also launch the configure activity if needed
525 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
526 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800527 if (provider != null) {
528 new WidgetAddFlowHandler(provider)
529 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700530 }
531 }
532 break;
533 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700535
Adam Cohendb364c32014-05-20 14:23:40 -0700536 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800537 }
538
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800539 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700540 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700541 if (isWorkspaceLoading()) {
542 // process the result once the workspace has loaded.
543 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
544 return;
545 }
546 mPendingActivityResult = null;
547
Winson Chunge341d302013-08-16 14:31:00 -0700548 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700549 final PendingRequestArgs requestArgs = mPendingRequestArgs;
550 setWaitingForResult(null);
551 if (requestArgs == null) {
552 return;
553 }
554
555 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700556
Adam Cohenad4e15c2013-10-17 16:21:35 -0700557 Runnable exitSpringLoaded = new Runnable() {
558 @Override
559 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700560 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700561 }
562 };
563
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700565 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700566 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700567 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
568 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700569 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700570 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 addAppWidgetImpl(
574 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800575 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700576 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 }
578 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200579 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700580 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700581 // User could have free-scrolled between pages before picking a wallpaper; make sure
582 // we move to the closest one now.
583 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700584 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200585 }
586 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700587 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200588
Adam Cohened66b2b2012-01-23 17:28:51 -0800589 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700590 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700591
Adam Cohened66b2b2012-01-23 17:28:51 -0800592 // We have special handling for widgets
593 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800594 final int appWidgetId;
595 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
596 : -1;
597 if (widgetId < 0) {
598 appWidgetId = pendingAddWidgetId;
599 } else {
600 appWidgetId = widgetId;
601 }
602
Adam Cohenad4e15c2013-10-17 16:21:35 -0700603 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800604 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700605 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
606 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700608 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700609 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 @Override
611 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700612 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700613 }
614 };
Adam Cohendb364c32014-05-20 14:23:40 -0700615
Sunny Goyal2100c782016-08-22 16:00:03 -0700616 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
617 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
618 } else {
619 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
620 // When the screen id represents an actual screen (as opposed to a rank)
621 // we make sure that the drop page actually exists.
622 requestArgs.screenId =
623 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700624 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700625 final CellLayout dropLayout =
626 mWorkspace.getScreenWithId(requestArgs.screenId);
627
628 dropLayout.setDropPending(true);
629 final Runnable onComplete = new Runnable() {
630 @Override
631 public void run() {
632 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
633 dropLayout.setDropPending(false);
634 }
635 };
636 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
637 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700638 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 return;
640 }
641
Sunny Goyald478c832016-04-01 12:04:16 -0700642 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
643 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700644 if (resultCode == RESULT_OK) {
645 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700646 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700647 }
648 // Leave the widget in the pending state if the user canceled the configure.
649 return;
650 }
651
Sunny Goyalaad90582015-07-09 14:22:50 -0700652 if (requestCode == REQUEST_CREATE_SHORTCUT) {
653 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
655 completeAdd(requestCode, data, -1, requestArgs);
656 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
657 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
658
Sunny Goyalaad90582015-07-09 14:22:50 -0700659 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700660 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
661 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800664 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800665 }
666
667 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700668 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800669 final int requestCode, final int resultCode, final Intent data) {
670 handleActivityResult(requestCode, resultCode, data);
671 if (mLauncherCallbacks != null) {
672 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
673 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 }
675
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700676 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700677 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600678 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 PendingRequestArgs pendingArgs = mPendingRequestArgs;
680 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
681 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
682 setWaitingForResult(null);
683
Sunny Goyal28c6b962015-10-12 11:42:05 -0700684 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700685 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700686 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 Intent intent = pendingArgs.getPendingIntent();
690
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 if (grantResults.length > 0
692 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700693 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700694 } else {
695 // TODO: Show a snack bar with link to settings
696 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700697 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700698 }
699 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600700 if (mLauncherCallbacks != null) {
701 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
702 grantResults);
703 }
704 }
705
Adam Cohendb364c32014-05-20 14:23:40 -0700706 /**
707 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
708 *
709 * @param screenId the screen id to check
710 * @return the new screen, or screenId if it exists
711 */
712 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800713 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700714 if (dropLayout == null) {
715 // it's possible that the add screen was removed because it was
716 // empty and a re-bind occurred
717 mWorkspace.addExtraEmptyScreen();
718 return mWorkspace.commitExtraEmptyScreen();
719 } else {
720 return screenId;
721 }
722 }
723
Sunny Goyal2100c782016-08-22 16:00:03 -0700724 @Thunk void completeTwoStageWidgetDrop(
725 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
726 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 Runnable onCompleteRunnable = null;
728 int animationType = 0;
729
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700730 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 if (resultCode == RESULT_OK) {
732 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
733 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800734 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 onCompleteRunnable = new Runnable() {
737 @Override
738 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700739 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700740 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 }
742 };
743 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800744 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700747 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700748 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
750 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 // The animated view may be null in the case of a rotation during widget configuration
753 onCompleteRunnable.run();
754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
756
757 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700758 protected void onStop() {
759 super.onStop();
760 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700761
762 if (mLauncherCallbacks != null) {
763 mLauncherCallbacks.onStop();
764 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800765 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800766
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700767 if (!mAppLaunchSuccess) {
768 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700769 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700770 }
Tony Wickham010d2552017-01-20 08:15:28 -0800771 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700772 }
773
774 @Override
775 protected void onStart() {
776 super.onStart();
777 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700778
779 if (mLauncherCallbacks != null) {
780 mLauncherCallbacks.onStart();
781 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800782 mAppWidgetHost.setListenIfResumed(true);
Tony Wickham010d2552017-01-20 08:15:28 -0800783
784 if (!isWorkspaceLoading()) {
785 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
786 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700787
Jon Miranda7fda2852017-07-19 16:07:01 -0700788 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700789 if (mScrimAnimator != null) {
790 mScrimAnimator.cancel();
791 }
792 mDragLayer.getBackground().setAlpha(0);
793 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
794 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
795 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
796 @Override
797 public void onAnimationEnd(Animator animation) {
798 mScrimAnimator = null;
799 }
800 });
801 mScrimAnimator.setDuration(600);
802 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
803 mScrimAnimator.start();
804 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700805 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700806 }
807
808 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700810 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700812 TraceHelper.partitionSection("ON_RESUME", "superCall");
813
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700814 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700815 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800816 mPaused = false;
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700817 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700818 // Process any items that were added while Launcher was away.
819 InstallShortcutReceiver.disableAndFlushInstallQueue(
820 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700821
Sunny Goyala474a9b2017-05-04 16:47:11 -0700822 // Refresh shortcuts if the permission changed.
823 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700824
Sunny Goyal7ede6112017-12-05 15:11:21 -0800825 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700826 if (mLauncherCallbacks != null) {
827 mLauncherCallbacks.onResume();
828 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800829
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700830 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700831 }
832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700834 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700835 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700836 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700837
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700838 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700839 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800840 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700841 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700842
Adam Cohen9211d422014-10-07 18:14:53 -0700843 if (mLauncherCallbacks != null) {
844 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700845 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700846 }
847
Adam Cohenc2d6e892014-10-16 09:49:24 -0700848 public interface LauncherOverlay {
849
850 /**
851 * Touch interaction leading to overscroll has begun
852 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700853 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700854
855 /**
856 * Touch interaction related to overscroll has ended
857 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700858 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859
860 /**
861 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
862 * screen (or in the case of RTL, the rightmost screen).
863 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700864 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700865
866 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800867 * Called when the launcher is ready to use the overlay
868 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700870 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700871 }
872
873 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700874 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700875 }
876
877 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
878
Sunny Goyalc86df472016-02-25 09:19:38 -0800879 public void onScrollChanged(float progress) {
880 if (mWorkspace != null) {
881 mWorkspace.onOverlayScrollChanged(progress);
882 }
883 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700884 }
885
Sunny Goyal16764582017-11-06 16:00:34 -0800886 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700887 if (mLauncherCallbacks != null) {
888 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700889 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800890 // On O and above we there is always some setting present settings (add icon to
891 // home screen or icon badging). On earlier APIs we will have the allow rotation
892 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700893 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700894 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800895 }
896
Sunny Goyalf9403d92017-10-18 10:55:56 -0700897 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700898 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700899 }
900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 /**
902 * Restores the previous state, if it exists.
903 *
904 * @param savedState The previous state.
905 */
906 private void restoreState(Bundle savedState) {
907 if (savedState == null) {
908 return;
909 }
910
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700911 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700912 LauncherState[] stateValues = LauncherState.values();
913 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800914 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700915 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800916 }
917
Sunny Goyal2100c782016-08-22 16:00:03 -0700918 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
919 if (requestArgs != null) {
920 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700922
923 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700924
925 SparseArray<Parcelable> widgetsState =
926 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
927 if (widgetsState != null) {
928 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
931
932 /**
933 * Finds all the views we need and configure them properly.
934 */
935 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -0700936 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700937 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700938 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700939 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800940 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700941
Sunny Goyal784f9c32016-03-23 16:56:54 -0700942 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
943 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
944 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945
Winson Chung4c98d922011-05-31 16:50:48 -0700946 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800947 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700948
Winson Chung3d503fb2011-07-13 17:25:49 -0700949 // Setup the hotseat
950 mHotseat = (Hotseat) findViewById(R.id.hotseat);
951 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700952 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 }
954
Winson Chung4c98d922011-05-31 16:50:48 -0700955 // Setup the workspace
956 mWorkspace.setHapticFeedbackEnabled(false);
957 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700958 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700959 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
960 // default state, otherwise we will update to the wrong offsets in RTL
961 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700962 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700963 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700964
Tony Wickham34d2c912015-09-11 09:27:58 -0700965 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700966 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700967
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700968 // Setup Apps
969 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700970
Winson Chung3d503fb2011-07-13 17:25:49 -0700971 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700972 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700973 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400974
Hyunyoung Songd725f642017-08-17 22:26:35 -0700975 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 /**
979 * Creates a view representing a shortcut.
980 *
981 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800983 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700984 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986
987 /**
988 * Creates a view representing a shortcut inflated from the specified resource.
989 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 * @param parent The group the shortcut belongs to.
991 * @param info The data structure describing the shortcut.
992 *
993 * @return A View inflated from layoutResId.
994 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700995 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800996 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
997 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800998 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001000 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 return favorite;
1002 }
1003
1004 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001005 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 *
1007 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001009 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001010 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001011 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1012 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001013 return;
1014 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001015
Jon Mirandac476d6e2017-05-04 16:30:01 -07001016 int[] cellXY = mTmpAddItemCellCoordinates;
1017 CellLayout layout = getCellLayout(container, screenId);
1018
Sunny Goyal782f0c92017-01-19 10:27:54 -08001019 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001020 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001021 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1022 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001023 }
1024
1025 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001026 // Legacy shortcuts are only supported for primary profile.
1027 info = Process.myUserHandle().equals(args.user)
1028 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001029
Sunny Goyalb57645f2017-03-20 13:57:28 -07001030 if (info == null) {
1031 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1032 return;
1033 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001034 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001035 // The app is trying to add a shortcut without sufficient permissions
1036 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1037 return;
1038 }
1039 }
1040
Jon Mirandac476d6e2017-05-04 16:30:01 -07001041 if (container < 0) {
1042 // Adding a shortcut to the Workspace.
1043 final View view = createShortcut(info);
1044 boolean foundCellSpan = false;
1045 // First we check if we already know the exact location where we want to add this item.
1046 if (cellX >= 0 && cellY >= 0) {
1047 cellXY[0] = cellX;
1048 cellXY[1] = cellY;
1049 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001050
Jon Mirandac476d6e2017-05-04 16:30:01 -07001051 // If appropriate, either create a folder or add to an existing folder
1052 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001053 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001054 return;
1055 }
1056 DragObject dragObject = new DragObject();
1057 dragObject.dragInfo = info;
1058 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1059 true)) {
1060 return;
1061 }
1062 } else {
1063 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1064 }
1065
1066 if (!foundCellSpan) {
1067 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001068 return;
1069 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001070
1071 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1072 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001073 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001074 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001075 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001076 if (folderIcon != null) {
1077 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1078 folderInfo.add(info, args.rank, false);
1079 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001080 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001081 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
1084
Sunny Goyale29897f2017-07-20 10:09:42 -07001085 public FolderIcon findFolderIcon(final long folderIconId) {
1086 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1087 @Override
1088 public boolean evaluate(ItemInfo info, View view) {
1089 return info != null && info.id == folderIconId;
1090 }
1091 });
1092 }
1093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001097 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001099 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001100 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1101
Adam Cohened66b2b2012-01-23 17:28:51 -08001102 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001103 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001104 }
Romain Guycbb89e42009-06-08 15:52:54 -07001105
Adam Cohen59400422014-03-05 18:07:04 -08001106 LauncherAppWidgetInfo launcherInfo;
1107 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001108 launcherInfo.spanX = itemInfo.spanX;
1109 launcherInfo.spanY = itemInfo.spanY;
1110 launcherInfo.minSpanX = itemInfo.minSpanX;
1111 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001112 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001114 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001115 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116
Sunny Goyal2100c782016-08-22 16:00:03 -07001117 if (hostView == null) {
1118 // Perform actual inflation because we're live
1119 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001121 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301122 prepareAppWidget(hostView, launcherInfo);
1123 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
Romain Guycbb89e42009-06-08 15:52:54 -07001125
Sunny Goyald5462aa2016-11-22 16:52:39 +05301126 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001127 hostView.setTag(item);
1128 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001129 hostView.setFocusable(true);
1130 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001131 }
1132
Adam Cohended9f8d2010-11-03 13:25:16 -07001133 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1134 @Override
1135 public void onReceive(Context context, Intent intent) {
1136 final String action = intent.getAction();
1137 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001138 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001139 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001140 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001141 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001142 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001143 mShouldFadeInScrim = true;
1144 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1145 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1146 // the user unlocked and the Launcher is not in the foreground.
1147 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001148 }
1149 }
1150 };
1151
Tony Wickham010d2552017-01-20 08:15:28 -08001152 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001153 mWorkspace.updateIconBadges(updatedBadges);
1154 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001155
Sunny Goyal29947f02017-12-18 13:49:44 -08001156 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1157 if (popup != null) {
1158 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001159 }
1160 }
1161
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 @Override
1163 public void onAttachedToWindow() {
1164 super.onAttachedToWindow();
1165
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001166 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001167 if (mLauncherCallbacks != null) {
1168 mLauncherCallbacks.onAttachedToWindow();
1169 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001170 }
1171
1172 @Override
1173 public void onDetachedFromWindow() {
1174 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001175
1176 if (mLauncherCallbacks != null) {
1177 mLauncherCallbacks.onDetachedFromWindow();
1178 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001179 }
1180
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001181 public AllAppsTransitionController getAllAppsController() {
1182 return mAllAppsController;
1183 }
1184
Winson Chunga6945242014-01-08 14:04:34 -08001185 public DragLayer getDragLayer() {
1186 return mDragLayer;
1187 }
1188
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001189 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001190 return mAppsView;
1191 }
1192
Winson Chunga6945242014-01-08 14:04:34 -08001193 public Workspace getWorkspace() {
1194 return mWorkspace;
1195 }
1196
1197 public Hotseat getHotseat() {
1198 return mHotseat;
1199 }
1200
Winson Chung8ae41982017-11-10 11:42:13 -08001201 public <T extends ViewGroup> T getOverviewPanel() {
1202 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001203 }
1204
Sunny Goyal47328fd2016-05-25 18:56:41 -07001205 public DropTargetBar getDropTargetBar() {
1206 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001207 }
1208
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209 public LauncherAppWidgetHost getAppWidgetHost() {
1210 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
Romain Guycbb89e42009-06-08 15:52:54 -07001212
Winson Chunga9abd0e2010-10-27 17:18:37 -07001213 public LauncherModel getModel() {
1214 return mModel;
1215 }
1216
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001217 public ModelWriter getModelWriter() {
1218 return mModelWriter;
1219 }
1220
Adam Cohen79d90c52016-04-22 13:29:20 -07001221 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001222 return mSharedPrefs;
1223 }
1224
Jon Miranda6bed3502017-09-19 14:43:17 -07001225 public int getOrientation() { return mOrientation; }
1226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 @Override
1228 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001229 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 super.onNewIntent(intent);
1231
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001232 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001233 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1234 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001235
1236 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001237 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001238 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001239 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001240 boolean internalStateHandled = InternalStateHandler
1241 .handleNewIntent(this, intent, alreadyOnHome);
1242
Winson15f8b172015-08-19 11:02:39 -07001243 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001244 if (!internalStateHandled) {
1245 // Note: There should be at most one log per method call. This is enforced
1246 // implicitly by using if-else statements.
1247 UserEventDispatcher ued = getUserEventDispatcher();
1248 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1249 if (topOpenView != null) {
1250 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1251 } else if (alreadyOnHome) {
1252 Target target = newContainerTarget(mStateManager.getState().containerType);
1253 target.pageIndex = mWorkspace.getCurrentPage();
1254 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1255 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001256
Sunny Goyald3864fa2017-11-14 15:06:36 -08001257 // In all these cases, only animate if we're already on home
1258 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001259
Sunny Goyald3864fa2017-11-14 15:06:36 -08001260 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1261
1262 // Reset the apps view
1263 if (!alreadyOnHome && mAppsView != null) {
1264 mAppsView.reset();
1265 }
1266
1267 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1268 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1269 }
1270 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001271
1272 final View v = getWindow().peekDecorView();
1273 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001274 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001275 }
1276
Adam Cohen9211d422014-10-07 18:14:53 -07001277 if (mLauncherCallbacks != null) {
1278 mLauncherCallbacks.onHomeIntent();
1279 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
Winson15f8b172015-08-19 11:02:39 -07001281
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001282 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001283 }
1284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001286 public void onRestoreInstanceState(Bundle state) {
1287 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001288 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
1290
1291 @Override
1292 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001293 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001294 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001295
Adam Cohen21cd0022013-10-09 18:57:02 -07001296 }
Sunny Goyalea609262017-10-25 15:47:38 -07001297 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001298
1299
1300 AbstractFloatingView widgets = AbstractFloatingView
1301 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1302 if (widgets != null) {
1303 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1304 widgets.saveHierarchyState(widgetsState);
1305 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1306 } else {
1307 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1308 }
1309
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001310 // We close any open folders and shortcut containers since they will not be re-opened,
1311 // and we need to make sure this state is reflected.
1312 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001313
Sunny Goyal2100c782016-08-22 16:00:03 -07001314 if (mPendingRequestArgs != null) {
1315 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1316 }
1317 if (mPendingActivityResult != null) {
1318 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001321 super.onSaveInstanceState(outState);
1322
Adam Cohen9211d422014-10-07 18:14:53 -07001323 if (mLauncherCallbacks != null) {
1324 mLauncherCallbacks.onSaveInstanceState(outState);
1325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
1328 @Override
1329 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001331
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001332 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001333 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001334
Winson Chungcd2b0142011-06-08 16:02:26 -07001335 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001336 // It's possible to receive onDestroy after a new Launcher activity has
1337 // been created. In this case, don't interfere with the new Launcher.
1338 if (mModel.isCurrentCallbacks(this)) {
1339 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001340 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001341 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001342
Sunny Goyal745bad92016-05-02 10:54:12 -07001343 if (mRotationPrefChangeHandler != null) {
1344 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1345 }
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001348 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001350 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001354 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1355
Michael Jurka2ecf9952012-06-18 12:52:28 -07001356 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001357
Tony2917a8b2017-07-31 23:29:42 -07001358 clearPendingBinds();
1359
Adam Cohen9211d422014-10-07 18:14:53 -07001360 if (mLauncherCallbacks != null) {
1361 mLauncherCallbacks.onDestroy();
1362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364
Sunny Goyalae502842016-06-17 08:43:56 -07001365 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1366 return mAccessibilityDelegate;
1367 }
1368
Adam Cohena9cf38f2011-05-02 15:36:58 -07001369 public DragController getDragController() {
1370 return mDragController;
1371 }
1372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001374 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001375 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001376 }
1377
1378 @Override
1379 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1380 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001381 try {
1382 super.startIntentSenderForResult(intent, requestCode,
1383 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1384 } catch (IntentSender.SendIntentException e) {
1385 throw new ActivityNotFoundException();
1386 }
1387 }
1388
Winson Chung70d72102011-08-12 11:24:35 -07001389 /**
1390 * Indicates that we want global search for this activity by setting the globalSearch
1391 * argument for {@link #startSearch} to true.
1392 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001394 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 Bundle appSearchData, boolean globalSearch) {
1396 if (appSearchData == null) {
1397 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001398 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001400
Sunny Goyal6f28e712016-09-13 14:19:29 -07001401 if (mLauncherCallbacks == null ||
1402 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1403 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001404 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001405 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001406
1407 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001408 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001409 }
1410
Joe Onorato9c1289c2009-08-17 11:03:03 -04001411 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001412 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001413 }
1414
Adam Cohen517a7f52014-03-01 12:12:59 -08001415 public boolean isWorkspaceLoading() {
1416 return mWorkspaceLoading;
1417 }
1418
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001419 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001420 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001421 }
1422
Sunny Goyal04a324a2017-01-20 21:08:59 -08001423 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001424 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001425 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001426
Sunny Goyal2100c782016-08-22 16:00:03 -07001427 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001428 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001429 if (LOGD) {
1430 Log.d(TAG, "Adding widget from drop");
1431 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001432 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001433 }
1434
Sunny Goyal2100c782016-08-22 16:00:03 -07001435 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001436 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1437 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1438 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001439
Adam Cohenad4e15c2013-10-17 16:21:35 -07001440 Runnable onComplete = new Runnable() {
1441 @Override
1442 public void run() {
1443 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001444 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001445 }
1446 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001447 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001448 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
1450 }
Romain Guycbb89e42009-06-08 15:52:54 -07001451
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001452 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1453 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 info.container = container;
1455 info.screenId = screenId;
1456 if (cell != null) {
1457 info.cellX = cell[0];
1458 info.cellY = cell[1];
1459 }
1460 info.spanX = spanX;
1461 info.spanY = spanY;
1462
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001463 switch (info.itemType) {
1464 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1465 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001466 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001467 break;
1468 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001469 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001470 break;
1471 default:
1472 throw new IllegalStateException("Unknown item type: " + info.itemType);
1473 }
1474 }
1475
Adam Cohenfbba09b2011-07-18 21:43:05 -07001476 /**
1477 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001478 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001479 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001480 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1481 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001482 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1483 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1484 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001485 }
1486
Adam Cohenfbba09b2011-07-18 21:43:05 -07001487 /**
1488 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001489 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001490 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001491 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001492 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001493 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001494 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001495 // In the case where we've prebound the widget, we remove it from the DragLayer
1496 if (LOGD) {
1497 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1498 }
1499 getDragLayer().removeView(hostView);
1500
Adam Cohened66b2b2012-01-23 17:28:51 -08001501 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001502 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001503
1504 // Clear the boundWidget so that it doesn't get destroyed.
1505 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001506 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001507 // In this case, we either need to start an activity to get permission to bind
1508 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001509 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1510 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1511 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1512 this, info.componentName);
1513 } else {
1514 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1515 }
Adam Cohendd70d662012-10-04 16:53:44 -07001516 Bundle options = info.bindOptions;
1517
Sunny Goyalffe83f12014-08-14 17:39:34 -07001518 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1519 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001520 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001521 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001522 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001523 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001524 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001525 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 }
1527
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001529 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001530 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 folderInfo.title = getText(R.string.folder_name);
1532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001534 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001537 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301538 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001539 // Force measure the new folder icon
1540 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1541 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001542 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Winsonc0b52fe2015-09-09 16:38:15 -07001545 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001546 * Unbinds the view for the specified item, and removes the item and all its children.
1547 *
1548 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001549 * @param itemInfo the {@link ItemInfo} for this view.
1550 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001551 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001552 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001553 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001554 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001555 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1556 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001557 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001558 } else {
1559 mWorkspace.removeWorkspaceItem(v);
1560 }
Winsonc0b52fe2015-09-09 16:38:15 -07001561 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001562 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001563 }
1564 } else if (itemInfo instanceof FolderInfo) {
1565 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001566 if (v instanceof FolderIcon) {
1567 ((FolderIcon) v).removeListeners();
1568 }
Winsonc0b52fe2015-09-09 16:38:15 -07001569 mWorkspace.removeWorkspaceItem(v);
1570 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001571 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001572 }
1573 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1574 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001575 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001576 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001577 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001578 }
1579 } else {
1580 return false;
1581 }
1582 return true;
1583 }
1584
1585 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001586 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001587 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001588 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001589 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001590 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001591 // Deleting an app widget ID is a void call but writes to disk before returning
1592 // to the caller...
1593 new AsyncTask<Void, Void, Void>() {
1594 public Void doInBackground(Void ... args) {
1595 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1596 return null;
1597 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001598 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001599 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001600 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001601 }
1602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 @Override
1604 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001605 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
1607
Joe Onorato88ec0992009-11-19 13:16:06 -08001608 @Override
1609 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001610 if (finishAutoCancelActionMode()) {
1611 return;
1612 }
Adam Cohen9211d422014-10-07 18:14:53 -07001613 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1614 return;
1615 }
1616
Sunny Goyal45478022015-06-08 16:52:41 -07001617 if (mDragController.isDragging()) {
1618 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001619 return;
1620 }
1621
Jon Mirandafeba90f2016-10-06 10:53:29 -07001622 // Note: There should be at most one log per method call. This is enforced implicitly
1623 // by using if-else statements.
1624 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001625 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1626 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001627 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001628 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001629 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001630 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001631 } else {
1632 // Back button is a no-op here, but give at least some feedback for the button press
1633 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001634 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001635 }
1636
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 * Launches the intent referred by the clicked shortcut.
1639 *
1640 * @param v The view representing the clicked shortcut.
1641 */
1642 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001643 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1644 // view has detached (it's possible for this to happen if the view is removed mid touch).
1645 if (v.getWindowToken() == null) {
1646 return;
1647 }
1648
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001649 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001650 return;
1651 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001652
Adam Cohen1697b792013-09-17 19:08:21 -07001653 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001654 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001655 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1656 LauncherLogProto.Action.Direction.NONE,
1657 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001658 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001659 }
1660 return;
1661 }
1662
1663 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001664 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001665 int page = mWorkspace.indexOfChild(v);
1666 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1667 LauncherLogProto.Action.Direction.NONE,
1668 LauncherLogProto.ContainerType.OVERVIEW, page);
1669 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001670 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001671 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001672 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001673 }
1674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001676 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001677 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001679 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001680 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001681 }
Sunny Goyal508da152014-08-14 10:53:27 -07001682 } else if (tag instanceof AppInfo) {
1683 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001684 } else if (tag instanceof LauncherAppWidgetInfo) {
1685 if (v instanceof PendingAppWidgetHostView) {
1686 onClickPendingWidget((PendingAppWidgetHostView) v);
1687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 }
1689 }
1690
Sunny Goyal70660032015-05-14 00:07:08 -07001691 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001692 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001693 return false;
1694 }
1695
Michael Jurkaaf442092010-06-10 17:01:57 -07001696 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001697 * Event handler for the app widget view which has not fully restored.
1698 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001699 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001700 if (mIsSafeModeEnabled) {
1701 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1702 return;
1703 }
1704
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001705 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001706 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001707 LauncherAppWidgetProviderInfo appWidgetInfo =
1708 mAppWidgetManager.findProvider(info.providerName, info.user);
1709 if (appWidgetInfo == null) {
1710 return;
1711 }
1712 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1713
Sunny Goyald478c832016-04-01 12:04:16 -07001714 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1715 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1716 // This should not happen, as we make sure that an Id is allocated during bind.
1717 return;
1718 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001719 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1720 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001721 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001722 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001723 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001724 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001725 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001726 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001727 }
1728 }
1729
Sunny Goyale6e72002017-01-12 16:55:36 -08001730 private void onClickPendingAppItem(final View v, final String packageName,
1731 boolean downloadStarted) {
1732 if (downloadStarted) {
1733 // If the download has started, simply direct to the market app.
1734 startMarketIntentForPackage(v, packageName);
1735 return;
1736 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001737 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001738 .setTitle(R.string.abandoned_promises_title)
1739 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001740 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1741 @Override
1742 public void onClick(DialogInterface dialogInterface, int i) {
1743 startMarketIntentForPackage(v, packageName);
1744 }
1745 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001746 .setNeutralButton(R.string.abandoned_clean_this,
1747 new DialogInterface.OnClickListener() {
1748 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001749 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001750 mWorkspace.removeAbandonedPromise(packageName, user);
1751 }
1752 })
1753 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001754 }
1755
1756 private void startMarketIntentForPackage(View v, String packageName) {
1757 ItemInfo item = (ItemInfo) v.getTag();
1758 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001759 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001760 }
1761
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001762 /**
1763 * Event handler for an app shortcut click.
1764 *
1765 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1766 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001767 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001768 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1769 Object tag = v.getTag();
1770 if (!(tag instanceof ShortcutInfo)) {
1771 throw new IllegalArgumentException("Input must be a Shortcut");
1772 }
1773
1774 // Open shortcut
1775 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001776
Sunny Goyal076839c2017-10-30 13:52:20 -07001777 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001778 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1779 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001780 ~FLAG_DISABLED_SUSPENDED &
1781 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001782 // If the app is only disabled because of the above flags, launch activity anyway.
1783 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001784 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001785 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1786 // Use a message specific to this shortcut, if it has one.
1787 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1788 return;
1789 }
1790 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001791 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001792 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001793 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001794 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1795 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001796 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001797 }
1798 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1799 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001800 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001801 }
1802
Chris Wren40c5ed32014-06-24 18:24:23 -04001803 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001804 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001805 String packageName = shortcut.intent.getComponent() != null ?
1806 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1807 if (!TextUtils.isEmpty(packageName)) {
1808 onClickPendingAppItem(v, packageName,
1809 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1810 return;
1811 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001812 }
1813
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001814 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001815 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001816 }
1817
Sunny Goyala7ce1662016-05-31 15:01:35 -07001818 private void startAppShortcutOrInfoActivity(View v) {
1819 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001820 Intent intent;
1821 if (item instanceof PromiseAppInfo) {
1822 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
1823 intent = promiseAppInfo.getMarketIntent();
1824 } else {
1825 intent = item.getIntent();
1826 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001827 if (intent == null) {
1828 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001829 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001830 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001831 }
1832
1833 /**
1834 * Event handler for a folder icon click.
1835 *
1836 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1837 */
1838 protected void onClickFolderIcon(View v) {
1839 if (LOGD) Log.d(TAG, "onClickFolder");
1840 if (!(v instanceof FolderIcon)){
1841 throw new IllegalArgumentException("Input must be a FolderIcon");
1842 }
1843
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001844 Folder folder = ((FolderIcon) v).getFolder();
1845 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001846 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001847 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001848 }
Michael Jurka5130e402011-10-13 04:55:35 -07001849 }
1850
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001851 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001852 * Event handler for the wallpaper picker button that appears after a long press
1853 * on the home screen.
1854 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001855 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001856 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001857 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1858 return;
1859 }
1860
Tony Wickham785f7a52015-08-31 17:28:32 -07001861 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1862 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001863 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001864 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001865 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001866
1867 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001868 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1869 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001870 intent.setPackage(pickerPackage);
1871 }
1872
Sunny Goyala5ace712017-11-15 17:12:51 -08001873 final Bundle launchOptions;
1874 if (v != null) {
1875 intent.setSourceBounds(getViewBounds(v));
1876 // If there is no target package, use the default intent chooser animation
1877 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
1878 } else {
1879 launchOptions = null;
1880 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001881 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001882 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001883 } catch (ActivityNotFoundException e) {
1884 setWaitingForResult(null);
1885 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1886 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001887 }
1888
Sunny Goyala7ce1662016-05-31 15:01:35 -07001889 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001891 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1892 try {
1893 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1894 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1895 // is enabled by default on NYC.
1896 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1897 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001898
1899 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1900 String id = ((ShortcutInfo) info).getDeepShortcutId();
1901 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301902 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001903 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001904 } else {
1905 // Could be launching some bookkeeping activity
1906 startActivity(intent, optsBundle);
1907 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001908 } finally {
1909 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001912 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1913 // corresponding permission. Show the appropriate permission prompt if that
1914 // is the case.
1915 if (intent.getComponent() == null
1916 && Intent.ACTION_CALL.equals(intent.getAction())
1917 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1918 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001919
1920 setWaitingForResult(PendingRequestArgs
1921 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001922 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1923 REQUEST_PERMISSION_CALL_PHONE);
1924 } else {
1925 // No idea why this was thrown.
1926 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 }
1929 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001930
Sunny Goyalfe770c92016-09-27 14:38:58 -07001931 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07001932 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001933 if (Utilities.ATLEAST_MARSHMALLOW) {
1934 int left = 0, top = 0;
1935 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001936 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001937 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001938 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07001939 if (icon != null) {
1940 Rect bounds = icon.getBounds();
1941 left = (width - bounds.width()) / 2;
1942 top = v.getPaddingTop();
1943 width = bounds.width();
1944 height = bounds.height();
1945 }
1946 }
1947 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
1948 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
1949 // On L devices, we use the device default slide-up transition.
1950 // On L MR1 devices, we use a custom version of the slide-up transition which
1951 // doesn't have the delay present in the device default.
1952 return ActivityOptions.makeCustomAnimation(
1953 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
1954 }
1955 return null;
1956 }
1957
Tonye3c59252017-05-02 21:32:27 -07001958 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001959 int[] pos = new int[2];
1960 v.getLocationOnScreen(pos);
1961 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1962 }
1963
Sunny Goyala7ce1662016-05-31 15:01:35 -07001964 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001965 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001966 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1967 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001968 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001969 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001970 // Only launch using the new animation if the shortcut has not opted out (this is a
1971 // private contract between launcher and may be ignored in the future).
1972 boolean useLaunchAnimation = (v != null) &&
1973 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1974 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
1975
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001976 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001977
1978 // Prepare intent
1979 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1980 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001981 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001982 }
1983 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001984 if (Utilities.ATLEAST_MARSHMALLOW
1985 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001986 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001987 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08001988 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001989 // Shortcuts need some special checks due to legacy reasons.
1990 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001991 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001992 // Could be launching some bookkeeping activity
1993 startActivity(intent, optsBundle);
1994 } else {
1995 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1996 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1997 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001998
1999 if (v instanceof BubbleTextView) {
2000 // This is set to the view that launched the activity that navigated the user away
2001 // from launcher. Since there is no callback for when the activity has finished
2002 // launching, enable the press state and keep this reference to reset the press
2003 // state when we return to launcher.
2004 BubbleTextView btv = (BubbleTextView) v;
2005 btv.setStayPressed(true);
2006 setOnResumeCallback(btv);
2007 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002008 mAppLaunchSuccess = true;
2009 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002010 } catch (ActivityNotFoundException|SecurityException e) {
2011 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2012 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2013 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002014 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002015 }
2016
Tony Wickhamdadb3042016-02-24 11:07:00 -08002017 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002018 public boolean dispatchTouchEvent(MotionEvent ev) {
2019 mLastDispatchTouchEventX = ev.getX();
2020 return super.dispatchTouchEvent(ev);
2021 }
2022
2023 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002025 if (!isDraggingEnabled()) return false;
2026 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002027 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028
Jon Miranda51f037d2016-11-07 08:37:20 -08002029 boolean ignoreLongPressToOverview =
2030 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002031
Adam Cohen1697b792013-09-17 19:08:21 -07002032 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002033 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002034 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302035 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2036 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002037 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002038 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002039 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2040 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2041 return true;
2042 } else {
2043 return false;
2044 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002045 } else {
2046 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002047 }
Adam Cohen1697b792013-09-17 19:08:21 -07002048 }
2049
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002050 CellLayout.CellInfo longClickCellInfo = null;
2051 View itemUnderLongClick = null;
2052 if (v.getTag() instanceof ItemInfo) {
2053 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002054 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002055 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002056 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058
Winson Chung3d503fb2011-07-13 17:25:49 -07002059 // The hotseat touch handling does not go through Workspace, and we always allow long press
2060 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002061 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002062 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002063 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002064 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002065 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302066 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2067 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002068 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002069 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002070 return false;
2071 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302072 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2073 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002074 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002075 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002076 }
Jon Miranda379198e2016-09-23 08:54:40 -07002077 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2078 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002080 final boolean isAllAppsButton =
2081 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2082 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2083 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002084 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002086 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088 }
2089 }
2090 return true;
2091 }
2092
Winson Chung3d503fb2011-07-13 17:25:49 -07002093 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002094 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002095 return mHotseat != null && layout != null &&
2096 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2097 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002098
Winson Chung3d503fb2011-07-13 17:25:49 -07002099 /**
2100 * Returns the CellLayout of the specified container at the specified screen.
2101 */
Sunny Goyal92820592015-03-02 11:31:35 -08002102 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002103 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2104 if (mHotseat != null) {
2105 return mHotseat.getLayout();
2106 } else {
2107 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002108 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002109 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002110 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002111 }
2112 }
2113
Michael Jurkae326f182011-11-21 14:05:46 -08002114 @Override
2115 public void onTrimMemory(int level) {
2116 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002117 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2118 // The widget preview db can result in holding onto over
2119 // 3MB of memory for caching which isn't necessary.
2120 SQLiteDatabase.releaseMemory();
2121
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002122 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002123 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002124 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002125 if (mLauncherCallbacks != null) {
2126 mLauncherCallbacks.onTrimMemory(level);
2127 }
Michael Jurkae326f182011-11-21 14:05:46 -08002128 }
2129
Michael Jurkad7c28052012-04-27 15:43:36 -07002130 @Override
2131 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002132 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002133 final List<CharSequence> text = event.getText();
2134 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002135 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002136 // TODO: When can workspace be null?
2137 text.add(mWorkspace == null
2138 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002139 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002140 return result;
2141 }
2142
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002143 public void setOnResumeCallback(OnResumeCallback callback) {
2144 if (mOnResumeCallback != null) {
2145 mOnResumeCallback.onLauncherResume();
2146 }
2147 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002148 }
2149
Michael Jurka7607c2f2013-04-03 14:33:19 -07002150 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002153 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002154 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002155 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002156 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002157 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002158 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002159 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002161
Joe Onorato9c1289c2009-08-17 11:03:03 -04002162 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002163 * Clear any pending bind callbacks. This is called when is loader is planning to
2164 * perform a full rebind from scratch.
2165 */
2166 @Override
2167 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002168 if (mPendingExecutor != null) {
2169 mPendingExecutor.markCompleted();
2170 mPendingExecutor = null;
2171 }
2172 }
2173
2174 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 * Refreshes the shortcuts shown on the workspace.
2176 *
2177 * Implementation of the method from LauncherModel.Callbacks.
2178 */
2179 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002180 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002181 // Floating panels (except the full widget sheet) are associated with individual icons. If
2182 // we are starting a fresh bind, close all such panels as all the icons are about
2183 // to go away.
2184 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002185 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002186
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002187 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002188
Winson Chungd64d1762013-08-20 14:37:16 -07002189 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002190 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002191 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002192
Winson Chung3d503fb2011-07-13 17:25:49 -07002193 if (mHotseat != null) {
2194 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002196 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198
Adam Cohendcd297f2013-06-18 13:13:40 -07002199 @Override
2200 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002201 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002202 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2203 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002204 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2205 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002206 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002207 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2208 // If there are no screens, we need to have an empty screen
2209 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002210 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002211 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002212
Winsonc7d2e832016-07-28 12:24:55 -07002213 // After we have added all the screens, if the wallpaper was locked to the default state,
2214 // then notify to indicate that it can be released and a proper wallpaper offset can be
2215 // computed before the next layout
2216 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002217 }
2218
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002219 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002220 int count = orderedScreenIds.size();
2221 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002222 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002223 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002224 // No need to bind the first screen, as its always bound.
2225 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2226 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 }
2229
Sunny Goyalb23980c2017-08-17 07:45:25 -07002230 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002231 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2232 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002233 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002234 if (newScreens != null) {
2235 bindAddScreens(newScreens);
2236 }
Winson Chungd64d1762013-08-20 14:37:16 -07002237
2238 // We add the items without animation on non-visible pages, and with
2239 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002240 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002241 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002242 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002243 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002244 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002245 }
Winson Chungc58497e2013-09-03 17:48:37 -07002246
Winson Chung87412982013-10-03 18:34:14 -07002247 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002248 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002249 }
2250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 * Bind the items start-end from the list.
2253 *
2254 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002256 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002257 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002258 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002259 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002260 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002261 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002262 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002263 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002264 int end = items.size();
2265 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002266 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002267
2268 // Short circuit if we are loading dock items for a configuration which has no dock
2269 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2270 mHotseat == null) {
2271 continue;
2272 }
2273
Sunny Goyal639e9062015-08-19 19:17:06 -07002274 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002275 switch (item.itemType) {
2276 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2277 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002278 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002279 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002280 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002281 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002282 }
2283 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002284 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002285 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002286 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002287 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002288 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002289 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2290 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002291 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002292 if (view == null) {
2293 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002294 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002295 break;
2296 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002297 default:
2298 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002299 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002300
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002301 /*
2302 * Remove colliding items.
2303 */
2304 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2305 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2306 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2307 View v = cl.getChildAt(item.cellX, item.cellY);
2308 Object tag = v.getTag();
2309 String desc = "Collision while binding workspace item: " + item
2310 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002311 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002312 throw (new RuntimeException(desc));
2313 } else {
2314 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002315 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002316 continue;
2317 }
2318 }
2319 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302320 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002321 if (animateIcons) {
2322 // Animate all the applications up now
2323 view.setAlpha(0f);
2324 view.setScaleX(0f);
2325 view.setScaleY(0f);
2326 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002327 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002330
Winson Chung997a9232013-07-24 15:33:46 -07002331 if (animateIcons) {
2332 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002333 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002334 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002335 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002336 final Runnable startBounceAnimRunnable = new Runnable() {
2337 public void run() {
2338 anim.playTogether(bounceAnims);
2339 anim.start();
2340 }
2341 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002342 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002343 // We post the animation slightly delayed to prevent slowdowns
2344 // when we are loading right after we return to launcher.
2345 mWorkspace.postDelayed(new Runnable() {
2346 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002347 if (mWorkspace != null) {
2348 mWorkspace.snapToPage(newScreenIndex);
2349 mWorkspace.postDelayed(startBounceAnimRunnable,
2350 NEW_APPS_ANIMATION_DELAY);
2351 }
Winson Chung94d67682013-09-25 16:29:40 -07002352 }
2353 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002354 } else {
2355 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002356 }
2357 }
Winson Chung64359a52013-07-08 17:17:08 -07002358 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002359 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360 }
2361
Joe Onorato9c1289c2009-08-17 11:03:03 -04002362 /**
2363 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002364 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002365 public void bindAppWidget(LauncherAppWidgetInfo item) {
2366 View view = inflateAppWidget(item);
2367 if (view != null) {
2368 mWorkspace.addInScreen(view, item);
2369 mWorkspace.requestLayout();
2370 }
2371 }
2372
2373 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002374 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302375 PendingAppWidgetHostView view =
2376 new PendingAppWidgetHostView(this, item, mIconCache, true);
2377 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002378 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002379 }
2380
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002381 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002382
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002383 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002384
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002385 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2386 // If the provider is not ready, bind as a pending widget.
2387 appWidgetInfo = null;
2388 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2389 // The widget id is not valid. Try to find the widget based on the provider info.
2390 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2391 } else {
2392 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2393 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002394
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002395 // If the provider is ready, but the width is not yet restored, try to restore it.
2396 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2397 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002398 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002399 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2400 + " belongs to component " + item.providerName
2401 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002402 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002403 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002404 }
Sunny Goyalff572272014-07-23 13:58:07 -07002405
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002406 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002407 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002408 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2409 // Id has not been allocated yet. Allocate a new id.
2410 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2411 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002412
Sunny Goyald478c832016-04-01 12:04:16 -07002413 // Also try to bind the widget. If the bind fails, the user will be shown
2414 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002415 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002416 pendingInfo.spanX = item.spanX;
2417 pendingInfo.spanY = item.spanY;
2418 pendingInfo.minSpanX = item.minSpanX;
2419 pendingInfo.minSpanY = item.minSpanY;
2420 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002421
2422 boolean isDirectConfig =
2423 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2424 if (isDirectConfig && item.bindOptions != null) {
2425 Bundle newOptions = item.bindOptions.getExtras();
2426 if (options != null) {
2427 newOptions.putAll(options);
2428 }
2429 options = newOptions;
2430 }
Sunny Goyald478c832016-04-01 12:04:16 -07002431 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2432 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002433
Sunny Goyal86df1382016-08-10 15:03:22 -07002434 // We tried to bind once. If we were not able to bind, we would need to
2435 // go through the permission dialog, which means we cannot skip the config
2436 // activity.
2437 item.bindOptions = null;
2438 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2439
Sunny Goyald478c832016-04-01 12:04:16 -07002440 // Bind succeeded
2441 if (success) {
2442 // If the widget has a configure activity, it is still needs to set it up,
2443 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002444 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002445 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2446 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002447 }
Sunny Goyald478c832016-04-01 12:04:16 -07002448
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002449 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002450 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002451 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002452 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002453 // The widget was marked as UI not ready, but there is no configure activity to
2454 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002455 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002456 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002457 }
Sunny Goyalff572272014-07-23 13:58:07 -07002458 }
2459
Sunny Goyald5462aa2016-11-22 16:52:39 +05302460 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002461 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002462 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002463 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002464 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002465 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002466 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002467 }
2468
Sunny Goyal233ee962015-08-03 13:05:01 -07002469 item.minSpanX = appWidgetInfo.minSpanX;
2470 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302471 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002472 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302473 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002474 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302475 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002476
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002477 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002478 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002479 }
2480
Sunny Goyalff572272014-07-23 13:58:07 -07002481 /**
2482 * Restores a pending widget.
2483 *
2484 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002485 */
Sunny Goyald478c832016-04-01 12:04:16 -07002486 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002487 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2488 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2489 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002490 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002491 }
2492
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002493 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002494 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002495 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2496 info.pendingItemInfo = null;
2497 }
Sunny Goyalff572272014-07-23 13:58:07 -07002498
Sunny Goyalba47faa2017-10-04 16:50:57 -07002499 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002500 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002501 }
2502
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002503 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002504 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002505 }
2506
Adam Cohen1462de32012-07-24 22:34:36 -07002507 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002508 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002509 }
2510
Sunny Goyal527c7d32015-08-28 15:19:36 -07002511 @Override
2512 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2513 if (mPendingExecutor != null) {
2514 mPendingExecutor.markCompleted();
2515 }
2516 mPendingExecutor = executor;
2517 executor.attachTo(this);
2518 }
2519
2520 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2521 if (mPendingExecutor == executor) {
2522 mPendingExecutor = null;
2523 }
2524 }
2525
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002526 @Override
2527 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002528 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002529 mDragLayer.animate().alpha(1).withEndAction(
2530 executor == null ? null : executor::onLoadAnimationCompleted).start();
2531 } else if (executor != null) {
2532 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002533 }
2534 }
2535
Joe Onorato9c1289c2009-08-17 11:03:03 -04002536 /**
2537 * Callback saying that there aren't any more items to bind.
2538 *
2539 * Implementation of the method from LauncherModel.Callbacks.
2540 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002541 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002542 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002543 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002544
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002545 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002546
Sunny Goyal2100c782016-08-22 16:00:03 -07002547 if (mPendingActivityResult != null) {
2548 handleActivityResult(mPendingActivityResult.requestCode,
2549 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2550 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002551 }
2552
Sunny Goyala474a9b2017-05-04 16:47:11 -07002553 InstallShortcutReceiver.disableAndFlushInstallQueue(
2554 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002555
Tony Wickham010d2552017-01-20 08:15:28 -08002556 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002557 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002558 }
2559
Winson Chunga2413752012-04-03 14:22:34 -07002560 private boolean canRunNewAppsAnimation() {
2561 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002562 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002563 }
2564
Winson Chungc9168342013-06-26 14:54:55 -07002565 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002566 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002567 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2568 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002569 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002570 return bounceAnim;
2571 }
2572
Adam Cohen27772732013-11-11 14:00:56 +00002573 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002574 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002575 }
2576
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002577 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578 * Add the icons for all apps.
2579 *
2580 * Implementation of the method from LauncherModel.Callbacks.
2581 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002582 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002583 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002584 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002585 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002586 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002587 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002588 return;
2589 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002590
Winson Chungb745afb2015-03-02 11:51:23 -08002591 mAppsView.setApps(apps);
2592 }
Adam Cohen9211d422014-10-07 18:14:53 -07002593 if (mLauncherCallbacks != null) {
2594 mLauncherCallbacks.bindAllApplications(apps);
2595 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002596 }
2597
2598 /**
Tony2917a8b2017-07-31 23:29:42 -07002599 * Returns an Executor that will run after the launcher is first drawn (including after the
2600 * initial fade in animation). Returns null if the first draw has already occurred.
2601 */
2602 public @Nullable Executor getPendingExecutor() {
2603 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2604 }
2605
2606 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002607 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2608 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2609 */
2610 @Override
2611 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002612 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002613 }
2614
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002615 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002616 * A package was updated.
2617 *
2618 * Implementation of the method from LauncherModel.Callbacks.
2619 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002620 @Override
2621 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002622 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002623 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002624 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002625 }
2626
Sunny Goyal4390ace2014-10-13 11:33:11 -07002627 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002628 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002629 if (mAppsView != null) {
2630 mAppsView.updatePromiseAppProgress(app);
2631 }
2632 }
2633
2634 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002635 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002636 mWorkspace.widgetsRestored(widgets);
2637 }
2638
Joe Onorato9c1289c2009-08-17 11:03:03 -04002639 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002640 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002641 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002642 *
2643 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002644 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002645 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002646 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002647 if (!updated.isEmpty()) {
2648 mWorkspace.updateShortcuts(updated);
2649 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002650 }
2651
2652 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002653 * Update the state of a package, typically related to install state.
2654 *
2655 * Implementation of the method from LauncherModel.Callbacks.
2656 */
Sunny Goyale755d462014-07-22 13:48:29 -07002657 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002658 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002659 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002660 }
2661
2662 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002663 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002664 * in addition to specific applications to remove, the reason being that
2665 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002666 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002667 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002668 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002669 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002670 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002671 mWorkspace.removeItemsByMatcher(matcher);
2672 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002673 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002674
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002675 @Override
2676 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002677 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002678 if (mAppsView != null) {
2679 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002680 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002681 }
Joe Onoratobe386092009-11-17 17:32:16 -08002682
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002683 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002684 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2685 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002686 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2687 if (topView != null) {
2688 topView.onWidgetsBound();
2689 }
2690 }
2691
Tony Wickham86222d22017-03-29 15:30:43 -07002692 /**
2693 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2694 * refreshes the widgets and shortcuts associated with the given package/user
2695 */
2696 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002697 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002698 }
2699
Sunny Goyalc99cb172017-10-19 16:15:09 -07002700 public boolean isRotationEnabled () {
2701 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002702 }
2703
Winson Chung80baf5a2010-08-09 16:03:15 -07002704 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002705 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002706 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002707 @Override
2708 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2709 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002710
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002711 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2712 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002713 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002714 writer.println(prefix + " Homescreen " + i);
2715
2716 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2717 for (int j = 0; j < layout.getChildCount(); j++) {
2718 Object tag = layout.getChildAt(j).getTag();
2719 if (tag != null) {
2720 writer.println(prefix + " " + tag.toString());
2721 }
2722 }
2723 }
2724
2725 writer.println(prefix + " Hotseat");
2726 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002727 for (int j = 0; j < layout.getChildCount(); j++) {
2728 Object tag = layout.getChildAt(j).getTag();
2729 if (tag != null) {
2730 writer.println(prefix + " " + tag.toString());
2731 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002732 }
Sunny Goyala1365452015-10-01 15:46:24 -07002733
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002734 try {
2735 FileLog.flushAll(writer);
2736 } catch (Exception e) {
2737 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002738 }
2739 }
2740
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002741 writer.println(prefix + "Misc:");
2742 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2743 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2744 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2745
2746 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002747
Adam Cohen9211d422014-10-07 18:14:53 -07002748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.dump(prefix, fd, writer, args);
2750 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002751 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002752
Sunny Goyal66b24572016-09-21 15:57:55 -07002753 @Override
2754 @TargetApi(Build.VERSION_CODES.N)
2755 public void onProvideKeyboardShortcuts(
2756 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2757
2758 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002759 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002760 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2761 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2762 }
2763 View currentFocus = getCurrentFocus();
2764 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2765 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2766 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2767 }
Tony18c4aa42017-05-10 21:23:57 -05002768 if (currentFocus.getTag() instanceof ItemInfo
2769 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002770 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2771 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2772 }
2773 if (!shortcutInfos.isEmpty()) {
2774 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2775 }
2776
2777 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2778 }
2779
2780 @Override
2781 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2782 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2783 switch (keyCode) {
2784 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002785 if (isInState(NORMAL)) {
2786 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002787 return true;
2788 }
2789 break;
2790 case KeyEvent.KEYCODE_S: {
2791 View focusedView = getCurrentFocus();
2792 if (focusedView instanceof BubbleTextView
2793 && focusedView.getTag() instanceof ItemInfo
2794 && mAccessibilityDelegate.performAction(focusedView,
2795 (ItemInfo) focusedView.getTag(),
2796 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002797 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002798 return true;
2799 }
2800 break;
2801 }
2802 case KeyEvent.KEYCODE_O:
2803 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2804 return true;
2805 }
2806 break;
2807 }
2808 }
2809 return super.onKeyShortcut(keyCode, event);
2810 }
2811
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002812 public static Launcher getLauncher(Context context) {
2813 if (context instanceof Launcher) {
2814 return (Launcher) context;
2815 }
2816 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2817 }
2818
Sunny Goyal5a81c382017-03-20 15:08:06 -07002819 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002820
2821 @Override
2822 public void onSharedPreferenceChanged(
2823 SharedPreferences sharedPreferences, String key) {
2824 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002825 // Recreate the activity so that it initializes the rotation preference again.
2826 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002827 }
2828 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002829 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002830
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002831 @Override
2832 public void onActionModeStarted(ActionMode mode) {
2833 super.onActionModeStarted(mode);
2834 mCurrentActionMode = mode;
2835 }
2836
2837 @Override
2838 public void onActionModeFinished(ActionMode mode) {
2839 super.onActionModeFinished(mode);
2840 mCurrentActionMode = null;
2841 }
2842
2843 public boolean finishAutoCancelActionMode() {
2844 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2845 mCurrentActionMode.finish();
2846 return true;
2847 }
2848 return false;
2849 }
2850
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002851 /**
2852 * Callback for listening for onResume
2853 */
2854 public interface OnResumeCallback {
2855
2856 void onLauncherResume();
2857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858}