blob: fcf36ca885c8e04d0de8a72e5930d00bac1d3cc7 [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 Goyalf8d56fc2018-01-31 15:18:11 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Sunny Goyalc99cb172017-10-19 16:15:09 -070021import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
22import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
23
Sunny Goyal076839c2017-10-30 13:52:20 -070024import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
27import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
28import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070029import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070030import static com.android.launcher3.LauncherState.ALL_APPS;
31import static com.android.launcher3.LauncherState.NORMAL;
32import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070033import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070034
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080045import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070046import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080048import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080049import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070051import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040052import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070054import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070055import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070056import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070057import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080058import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.content.pm.PackageManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080060import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Sunny Goyal02424b22018-01-19 11:24:32 -080063import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020067import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070069import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080070import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070071import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070072import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070073import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070074import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070076import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070077import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080078import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.HapticFeedbackConstants;
81import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070082import android.view.KeyboardShortcutGroup;
83import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080084import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080088import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070096import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070097import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070099import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -0800100import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700103import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700107import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700108import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700109import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700112import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700113import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700114import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700115import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800116import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800117import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800118import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700120import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800121import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800122import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800123import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530124import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
125import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700126import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700128import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700129import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700130import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700131import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800132import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700133import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700134import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700135import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700136import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700137import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700138import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700139import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800140import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800141import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700142import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800143import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800144import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700145import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700146import com.android.launcher3.widget.WidgetListRowEntry;
147import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700148import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700149
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700150import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700151import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700152import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700153import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700154import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700155import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800156import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700157import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159/**
160 * Default launcher application.
161 */
Sunny Goyal27835952017-01-13 12:15:53 -0800162public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700163 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
164 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700165 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700166 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700167 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chunga2413752012-04-03 14:22:34 -0700169 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700175 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Michael Jurka8b805b12012-04-18 14:23:14 -0700177 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700178 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
179 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700180
Jon Mirandac476d6e2017-05-04 16:30:01 -0700181 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700182
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700183 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
184
Mathew Inwood876a8462013-06-14 14:12:41 +0100185 /**
186 * IntentStarter uses request codes starting with this. This must be greater than all activity
187 * request codes used internally.
188 */
189 protected static final int REQUEST_LAST = 100;
190
Winson Chung2672ff92012-05-04 16:22:30 -0700191 // The Intent extra that defines whether to ignore the launch animation
192 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400193 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 // Type: int
196 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700197 // Type: int
198 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700199 // Type: PendingRequestArgs
200 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
201 // Type: ActivityResultInfo
202 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700203 // Type: SparseArray<Parcelable>
204 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800206 // When starting an action mode, setting this tag will cause the action mode to be cancelled
207 // automatically when user interacts with the launcher.
208 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
209
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700210 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
219 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Jon Miranda54441f52018-01-24 15:38:25 -0800221 private LauncherAppTransitionManager mAppTransitionManager;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800222 private Configuration mOldConfig;
Jon Miranda54441f52018-01-24 15:38:25 -0800223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700226 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800227 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700228
Sunny Goyalffe83f12014-08-14 17:39:34 -0700229 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700230 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700232 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700233
Sunny Goyal316490e2015-06-02 09:38:28 -0700234 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700235
Sunny Goyal47328fd2016-05-25 18:56:41 -0700236 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700237
238 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700239 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700240 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700241
Winson Chung8ae41982017-11-10 11:42:13 -0800242 // UI and state for the overview panel
243 private ViewGroup mOverviewPanel;
244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700247 private OnResumeCallback mOnResumeCallback;
248
Sunny Goyal527c7d32015-08-28 15:19:36 -0700249 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800252 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700254 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255
Jon Miranda2d89ea82017-05-04 11:47:53 -0700256 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700257 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700258
Tony Wickham010d2552017-01-20 08:15:28 -0800259 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700260
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800261 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700262
Winson Chung46353de2012-02-16 14:05:10 -0800263 // We only want to get the SharedPreferences once since it does an FS stat each time we get
264 // it from the context.
265 private SharedPreferences mSharedPrefs;
266
Sunny Goyal2100c782016-08-22 16:00:03 -0700267 // Activity result which needs to be processed after workspace has loaded.
268 private ActivityResultInfo mPendingActivityResult;
269 /**
270 * Holds extra information required to handle a result from an external call, like
271 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
272 */
273 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800274
Sunny Goyal02424b22018-01-19 11:24:32 -0800275 private final PointF mLastDispatchTouchEvent = new PointF();
Jon Miranda379198e2016-09-23 08:54:40 -0700276
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700277 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700278 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700279 private boolean mAppLaunchSuccess;
280
Sunny Goyal745bad92016-05-02 10:54:12 -0700281 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800282 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 @Override
285 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700286 if (DEBUG_STRICT_MODE) {
287 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
288 .detectDiskReads()
289 .detectDiskWrites()
290 .detectNetwork() // or .detectAll() for all detectable problems
291 .penaltyLog()
292 .build());
293 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
294 .detectLeakedSqlLiteObjects()
295 .detectLeakedClosableObjects()
296 .penaltyLog()
297 .penaltyDeath()
298 .build());
299 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700300 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700301
Adam Cohen9211d422014-10-07 18:14:53 -0700302 if (mLauncherCallbacks != null) {
303 mLauncherCallbacks.preOnCreate();
304 }
305
Mario Bertschler27288382017-05-24 15:35:09 -0700306 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
307 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700308 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700311 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400312
Sunny Goyal87f784c2017-01-11 10:48:34 -0800313 LauncherAppState app = LauncherAppState.getInstance(this);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800314 mOldConfig = new Configuration(getResources().getConfiguration());
315 initDeviceProfile(app.getInvariantDeviceProfile());
Winson Chung6e1c0d32013-10-25 15:24:24 -0700316
Sunny Goyalf7258242015-10-19 16:59:07 -0700317 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700318 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800319 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800320 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800321 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700322 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700323
Joe Onorato41a12d22009-10-31 18:30:00 -0400324 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700325 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800326 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700327
Sunny Goyalffe83f12014-08-14 17:39:34 -0700328 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700329
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700330 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700331 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700332
Sunny Goyal60820d72017-05-09 12:40:11 -0700333 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800336 mPopupDataProvider = new PopupDataProvider(this);
337
Sunny Goyald3864fa2017-11-14 15:06:36 -0800338 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
339 // In case we are on a device with locked rotation, we should look at preferences to check
340 // if the user has specifically allowed rotation.
341 if (!mRotationEnabled) {
342 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
343 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
344 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346
Sunny Goyald3864fa2017-11-14 15:06:36 -0800347 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
348 if (internalStateHandled) {
349 // Temporarily enable the rotation
350 mRotationEnabled = true;
351
352 if (savedInstanceState != null) {
353 // InternalStateHandler has already set the appropriate state.
354 // We dont need to do anything.
355 savedInstanceState.remove(RUNTIME_STATE);
356 }
357 }
358 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800359
Sunny Goyal2100c782016-08-22 16:00:03 -0700360 // We only load the page synchronously if the user rotates (or triggers a
361 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700362 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
363 if (savedInstanceState != null) {
364 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
365 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800366
Sunny Goyalfe770c92016-09-27 14:38:58 -0700367 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800368 if (!internalStateHandled) {
369 // If we are not binding synchronously, show a fade in animation when
370 // the first page bind completes.
371 mDragLayer.setAlpha(0);
372 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700373 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700374 // Pages bound synchronously.
375 mWorkspace.setCurrentPage(currentScreen);
376
Sunny Goyal2100c782016-08-22 16:00:03 -0700377 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
379
380 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800381 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800382
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700383 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
384 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700385 setRequestedOrientation(mRotationEnabled
386 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700387
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800388 setContentView(mLauncherView);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800389 getRootView().dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700390
Jon Miranda7fda2852017-07-19 16:07:01 -0700391 // Listen for broadcasts
392 IntentFilter filter = new IntentFilter();
393 filter.addAction(Intent.ACTION_SCREEN_OFF);
394 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
395 registerReceiver(mReceiver, filter);
396 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700397
Sunny Goyal8392c822017-06-20 10:03:56 -0700398 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
399 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700400
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800401 mAppTransitionManager = LauncherAppTransitionManager.newInstance(this);
Jon Miranda7853bdb2018-01-22 17:32:30 -0800402
Sunny Goyal14b32402017-07-31 10:03:28 -0700403 if (mLauncherCallbacks != null) {
404 mLauncherCallbacks.onCreate(savedInstanceState);
405 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700406
407 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700408 }
409
Sunny Goyal7779d622015-06-11 16:18:39 -0700410 @Override
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800411 public void onConfigurationChanged(Configuration newConfig) {
412 int diff = newConfig.diff(mOldConfig);
413 if ((diff & (CONFIG_ORIENTATION | CONFIG_SCREEN_SIZE)) != 0) {
414 mUserEventDispatcher = null;
415 initDeviceProfile(mDeviceProfile.inv);
Sunny Goyalfde55052018-02-01 14:46:13 -0800416 dispatchDeviceProfileChanged();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800417
418 getRootView().dispatchInsets();
419 getStateManager().reapplyState();
420
421 // TODO: We can probably avoid rebind when only screen size changed.
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800422 int currentPage = mWorkspace.getNextPage();
423 if (mModel.startLoader(currentPage)) {
424 mWorkspace.setCurrentPage(currentPage);
425 setWorkspaceLoading(true);
426 }
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800427 }
428
429 mOldConfig.setTo(newConfig);
430 super.onConfigurationChanged(newConfig);
431 }
432
433 private void initDeviceProfile(InvariantDeviceProfile idp) {
434 // Load configuration-specific DeviceProfile
435 mDeviceProfile = idp.getDeviceProfile(this);
436 if (isInMultiWindowModeCompat()) {
437 Display display = getWindowManager().getDefaultDisplay();
438 Point mwSize = new Point();
439 display.getSize(mwSize);
440 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
441 }
442 }
443
444 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700445 public void onThemeChanged() {
446 recreate();
447 }
448
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700449 public LauncherStateManager getStateManager() {
450 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700451 }
452
Mario Bertschlera6936942017-05-31 14:48:19 -0700453 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700454 if (isDark) {
455 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700456 } else if (supportsDarkText) {
457 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700458 }
459 }
460
461 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700462 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800463 return mLauncherView.findViewById(id);
464 }
465
466 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700467 public void onAppWidgetHostReset() {
468 if (mAppWidgetHost != null) {
469 mAppWidgetHost.startListening();
470 }
471 }
472
Adam Cohen9211d422014-10-07 18:14:53 -0700473 private LauncherCallbacks mLauncherCallbacks;
474
Sunny Goyal32554d12015-12-03 15:31:25 -0800475 /**
476 * Call this after onCreate to set or clear overlay.
477 */
478 public void setLauncherOverlay(LauncherOverlay overlay) {
479 if (overlay != null) {
480 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
481 }
482 mWorkspace.setLauncherOverlay(overlay);
483 }
484
Adam Cohen9211d422014-10-07 18:14:53 -0700485 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
486 mLauncherCallbacks = callbacks;
487 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700488 }
489
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700490 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700491 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onLauncherProviderChange();
494 }
495 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700496
Hyunyoung Song3f471442015-04-08 19:01:34 -0700497 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700498 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
499 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700500 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700501 }
502
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000503 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700504 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
505 // This cast is safe as long as the id < 0x00FFFFFF
506 // Since we jail all the dynamically generated views, there should be no clashes
507 // with any other views.
508 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000509 }
510
Tony Wickham010d2552017-01-20 08:15:28 -0800511 public PopupDataProvider getPopupDataProvider() {
512 return mPopupDataProvider;
513 }
514
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000515 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700516 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
517 * a configuration step, this allows the proper animations to run after other transitions.
518 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 private long completeAdd(
520 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
521 long screenId = info.screenId;
522 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700523 // When the screen id represents an actual screen (as opposed to a rank) we make sure
524 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700526 }
Adam Cohendb364c32014-05-20 14:23:40 -0700527
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800529 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700530 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700531 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800532 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700534 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700535 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700536 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700537 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700538 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700539 int widgetId = appWidgetId;
540 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700541 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700543 // Since the view was just bound, also launch the configure activity if needed
544 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
545 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800546 if (provider != null) {
547 new WidgetAddFlowHandler(provider)
548 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700549 }
550 }
551 break;
552 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800553 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700554
Adam Cohendb364c32014-05-20 14:23:40 -0700555 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800556 }
557
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800558 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700559 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 if (isWorkspaceLoading()) {
561 // process the result once the workspace has loaded.
562 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
563 return;
564 }
565 mPendingActivityResult = null;
566
Winson Chunge341d302013-08-16 14:31:00 -0700567 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700568 final PendingRequestArgs requestArgs = mPendingRequestArgs;
569 setWaitingForResult(null);
570 if (requestArgs == null) {
571 return;
572 }
573
574 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700575
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 Runnable exitSpringLoaded = new Runnable() {
577 @Override
578 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700579 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700580 }
581 };
582
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700584 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700585 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700586 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
587 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700588 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700589 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700590 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700591 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700592 addAppWidgetImpl(
593 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800594 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700595 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700596 }
597 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200598 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700599 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700600 // User could have free-scrolled between pages before picking a wallpaper; make sure
601 // we move to the closest one now.
602 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700603 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200604 }
605 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700606 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200607
Adam Cohened66b2b2012-01-23 17:28:51 -0800608 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700609 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700610
Adam Cohened66b2b2012-01-23 17:28:51 -0800611 // We have special handling for widgets
612 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800613 final int appWidgetId;
614 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
615 : -1;
616 if (widgetId < 0) {
617 appWidgetId = pendingAddWidgetId;
618 } else {
619 appWidgetId = widgetId;
620 }
621
Adam Cohenad4e15c2013-10-17 16:21:35 -0700622 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800623 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700624 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
625 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700626 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700627 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700628 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700629 @Override
630 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700631 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700632 }
633 };
Adam Cohendb364c32014-05-20 14:23:40 -0700634
Sunny Goyal2100c782016-08-22 16:00:03 -0700635 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
636 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
637 } else {
638 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
639 // When the screen id represents an actual screen (as opposed to a rank)
640 // we make sure that the drop page actually exists.
641 requestArgs.screenId =
642 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700643 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700644 final CellLayout dropLayout =
645 mWorkspace.getScreenWithId(requestArgs.screenId);
646
647 dropLayout.setDropPending(true);
648 final Runnable onComplete = new Runnable() {
649 @Override
650 public void run() {
651 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
652 dropLayout.setDropPending(false);
653 }
654 };
655 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
656 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700657 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 return;
659 }
660
Sunny Goyald478c832016-04-01 12:04:16 -0700661 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
662 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700663 if (resultCode == RESULT_OK) {
664 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700666 }
667 // Leave the widget in the pending state if the user canceled the configure.
668 return;
669 }
670
Sunny Goyalaad90582015-07-09 14:22:50 -0700671 if (requestCode == REQUEST_CREATE_SHORTCUT) {
672 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
674 completeAdd(requestCode, data, -1, requestArgs);
675 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
676 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
677
Sunny Goyalaad90582015-07-09 14:22:50 -0700678 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700679 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
680 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800684 }
685
686 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700687 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800688 final int requestCode, final int resultCode, final Intent data) {
689 handleActivityResult(requestCode, resultCode, data);
690 if (mLauncherCallbacks != null) {
691 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
692 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 }
694
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700695 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700696 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600697 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 PendingRequestArgs pendingArgs = mPendingRequestArgs;
699 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
700 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
701 setWaitingForResult(null);
702
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700704 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700705 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700706 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700707 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700708 Intent intent = pendingArgs.getPendingIntent();
709
Sunny Goyal28c6b962015-10-12 11:42:05 -0700710 if (grantResults.length > 0
711 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700712 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700713 } else {
714 // TODO: Show a snack bar with link to settings
715 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700716 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700717 }
718 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600719 if (mLauncherCallbacks != null) {
720 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
721 grantResults);
722 }
723 }
724
Adam Cohendb364c32014-05-20 14:23:40 -0700725 /**
726 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
727 *
728 * @param screenId the screen id to check
729 * @return the new screen, or screenId if it exists
730 */
731 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800732 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700733 if (dropLayout == null) {
734 // it's possible that the add screen was removed because it was
735 // empty and a re-bind occurred
736 mWorkspace.addExtraEmptyScreen();
737 return mWorkspace.commitExtraEmptyScreen();
738 } else {
739 return screenId;
740 }
741 }
742
Sunny Goyal2100c782016-08-22 16:00:03 -0700743 @Thunk void completeTwoStageWidgetDrop(
744 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
745 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 Runnable onCompleteRunnable = null;
747 int animationType = 0;
748
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700749 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 if (resultCode == RESULT_OK) {
751 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
752 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800753 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 onCompleteRunnable = new Runnable() {
756 @Override
757 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700758 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700759 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 }
761 };
762 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800763 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700766 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700767 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700768 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
769 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700771 // The animated view may be null in the case of a rotation during widget configuration
772 onCompleteRunnable.run();
773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775
776 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700777 protected void onStop() {
778 super.onStop();
779 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700780
781 if (mLauncherCallbacks != null) {
782 mLauncherCallbacks.onStop();
783 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800784 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800785
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700786 if (!mAppLaunchSuccess) {
787 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700788 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700789 }
Tony Wickham010d2552017-01-20 08:15:28 -0800790 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700791 }
792
793 @Override
794 protected void onStart() {
795 super.onStart();
796 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700797
798 if (mLauncherCallbacks != null) {
799 mLauncherCallbacks.onStart();
800 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800801 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800802 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700803
Jon Mirandade43a712018-01-18 11:35:10 -0800804 if (mShouldFadeInScrim && mLauncherView.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700805 if (mScrimAnimator != null) {
806 mScrimAnimator.cancel();
807 }
Jon Mirandade43a712018-01-18 11:35:10 -0800808 mLauncherView.getBackground().setAlpha(0);
809 mScrimAnimator = ObjectAnimator.ofInt(mLauncherView.getBackground(),
Jon Miranda2d89ea82017-05-04 11:47:53 -0700810 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
811 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
812 @Override
813 public void onAnimationEnd(Animator animation) {
814 mScrimAnimator = null;
815 }
816 });
817 mScrimAnimator.setDuration(600);
818 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
819 mScrimAnimator.start();
820 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700821 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700822 }
823
824 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700826 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700828 TraceHelper.partitionSection("ON_RESUME", "superCall");
829
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700830 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700831 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700832 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700833 // Process any items that were added while Launcher was away.
834 InstallShortcutReceiver.disableAndFlushInstallQueue(
835 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700836
Sunny Goyala474a9b2017-05-04 16:47:11 -0700837 // Refresh shortcuts if the permission changed.
838 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700839
Sunny Goyal7ede6112017-12-05 15:11:21 -0800840 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700841 if (mLauncherCallbacks != null) {
842 mLauncherCallbacks.onResume();
843 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800844
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700845 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700846 }
847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700850 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700851 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700852
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700853 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800854 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700855 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700856
Adam Cohen9211d422014-10-07 18:14:53 -0700857 if (mLauncherCallbacks != null) {
858 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700859 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700860 }
861
Adam Cohenc2d6e892014-10-16 09:49:24 -0700862 public interface LauncherOverlay {
863
864 /**
865 * Touch interaction leading to overscroll has begun
866 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700867 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700868
869 /**
870 * Touch interaction related to overscroll has ended
871 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700872 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700873
874 /**
875 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
876 * screen (or in the case of RTL, the rightmost screen).
877 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700878 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700879
880 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800881 * Called when the launcher is ready to use the overlay
882 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700883 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700884 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885 }
886
887 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700888 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700889 }
890
891 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
892
Sunny Goyalc86df472016-02-25 09:19:38 -0800893 public void onScrollChanged(float progress) {
894 if (mWorkspace != null) {
895 mWorkspace.onOverlayScrollChanged(progress);
896 }
897 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 }
899
Sunny Goyal16764582017-11-06 16:00:34 -0800900 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700901 if (mLauncherCallbacks != null) {
902 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700903 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800904 // On O and above we there is always some setting present settings (add icon to
905 // home screen or icon badging). On earlier APIs we will have the allow rotation
906 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700907 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700908 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800909 }
910
Sunny Goyalf9403d92017-10-18 10:55:56 -0700911 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700912 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700913 }
914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 /**
916 * Restores the previous state, if it exists.
917 *
918 * @param savedState The previous state.
919 */
920 private void restoreState(Bundle savedState) {
921 if (savedState == null) {
922 return;
923 }
924
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700925 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700926 LauncherState[] stateValues = LauncherState.values();
927 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800928 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700929 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800930 }
931
Sunny Goyal2100c782016-08-22 16:00:03 -0700932 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
933 if (requestArgs != null) {
934 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700936
937 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700938
939 SparseArray<Parcelable> widgetsState =
940 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
941 if (widgetsState != null) {
942 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 /**
947 * Finds all the views we need and configure them properly.
948 */
949 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800950 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700951 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700952 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700953 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800954 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700955
Sunny Goyal784f9c32016-03-23 16:56:54 -0700956 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
957 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
958 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
Winson Chung4c98d922011-05-31 16:50:48 -0700960 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800961 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700962
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 // Setup the hotseat
964 mHotseat = (Hotseat) findViewById(R.id.hotseat);
965 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700966 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 }
968
Winson Chung4c98d922011-05-31 16:50:48 -0700969 // Setup the workspace
970 mWorkspace.setHapticFeedbackEnabled(false);
971 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700972 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700973 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
974 // default state, otherwise we will update to the wrong offsets in RTL
975 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700976 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700977 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700978
Tony Wickham34d2c912015-09-11 09:27:58 -0700979 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700980 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700981
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700982 // Setup Apps
983 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700984
Winson Chung3d503fb2011-07-13 17:25:49 -0700985 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700986 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700987 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400988
Hyunyoung Songd725f642017-08-17 22:26:35 -0700989 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
991
992 /**
993 * Creates a view representing a shortcut.
994 *
995 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700998 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
1001 /**
1002 * Creates a view representing a shortcut inflated from the specified resource.
1003 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 * @param parent The group the shortcut belongs to.
1005 * @param info The data structure describing the shortcut.
1006 *
1007 * @return A View inflated from layoutResId.
1008 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001009 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001010 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1011 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001012 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001014 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 return favorite;
1016 }
1017
1018 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001019 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 *
1021 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001024 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001025 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1026 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001027 return;
1028 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001029
Jon Mirandac476d6e2017-05-04 16:30:01 -07001030 int[] cellXY = mTmpAddItemCellCoordinates;
1031 CellLayout layout = getCellLayout(container, screenId);
1032
Sunny Goyal782f0c92017-01-19 10:27:54 -08001033 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001034 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001035 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1036 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001037 }
1038
1039 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001040 // Legacy shortcuts are only supported for primary profile.
1041 info = Process.myUserHandle().equals(args.user)
1042 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001043
Sunny Goyalb57645f2017-03-20 13:57:28 -07001044 if (info == null) {
1045 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1046 return;
1047 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001048 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001049 // The app is trying to add a shortcut without sufficient permissions
1050 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1051 return;
1052 }
1053 }
1054
Jon Mirandac476d6e2017-05-04 16:30:01 -07001055 if (container < 0) {
1056 // Adding a shortcut to the Workspace.
1057 final View view = createShortcut(info);
1058 boolean foundCellSpan = false;
1059 // First we check if we already know the exact location where we want to add this item.
1060 if (cellX >= 0 && cellY >= 0) {
1061 cellXY[0] = cellX;
1062 cellXY[1] = cellY;
1063 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001064
Jon Mirandac476d6e2017-05-04 16:30:01 -07001065 // If appropriate, either create a folder or add to an existing folder
1066 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001067 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001068 return;
1069 }
1070 DragObject dragObject = new DragObject();
1071 dragObject.dragInfo = info;
1072 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1073 true)) {
1074 return;
1075 }
1076 } else {
1077 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1078 }
1079
1080 if (!foundCellSpan) {
1081 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001082 return;
1083 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001084
1085 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1086 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001087 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001088 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001089 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001090 if (folderIcon != null) {
1091 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1092 folderInfo.add(info, args.rank, false);
1093 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001094 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001095 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098
Sunny Goyale29897f2017-07-20 10:09:42 -07001099 public FolderIcon findFolderIcon(final long folderIconId) {
1100 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1101 @Override
1102 public boolean evaluate(ItemInfo info, View view) {
1103 return info != null && info.id == folderIconId;
1104 }
1105 });
1106 }
1107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001109 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001111 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001113 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001114 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1115
Adam Cohened66b2b2012-01-23 17:28:51 -08001116 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001117 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Adam Cohen59400422014-03-05 18:07:04 -08001120 LauncherAppWidgetInfo launcherInfo;
1121 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001122 launcherInfo.spanX = itemInfo.spanX;
1123 launcherInfo.spanY = itemInfo.spanY;
1124 launcherInfo.minSpanX = itemInfo.minSpanX;
1125 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001126 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001127
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001128 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001129 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130
Sunny Goyal2100c782016-08-22 16:00:03 -07001131 if (hostView == null) {
1132 // Perform actual inflation because we're live
1133 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001135 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301136 prepareAppWidget(hostView, launcherInfo);
1137 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Sunny Goyald5462aa2016-11-22 16:52:39 +05301140 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001141 hostView.setTag(item);
1142 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001143 hostView.setFocusable(true);
1144 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001145 }
1146
Adam Cohended9f8d2010-11-03 13:25:16 -07001147 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1148 @Override
1149 public void onReceive(Context context, Intent intent) {
1150 final String action = intent.getAction();
1151 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001152 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001153 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001154 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001155 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001157 mShouldFadeInScrim = true;
1158 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1159 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1160 // the user unlocked and the Launcher is not in the foreground.
1161 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 }
1163 }
1164 };
1165
Tony Wickham010d2552017-01-20 08:15:28 -08001166 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001167 mWorkspace.updateIconBadges(updatedBadges);
1168 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001169
Sunny Goyal29947f02017-12-18 13:49:44 -08001170 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1171 if (popup != null) {
1172 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001173 }
1174 }
1175
Adam Cohended9f8d2010-11-03 13:25:16 -07001176 @Override
1177 public void onAttachedToWindow() {
1178 super.onAttachedToWindow();
1179
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001180 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001181 if (mLauncherCallbacks != null) {
1182 mLauncherCallbacks.onAttachedToWindow();
1183 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001184 }
1185
1186 @Override
1187 public void onDetachedFromWindow() {
1188 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001189
1190 if (mLauncherCallbacks != null) {
1191 mLauncherCallbacks.onDetachedFromWindow();
1192 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 }
1194
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001195 public AllAppsTransitionController getAllAppsController() {
1196 return mAllAppsController;
1197 }
1198
Winson Chung1a341002018-01-17 10:00:23 -08001199 public LauncherRootView getRootView() {
1200 return (LauncherRootView) mLauncherView;
1201 }
1202
Winson Chunga6945242014-01-08 14:04:34 -08001203 public DragLayer getDragLayer() {
1204 return mDragLayer;
1205 }
1206
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001207 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001208 return mAppsView;
1209 }
1210
Winson Chunga6945242014-01-08 14:04:34 -08001211 public Workspace getWorkspace() {
1212 return mWorkspace;
1213 }
1214
1215 public Hotseat getHotseat() {
1216 return mHotseat;
1217 }
1218
Winson Chung8ae41982017-11-10 11:42:13 -08001219 public <T extends ViewGroup> T getOverviewPanel() {
1220 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001221 }
1222
Sunny Goyal47328fd2016-05-25 18:56:41 -07001223 public DropTargetBar getDropTargetBar() {
1224 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001225 }
1226
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001227 public LauncherAppWidgetHost getAppWidgetHost() {
1228 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
Romain Guycbb89e42009-06-08 15:52:54 -07001230
Winson Chunga9abd0e2010-10-27 17:18:37 -07001231 public LauncherModel getModel() {
1232 return mModel;
1233 }
1234
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001235 public ModelWriter getModelWriter() {
1236 return mModelWriter;
1237 }
1238
Adam Cohen79d90c52016-04-22 13:29:20 -07001239 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001240 return mSharedPrefs;
1241 }
1242
Sunny Goyalf8d56fc2018-01-31 15:18:11 -08001243 public int getOrientation() { return mOldConfig.orientation; }
Jon Miranda6bed3502017-09-19 14:43:17 -07001244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 @Override
1246 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001247 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 super.onNewIntent(intent);
1249
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001250 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001251 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1252 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001253
1254 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001255 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001256 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001257 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001258 boolean internalStateHandled = InternalStateHandler
Sunny Goyal8b264562018-02-03 23:00:13 -08001259 .handleNewIntent(this, intent, isStarted());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001260
Winson15f8b172015-08-19 11:02:39 -07001261 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001262 if (!internalStateHandled) {
1263 // Note: There should be at most one log per method call. This is enforced
1264 // implicitly by using if-else statements.
1265 UserEventDispatcher ued = getUserEventDispatcher();
1266 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1267 if (topOpenView != null) {
1268 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1269 } else if (alreadyOnHome) {
1270 Target target = newContainerTarget(mStateManager.getState().containerType);
1271 target.pageIndex = mWorkspace.getCurrentPage();
1272 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1273 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001274
Sunny Goyald3864fa2017-11-14 15:06:36 -08001275 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001276 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001277
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001278 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001279
1280 // Reset the apps view
1281 if (!alreadyOnHome && mAppsView != null) {
1282 mAppsView.reset();
1283 }
1284
1285 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1286 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1287 }
1288 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001289
1290 final View v = getWindow().peekDecorView();
1291 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001292 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001293 }
1294
Adam Cohen9211d422014-10-07 18:14:53 -07001295 if (mLauncherCallbacks != null) {
1296 mLauncherCallbacks.onHomeIntent();
1297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
Winson15f8b172015-08-19 11:02:39 -07001299
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001300 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001304 public void onRestoreInstanceState(Bundle state) {
1305 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001306 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
1309 @Override
1310 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001311 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001312 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001313
Adam Cohen21cd0022013-10-09 18:57:02 -07001314 }
Sunny Goyalea609262017-10-25 15:47:38 -07001315 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001316
1317
1318 AbstractFloatingView widgets = AbstractFloatingView
1319 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1320 if (widgets != null) {
1321 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1322 widgets.saveHierarchyState(widgetsState);
1323 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1324 } else {
1325 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1326 }
1327
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001328 // We close any open folders and shortcut containers since they will not be re-opened,
1329 // and we need to make sure this state is reflected.
1330 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001331
Sunny Goyal2100c782016-08-22 16:00:03 -07001332 if (mPendingRequestArgs != null) {
1333 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1334 }
1335 if (mPendingActivityResult != null) {
1336 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001339 super.onSaveInstanceState(outState);
1340
Adam Cohen9211d422014-10-07 18:14:53 -07001341 if (mLauncherCallbacks != null) {
1342 mLauncherCallbacks.onSaveInstanceState(outState);
1343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
1346 @Override
1347 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001349
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001350 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001351 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001352
Winson Chungcd2b0142011-06-08 16:02:26 -07001353 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001354 // It's possible to receive onDestroy after a new Launcher activity has
1355 // been created. In this case, don't interfere with the new Launcher.
1356 if (mModel.isCurrentCallbacks(this)) {
1357 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001358 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001359 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001360
Sunny Goyal745bad92016-05-02 10:54:12 -07001361 if (mRotationPrefChangeHandler != null) {
1362 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1363 }
1364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001366 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001368 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001372 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1373
Michael Jurka2ecf9952012-06-18 12:52:28 -07001374 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001375
Tony2917a8b2017-07-31 23:29:42 -07001376 clearPendingBinds();
1377
Adam Cohen9211d422014-10-07 18:14:53 -07001378 if (mLauncherCallbacks != null) {
1379 mLauncherCallbacks.onDestroy();
1380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382
Sunny Goyalae502842016-06-17 08:43:56 -07001383 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1384 return mAccessibilityDelegate;
1385 }
1386
Adam Cohena9cf38f2011-05-02 15:36:58 -07001387 public DragController getDragController() {
1388 return mDragController;
1389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001392 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001393 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001394 }
1395
1396 @Override
1397 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1398 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001399 try {
1400 super.startIntentSenderForResult(intent, requestCode,
1401 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1402 } catch (IntentSender.SendIntentException e) {
1403 throw new ActivityNotFoundException();
1404 }
1405 }
1406
Winson Chung70d72102011-08-12 11:24:35 -07001407 /**
1408 * Indicates that we want global search for this activity by setting the globalSearch
1409 * argument for {@link #startSearch} to true.
1410 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001412 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 Bundle appSearchData, boolean globalSearch) {
1414 if (appSearchData == null) {
1415 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001416 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001418
Sunny Goyal6f28e712016-09-13 14:19:29 -07001419 if (mLauncherCallbacks == null ||
1420 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1421 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001422 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001423 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001424
1425 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001426 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001427 }
1428
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001430 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 }
1432
Adam Cohen517a7f52014-03-01 12:12:59 -08001433 public boolean isWorkspaceLoading() {
1434 return mWorkspaceLoading;
1435 }
1436
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001437 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001438 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001439 }
1440
Sunny Goyal04a324a2017-01-20 21:08:59 -08001441 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001442 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001443 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001444
Sunny Goyal2100c782016-08-22 16:00:03 -07001445 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001446 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001447 if (LOGD) {
1448 Log.d(TAG, "Adding widget from drop");
1449 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001450 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001451 }
1452
Sunny Goyal2100c782016-08-22 16:00:03 -07001453 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001454 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1455 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1456 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001457
Adam Cohenad4e15c2013-10-17 16:21:35 -07001458 Runnable onComplete = new Runnable() {
1459 @Override
1460 public void run() {
1461 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001462 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001463 }
1464 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001465 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001466 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468 }
Romain Guycbb89e42009-06-08 15:52:54 -07001469
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001470 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1471 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001472 info.container = container;
1473 info.screenId = screenId;
1474 if (cell != null) {
1475 info.cellX = cell[0];
1476 info.cellY = cell[1];
1477 }
1478 info.spanX = spanX;
1479 info.spanY = spanY;
1480
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001481 switch (info.itemType) {
1482 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1483 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001484 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001485 break;
1486 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001487 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001488 break;
1489 default:
1490 throw new IllegalStateException("Unknown item type: " + info.itemType);
1491 }
1492 }
1493
Adam Cohenfbba09b2011-07-18 21:43:05 -07001494 /**
1495 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001496 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001497 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001498 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1499 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001500 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1501 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1502 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 }
1504
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 /**
1506 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001508 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001509 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001510 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001511 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001512 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001513 // In the case where we've prebound the widget, we remove it from the DragLayer
1514 if (LOGD) {
1515 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1516 }
1517 getDragLayer().removeView(hostView);
1518
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001520 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001521
1522 // Clear the boundWidget so that it doesn't get destroyed.
1523 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001525 // In this case, we either need to start an activity to get permission to bind
1526 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001527 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1528 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1529 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1530 this, info.componentName);
1531 } else {
1532 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1533 }
Adam Cohendd70d662012-10-04 16:53:44 -07001534 Bundle options = info.bindOptions;
1535
Sunny Goyalffe83f12014-08-14 17:39:34 -07001536 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1537 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001538 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001539 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001540 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001541 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001542 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001544 }
1545
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001548 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 folderInfo.title = getText(R.string.folder_name);
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001552 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
1554 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001555 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301556 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001557 // Force measure the new folder icon
1558 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1559 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001560 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Winsonc0b52fe2015-09-09 16:38:15 -07001563 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001564 * Unbinds the view for the specified item, and removes the item and all its children.
1565 *
1566 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001567 * @param itemInfo the {@link ItemInfo} for this view.
1568 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001569 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001570 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001571 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001572 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001573 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1574 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001575 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001576 } else {
1577 mWorkspace.removeWorkspaceItem(v);
1578 }
Winsonc0b52fe2015-09-09 16:38:15 -07001579 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001580 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001581 }
1582 } else if (itemInfo instanceof FolderInfo) {
1583 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001584 if (v instanceof FolderIcon) {
1585 ((FolderIcon) v).removeListeners();
1586 }
Winsonc0b52fe2015-09-09 16:38:15 -07001587 mWorkspace.removeWorkspaceItem(v);
1588 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001589 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001590 }
1591 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1592 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001593 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001594 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001595 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001596 }
1597 } else {
1598 return false;
1599 }
1600 return true;
1601 }
1602
1603 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001604 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001605 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001606 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001607 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001608 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001609 // Deleting an app widget ID is a void call but writes to disk before returning
1610 // to the caller...
1611 new AsyncTask<Void, Void, Void>() {
1612 public Void doInBackground(Void ... args) {
1613 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1614 return null;
1615 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001616 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001617 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001618 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 @Override
1622 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001623 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 }
1625
Joe Onorato88ec0992009-11-19 13:16:06 -08001626 @Override
1627 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001628 if (finishAutoCancelActionMode()) {
1629 return;
1630 }
Adam Cohen9211d422014-10-07 18:14:53 -07001631 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1632 return;
1633 }
1634
Sunny Goyal45478022015-06-08 16:52:41 -07001635 if (mDragController.isDragging()) {
1636 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001637 return;
1638 }
1639
Jon Mirandafeba90f2016-10-06 10:53:29 -07001640 // Note: There should be at most one log per method call. This is enforced implicitly
1641 // by using if-else statements.
1642 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001643 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1644 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001645 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001646 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001647 LauncherState lastState = mStateManager.getLastState();
Sunny Goyalea609262017-10-25 15:47:38 -07001648 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001649 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001650 } else {
1651 // Back button is a no-op here, but give at least some feedback for the button press
1652 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001653 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001654 }
1655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 * Launches the intent referred by the clicked shortcut.
1658 *
1659 * @param v The view representing the clicked shortcut.
1660 */
1661 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001662 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1663 // view has detached (it's possible for this to happen if the view is removed mid touch).
1664 if (v.getWindowToken() == null) {
1665 return;
1666 }
1667
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001668 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001669 return;
1670 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001671
Adam Cohen1697b792013-09-17 19:08:21 -07001672 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001673 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001674 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1675 LauncherLogProto.Action.Direction.NONE,
1676 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001677 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001678 }
1679 return;
1680 }
1681
1682 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001683 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001684 int page = mWorkspace.indexOfChild(v);
1685 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1686 LauncherLogProto.Action.Direction.NONE,
1687 LauncherLogProto.ContainerType.OVERVIEW, page);
1688 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001689 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001690 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001691 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001692 }
1693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001695 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001696 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001698 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001699 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001700 }
Sunny Goyal508da152014-08-14 10:53:27 -07001701 } else if (tag instanceof AppInfo) {
1702 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001703 } else if (tag instanceof LauncherAppWidgetInfo) {
1704 if (v instanceof PendingAppWidgetHostView) {
1705 onClickPendingWidget((PendingAppWidgetHostView) v);
1706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
1708 }
1709
Sunny Goyal70660032015-05-14 00:07:08 -07001710 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001711 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001712 return false;
1713 }
1714
Michael Jurkaaf442092010-06-10 17:01:57 -07001715 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001716 * Event handler for the app widget view which has not fully restored.
1717 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001718 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001719 if (mIsSafeModeEnabled) {
1720 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1721 return;
1722 }
1723
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001724 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001725 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001726 LauncherAppWidgetProviderInfo appWidgetInfo =
1727 mAppWidgetManager.findProvider(info.providerName, info.user);
1728 if (appWidgetInfo == null) {
1729 return;
1730 }
1731 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1732
Sunny Goyald478c832016-04-01 12:04:16 -07001733 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1734 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1735 // This should not happen, as we make sure that an Id is allocated during bind.
1736 return;
1737 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001738 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1739 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001740 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001741 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001742 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001743 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001744 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001745 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001746 }
1747 }
1748
Sunny Goyale6e72002017-01-12 16:55:36 -08001749 private void onClickPendingAppItem(final View v, final String packageName,
1750 boolean downloadStarted) {
1751 if (downloadStarted) {
1752 // If the download has started, simply direct to the market app.
1753 startMarketIntentForPackage(v, packageName);
1754 return;
1755 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001756 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001757 .setTitle(R.string.abandoned_promises_title)
1758 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001759 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1760 @Override
1761 public void onClick(DialogInterface dialogInterface, int i) {
1762 startMarketIntentForPackage(v, packageName);
1763 }
1764 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001765 .setNeutralButton(R.string.abandoned_clean_this,
1766 new DialogInterface.OnClickListener() {
1767 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001768 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001769 mWorkspace.removeAbandonedPromise(packageName, user);
1770 }
1771 })
1772 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001773 }
1774
1775 private void startMarketIntentForPackage(View v, String packageName) {
1776 ItemInfo item = (ItemInfo) v.getTag();
Sunny Goyal67e75e22018-01-22 15:14:00 -08001777 Intent intent = new PackageManagerHelper(v.getContext()).getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001778 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001779 }
1780
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001781 /**
1782 * Event handler for an app shortcut click.
1783 *
1784 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1785 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001786 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001787 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1788 Object tag = v.getTag();
1789 if (!(tag instanceof ShortcutInfo)) {
1790 throw new IllegalArgumentException("Input must be a Shortcut");
1791 }
1792
1793 // Open shortcut
1794 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001795
Sunny Goyal076839c2017-10-30 13:52:20 -07001796 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001797 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1798 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001799 ~FLAG_DISABLED_SUSPENDED &
1800 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001801 // If the app is only disabled because of the above flags, launch activity anyway.
1802 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001803 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001804 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1805 // Use a message specific to this shortcut, if it has one.
1806 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1807 return;
1808 }
1809 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001810 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001811 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001812 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001813 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1814 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001815 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001816 }
1817 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1818 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001819 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001820 }
1821
Chris Wren40c5ed32014-06-24 18:24:23 -04001822 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001823 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001824 String packageName = shortcut.intent.getComponent() != null ?
1825 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1826 if (!TextUtils.isEmpty(packageName)) {
1827 onClickPendingAppItem(v, packageName,
1828 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1829 return;
1830 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001831 }
1832
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001833 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001834 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001835 }
1836
Sunny Goyala7ce1662016-05-31 15:01:35 -07001837 private void startAppShortcutOrInfoActivity(View v) {
1838 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001839 Intent intent;
1840 if (item instanceof PromiseAppInfo) {
1841 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
Sunny Goyal67e75e22018-01-22 15:14:00 -08001842 intent = promiseAppInfo.getMarketIntent(this);
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001843 } else {
1844 intent = item.getIntent();
1845 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001846 if (intent == null) {
1847 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001848 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001849 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001850 }
1851
1852 /**
1853 * Event handler for a folder icon click.
1854 *
1855 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1856 */
1857 protected void onClickFolderIcon(View v) {
1858 if (LOGD) Log.d(TAG, "onClickFolder");
1859 if (!(v instanceof FolderIcon)){
1860 throw new IllegalArgumentException("Input must be a FolderIcon");
1861 }
1862
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001863 Folder folder = ((FolderIcon) v).getFolder();
1864 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001865 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001866 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001867 }
Michael Jurka5130e402011-10-13 04:55:35 -07001868 }
1869
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001870 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001871 * Event handler for the wallpaper picker button that appears after a long press
1872 * on the home screen.
1873 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001874 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001875 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001876 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1877 return;
1878 }
1879
Tony Wickham785f7a52015-08-31 17:28:32 -07001880 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1881 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001882 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001883 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001884 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001885
1886 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001887 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1888 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001889 intent.setPackage(pickerPackage);
1890 }
1891
Sunny Goyala5ace712017-11-15 17:12:51 -08001892 final Bundle launchOptions;
1893 if (v != null) {
1894 intent.setSourceBounds(getViewBounds(v));
1895 // If there is no target package, use the default intent chooser animation
Jon Miranda17940f02018-01-18 14:46:34 -08001896 launchOptions = hasTargetPackage
1897 ? getActivityLaunchOptions(v, isInMultiWindowModeCompat())
1898 : null;
Sunny Goyala5ace712017-11-15 17:12:51 -08001899 } else {
1900 launchOptions = null;
1901 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001902 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001903 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001904 } catch (ActivityNotFoundException e) {
1905 setWaitingForResult(null);
1906 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1907 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001908 }
1909
Sunny Goyala7ce1662016-05-31 15:01:35 -07001910 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001912 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1913 try {
1914 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1915 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1916 // is enabled by default on NYC.
1917 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1918 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001919
1920 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1921 String id = ((ShortcutInfo) info).getDeepShortcutId();
1922 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301923 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001924 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001925 } else {
1926 // Could be launching some bookkeeping activity
1927 startActivity(intent, optsBundle);
1928 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001929 } finally {
1930 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001933 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1934 // corresponding permission. Show the appropriate permission prompt if that
1935 // is the case.
1936 if (intent.getComponent() == null
1937 && Intent.ACTION_CALL.equals(intent.getAction())
1938 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1939 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001940
1941 setWaitingForResult(PendingRequestArgs
1942 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001943 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1944 REQUEST_PERMISSION_CALL_PHONE);
1945 } else {
1946 // No idea why this was thrown.
1947 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001951
Sunny Goyalfe770c92016-09-27 14:38:58 -07001952 @TargetApi(Build.VERSION_CODES.M)
Jon Miranda17940f02018-01-18 14:46:34 -08001953 public Bundle getActivityLaunchOptions(View v, boolean useDefaultLaunchOptions) {
1954 return useDefaultLaunchOptions
Jon Miranda54441f52018-01-24 15:38:25 -08001955 ? mAppTransitionManager.getDefaultActivityLaunchOptions(this, v)
1956 : mAppTransitionManager.getActivityLaunchOptions(this, v);
Sunny Goyala7ce1662016-05-31 15:01:35 -07001957 }
1958
Tonye3c59252017-05-02 21:32:27 -07001959 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001960 int[] pos = new int[2];
1961 v.getLocationOnScreen(pos);
1962 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1963 }
1964
Sunny Goyala7ce1662016-05-31 15:01:35 -07001965 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001966 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001967 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1968 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001969 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001970 }
Jon Miranda17940f02018-01-18 14:46:34 -08001971
1972 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
1973 && (item instanceof ShortcutInfo)
1974 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
1975 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
1976 && !((ShortcutInfo) item).isPromise();
1977
Sunny Goyala7ce1662016-05-31 15:01:35 -07001978 // Only launch using the new animation if the shortcut has not opted out (this is a
1979 // private contract between launcher and may be ignored in the future).
1980 boolean useLaunchAnimation = (v != null) &&
1981 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Jon Miranda17940f02018-01-18 14:46:34 -08001982 Bundle optsBundle = useLaunchAnimation
1983 ? getActivityLaunchOptions(v, isShortcut || isInMultiWindowModeCompat())
1984 : null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001985
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001986 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001987
1988 // Prepare intent
1989 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1990 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001991 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001992 }
1993 try {
Jon Miranda17940f02018-01-18 14:46:34 -08001994 if (isShortcut) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001995 // Shortcuts need some special checks due to legacy reasons.
1996 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001997 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001998 // Could be launching some bookkeeping activity
1999 startActivity(intent, optsBundle);
2000 } else {
2001 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2002 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2003 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002004
2005 if (v instanceof BubbleTextView) {
2006 // This is set to the view that launched the activity that navigated the user away
2007 // from launcher. Since there is no callback for when the activity has finished
2008 // launching, enable the press state and keep this reference to reset the press
2009 // state when we return to launcher.
2010 BubbleTextView btv = (BubbleTextView) v;
2011 btv.setStayPressed(true);
2012 setOnResumeCallback(btv);
2013 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002014 mAppLaunchSuccess = true;
2015 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002016 } catch (ActivityNotFoundException|SecurityException e) {
2017 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2018 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2019 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002020 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002021 }
2022
Tony Wickhamdadb3042016-02-24 11:07:00 -08002023 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002024 public boolean dispatchTouchEvent(MotionEvent ev) {
Sunny Goyal02424b22018-01-19 11:24:32 -08002025 mLastDispatchTouchEvent.set(ev.getX(), ev.getY());
Jon Miranda379198e2016-09-23 08:54:40 -07002026 return super.dispatchTouchEvent(ev);
2027 }
2028
2029 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002031 if (!isDraggingEnabled()) return false;
2032 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002033 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034
Jon Miranda51f037d2016-11-07 08:37:20 -08002035 boolean ignoreLongPressToOverview =
Sunny Goyal02424b22018-01-19 11:24:32 -08002036 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEvent.x);
Jon Miranda379198e2016-09-23 08:54:40 -07002037
Adam Cohen1697b792013-09-17 19:08:21 -07002038 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002039 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002040 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302041 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2042 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002043 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002044 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohen93c97562013-09-26 13:48:01 -07002045 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2046 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2047 return true;
2048 } else {
2049 return false;
2050 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002051 } else {
2052 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002053 }
Adam Cohen1697b792013-09-17 19:08:21 -07002054 }
2055
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002056 CellLayout.CellInfo longClickCellInfo = null;
2057 View itemUnderLongClick = null;
2058 if (v.getTag() instanceof ItemInfo) {
2059 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002060 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002061 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002062 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 }
2064
Winson Chung3d503fb2011-07-13 17:25:49 -07002065 // The hotseat touch handling does not go through Workspace, and we always allow long press
2066 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002067 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002068 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002069 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002070 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002071 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302072 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2073 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002074 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002075 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002076 return false;
2077 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302078 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2079 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002080 mWorkspace.getCurrentPage());
Sunny Goyal02424b22018-01-19 11:24:32 -08002081 UiFactory.onWorkspaceLongPress(this, mLastDispatchTouchEvent);
Adam Cohendedbd962013-07-11 14:21:49 -07002082 }
Jon Miranda379198e2016-09-23 08:54:40 -07002083 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2084 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002086 final boolean isAllAppsButton =
2087 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2088 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2089 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002090 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002092 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
2094 }
2095 }
2096 return true;
2097 }
2098
Winson Chung3d503fb2011-07-13 17:25:49 -07002099 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002100 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002101 return mHotseat != null && layout != null &&
2102 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2103 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002104
Winson Chung3d503fb2011-07-13 17:25:49 -07002105 /**
2106 * Returns the CellLayout of the specified container at the specified screen.
2107 */
Sunny Goyal92820592015-03-02 11:31:35 -08002108 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002109 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2110 if (mHotseat != null) {
2111 return mHotseat.getLayout();
2112 } else {
2113 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002114 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002115 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002116 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002117 }
2118 }
2119
Michael Jurkae326f182011-11-21 14:05:46 -08002120 @Override
2121 public void onTrimMemory(int level) {
2122 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002123 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2124 // The widget preview db can result in holding onto over
2125 // 3MB of memory for caching which isn't necessary.
2126 SQLiteDatabase.releaseMemory();
2127
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002128 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002129 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002130 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002131 if (mLauncherCallbacks != null) {
2132 mLauncherCallbacks.onTrimMemory(level);
2133 }
Michael Jurkae326f182011-11-21 14:05:46 -08002134 }
2135
Michael Jurkad7c28052012-04-27 15:43:36 -07002136 @Override
2137 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002138 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002139 final List<CharSequence> text = event.getText();
2140 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002141 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002142 // TODO: When can workspace be null?
2143 text.add(mWorkspace == null
2144 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002145 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002146 return result;
2147 }
2148
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002149 public void setOnResumeCallback(OnResumeCallback callback) {
2150 if (mOnResumeCallback != null) {
2151 mOnResumeCallback.onLauncherResume();
2152 }
2153 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002154 }
2155
Michael Jurka7607c2f2013-04-03 14:33:19 -07002156 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002159 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002161 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002162 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002163 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002164 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002167
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002169 * Clear any pending bind callbacks. This is called when is loader is planning to
2170 * perform a full rebind from scratch.
2171 */
2172 @Override
2173 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002174 if (mPendingExecutor != null) {
2175 mPendingExecutor.markCompleted();
2176 mPendingExecutor = null;
2177 }
2178 }
2179
2180 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 * Refreshes the shortcuts shown on the workspace.
2182 *
2183 * Implementation of the method from LauncherModel.Callbacks.
2184 */
2185 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002186 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002187 // Floating panels (except the full widget sheet) are associated with individual icons. If
2188 // we are starting a fresh bind, close all such panels as all the icons are about
2189 // to go away.
2190 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002191 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002192
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002193 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002194
Winson Chungd64d1762013-08-20 14:37:16 -07002195 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002196 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002197 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002198
Winson Chung3d503fb2011-07-13 17:25:49 -07002199 if (mHotseat != null) {
2200 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002202 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 }
2204
Adam Cohendcd297f2013-06-18 13:13:40 -07002205 @Override
2206 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002207 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002208 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2209 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002210 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2211 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002212 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002213 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2214 // If there are no screens, we need to have an empty screen
2215 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002216 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002217 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002218
Winsonc7d2e832016-07-28 12:24:55 -07002219 // After we have added all the screens, if the wallpaper was locked to the default state,
2220 // then notify to indicate that it can be released and a proper wallpaper offset can be
2221 // computed before the next layout
2222 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002223 }
2224
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002225 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 int count = orderedScreenIds.size();
2227 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002228 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002229 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002230 // No need to bind the first screen, as its always bound.
2231 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2232 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002233 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 }
2235
Sunny Goyalb23980c2017-08-17 07:45:25 -07002236 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002237 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2238 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002239 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002240 if (newScreens != null) {
2241 bindAddScreens(newScreens);
2242 }
Winson Chungd64d1762013-08-20 14:37:16 -07002243
2244 // We add the items without animation on non-visible pages, and with
2245 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002246 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002247 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002248 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002249 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002250 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002251 }
Winson Chungc58497e2013-09-03 17:48:37 -07002252
Winson Chung87412982013-10-03 18:34:14 -07002253 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002254 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002255 }
2256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 * Bind the items start-end from the list.
2259 *
2260 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002262 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002263 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002264 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002265 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002266 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002267 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002268 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002269 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002270 int end = items.size();
2271 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002272 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002273
2274 // Short circuit if we are loading dock items for a configuration which has no dock
2275 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2276 mHotseat == null) {
2277 continue;
2278 }
2279
Sunny Goyal639e9062015-08-19 19:17:06 -07002280 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002281 switch (item.itemType) {
2282 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2283 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002284 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002285 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002286 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002287 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002288 }
2289 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002290 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002291 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002292 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002293 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002294 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002295 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2296 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002297 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002298 if (view == null) {
2299 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002300 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002301 break;
2302 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002303 default:
2304 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002305 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002306
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002307 /*
2308 * Remove colliding items.
2309 */
2310 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2311 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2312 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2313 View v = cl.getChildAt(item.cellX, item.cellY);
2314 Object tag = v.getTag();
2315 String desc = "Collision while binding workspace item: " + item
2316 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002317 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002318 throw (new RuntimeException(desc));
2319 } else {
2320 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002321 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002322 continue;
2323 }
2324 }
2325 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302326 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002327 if (animateIcons) {
2328 // Animate all the applications up now
2329 view.setAlpha(0f);
2330 view.setScaleX(0f);
2331 view.setScaleY(0f);
2332 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002333 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002336
Winson Chung997a9232013-07-24 15:33:46 -07002337 if (animateIcons) {
2338 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002339 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002340 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002341 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002342 final Runnable startBounceAnimRunnable = new Runnable() {
2343 public void run() {
2344 anim.playTogether(bounceAnims);
2345 anim.start();
2346 }
2347 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002348 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002349 // We post the animation slightly delayed to prevent slowdowns
2350 // when we are loading right after we return to launcher.
2351 mWorkspace.postDelayed(new Runnable() {
2352 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002353 if (mWorkspace != null) {
2354 mWorkspace.snapToPage(newScreenIndex);
2355 mWorkspace.postDelayed(startBounceAnimRunnable,
2356 NEW_APPS_ANIMATION_DELAY);
2357 }
Winson Chung94d67682013-09-25 16:29:40 -07002358 }
2359 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002360 } else {
2361 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002362 }
2363 }
Winson Chung64359a52013-07-08 17:17:08 -07002364 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002365 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
2367
Joe Onorato9c1289c2009-08-17 11:03:03 -04002368 /**
2369 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002370 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002371 public void bindAppWidget(LauncherAppWidgetInfo item) {
2372 View view = inflateAppWidget(item);
2373 if (view != null) {
2374 mWorkspace.addInScreen(view, item);
2375 mWorkspace.requestLayout();
2376 }
2377 }
2378
2379 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002380 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302381 PendingAppWidgetHostView view =
2382 new PendingAppWidgetHostView(this, item, mIconCache, true);
2383 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002384 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002385 }
2386
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002387 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002388
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002389 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002390
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002391 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2392 // If the provider is not ready, bind as a pending widget.
2393 appWidgetInfo = null;
2394 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2395 // The widget id is not valid. Try to find the widget based on the provider info.
2396 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2397 } else {
2398 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2399 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002400
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002401 // If the provider is ready, but the width is not yet restored, try to restore it.
2402 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2403 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002404 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002405 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2406 + " belongs to component " + item.providerName
2407 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002408 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002409 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002410 }
Sunny Goyalff572272014-07-23 13:58:07 -07002411
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002412 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002413 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002414 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2415 // Id has not been allocated yet. Allocate a new id.
2416 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2417 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002418
Sunny Goyald478c832016-04-01 12:04:16 -07002419 // Also try to bind the widget. If the bind fails, the user will be shown
2420 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002421 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002422 pendingInfo.spanX = item.spanX;
2423 pendingInfo.spanY = item.spanY;
2424 pendingInfo.minSpanX = item.minSpanX;
2425 pendingInfo.minSpanY = item.minSpanY;
2426 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002427
2428 boolean isDirectConfig =
2429 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2430 if (isDirectConfig && item.bindOptions != null) {
2431 Bundle newOptions = item.bindOptions.getExtras();
2432 if (options != null) {
2433 newOptions.putAll(options);
2434 }
2435 options = newOptions;
2436 }
Sunny Goyald478c832016-04-01 12:04:16 -07002437 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2438 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002439
Sunny Goyal86df1382016-08-10 15:03:22 -07002440 // We tried to bind once. If we were not able to bind, we would need to
2441 // go through the permission dialog, which means we cannot skip the config
2442 // activity.
2443 item.bindOptions = null;
2444 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2445
Sunny Goyald478c832016-04-01 12:04:16 -07002446 // Bind succeeded
2447 if (success) {
2448 // If the widget has a configure activity, it is still needs to set it up,
2449 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002450 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002451 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2452 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002453 }
Sunny Goyald478c832016-04-01 12:04:16 -07002454
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002455 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002456 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002457 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002458 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002459 // The widget was marked as UI not ready, but there is no configure activity to
2460 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002461 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002462 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002463 }
Sunny Goyalff572272014-07-23 13:58:07 -07002464 }
2465
Sunny Goyald5462aa2016-11-22 16:52:39 +05302466 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002467 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002468 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002469 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002470 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002471 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002472 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002473 }
2474
Sunny Goyal233ee962015-08-03 13:05:01 -07002475 item.minSpanX = appWidgetInfo.minSpanX;
2476 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302477 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002478 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302479 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002480 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302481 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002482
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002483 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002484 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 }
2486
Sunny Goyalff572272014-07-23 13:58:07 -07002487 /**
2488 * Restores a pending widget.
2489 *
2490 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002491 */
Sunny Goyald478c832016-04-01 12:04:16 -07002492 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002493 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2494 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2495 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002496 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002497 }
2498
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002499 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002500 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002501 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2502 info.pendingItemInfo = null;
2503 }
Sunny Goyalff572272014-07-23 13:58:07 -07002504
Sunny Goyalba47faa2017-10-04 16:50:57 -07002505 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002506 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002507 }
2508
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002509 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002510 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002511 }
2512
Adam Cohen1462de32012-07-24 22:34:36 -07002513 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002514 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002515 }
2516
Sunny Goyal527c7d32015-08-28 15:19:36 -07002517 @Override
2518 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2519 if (mPendingExecutor != null) {
2520 mPendingExecutor.markCompleted();
2521 }
2522 mPendingExecutor = executor;
2523 executor.attachTo(this);
2524 }
2525
2526 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2527 if (mPendingExecutor == executor) {
2528 mPendingExecutor = null;
2529 }
2530 }
2531
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002532 @Override
2533 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002534 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002535 mDragLayer.animate().alpha(1).withEndAction(
2536 executor == null ? null : executor::onLoadAnimationCompleted).start();
2537 } else if (executor != null) {
2538 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002539 }
2540 }
2541
Joe Onorato9c1289c2009-08-17 11:03:03 -04002542 /**
2543 * Callback saying that there aren't any more items to bind.
2544 *
2545 * Implementation of the method from LauncherModel.Callbacks.
2546 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002547 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002548 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002549 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002550
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002551 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002552
Sunny Goyal2100c782016-08-22 16:00:03 -07002553 if (mPendingActivityResult != null) {
2554 handleActivityResult(mPendingActivityResult.requestCode,
2555 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2556 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002557 }
2558
Sunny Goyala474a9b2017-05-04 16:47:11 -07002559 InstallShortcutReceiver.disableAndFlushInstallQueue(
2560 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002561
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002562 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002563 }
2564
Winson Chunga2413752012-04-03 14:22:34 -07002565 private boolean canRunNewAppsAnimation() {
2566 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002567 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002568 }
2569
Winson Chungc9168342013-06-26 14:54:55 -07002570 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002571 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002572 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2573 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002574 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002575 return bounceAnim;
2576 }
2577
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002578 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002579 * Add the icons for all apps.
2580 *
2581 * Implementation of the method from LauncherModel.Callbacks.
2582 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002583 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002584 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002585 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002586 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002587 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002588 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002589 return;
2590 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002591
Winson Chungb745afb2015-03-02 11:51:23 -08002592 mAppsView.setApps(apps);
2593 }
Adam Cohen9211d422014-10-07 18:14:53 -07002594 if (mLauncherCallbacks != null) {
2595 mLauncherCallbacks.bindAllApplications(apps);
2596 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002597 }
2598
2599 /**
Tony2917a8b2017-07-31 23:29:42 -07002600 * Returns an Executor that will run after the launcher is first drawn (including after the
2601 * initial fade in animation). Returns null if the first draw has already occurred.
2602 */
2603 public @Nullable Executor getPendingExecutor() {
2604 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2605 }
2606
2607 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002608 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2609 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2610 */
2611 @Override
2612 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002613 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002614 }
2615
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002616 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002617 * A package was updated.
2618 *
2619 * Implementation of the method from LauncherModel.Callbacks.
2620 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002621 @Override
2622 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002623 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002624 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002626 }
2627
Sunny Goyal4390ace2014-10-13 11:33:11 -07002628 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002629 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002630 if (mAppsView != null) {
2631 mAppsView.updatePromiseAppProgress(app);
2632 }
2633 }
2634
2635 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002636 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002637 mWorkspace.widgetsRestored(widgets);
2638 }
2639
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002641 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002642 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002643 *
2644 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002645 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002646 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002647 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002648 if (!updated.isEmpty()) {
2649 mWorkspace.updateShortcuts(updated);
2650 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002651 }
2652
2653 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002654 * Update the state of a package, typically related to install state.
2655 *
2656 * Implementation of the method from LauncherModel.Callbacks.
2657 */
Sunny Goyale755d462014-07-22 13:48:29 -07002658 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002659 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002660 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002661 }
2662
2663 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002664 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002665 * in addition to specific applications to remove, the reason being that
2666 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002667 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002668 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002669 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002670 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002671 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002672 mWorkspace.removeItemsByMatcher(matcher);
2673 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002674 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002675
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002676 @Override
2677 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002678 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002679 if (mAppsView != null) {
2680 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002681 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002682 }
Joe Onoratobe386092009-11-17 17:32:16 -08002683
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002684 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002685 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2686 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002687 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2688 if (topView != null) {
2689 topView.onWidgetsBound();
2690 }
2691 }
2692
Tony Wickham86222d22017-03-29 15:30:43 -07002693 /**
2694 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2695 * refreshes the widgets and shortcuts associated with the given package/user
2696 */
2697 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002698 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002699 }
2700
Sunny Goyalc99cb172017-10-19 16:15:09 -07002701 public boolean isRotationEnabled () {
2702 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002703 }
2704
Winson Chung80baf5a2010-08-09 16:03:15 -07002705 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002706 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002707 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002708 @Override
2709 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2710 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002711
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002712 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2713 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002714 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002715 writer.println(prefix + " Homescreen " + i);
2716
2717 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2718 for (int j = 0; j < layout.getChildCount(); j++) {
2719 Object tag = layout.getChildAt(j).getTag();
2720 if (tag != null) {
2721 writer.println(prefix + " " + tag.toString());
2722 }
2723 }
2724 }
2725
2726 writer.println(prefix + " Hotseat");
2727 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002728 for (int j = 0; j < layout.getChildCount(); j++) {
2729 Object tag = layout.getChildAt(j).getTag();
2730 if (tag != null) {
2731 writer.println(prefix + " " + tag.toString());
2732 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002733 }
Sunny Goyala1365452015-10-01 15:46:24 -07002734
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002735 try {
2736 FileLog.flushAll(writer);
2737 } catch (Exception e) {
2738 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002739 }
2740 }
2741
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002742 writer.println(prefix + "Misc:");
2743 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2744 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2745 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2746
2747 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002748
Adam Cohen9211d422014-10-07 18:14:53 -07002749 if (mLauncherCallbacks != null) {
2750 mLauncherCallbacks.dump(prefix, fd, writer, args);
2751 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002752 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002753
Sunny Goyal66b24572016-09-21 15:57:55 -07002754 @Override
2755 @TargetApi(Build.VERSION_CODES.N)
2756 public void onProvideKeyboardShortcuts(
2757 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2758
2759 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002760 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002761 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2762 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2763 }
2764 View currentFocus = getCurrentFocus();
2765 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2766 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2767 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2768 }
Tony18c4aa42017-05-10 21:23:57 -05002769 if (currentFocus.getTag() instanceof ItemInfo
2770 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002771 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2772 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2773 }
2774 if (!shortcutInfos.isEmpty()) {
2775 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2776 }
2777
2778 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2779 }
2780
2781 @Override
2782 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2783 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2784 switch (keyCode) {
2785 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002786 if (isInState(NORMAL)) {
2787 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002788 return true;
2789 }
2790 break;
2791 case KeyEvent.KEYCODE_S: {
2792 View focusedView = getCurrentFocus();
2793 if (focusedView instanceof BubbleTextView
2794 && focusedView.getTag() instanceof ItemInfo
2795 && mAccessibilityDelegate.performAction(focusedView,
2796 (ItemInfo) focusedView.getTag(),
2797 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002798 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002799 return true;
2800 }
2801 break;
2802 }
2803 case KeyEvent.KEYCODE_O:
2804 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2805 return true;
2806 }
2807 break;
2808 }
2809 }
2810 return super.onKeyShortcut(keyCode, event);
2811 }
2812
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002813 public static Launcher getLauncher(Context context) {
2814 if (context instanceof Launcher) {
2815 return (Launcher) context;
2816 }
2817 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2818 }
2819
Sunny Goyal5a81c382017-03-20 15:08:06 -07002820 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002821
2822 @Override
2823 public void onSharedPreferenceChanged(
2824 SharedPreferences sharedPreferences, String key) {
2825 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002826 // Recreate the activity so that it initializes the rotation preference again.
2827 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002828 }
2829 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002830 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002831
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002832 @Override
2833 public void onActionModeStarted(ActionMode mode) {
2834 super.onActionModeStarted(mode);
2835 mCurrentActionMode = mode;
2836 }
2837
2838 @Override
2839 public void onActionModeFinished(ActionMode mode) {
2840 super.onActionModeFinished(mode);
2841 mCurrentActionMode = null;
2842 }
2843
2844 public boolean finishAutoCancelActionMode() {
2845 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2846 mCurrentActionMode.finish();
2847 return true;
2848 }
2849 return false;
2850 }
2851
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002852 /**
2853 * Callback for listening for onResume
2854 */
2855 public interface OnResumeCallback {
2856
2857 void onLauncherResume();
2858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859}