blob: 32342b29dda37bac0b58cb015de5686536d4d3c2 [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;
Sunny Goyal02424b22018-01-19 11:24:32 -080059import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070062import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020063import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070065import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070069import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070073import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080074import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070078import android.view.KeyboardShortcutGroup;
79import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080080import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal651077b2014-06-30 14:15:31 -070090import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070091import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070092import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070093import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080096import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070099import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700101import com.android.launcher3.dragndrop.DragController;
102import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700103import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700105import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000106import com.android.launcher3.folder.Folder;
107import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700108import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700109import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700110import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800114import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700116import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800117import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800118import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800119import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530120import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
121import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700122import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700124import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700125import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700126import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700127import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800128import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700129import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700133import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800136import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800137import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800139import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800140import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700141import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142import com.android.launcher3.widget.WidgetListRowEntry;
143import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700144import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700145
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700147import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700148import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700149import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700150import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700151import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800152import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700153import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155/**
156 * Default launcher application.
157 */
Sunny Goyal27835952017-01-13 12:15:53 -0800158public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700159 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
160 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700161 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700162 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700163 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chunga2413752012-04-03 14:22:34 -0700165 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700171 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Michael Jurka8b805b12012-04-18 14:23:14 -0700173 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700174 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
175 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700176
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700178
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700179 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
180
Mathew Inwood876a8462013-06-14 14:12:41 +0100181 /**
182 * IntentStarter uses request codes starting with this. This must be greater than all activity
183 * request codes used internally.
184 */
185 protected static final int REQUEST_LAST = 100;
186
Winson Chung2672ff92012-05-04 16:22:30 -0700187 // The Intent extra that defines whether to ignore the launch animation
188 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400189 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: int
192 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700193 // Type: int
194 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700195 // Type: PendingRequestArgs
196 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
197 // Type: ActivityResultInfo
198 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700199 // Type: SparseArray<Parcelable>
200 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800202 // When starting an action mode, setting this tag will cause the action mode to be cancelled
203 // automatically when user interacts with the launcher.
204 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
205
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700206 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chunga2413752012-04-03 14:22:34 -0700212 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700213 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
214 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
215 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700218 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800220 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700221
Sunny Goyalffe83f12014-08-14 17:39:34 -0700222 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700223 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700225 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700226
Sunny Goyal316490e2015-06-02 09:38:28 -0700227 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700228
Sunny Goyal47328fd2016-05-25 18:56:41 -0700229 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700230
231 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700232 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700233 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
Winson Chung8ae41982017-11-10 11:42:13 -0800235 // UI and state for the overview panel
236 private ViewGroup mOverviewPanel;
237
Jon Miranda6bed3502017-09-19 14:43:17 -0700238 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
239 // that results in widgets being inflated in the wrong orientation.
240 private int mOrientation;
241
Adam Cohen091440a2015-03-18 14:16:05 -0700242 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400243
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700244 private OnResumeCallback mOnResumeCallback;
245
Sunny Goyal527c7d32015-08-28 15:19:36 -0700246 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700247
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800249 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700251 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252
Jon Miranda2d89ea82017-05-04 11:47:53 -0700253 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700254 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700255
Tony Wickham010d2552017-01-20 08:15:28 -0800256 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700257
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800258 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700259
Winson Chung46353de2012-02-16 14:05:10 -0800260 // We only want to get the SharedPreferences once since it does an FS stat each time we get
261 // it from the context.
262 private SharedPreferences mSharedPrefs;
263
Sunny Goyal2100c782016-08-22 16:00:03 -0700264 // Activity result which needs to be processed after workspace has loaded.
265 private ActivityResultInfo mPendingActivityResult;
266 /**
267 * Holds extra information required to handle a result from an external call, like
268 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
269 */
270 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800271
Sunny Goyal02424b22018-01-19 11:24:32 -0800272 private final PointF mLastDispatchTouchEvent = new PointF();
Jon Miranda379198e2016-09-23 08:54:40 -0700273
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700274 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700275 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700276 private boolean mAppLaunchSuccess;
277
Sunny Goyal745bad92016-05-02 10:54:12 -0700278 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800279 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 @Override
282 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700283 if (DEBUG_STRICT_MODE) {
284 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
285 .detectDiskReads()
286 .detectDiskWrites()
287 .detectNetwork() // or .detectAll() for all detectable problems
288 .penaltyLog()
289 .build());
290 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
291 .detectLeakedSqlLiteObjects()
292 .detectLeakedClosableObjects()
293 .penaltyLog()
294 .penaltyDeath()
295 .build());
296 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700297 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700298
Adam Cohen9211d422014-10-07 18:14:53 -0700299 if (mLauncherCallbacks != null) {
300 mLauncherCallbacks.preOnCreate();
301 }
302
Mario Bertschler27288382017-05-24 15:35:09 -0700303 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
304 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700305 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700308 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400309
Sunny Goyal87f784c2017-01-11 10:48:34 -0800310 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700311
Adam Cohen2e6da152015-05-06 11:42:25 -0700312 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800313 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700314 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700315 Display display = getWindowManager().getDefaultDisplay();
316 Point mwSize = new Point();
317 display.getSize(mwSize);
318 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
319 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700320
Jon Miranda6bed3502017-09-19 14:43:17 -0700321 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700322 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700323 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800324 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800325 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800326 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700327 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700328
Joe Onorato41a12d22009-10-31 18:30:00 -0400329 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700330 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800331 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700332
Sunny Goyalffe83f12014-08-14 17:39:34 -0700333 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700334
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700335 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700336 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700337
Sunny Goyal60820d72017-05-09 12:40:11 -0700338 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800341 mPopupDataProvider = new PopupDataProvider(this);
342
Sunny Goyald3864fa2017-11-14 15:06:36 -0800343 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
344 // In case we are on a device with locked rotation, we should look at preferences to check
345 // if the user has specifically allowed rotation.
346 if (!mRotationEnabled) {
347 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
348 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
349 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351
Sunny Goyald3864fa2017-11-14 15:06:36 -0800352 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
353 if (internalStateHandled) {
354 // Temporarily enable the rotation
355 mRotationEnabled = true;
356
357 if (savedInstanceState != null) {
358 // InternalStateHandler has already set the appropriate state.
359 // We dont need to do anything.
360 savedInstanceState.remove(RUNTIME_STATE);
361 }
362 }
363 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800364
Sunny Goyal2100c782016-08-22 16:00:03 -0700365 // We only load the page synchronously if the user rotates (or triggers a
366 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700367 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
368 if (savedInstanceState != null) {
369 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
370 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800371
Sunny Goyalfe770c92016-09-27 14:38:58 -0700372 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800373 if (!internalStateHandled) {
374 // If we are not binding synchronously, show a fade in animation when
375 // the first page bind completes.
376 mDragLayer.setAlpha(0);
377 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700378 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700379 // Pages bound synchronously.
380 mWorkspace.setCurrentPage(currentScreen);
381
Sunny Goyal2100c782016-08-22 16:00:03 -0700382 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
384
385 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800386 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800387
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700388 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
389 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700390 setRequestedOrientation(mRotationEnabled
391 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700392
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800393 setContentView(mLauncherView);
Sunny Goyal07b69292018-01-08 14:19:34 -0800394 ((LauncherRootView) mLauncherView).dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700395
Jon Miranda7fda2852017-07-19 16:07:01 -0700396 // Listen for broadcasts
397 IntentFilter filter = new IntentFilter();
398 filter.addAction(Intent.ACTION_SCREEN_OFF);
399 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
400 registerReceiver(mReceiver, filter);
401 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700402
Sunny Goyal8392c822017-06-20 10:03:56 -0700403 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
404 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700405
406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.onCreate(savedInstanceState);
408 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700409
410 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700411 }
412
Sunny Goyal7779d622015-06-11 16:18:39 -0700413 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700414 public void onThemeChanged() {
415 recreate();
416 }
417
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700418 public LauncherStateManager getStateManager() {
419 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700420 }
421
Mario Bertschlera6936942017-05-31 14:48:19 -0700422 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700423 if (isDark) {
424 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700425 } else if (supportsDarkText) {
426 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700427 }
428 }
429
430 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700431 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800432 return mLauncherView.findViewById(id);
433 }
434
435 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700436 public void onAppWidgetHostReset() {
437 if (mAppWidgetHost != null) {
438 mAppWidgetHost.startListening();
439 }
440 }
441
Adam Cohen9211d422014-10-07 18:14:53 -0700442 private LauncherCallbacks mLauncherCallbacks;
443
Sunny Goyal32554d12015-12-03 15:31:25 -0800444 /**
445 * Call this after onCreate to set or clear overlay.
446 */
447 public void setLauncherOverlay(LauncherOverlay overlay) {
448 if (overlay != null) {
449 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
450 }
451 mWorkspace.setLauncherOverlay(overlay);
452 }
453
Adam Cohen9211d422014-10-07 18:14:53 -0700454 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
455 mLauncherCallbacks = callbacks;
456 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700457 }
458
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700459 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700460 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700461 if (mLauncherCallbacks != null) {
462 mLauncherCallbacks.onLauncherProviderChange();
463 }
464 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700465
Hyunyoung Song3f471442015-04-08 19:01:34 -0700466 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700467 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
468 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700469 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700470 }
471
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000472 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700473 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
474 // This cast is safe as long as the id < 0x00FFFFFF
475 // Since we jail all the dynamically generated views, there should be no clashes
476 // with any other views.
477 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000478 }
479
Tony Wickham010d2552017-01-20 08:15:28 -0800480 public PopupDataProvider getPopupDataProvider() {
481 return mPopupDataProvider;
482 }
483
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000484 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700485 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
486 * a configuration step, this allows the proper animations to run after other transitions.
487 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700488 private long completeAdd(
489 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
490 long screenId = info.screenId;
491 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700492 // When the screen id represents an actual screen (as opposed to a rank) we make sure
493 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700494 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700495 }
Adam Cohendb364c32014-05-20 14:23:40 -0700496
Sunny Goyal2100c782016-08-22 16:00:03 -0700497 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800498 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700499 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700500 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800501 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700503 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700504 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700506 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700507 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 int widgetId = appWidgetId;
509 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700510 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700512 // Since the view was just bound, also launch the configure activity if needed
513 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
514 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800515 if (provider != null) {
516 new WidgetAddFlowHandler(provider)
517 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700518 }
519 }
520 break;
521 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700523
Adam Cohendb364c32014-05-20 14:23:40 -0700524 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800525 }
526
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800527 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700528 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700529 if (isWorkspaceLoading()) {
530 // process the result once the workspace has loaded.
531 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
532 return;
533 }
534 mPendingActivityResult = null;
535
Winson Chunge341d302013-08-16 14:31:00 -0700536 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700537 final PendingRequestArgs requestArgs = mPendingRequestArgs;
538 setWaitingForResult(null);
539 if (requestArgs == null) {
540 return;
541 }
542
543 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700544
Adam Cohenad4e15c2013-10-17 16:21:35 -0700545 Runnable exitSpringLoaded = new Runnable() {
546 @Override
547 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700548 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700549 }
550 };
551
Michael Jurka8b805b12012-04-18 14:23:14 -0700552 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700553 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700554 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700555 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
556 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700557 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700558 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700559 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700561 addAppWidgetImpl(
562 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800563 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700564 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700565 }
566 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200567 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700568 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700569 // User could have free-scrolled between pages before picking a wallpaper; make sure
570 // we move to the closest one now.
571 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700572 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200573 }
574 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200576
Adam Cohened66b2b2012-01-23 17:28:51 -0800577 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700578 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700579
Adam Cohened66b2b2012-01-23 17:28:51 -0800580 // We have special handling for widgets
581 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800582 final int appWidgetId;
583 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
584 : -1;
585 if (widgetId < 0) {
586 appWidgetId = pendingAddWidgetId;
587 } else {
588 appWidgetId = widgetId;
589 }
590
Adam Cohenad4e15c2013-10-17 16:21:35 -0700591 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800592 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700593 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
594 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700595 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700596 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700597 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700598 @Override
599 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700600 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700601 }
602 };
Adam Cohendb364c32014-05-20 14:23:40 -0700603
Sunny Goyal2100c782016-08-22 16:00:03 -0700604 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
605 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
606 } else {
607 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
608 // When the screen id represents an actual screen (as opposed to a rank)
609 // we make sure that the drop page actually exists.
610 requestArgs.screenId =
611 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700612 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 final CellLayout dropLayout =
614 mWorkspace.getScreenWithId(requestArgs.screenId);
615
616 dropLayout.setDropPending(true);
617 final Runnable onComplete = new Runnable() {
618 @Override
619 public void run() {
620 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
621 dropLayout.setDropPending(false);
622 }
623 };
624 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
625 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700626 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800627 return;
628 }
629
Sunny Goyald478c832016-04-01 12:04:16 -0700630 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
631 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700632 if (resultCode == RESULT_OK) {
633 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700634 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700635 }
636 // Leave the widget in the pending state if the user canceled the configure.
637 return;
638 }
639
Sunny Goyalaad90582015-07-09 14:22:50 -0700640 if (requestCode == REQUEST_CREATE_SHORTCUT) {
641 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
643 completeAdd(requestCode, data, -1, requestArgs);
644 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
645 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
646
Sunny Goyalaad90582015-07-09 14:22:50 -0700647 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700648 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
649 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800653 }
654
655 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700656 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800657 final int requestCode, final int resultCode, final Intent data) {
658 handleActivityResult(requestCode, resultCode, data);
659 if (mLauncherCallbacks != null) {
660 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
661 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800662 }
663
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700664 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700665 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600666 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 PendingRequestArgs pendingArgs = mPendingRequestArgs;
668 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
669 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
670 setWaitingForResult(null);
671
Sunny Goyal28c6b962015-10-12 11:42:05 -0700672 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700674 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700676 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 Intent intent = pendingArgs.getPendingIntent();
678
Sunny Goyal28c6b962015-10-12 11:42:05 -0700679 if (grantResults.length > 0
680 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700681 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700682 } else {
683 // TODO: Show a snack bar with link to settings
684 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700685 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700686 }
687 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600688 if (mLauncherCallbacks != null) {
689 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
690 grantResults);
691 }
692 }
693
Adam Cohendb364c32014-05-20 14:23:40 -0700694 /**
695 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
696 *
697 * @param screenId the screen id to check
698 * @return the new screen, or screenId if it exists
699 */
700 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800701 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700702 if (dropLayout == null) {
703 // it's possible that the add screen was removed because it was
704 // empty and a re-bind occurred
705 mWorkspace.addExtraEmptyScreen();
706 return mWorkspace.commitExtraEmptyScreen();
707 } else {
708 return screenId;
709 }
710 }
711
Sunny Goyal2100c782016-08-22 16:00:03 -0700712 @Thunk void completeTwoStageWidgetDrop(
713 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
714 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 Runnable onCompleteRunnable = null;
716 int animationType = 0;
717
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700718 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800719 if (resultCode == RESULT_OK) {
720 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
721 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800722 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700723 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 onCompleteRunnable = new Runnable() {
725 @Override
726 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700728 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 }
730 };
731 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800732 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700737 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
738 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 // The animated view may be null in the case of a rotation during widget configuration
741 onCompleteRunnable.run();
742 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744
745 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700746 protected void onStop() {
747 super.onStop();
748 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700749
750 if (mLauncherCallbacks != null) {
751 mLauncherCallbacks.onStop();
752 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800753 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800754
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700755 if (!mAppLaunchSuccess) {
756 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700757 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700758 }
Tony Wickham010d2552017-01-20 08:15:28 -0800759 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700760 }
761
762 @Override
763 protected void onStart() {
764 super.onStart();
765 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700766
767 if (mLauncherCallbacks != null) {
768 mLauncherCallbacks.onStart();
769 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800770 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800771 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700772
Jon Mirandade43a712018-01-18 11:35:10 -0800773 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700774 if (mScrimAnimator != null) {
775 mScrimAnimator.cancel();
776 }
Jon Mirandade43a712018-01-18 11:35:10 -0800777 mLauncherView.getBackground().setAlpha(0);
778 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700779 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
780 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
781 @Override
782 public void onAnimationEnd(Animator animation) {
783 mScrimAnimator = null;
784 }
785 });
786 mScrimAnimator.setDuration(600);
787 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
788 mScrimAnimator.start();
789 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700790 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700791 }
792
793 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700795 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700797 TraceHelper.partitionSection("ON_RESUME", "superCall");
798
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700799 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700800 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700801 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700802 // Process any items that were added while Launcher was away.
803 InstallShortcutReceiver.disableAndFlushInstallQueue(
804 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700805
Sunny Goyala474a9b2017-05-04 16:47:11 -0700806 // Refresh shortcuts if the permission changed.
807 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700808
Sunny Goyal7ede6112017-12-05 15:11:21 -0800809 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700810 if (mLauncherCallbacks != null) {
811 mLauncherCallbacks.onResume();
812 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800813
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700814 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700815 }
816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700818 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700819 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700820 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700821
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700822 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800823 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700824 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700825
Adam Cohen9211d422014-10-07 18:14:53 -0700826 if (mLauncherCallbacks != null) {
827 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700828 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700829 }
830
Adam Cohenc2d6e892014-10-16 09:49:24 -0700831 public interface LauncherOverlay {
832
833 /**
834 * Touch interaction leading to overscroll has begun
835 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700836 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700837
838 /**
839 * Touch interaction related to overscroll has ended
840 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700841 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700842
843 /**
844 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
845 * screen (or in the case of RTL, the rightmost screen).
846 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700847 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700848
849 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800850 * Called when the launcher is ready to use the overlay
851 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700852 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700853 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700854 }
855
856 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700857 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700858 }
859
860 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
861
Sunny Goyalc86df472016-02-25 09:19:38 -0800862 public void onScrollChanged(float progress) {
863 if (mWorkspace != null) {
864 mWorkspace.onOverlayScrollChanged(progress);
865 }
866 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700867 }
868
Sunny Goyal16764582017-11-06 16:00:34 -0800869 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700870 if (mLauncherCallbacks != null) {
871 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700872 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800873 // On O and above we there is always some setting present settings (add icon to
874 // home screen or icon badging). On earlier APIs we will have the allow rotation
875 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700876 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700877 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800878 }
879
Sunny Goyalf9403d92017-10-18 10:55:56 -0700880 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700881 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700882 }
883
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 /**
885 * Restores the previous state, if it exists.
886 *
887 * @param savedState The previous state.
888 */
889 private void restoreState(Bundle savedState) {
890 if (savedState == null) {
891 return;
892 }
893
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700894 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700895 LauncherState[] stateValues = LauncherState.values();
896 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800897 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700898 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800899 }
900
Sunny Goyal2100c782016-08-22 16:00:03 -0700901 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
902 if (requestArgs != null) {
903 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700905
906 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700907
908 SparseArray<Parcelable> widgetsState =
909 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
910 if (widgetsState != null) {
911 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 }
914
915 /**
916 * Finds all the views we need and configure them properly.
917 */
918 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800919 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700920 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700921 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700922 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800923 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700924
Sunny Goyal784f9c32016-03-23 16:56:54 -0700925 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
926 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
927 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Winson Chung4c98d922011-05-31 16:50:48 -0700929 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800930 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700931
Winson Chung3d503fb2011-07-13 17:25:49 -0700932 // Setup the hotseat
933 mHotseat = (Hotseat) findViewById(R.id.hotseat);
934 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700935 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 }
937
Winson Chung4c98d922011-05-31 16:50:48 -0700938 // Setup the workspace
939 mWorkspace.setHapticFeedbackEnabled(false);
940 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700941 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700942 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
943 // default state, otherwise we will update to the wrong offsets in RTL
944 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700945 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700946 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700947
Tony Wickham34d2c912015-09-11 09:27:58 -0700948 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700949 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700950
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700951 // Setup Apps
952 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700953
Winson Chung3d503fb2011-07-13 17:25:49 -0700954 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700955 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700956 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400957
Hyunyoung Songd725f642017-08-17 22:26:35 -0700958 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
961 /**
962 * Creates a view representing a shortcut.
963 *
964 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800966 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700967 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969
970 /**
971 * Creates a view representing a shortcut inflated from the specified resource.
972 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 * @param parent The group the shortcut belongs to.
974 * @param info The data structure describing the shortcut.
975 *
976 * @return A View inflated from layoutResId.
977 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700978 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800979 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
980 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800981 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700983 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 return favorite;
985 }
986
987 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700988 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 *
990 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700993 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700994 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
995 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700996 return;
997 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700998
Jon Mirandac476d6e2017-05-04 16:30:01 -0700999 int[] cellXY = mTmpAddItemCellCoordinates;
1000 CellLayout layout = getCellLayout(container, screenId);
1001
Sunny Goyal782f0c92017-01-19 10:27:54 -08001002 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001003 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001004 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1005 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001006 }
1007
1008 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001009 // Legacy shortcuts are only supported for primary profile.
1010 info = Process.myUserHandle().equals(args.user)
1011 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001012
Sunny Goyalb57645f2017-03-20 13:57:28 -07001013 if (info == null) {
1014 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1015 return;
1016 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001017 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001018 // The app is trying to add a shortcut without sufficient permissions
1019 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1020 return;
1021 }
1022 }
1023
Jon Mirandac476d6e2017-05-04 16:30:01 -07001024 if (container < 0) {
1025 // Adding a shortcut to the Workspace.
1026 final View view = createShortcut(info);
1027 boolean foundCellSpan = false;
1028 // First we check if we already know the exact location where we want to add this item.
1029 if (cellX >= 0 && cellY >= 0) {
1030 cellXY[0] = cellX;
1031 cellXY[1] = cellY;
1032 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001033
Jon Mirandac476d6e2017-05-04 16:30:01 -07001034 // If appropriate, either create a folder or add to an existing folder
1035 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001036 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001037 return;
1038 }
1039 DragObject dragObject = new DragObject();
1040 dragObject.dragInfo = info;
1041 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1042 true)) {
1043 return;
1044 }
1045 } else {
1046 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1047 }
1048
1049 if (!foundCellSpan) {
1050 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001051 return;
1052 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001053
1054 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1055 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001056 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001057 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001058 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001059 if (folderIcon != null) {
1060 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1061 folderInfo.add(info, args.rank, false);
1062 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001063 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001064 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 }
1067
Sunny Goyale29897f2017-07-20 10:09:42 -07001068 public FolderIcon findFolderIcon(final long folderIconId) {
1069 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1070 @Override
1071 public boolean evaluate(ItemInfo info, View view) {
1072 return info != null && info.id == folderIconId;
1073 }
1074 });
1075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001080 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001082 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001083 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1084
Adam Cohened66b2b2012-01-23 17:28:51 -08001085 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001086 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Adam Cohen59400422014-03-05 18:07:04 -08001089 LauncherAppWidgetInfo launcherInfo;
1090 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001091 launcherInfo.spanX = itemInfo.spanX;
1092 launcherInfo.spanY = itemInfo.spanY;
1093 launcherInfo.minSpanX = itemInfo.minSpanX;
1094 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001095 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001096
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001097 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001098 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099
Sunny Goyal2100c782016-08-22 16:00:03 -07001100 if (hostView == null) {
1101 // Perform actual inflation because we're live
1102 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001104 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301105 prepareAppWidget(hostView, launcherInfo);
1106 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Sunny Goyald5462aa2016-11-22 16:52:39 +05301109 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001110 hostView.setTag(item);
1111 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001112 hostView.setFocusable(true);
1113 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001114 }
1115
Adam Cohended9f8d2010-11-03 13:25:16 -07001116 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1117 @Override
1118 public void onReceive(Context context, Intent intent) {
1119 final String action = intent.getAction();
1120 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001121 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001122 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001123 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001124 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001126 mShouldFadeInScrim = true;
1127 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1128 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1129 // the user unlocked and the Launcher is not in the foreground.
1130 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001131 }
1132 }
1133 };
1134
Tony Wickham010d2552017-01-20 08:15:28 -08001135 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001136 mWorkspace.updateIconBadges(updatedBadges);
1137 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001138
Sunny Goyal29947f02017-12-18 13:49:44 -08001139 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1140 if (popup != null) {
1141 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001142 }
1143 }
1144
Adam Cohended9f8d2010-11-03 13:25:16 -07001145 @Override
1146 public void onAttachedToWindow() {
1147 super.onAttachedToWindow();
1148
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001149 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001150 if (mLauncherCallbacks != null) {
1151 mLauncherCallbacks.onAttachedToWindow();
1152 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001153 }
1154
1155 @Override
1156 public void onDetachedFromWindow() {
1157 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001158
1159 if (mLauncherCallbacks != null) {
1160 mLauncherCallbacks.onDetachedFromWindow();
1161 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 }
1163
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001164 public AllAppsTransitionController getAllAppsController() {
1165 return mAllAppsController;
1166 }
1167
Winson Chunga6945242014-01-08 14:04:34 -08001168 public DragLayer getDragLayer() {
1169 return mDragLayer;
1170 }
1171
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001172 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001173 return mAppsView;
1174 }
1175
Winson Chunga6945242014-01-08 14:04:34 -08001176 public Workspace getWorkspace() {
1177 return mWorkspace;
1178 }
1179
1180 public Hotseat getHotseat() {
1181 return mHotseat;
1182 }
1183
Winson Chung8ae41982017-11-10 11:42:13 -08001184 public <T extends ViewGroup> T getOverviewPanel() {
1185 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001186 }
1187
Sunny Goyal47328fd2016-05-25 18:56:41 -07001188 public DropTargetBar getDropTargetBar() {
1189 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001190 }
1191
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 public LauncherAppWidgetHost getAppWidgetHost() {
1193 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
Romain Guycbb89e42009-06-08 15:52:54 -07001195
Winson Chunga9abd0e2010-10-27 17:18:37 -07001196 public LauncherModel getModel() {
1197 return mModel;
1198 }
1199
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001200 public ModelWriter getModelWriter() {
1201 return mModelWriter;
1202 }
1203
Adam Cohen79d90c52016-04-22 13:29:20 -07001204 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001205 return mSharedPrefs;
1206 }
1207
Jon Miranda6bed3502017-09-19 14:43:17 -07001208 public int getOrientation() { return mOrientation; }
1209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 @Override
1211 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001212 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 super.onNewIntent(intent);
1214
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001215 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001216 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1217 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001218
1219 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001220 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001221 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001222 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001223 boolean internalStateHandled = InternalStateHandler
1224 .handleNewIntent(this, intent, alreadyOnHome);
1225
Winson15f8b172015-08-19 11:02:39 -07001226 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001227 if (!internalStateHandled) {
1228 // Note: There should be at most one log per method call. This is enforced
1229 // implicitly by using if-else statements.
1230 UserEventDispatcher ued = getUserEventDispatcher();
1231 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1232 if (topOpenView != null) {
1233 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1234 } else if (alreadyOnHome) {
1235 Target target = newContainerTarget(mStateManager.getState().containerType);
1236 target.pageIndex = mWorkspace.getCurrentPage();
1237 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1238 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001239
Sunny Goyald3864fa2017-11-14 15:06:36 -08001240 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001241 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001242
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001243 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001244
1245 // Reset the apps view
1246 if (!alreadyOnHome && mAppsView != null) {
1247 mAppsView.reset();
1248 }
1249
1250 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1251 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1252 }
1253 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001254
1255 final View v = getWindow().peekDecorView();
1256 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001257 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001258 }
1259
Adam Cohen9211d422014-10-07 18:14:53 -07001260 if (mLauncherCallbacks != null) {
1261 mLauncherCallbacks.onHomeIntent();
1262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
Winson15f8b172015-08-19 11:02:39 -07001264
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001265 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001269 public void onRestoreInstanceState(Bundle state) {
1270 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001271 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
1273
1274 @Override
1275 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001276 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001277 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001278
Adam Cohen21cd0022013-10-09 18:57:02 -07001279 }
Sunny Goyalea609262017-10-25 15:47:38 -07001280 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001281
1282
1283 AbstractFloatingView widgets = AbstractFloatingView
1284 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1285 if (widgets != null) {
1286 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1287 widgets.saveHierarchyState(widgetsState);
1288 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1289 } else {
1290 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1291 }
1292
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001293 // We close any open folders and shortcut containers since they will not be re-opened,
1294 // and we need to make sure this state is reflected.
1295 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001296
Sunny Goyal2100c782016-08-22 16:00:03 -07001297 if (mPendingRequestArgs != null) {
1298 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1299 }
1300 if (mPendingActivityResult != null) {
1301 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001304 super.onSaveInstanceState(outState);
1305
Adam Cohen9211d422014-10-07 18:14:53 -07001306 if (mLauncherCallbacks != null) {
1307 mLauncherCallbacks.onSaveInstanceState(outState);
1308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
1310
1311 @Override
1312 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001315 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001316 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001317
Winson Chungcd2b0142011-06-08 16:02:26 -07001318 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001319 // It's possible to receive onDestroy after a new Launcher activity has
1320 // been created. In this case, don't interfere with the new Launcher.
1321 if (mModel.isCurrentCallbacks(this)) {
1322 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001323 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001324 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001325
Sunny Goyal745bad92016-05-02 10:54:12 -07001326 if (mRotationPrefChangeHandler != null) {
1327 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1328 }
1329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001331 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001333 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001337 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1338
Michael Jurka2ecf9952012-06-18 12:52:28 -07001339 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001340
Tony2917a8b2017-07-31 23:29:42 -07001341 clearPendingBinds();
1342
Adam Cohen9211d422014-10-07 18:14:53 -07001343 if (mLauncherCallbacks != null) {
1344 mLauncherCallbacks.onDestroy();
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
Sunny Goyalae502842016-06-17 08:43:56 -07001348 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1349 return mAccessibilityDelegate;
1350 }
1351
Adam Cohena9cf38f2011-05-02 15:36:58 -07001352 public DragController getDragController() {
1353 return mDragController;
1354 }
1355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001357 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001358 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001359 }
1360
1361 @Override
1362 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1363 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001364 try {
1365 super.startIntentSenderForResult(intent, requestCode,
1366 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1367 } catch (IntentSender.SendIntentException e) {
1368 throw new ActivityNotFoundException();
1369 }
1370 }
1371
Winson Chung70d72102011-08-12 11:24:35 -07001372 /**
1373 * Indicates that we want global search for this activity by setting the globalSearch
1374 * argument for {@link #startSearch} to true.
1375 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001377 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 Bundle appSearchData, boolean globalSearch) {
1379 if (appSearchData == null) {
1380 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001381 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001383
Sunny Goyal6f28e712016-09-13 14:19:29 -07001384 if (mLauncherCallbacks == null ||
1385 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1386 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001387 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001388 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001389
1390 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001391 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001392 }
1393
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001395 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001396 }
1397
Adam Cohen517a7f52014-03-01 12:12:59 -08001398 public boolean isWorkspaceLoading() {
1399 return mWorkspaceLoading;
1400 }
1401
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001402 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001403 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001404 }
1405
Sunny Goyal04a324a2017-01-20 21:08:59 -08001406 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001407 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001408 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001409
Sunny Goyal2100c782016-08-22 16:00:03 -07001410 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001411 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001412 if (LOGD) {
1413 Log.d(TAG, "Adding widget from drop");
1414 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001415 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001416 }
1417
Sunny Goyal2100c782016-08-22 16:00:03 -07001418 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001419 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1420 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1421 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001422
Adam Cohenad4e15c2013-10-17 16:21:35 -07001423 Runnable onComplete = new Runnable() {
1424 @Override
1425 public void run() {
1426 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001427 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001428 }
1429 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001430 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001431 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433 }
Romain Guycbb89e42009-06-08 15:52:54 -07001434
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001435 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1436 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001437 info.container = container;
1438 info.screenId = screenId;
1439 if (cell != null) {
1440 info.cellX = cell[0];
1441 info.cellY = cell[1];
1442 }
1443 info.spanX = spanX;
1444 info.spanY = spanY;
1445
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001446 switch (info.itemType) {
1447 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1448 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001449 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001450 break;
1451 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001452 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001453 break;
1454 default:
1455 throw new IllegalStateException("Unknown item type: " + info.itemType);
1456 }
1457 }
1458
Adam Cohenfbba09b2011-07-18 21:43:05 -07001459 /**
1460 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001461 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001462 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001463 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1464 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001465 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1466 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1467 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001468 }
1469
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 /**
1471 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001472 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001473 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001474 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001475 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001476 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001477 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001478 // In the case where we've prebound the widget, we remove it from the DragLayer
1479 if (LOGD) {
1480 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1481 }
1482 getDragLayer().removeView(hostView);
1483
Adam Cohened66b2b2012-01-23 17:28:51 -08001484 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001485 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001486
1487 // Clear the boundWidget so that it doesn't get destroyed.
1488 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001489 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001490 // In this case, we either need to start an activity to get permission to bind
1491 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001492 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1493 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1494 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1495 this, info.componentName);
1496 } else {
1497 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1498 }
Adam Cohendd70d662012-10-04 16:53:44 -07001499 Bundle options = info.bindOptions;
1500
Sunny Goyalffe83f12014-08-14 17:39:34 -07001501 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1502 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001503 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001504 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001505 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001506 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001507 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001508 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 }
1510
Adam Cohendcd297f2013-06-18 13:13:40 -07001511 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001512 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001513 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 folderInfo.title = getText(R.string.folder_name);
1515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001517 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518
1519 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001520 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301521 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001522 // Force measure the new folder icon
1523 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1524 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001525 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Winsonc0b52fe2015-09-09 16:38:15 -07001528 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001529 * Unbinds the view for the specified item, and removes the item and all its children.
1530 *
1531 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001532 * @param itemInfo the {@link ItemInfo} for this view.
1533 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001534 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001535 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001536 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001537 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001538 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1539 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001540 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001541 } else {
1542 mWorkspace.removeWorkspaceItem(v);
1543 }
Winsonc0b52fe2015-09-09 16:38:15 -07001544 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001545 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001546 }
1547 } else if (itemInfo instanceof FolderInfo) {
1548 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001549 if (v instanceof FolderIcon) {
1550 ((FolderIcon) v).removeListeners();
1551 }
Winsonc0b52fe2015-09-09 16:38:15 -07001552 mWorkspace.removeWorkspaceItem(v);
1553 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001554 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001555 }
1556 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1557 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001558 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001559 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001560 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001561 }
1562 } else {
1563 return false;
1564 }
1565 return true;
1566 }
1567
1568 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001569 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001570 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001571 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001572 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001573 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001574 // Deleting an app widget ID is a void call but writes to disk before returning
1575 // to the caller...
1576 new AsyncTask<Void, Void, Void>() {
1577 public Void doInBackground(Void ... args) {
1578 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1579 return null;
1580 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001581 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001582 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001583 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001584 }
1585
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 @Override
1587 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001588 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
1590
Joe Onorato88ec0992009-11-19 13:16:06 -08001591 @Override
1592 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001593 if (finishAutoCancelActionMode()) {
1594 return;
1595 }
Adam Cohen9211d422014-10-07 18:14:53 -07001596 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1597 return;
1598 }
1599
Sunny Goyal45478022015-06-08 16:52:41 -07001600 if (mDragController.isDragging()) {
1601 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001602 return;
1603 }
1604
Jon Mirandafeba90f2016-10-06 10:53:29 -07001605 // Note: There should be at most one log per method call. This is enforced implicitly
1606 // by using if-else statements.
1607 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001608 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1609 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001610 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001611 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001612 LauncherState lastState = mStateManager.getLastState();
Sunny Goyalea609262017-10-25 15:47:38 -07001613 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001614 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001615 } else {
1616 // Back button is a no-op here, but give at least some feedback for the button press
1617 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001618 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 * Launches the intent referred by the clicked shortcut.
1623 *
1624 * @param v The view representing the clicked shortcut.
1625 */
1626 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001627 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1628 // view has detached (it's possible for this to happen if the view is removed mid touch).
1629 if (v.getWindowToken() == null) {
1630 return;
1631 }
1632
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001633 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001634 return;
1635 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001636
Adam Cohen1697b792013-09-17 19:08:21 -07001637 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001638 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001639 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1640 LauncherLogProto.Action.Direction.NONE,
1641 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001642 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001643 }
1644 return;
1645 }
1646
1647 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001648 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001649 int page = mWorkspace.indexOfChild(v);
1650 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1651 LauncherLogProto.Action.Direction.NONE,
1652 LauncherLogProto.ContainerType.OVERVIEW, page);
1653 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001654 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001655 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001656 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001657 }
1658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001660 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001661 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001663 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001664 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001665 }
Sunny Goyal508da152014-08-14 10:53:27 -07001666 } else if (tag instanceof AppInfo) {
1667 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001668 } else if (tag instanceof LauncherAppWidgetInfo) {
1669 if (v instanceof PendingAppWidgetHostView) {
1670 onClickPendingWidget((PendingAppWidgetHostView) v);
1671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
1673 }
1674
Sunny Goyal70660032015-05-14 00:07:08 -07001675 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001676 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001677 return false;
1678 }
1679
Michael Jurkaaf442092010-06-10 17:01:57 -07001680 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001681 * Event handler for the app widget view which has not fully restored.
1682 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001683 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001684 if (mIsSafeModeEnabled) {
1685 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1686 return;
1687 }
1688
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001689 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001690 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001691 LauncherAppWidgetProviderInfo appWidgetInfo =
1692 mAppWidgetManager.findProvider(info.providerName, info.user);
1693 if (appWidgetInfo == null) {
1694 return;
1695 }
1696 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1697
Sunny Goyald478c832016-04-01 12:04:16 -07001698 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1699 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1700 // This should not happen, as we make sure that an Id is allocated during bind.
1701 return;
1702 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001703 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1704 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001705 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001706 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001707 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001708 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001709 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001710 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001711 }
1712 }
1713
Sunny Goyale6e72002017-01-12 16:55:36 -08001714 private void onClickPendingAppItem(final View v, final String packageName,
1715 boolean downloadStarted) {
1716 if (downloadStarted) {
1717 // If the download has started, simply direct to the market app.
1718 startMarketIntentForPackage(v, packageName);
1719 return;
1720 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001721 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001722 .setTitle(R.string.abandoned_promises_title)
1723 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001724 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1725 @Override
1726 public void onClick(DialogInterface dialogInterface, int i) {
1727 startMarketIntentForPackage(v, packageName);
1728 }
1729 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001730 .setNeutralButton(R.string.abandoned_clean_this,
1731 new DialogInterface.OnClickListener() {
1732 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001733 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001734 mWorkspace.removeAbandonedPromise(packageName, user);
1735 }
1736 })
1737 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001738 }
1739
1740 private void startMarketIntentForPackage(View v, String packageName) {
1741 ItemInfo item = (ItemInfo) v.getTag();
1742 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001743 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001744 }
1745
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001746 /**
1747 * Event handler for an app shortcut click.
1748 *
1749 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1750 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001751 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001752 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1753 Object tag = v.getTag();
1754 if (!(tag instanceof ShortcutInfo)) {
1755 throw new IllegalArgumentException("Input must be a Shortcut");
1756 }
1757
1758 // Open shortcut
1759 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001760
Sunny Goyal076839c2017-10-30 13:52:20 -07001761 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001762 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1763 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001764 ~FLAG_DISABLED_SUSPENDED &
1765 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001766 // If the app is only disabled because of the above flags, launch activity anyway.
1767 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001768 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001769 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1770 // Use a message specific to this shortcut, if it has one.
1771 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1772 return;
1773 }
1774 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001775 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001776 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001777 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001778 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1779 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001780 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001781 }
1782 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1783 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001784 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001785 }
1786
Chris Wren40c5ed32014-06-24 18:24:23 -04001787 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001788 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001789 String packageName = shortcut.intent.getComponent() != null ?
1790 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1791 if (!TextUtils.isEmpty(packageName)) {
1792 onClickPendingAppItem(v, packageName,
1793 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1794 return;
1795 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001796 }
1797
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001798 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001799 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001800 }
1801
Sunny Goyala7ce1662016-05-31 15:01:35 -07001802 private void startAppShortcutOrInfoActivity(View v) {
1803 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001804 Intent intent;
1805 if (item instanceof PromiseAppInfo) {
1806 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
1807 intent = promiseAppInfo.getMarketIntent();
1808 } else {
1809 intent = item.getIntent();
1810 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001811 if (intent == null) {
1812 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001813 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001814 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001815 }
1816
1817 /**
1818 * Event handler for a folder icon click.
1819 *
1820 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1821 */
1822 protected void onClickFolderIcon(View v) {
1823 if (LOGD) Log.d(TAG, "onClickFolder");
1824 if (!(v instanceof FolderIcon)){
1825 throw new IllegalArgumentException("Input must be a FolderIcon");
1826 }
1827
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001828 Folder folder = ((FolderIcon) v).getFolder();
1829 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001830 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001831 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001832 }
Michael Jurka5130e402011-10-13 04:55:35 -07001833 }
1834
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001835 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001836 * Event handler for the wallpaper picker button that appears after a long press
1837 * on the home screen.
1838 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001839 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001840 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001841 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1842 return;
1843 }
1844
Tony Wickham785f7a52015-08-31 17:28:32 -07001845 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1846 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001847 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001848 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001849 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001850
1851 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001852 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1853 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001854 intent.setPackage(pickerPackage);
1855 }
1856
Sunny Goyala5ace712017-11-15 17:12:51 -08001857 final Bundle launchOptions;
1858 if (v != null) {
1859 intent.setSourceBounds(getViewBounds(v));
1860 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001861 launchOptions = hasTargetPackage
1862 ? getActivityLaunchOptions(v, isInMultiWindowModeCompat())
1863 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001864 } else {
1865 launchOptions = null;
1866 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001867 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001868 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001869 } catch (ActivityNotFoundException e) {
1870 setWaitingForResult(null);
1871 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1872 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001873 }
1874
Sunny Goyala7ce1662016-05-31 15:01:35 -07001875 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001877 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1878 try {
1879 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1880 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1881 // is enabled by default on NYC.
1882 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1883 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001884
1885 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1886 String id = ((ShortcutInfo) info).getDeepShortcutId();
1887 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301888 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001889 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001890 } else {
1891 // Could be launching some bookkeeping activity
1892 startActivity(intent, optsBundle);
1893 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001894 } finally {
1895 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001898 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1899 // corresponding permission. Show the appropriate permission prompt if that
1900 // is the case.
1901 if (intent.getComponent() == null
1902 && Intent.ACTION_CALL.equals(intent.getAction())
1903 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1904 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001905
1906 setWaitingForResult(PendingRequestArgs
1907 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001908 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1909 REQUEST_PERMISSION_CALL_PHONE);
1910 } else {
1911 // No idea why this was thrown.
1912 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
1915 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001916
Jon Miranda17940f02018-01-18 14:46:34 -08001917 public Bundle getDefaultActivityLaunchOptions(View v) {
1918 if (Utilities.ATLEAST_MARSHMALLOW) {
1919 int left = 0, top = 0;
1920 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
1921 if (v instanceof BubbleTextView) {
1922 // Launch from center of icon, not entire view
1923 Drawable icon = ((BubbleTextView) v).getIcon();
1924 if (icon != null) {
1925 Rect bounds = icon.getBounds();
1926 left = (width - bounds.width()) / 2;
1927 top = v.getPaddingTop();
1928 width = bounds.width();
1929 height = bounds.height();
1930 }
1931 }
1932 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height)
1933 .toBundle();
1934 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
1935 // On L devices, we use the device default slide-up transition.
1936 // On L MR1 devices, we use a custom version of the slide-up transition which
1937 // doesn't have the delay present in the device default.
1938 return ActivityOptions.makeCustomAnimation(
1939 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
1940 }
1941 return null;
1942 }
1943
Sunny Goyalfe770c92016-09-27 14:38:58 -07001944 @TargetApi(Build.VERSION_CODES.M)
Jon Miranda17940f02018-01-18 14:46:34 -08001945 public Bundle getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
1946 return useDefaultLaunchOptions
1947 ? getDefaultActivityLaunchOptions(v)
1948 : UiFactory.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001949 }
1950
Tonye3c59252017-05-02 21:32:27 -07001951 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001952 int[] pos = new int[2];
1953 v.getLocationOnScreen(pos);
1954 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1955 }
1956
Sunny Goyala7ce1662016-05-31 15:01:35 -07001957 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001958 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001959 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1960 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001961 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001962 }
Jon Miranda17940f02018-01-18 14:46:34 -08001963
1964 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1965 && (item instanceof ShortcutInfo)
1966 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1967 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1968 && !((ShortcutInfo) item).isPromise();
1969
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);
Jon Miranda17940f02018-01-18 14:46:34 -08001974 Bundle optsBundle = useLaunchAnimation
1975 ? getActivityLaunchOptions(v, isShortcut || isInMultiWindowModeCompat())
1976 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001977
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001978 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001979
1980 // Prepare intent
1981 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1982 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001983 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001984 }
1985 try {
Jon Miranda17940f02018-01-18 14:46:34 -08001986 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001987 // Shortcuts need some special checks due to legacy reasons.
1988 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001989 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001990 // Could be launching some bookkeeping activity
1991 startActivity(intent, optsBundle);
1992 } else {
1993 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1994 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1995 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001996
1997 if (v instanceof BubbleTextView) {
1998 // This is set to the view that launched the activity that navigated the user away
1999 // from launcher. Since there is no callback for when the activity has finished
2000 // launching, enable the press state and keep this reference to reset the press
2001 // state when we return to launcher.
2002 BubbleTextView btv = (BubbleTextView) v;
2003 btv.setStayPressed(true);
2004 setOnResumeCallback(btv);
2005 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002006 mAppLaunchSuccess = true;
2007 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002008 } catch (ActivityNotFoundException|SecurityException e) {
2009 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2010 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2011 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002012 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002013 }
2014
Tony Wickhamdadb3042016-02-24 11:07:00 -08002015 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002016 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002017 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002018 return super.dispatchTouchEvent(ev);
2019 }
2020
2021 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002023 if (!isDraggingEnabled()) return false;
2024 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002025 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026
Jon Miranda51f037d2016-11-07 08:37:20 -08002027 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002028 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002029
Adam Cohen1697b792013-09-17 19:08:21 -07002030 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002031 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002032 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302033 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2034 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002035 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002036 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002037 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2038 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2039 return true;
2040 } else {
2041 return false;
2042 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002043 } else {
2044 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002045 }
Adam Cohen1697b792013-09-17 19:08:21 -07002046 }
2047
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002048 CellLayout.CellInfo longClickCellInfo = null;
2049 View itemUnderLongClick = null;
2050 if (v.getTag() instanceof ItemInfo) {
2051 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002052 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002053 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002054 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 }
2056
Winson Chung3d503fb2011-07-13 17:25:49 -07002057 // The hotseat touch handling does not go through Workspace, and we always allow long press
2058 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002059 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002060 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002061 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002062 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002063 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302064 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2065 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002066 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002067 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002068 return false;
2069 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302070 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2071 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002072 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002073 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002074 }
Jon Miranda379198e2016-09-23 08:54:40 -07002075 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2076 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002078 final boolean isAllAppsButton =
2079 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2080 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2081 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002082 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002084 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 }
2086 }
2087 }
2088 return true;
2089 }
2090
Winson Chung3d503fb2011-07-13 17:25:49 -07002091 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002092 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002093 return mHotseat != null && layout != null &&
2094 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2095 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002096
Winson Chung3d503fb2011-07-13 17:25:49 -07002097 /**
2098 * Returns the CellLayout of the specified container at the specified screen.
2099 */
Sunny Goyal92820592015-03-02 11:31:35 -08002100 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002101 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2102 if (mHotseat != null) {
2103 return mHotseat.getLayout();
2104 } else {
2105 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002106 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002107 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002108 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002109 }
2110 }
2111
Michael Jurkae326f182011-11-21 14:05:46 -08002112 @Override
2113 public void onTrimMemory(int level) {
2114 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002115 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2116 // The widget preview db can result in holding onto over
2117 // 3MB of memory for caching which isn't necessary.
2118 SQLiteDatabase.releaseMemory();
2119
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002120 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002121 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002122 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002123 if (mLauncherCallbacks != null) {
2124 mLauncherCallbacks.onTrimMemory(level);
2125 }
Michael Jurkae326f182011-11-21 14:05:46 -08002126 }
2127
Michael Jurkad7c28052012-04-27 15:43:36 -07002128 @Override
2129 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002130 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002131 final List<CharSequence> text = event.getText();
2132 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002133 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002134 // TODO: When can workspace be null?
2135 text.add(mWorkspace == null
2136 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002137 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002138 return result;
2139 }
2140
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002141 public void setOnResumeCallback(OnResumeCallback callback) {
2142 if (mOnResumeCallback != null) {
2143 mOnResumeCallback.onLauncherResume();
2144 }
2145 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002146 }
2147
Michael Jurka7607c2f2013-04-03 14:33:19 -07002148 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002151 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002153 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002154 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002155 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002156 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002159
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002161 * Clear any pending bind callbacks. This is called when is loader is planning to
2162 * perform a full rebind from scratch.
2163 */
2164 @Override
2165 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002166 if (mPendingExecutor != null) {
2167 mPendingExecutor.markCompleted();
2168 mPendingExecutor = null;
2169 }
2170 }
2171
2172 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002173 * Refreshes the shortcuts shown on the workspace.
2174 *
2175 * Implementation of the method from LauncherModel.Callbacks.
2176 */
2177 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002178 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002179 // Floating panels (except the full widget sheet) are associated with individual icons. If
2180 // we are starting a fresh bind, close all such panels as all the icons are about
2181 // to go away.
2182 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002183 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002184
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002185 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002186
Winson Chungd64d1762013-08-20 14:37:16 -07002187 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002188 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002189 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002190
Winson Chung3d503fb2011-07-13 17:25:49 -07002191 if (mHotseat != null) {
2192 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002194 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 }
2196
Adam Cohendcd297f2013-06-18 13:13:40 -07002197 @Override
2198 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002199 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002200 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2201 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002202 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2203 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002204 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002205 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2206 // If there are no screens, we need to have an empty screen
2207 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002208 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002209 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002210
Winsonc7d2e832016-07-28 12:24:55 -07002211 // After we have added all the screens, if the wallpaper was locked to the default state,
2212 // then notify to indicate that it can be released and a proper wallpaper offset can be
2213 // computed before the next layout
2214 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002215 }
2216
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002217 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002218 int count = orderedScreenIds.size();
2219 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002220 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002221 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002222 // No need to bind the first screen, as its always bound.
2223 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2224 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 }
2227
Sunny Goyalb23980c2017-08-17 07:45:25 -07002228 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002229 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2230 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002231 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002232 if (newScreens != null) {
2233 bindAddScreens(newScreens);
2234 }
Winson Chungd64d1762013-08-20 14:37:16 -07002235
2236 // We add the items without animation on non-visible pages, and with
2237 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002238 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002239 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002240 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002241 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002242 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002243 }
Winson Chungc58497e2013-09-03 17:48:37 -07002244
Winson Chung87412982013-10-03 18:34:14 -07002245 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002246 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002247 }
2248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 * Bind the items start-end from the list.
2251 *
2252 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002254 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002255 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002256 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002257 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002258 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002259 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002260 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002261 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002262 int end = items.size();
2263 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002264 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002265
2266 // Short circuit if we are loading dock items for a configuration which has no dock
2267 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2268 mHotseat == null) {
2269 continue;
2270 }
2271
Sunny Goyal639e9062015-08-19 19:17:06 -07002272 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 switch (item.itemType) {
2274 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2275 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002276 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002277 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002278 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002280 }
2281 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002282 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002283 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002284 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002285 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002286 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002287 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2288 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002289 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002290 if (view == null) {
2291 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002292 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002293 break;
2294 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002295 default:
2296 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002298
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002299 /*
2300 * Remove colliding items.
2301 */
2302 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2303 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2304 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2305 View v = cl.getChildAt(item.cellX, item.cellY);
2306 Object tag = v.getTag();
2307 String desc = "Collision while binding workspace item: " + item
2308 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002309 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002310 throw (new RuntimeException(desc));
2311 } else {
2312 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002313 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002314 continue;
2315 }
2316 }
2317 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302318 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002319 if (animateIcons) {
2320 // Animate all the applications up now
2321 view.setAlpha(0f);
2322 view.setScaleX(0f);
2323 view.setScaleY(0f);
2324 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002325 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002328
Winson Chung997a9232013-07-24 15:33:46 -07002329 if (animateIcons) {
2330 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002331 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002332 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002333 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002334 final Runnable startBounceAnimRunnable = new Runnable() {
2335 public void run() {
2336 anim.playTogether(bounceAnims);
2337 anim.start();
2338 }
2339 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002340 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002341 // We post the animation slightly delayed to prevent slowdowns
2342 // when we are loading right after we return to launcher.
2343 mWorkspace.postDelayed(new Runnable() {
2344 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002345 if (mWorkspace != null) {
2346 mWorkspace.snapToPage(newScreenIndex);
2347 mWorkspace.postDelayed(startBounceAnimRunnable,
2348 NEW_APPS_ANIMATION_DELAY);
2349 }
Winson Chung94d67682013-09-25 16:29:40 -07002350 }
2351 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002352 } else {
2353 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002354 }
2355 }
Winson Chung64359a52013-07-08 17:17:08 -07002356 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002357 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 }
2359
Joe Onorato9c1289c2009-08-17 11:03:03 -04002360 /**
2361 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002362 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002363 public void bindAppWidget(LauncherAppWidgetInfo item) {
2364 View view = inflateAppWidget(item);
2365 if (view != null) {
2366 mWorkspace.addInScreen(view, item);
2367 mWorkspace.requestLayout();
2368 }
2369 }
2370
2371 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002372 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302373 PendingAppWidgetHostView view =
2374 new PendingAppWidgetHostView(this, item, mIconCache, true);
2375 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002376 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002377 }
2378
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002379 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002380
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002381 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002382
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002383 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2384 // If the provider is not ready, bind as a pending widget.
2385 appWidgetInfo = null;
2386 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2387 // The widget id is not valid. Try to find the widget based on the provider info.
2388 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2389 } else {
2390 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2391 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002392
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002393 // If the provider is ready, but the width is not yet restored, try to restore it.
2394 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2395 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002396 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002397 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2398 + " belongs to component " + item.providerName
2399 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002400 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002401 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002402 }
Sunny Goyalff572272014-07-23 13:58:07 -07002403
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002404 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002405 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002406 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2407 // Id has not been allocated yet. Allocate a new id.
2408 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2409 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002410
Sunny Goyald478c832016-04-01 12:04:16 -07002411 // Also try to bind the widget. If the bind fails, the user will be shown
2412 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002413 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002414 pendingInfo.spanX = item.spanX;
2415 pendingInfo.spanY = item.spanY;
2416 pendingInfo.minSpanX = item.minSpanX;
2417 pendingInfo.minSpanY = item.minSpanY;
2418 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002419
2420 boolean isDirectConfig =
2421 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2422 if (isDirectConfig && item.bindOptions != null) {
2423 Bundle newOptions = item.bindOptions.getExtras();
2424 if (options != null) {
2425 newOptions.putAll(options);
2426 }
2427 options = newOptions;
2428 }
Sunny Goyald478c832016-04-01 12:04:16 -07002429 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2430 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002431
Sunny Goyal86df1382016-08-10 15:03:22 -07002432 // We tried to bind once. If we were not able to bind, we would need to
2433 // go through the permission dialog, which means we cannot skip the config
2434 // activity.
2435 item.bindOptions = null;
2436 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2437
Sunny Goyald478c832016-04-01 12:04:16 -07002438 // Bind succeeded
2439 if (success) {
2440 // If the widget has a configure activity, it is still needs to set it up,
2441 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002442 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002443 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2444 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002445 }
Sunny Goyald478c832016-04-01 12:04:16 -07002446
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002447 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002448 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002449 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002450 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002451 // The widget was marked as UI not ready, but there is no configure activity to
2452 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002453 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002454 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002455 }
Sunny Goyalff572272014-07-23 13:58:07 -07002456 }
2457
Sunny Goyald5462aa2016-11-22 16:52:39 +05302458 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002459 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002460 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002461 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002462 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002463 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002464 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002465 }
2466
Sunny Goyal233ee962015-08-03 13:05:01 -07002467 item.minSpanX = appWidgetInfo.minSpanX;
2468 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302469 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002470 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302471 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002472 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302473 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002474
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002475 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002476 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002477 }
2478
Sunny Goyalff572272014-07-23 13:58:07 -07002479 /**
2480 * Restores a pending widget.
2481 *
2482 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002483 */
Sunny Goyald478c832016-04-01 12:04:16 -07002484 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002485 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2486 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2487 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002488 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002489 }
2490
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002491 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002492 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002493 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2494 info.pendingItemInfo = null;
2495 }
Sunny Goyalff572272014-07-23 13:58:07 -07002496
Sunny Goyalba47faa2017-10-04 16:50:57 -07002497 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002498 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002499 }
2500
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002501 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002502 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002503 }
2504
Adam Cohen1462de32012-07-24 22:34:36 -07002505 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002506 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002507 }
2508
Sunny Goyal527c7d32015-08-28 15:19:36 -07002509 @Override
2510 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2511 if (mPendingExecutor != null) {
2512 mPendingExecutor.markCompleted();
2513 }
2514 mPendingExecutor = executor;
2515 executor.attachTo(this);
2516 }
2517
2518 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2519 if (mPendingExecutor == executor) {
2520 mPendingExecutor = null;
2521 }
2522 }
2523
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002524 @Override
2525 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002526 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002527 mDragLayer.animate().alpha(1).withEndAction(
2528 executor == null ? null : executor::onLoadAnimationCompleted).start();
2529 } else if (executor != null) {
2530 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002531 }
2532 }
2533
Joe Onorato9c1289c2009-08-17 11:03:03 -04002534 /**
2535 * Callback saying that there aren't any more items to bind.
2536 *
2537 * Implementation of the method from LauncherModel.Callbacks.
2538 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002539 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002540 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002541 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002542
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002543 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002544
Sunny Goyal2100c782016-08-22 16:00:03 -07002545 if (mPendingActivityResult != null) {
2546 handleActivityResult(mPendingActivityResult.requestCode,
2547 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2548 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002549 }
2550
Sunny Goyala474a9b2017-05-04 16:47:11 -07002551 InstallShortcutReceiver.disableAndFlushInstallQueue(
2552 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002553
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002554 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002555 }
2556
Winson Chunga2413752012-04-03 14:22:34 -07002557 private boolean canRunNewAppsAnimation() {
2558 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002559 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002560 }
2561
Winson Chungc9168342013-06-26 14:54:55 -07002562 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002563 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002564 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2565 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002566 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002567 return bounceAnim;
2568 }
2569
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002570 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002571 * Add the icons for all apps.
2572 *
2573 * Implementation of the method from LauncherModel.Callbacks.
2574 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002575 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002576 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002577 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002578 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002579 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002580 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002581 return;
2582 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002583
Winson Chungb745afb2015-03-02 11:51:23 -08002584 mAppsView.setApps(apps);
2585 }
Adam Cohen9211d422014-10-07 18:14:53 -07002586 if (mLauncherCallbacks != null) {
2587 mLauncherCallbacks.bindAllApplications(apps);
2588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002589 }
2590
2591 /**
Tony2917a8b2017-07-31 23:29:42 -07002592 * Returns an Executor that will run after the launcher is first drawn (including after the
2593 * initial fade in animation). Returns null if the first draw has already occurred.
2594 */
2595 public @Nullable Executor getPendingExecutor() {
2596 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2597 }
2598
2599 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002600 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2601 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2602 */
2603 @Override
2604 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002605 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002606 }
2607
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002608 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609 * A package was updated.
2610 *
2611 * Implementation of the method from LauncherModel.Callbacks.
2612 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002613 @Override
2614 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002615 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002616 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 }
2619
Sunny Goyal4390ace2014-10-13 11:33:11 -07002620 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002621 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002622 if (mAppsView != null) {
2623 mAppsView.updatePromiseAppProgress(app);
2624 }
2625 }
2626
2627 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002628 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002629 mWorkspace.widgetsRestored(widgets);
2630 }
2631
Joe Onorato9c1289c2009-08-17 11:03:03 -04002632 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002633 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002634 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002635 *
2636 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002637 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002638 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002639 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002640 if (!updated.isEmpty()) {
2641 mWorkspace.updateShortcuts(updated);
2642 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002643 }
2644
2645 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002646 * Update the state of a package, typically related to install state.
2647 *
2648 * Implementation of the method from LauncherModel.Callbacks.
2649 */
Sunny Goyale755d462014-07-22 13:48:29 -07002650 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002651 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002652 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002653 }
2654
2655 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002656 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002657 * in addition to specific applications to remove, the reason being that
2658 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002659 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002660 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002661 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002662 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002663 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002664 mWorkspace.removeItemsByMatcher(matcher);
2665 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002666 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002667
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002668 @Override
2669 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002670 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002671 if (mAppsView != null) {
2672 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002673 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002674 }
Joe Onoratobe386092009-11-17 17:32:16 -08002675
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002676 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002677 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2678 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002679 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2680 if (topView != null) {
2681 topView.onWidgetsBound();
2682 }
2683 }
2684
Tony Wickham86222d22017-03-29 15:30:43 -07002685 /**
2686 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2687 * refreshes the widgets and shortcuts associated with the given package/user
2688 */
2689 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002690 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002691 }
2692
Sunny Goyalc99cb172017-10-19 16:15:09 -07002693 public boolean isRotationEnabled () {
2694 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002695 }
2696
Winson Chung80baf5a2010-08-09 16:03:15 -07002697 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002698 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002699 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002700 @Override
2701 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2702 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002703
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002704 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2705 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002706 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002707 writer.println(prefix + " Homescreen " + i);
2708
2709 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2710 for (int j = 0; j < layout.getChildCount(); j++) {
2711 Object tag = layout.getChildAt(j).getTag();
2712 if (tag != null) {
2713 writer.println(prefix + " " + tag.toString());
2714 }
2715 }
2716 }
2717
2718 writer.println(prefix + " Hotseat");
2719 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002720 for (int j = 0; j < layout.getChildCount(); j++) {
2721 Object tag = layout.getChildAt(j).getTag();
2722 if (tag != null) {
2723 writer.println(prefix + " " + tag.toString());
2724 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002725 }
Sunny Goyala1365452015-10-01 15:46:24 -07002726
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002727 try {
2728 FileLog.flushAll(writer);
2729 } catch (Exception e) {
2730 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002731 }
2732 }
2733
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002734 writer.println(prefix + "Misc:");
2735 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2736 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2737 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2738
2739 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002740
Adam Cohen9211d422014-10-07 18:14:53 -07002741 if (mLauncherCallbacks != null) {
2742 mLauncherCallbacks.dump(prefix, fd, writer, args);
2743 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002744 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002745
Sunny Goyal66b24572016-09-21 15:57:55 -07002746 @Override
2747 @TargetApi(Build.VERSION_CODES.N)
2748 public void onProvideKeyboardShortcuts(
2749 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2750
2751 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002752 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002753 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2754 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2755 }
2756 View currentFocus = getCurrentFocus();
2757 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2758 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2759 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2760 }
Tony18c4aa42017-05-10 21:23:57 -05002761 if (currentFocus.getTag() instanceof ItemInfo
2762 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002763 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2764 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2765 }
2766 if (!shortcutInfos.isEmpty()) {
2767 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2768 }
2769
2770 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2771 }
2772
2773 @Override
2774 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2775 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2776 switch (keyCode) {
2777 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002778 if (isInState(NORMAL)) {
2779 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002780 return true;
2781 }
2782 break;
2783 case KeyEvent.KEYCODE_S: {
2784 View focusedView = getCurrentFocus();
2785 if (focusedView instanceof BubbleTextView
2786 && focusedView.getTag() instanceof ItemInfo
2787 && mAccessibilityDelegate.performAction(focusedView,
2788 (ItemInfo) focusedView.getTag(),
2789 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002790 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002791 return true;
2792 }
2793 break;
2794 }
2795 case KeyEvent.KEYCODE_O:
2796 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2797 return true;
2798 }
2799 break;
2800 }
2801 }
2802 return super.onKeyShortcut(keyCode, event);
2803 }
2804
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002805 public static Launcher getLauncher(Context context) {
2806 if (context instanceof Launcher) {
2807 return (Launcher) context;
2808 }
2809 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2810 }
2811
Sunny Goyal5a81c382017-03-20 15:08:06 -07002812 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002813
2814 @Override
2815 public void onSharedPreferenceChanged(
2816 SharedPreferences sharedPreferences, String key) {
2817 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002818 // Recreate the activity so that it initializes the rotation preference again.
2819 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002820 }
2821 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002822 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002823
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002824 @Override
2825 public void onActionModeStarted(ActionMode mode) {
2826 super.onActionModeStarted(mode);
2827 mCurrentActionMode = mode;
2828 }
2829
2830 @Override
2831 public void onActionModeFinished(ActionMode mode) {
2832 super.onActionModeFinished(mode);
2833 mCurrentActionMode = null;
2834 }
2835
2836 public boolean finishAutoCancelActionMode() {
2837 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2838 mCurrentActionMode.finish();
2839 return true;
2840 }
2841 return false;
2842 }
2843
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002844 /**
2845 * Callback for listening for onResume
2846 */
2847 public interface OnResumeCallback {
2848
2849 void onLauncherResume();
2850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851}