blob: 2a5f45399bb285623039e07e94a8560d437eee0e [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Sunny Goyal28c6b962015-10-12 11:42:05 -070033import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070035import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070039import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070041import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070049import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040050import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070052import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070053import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070054import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070055import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070057import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070058import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070068import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070072import android.util.SparseArray;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080073import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070077import android.view.KeyboardShortcutGroup;
78import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080079import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070088
Sunny Goyal651077b2014-06-30 14:15:31 -070089import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070091import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070092import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070093import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070094import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080095import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070096import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010097import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070098import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080099import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700100import com.android.launcher3.dragndrop.DragController;
101import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700102import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700103import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700104import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000105import com.android.launcher3.folder.Folder;
106import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700107import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700108import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700109import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700110import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800116import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800117import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530119import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700121import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700124import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700125import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700129import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700132import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700134import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800135import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800136import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800138import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800139import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700140import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700141import com.android.launcher3.widget.WidgetListRowEntry;
142import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700143import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700144
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700147import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700148import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700149import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700150import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800151import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700152import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154/**
155 * Default launcher application.
156 */
Sunny Goyal27835952017-01-13 12:15:53 -0800157public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700158 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
159 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700170 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
Winson Chung2672ff92012-05-04 16:22:30 -0700186 // The Intent extra that defines whether to ignore the launch animation
187 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400188 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
196 // Type: ActivityResultInfo
197 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700198 // Type: SparseArray<Parcelable>
199 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800201 // When starting an action mode, setting this tag will cause the action mode to be cancelled
202 // automatically when user interacts with the launcher.
203 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
204
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700205 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
213 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
214 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700215
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700217 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800219 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700220
Sunny Goyalffe83f12014-08-14 17:39:34 -0700221 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700222 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700223
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700224 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700225
Sunny Goyal316490e2015-06-02 09:38:28 -0700226 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700227
Sunny Goyal47328fd2016-05-25 18:56:41 -0700228 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700229
230 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700231 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700232 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700233
Winson Chung8ae41982017-11-10 11:42:13 -0800234 // UI and state for the overview panel
235 private ViewGroup mOverviewPanel;
236
Jon Miranda6bed3502017-09-19 14:43:17 -0700237 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
238 // that results in widgets being inflated in the wrong orientation.
239 private int mOrientation;
240
Adam Cohen091440a2015-03-18 14:16:05 -0700241 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700243 private OnResumeCallback mOnResumeCallback;
244
Sunny Goyal527c7d32015-08-28 15:19:36 -0700245 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700246
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800248 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700250 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Jon Miranda2d89ea82017-05-04 11:47:53 -0700252 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700253 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700254
Tony Wickham010d2552017-01-20 08:15:28 -0800255 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700256
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800257 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700258
Winson Chung46353de2012-02-16 14:05:10 -0800259 // We only want to get the SharedPreferences once since it does an FS stat each time we get
260 // it from the context.
261 private SharedPreferences mSharedPrefs;
262
Sunny Goyal2100c782016-08-22 16:00:03 -0700263 // Activity result which needs to be processed after workspace has loaded.
264 private ActivityResultInfo mPendingActivityResult;
265 /**
266 * Holds extra information required to handle a result from an external call, like
267 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
268 */
269 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800270
Jon Miranda379198e2016-09-23 08:54:40 -0700271 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700272
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700273 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700274 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700275 private boolean mAppLaunchSuccess;
276
Sunny Goyal745bad92016-05-02 10:54:12 -0700277 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800278 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 @Override
281 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700282 if (DEBUG_STRICT_MODE) {
283 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
284 .detectDiskReads()
285 .detectDiskWrites()
286 .detectNetwork() // or .detectAll() for all detectable problems
287 .penaltyLog()
288 .build());
289 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
290 .detectLeakedSqlLiteObjects()
291 .detectLeakedClosableObjects()
292 .penaltyLog()
293 .penaltyDeath()
294 .build());
295 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700296 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700297
Adam Cohen9211d422014-10-07 18:14:53 -0700298 if (mLauncherCallbacks != null) {
299 mLauncherCallbacks.preOnCreate();
300 }
301
Mario Bertschler27288382017-05-24 15:35:09 -0700302 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
303 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700304 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700307 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400308
Sunny Goyal87f784c2017-01-11 10:48:34 -0800309 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700310
Adam Cohen2e6da152015-05-06 11:42:25 -0700311 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800312 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700313 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700314 Display display = getWindowManager().getDefaultDisplay();
315 Point mwSize = new Point();
316 display.getSize(mwSize);
317 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
318 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700319
Jon Miranda6bed3502017-09-19 14:43:17 -0700320 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700321 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700322 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800323 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800324 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800325 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700326 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700327
Joe Onorato41a12d22009-10-31 18:30:00 -0400328 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700329 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800330 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700331
Sunny Goyalffe83f12014-08-14 17:39:34 -0700332 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700333
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700334 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700335 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700336
Sunny Goyal60820d72017-05-09 12:40:11 -0700337 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 setupViews();
Tony Wickham010d2552017-01-20 08:15:28 -0800340 mPopupDataProvider = new PopupDataProvider(this);
341
Sunny Goyald3864fa2017-11-14 15:06:36 -0800342 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
343 // In case we are on a device with locked rotation, we should look at preferences to check
344 // if the user has specifically allowed rotation.
345 if (!mRotationEnabled) {
346 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
347 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
348 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350
Sunny Goyald3864fa2017-11-14 15:06:36 -0800351 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
352 if (internalStateHandled) {
353 // Temporarily enable the rotation
354 mRotationEnabled = true;
355
356 if (savedInstanceState != null) {
357 // InternalStateHandler has already set the appropriate state.
358 // We dont need to do anything.
359 savedInstanceState.remove(RUNTIME_STATE);
360 }
361 }
362 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800363
Sunny Goyal2100c782016-08-22 16:00:03 -0700364 // We only load the page synchronously if the user rotates (or triggers a
365 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700366 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
367 if (savedInstanceState != null) {
368 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
369 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800370
Sunny Goyalfe770c92016-09-27 14:38:58 -0700371 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800372 if (!internalStateHandled) {
373 // If we are not binding synchronously, show a fade in animation when
374 // the first page bind completes.
375 mDragLayer.setAlpha(0);
376 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700377 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700378 // Pages bound synchronously.
379 mWorkspace.setCurrentPage(currentScreen);
380
Sunny Goyal2100c782016-08-22 16:00:03 -0700381 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 }
383
384 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800385 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800386
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700387 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
388 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700389 setRequestedOrientation(mRotationEnabled
390 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700391
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800392 setContentView(mLauncherView);
Sunny Goyal07b69292018-01-08 14:19:34 -0800393 ((LauncherRootView) mLauncherView).dispatchInsets();
Mario Bertschlera6936942017-05-31 14:48:19 -0700394
Jon Miranda7fda2852017-07-19 16:07:01 -0700395 // Listen for broadcasts
396 IntentFilter filter = new IntentFilter();
397 filter.addAction(Intent.ACTION_SCREEN_OFF);
398 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
399 registerReceiver(mReceiver, filter);
400 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700401
Sunny Goyal8392c822017-06-20 10:03:56 -0700402 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
403 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700404
405 if (mLauncherCallbacks != null) {
406 mLauncherCallbacks.onCreate(savedInstanceState);
407 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700408
409 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700410 }
411
Sunny Goyal7779d622015-06-11 16:18:39 -0700412 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700413 public void onThemeChanged() {
414 recreate();
415 }
416
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700417 public LauncherStateManager getStateManager() {
418 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700419 }
420
Mario Bertschlera6936942017-05-31 14:48:19 -0700421 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700422 if (isDark) {
423 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700424 } else if (supportsDarkText) {
425 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700426 }
427 }
428
429 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700430 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800431 return mLauncherView.findViewById(id);
432 }
433
434 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700435 public void onAppWidgetHostReset() {
436 if (mAppWidgetHost != null) {
437 mAppWidgetHost.startListening();
438 }
439 }
440
Adam Cohen9211d422014-10-07 18:14:53 -0700441 private LauncherCallbacks mLauncherCallbacks;
442
Sunny Goyal32554d12015-12-03 15:31:25 -0800443 /**
444 * Call this after onCreate to set or clear overlay.
445 */
446 public void setLauncherOverlay(LauncherOverlay overlay) {
447 if (overlay != null) {
448 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
449 }
450 mWorkspace.setLauncherOverlay(overlay);
451 }
452
Adam Cohen9211d422014-10-07 18:14:53 -0700453 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
454 mLauncherCallbacks = callbacks;
455 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700456 }
457
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700458 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700459 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700460 if (mLauncherCallbacks != null) {
461 mLauncherCallbacks.onLauncherProviderChange();
462 }
463 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700464
Hyunyoung Song3f471442015-04-08 19:01:34 -0700465 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700466 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
467 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700468 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700469 }
470
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000471 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700472 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
473 // This cast is safe as long as the id < 0x00FFFFFF
474 // Since we jail all the dynamically generated views, there should be no clashes
475 // with any other views.
476 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000477 }
478
Tony Wickham010d2552017-01-20 08:15:28 -0800479 public PopupDataProvider getPopupDataProvider() {
480 return mPopupDataProvider;
481 }
482
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000483 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700484 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
485 * a configuration step, this allows the proper animations to run after other transitions.
486 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700487 private long completeAdd(
488 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
489 long screenId = info.screenId;
490 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700491 // When the screen id represents an actual screen (as opposed to a rank) we make sure
492 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700493 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700494 }
Adam Cohendb364c32014-05-20 14:23:40 -0700495
Sunny Goyal2100c782016-08-22 16:00:03 -0700496 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800497 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700498 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700499 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800500 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700501 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700502 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700503 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700504 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700505 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700506 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700507 int widgetId = appWidgetId;
508 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700509 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700511 // Since the view was just bound, also launch the configure activity if needed
512 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
513 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800514 if (provider != null) {
515 new WidgetAddFlowHandler(provider)
516 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700517 }
518 }
519 break;
520 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700522
Adam Cohendb364c32014-05-20 14:23:40 -0700523 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800524 }
525
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800526 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700527 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 if (isWorkspaceLoading()) {
529 // process the result once the workspace has loaded.
530 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
531 return;
532 }
533 mPendingActivityResult = null;
534
Winson Chunge341d302013-08-16 14:31:00 -0700535 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700536 final PendingRequestArgs requestArgs = mPendingRequestArgs;
537 setWaitingForResult(null);
538 if (requestArgs == null) {
539 return;
540 }
541
542 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700543
Adam Cohenad4e15c2013-10-17 16:21:35 -0700544 Runnable exitSpringLoaded = new Runnable() {
545 @Override
546 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700547 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700548 }
549 };
550
Michael Jurka8b805b12012-04-18 14:23:14 -0700551 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700552 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700553 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700554 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
555 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700556 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700557 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700558 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700559 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 addAppWidgetImpl(
561 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800562 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700563 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700564 }
565 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200566 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700567 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700568 // User could have free-scrolled between pages before picking a wallpaper; make sure
569 // we move to the closest one now.
570 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700571 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200572 }
573 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200575
Adam Cohened66b2b2012-01-23 17:28:51 -0800576 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700577 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700578
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 // We have special handling for widgets
580 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800581 final int appWidgetId;
582 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
583 : -1;
584 if (widgetId < 0) {
585 appWidgetId = pendingAddWidgetId;
586 } else {
587 appWidgetId = widgetId;
588 }
589
Adam Cohenad4e15c2013-10-17 16:21:35 -0700590 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800591 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700592 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
593 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700594 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700595 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700596 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700597 @Override
598 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700599 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 }
601 };
Adam Cohendb364c32014-05-20 14:23:40 -0700602
Sunny Goyal2100c782016-08-22 16:00:03 -0700603 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
604 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
605 } else {
606 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
607 // When the screen id represents an actual screen (as opposed to a rank)
608 // we make sure that the drop page actually exists.
609 requestArgs.screenId =
610 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700611 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700612 final CellLayout dropLayout =
613 mWorkspace.getScreenWithId(requestArgs.screenId);
614
615 dropLayout.setDropPending(true);
616 final Runnable onComplete = new Runnable() {
617 @Override
618 public void run() {
619 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
620 dropLayout.setDropPending(false);
621 }
622 };
623 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
624 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700625 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800626 return;
627 }
628
Sunny Goyald478c832016-04-01 12:04:16 -0700629 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
630 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700631 if (resultCode == RESULT_OK) {
632 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700633 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700634 }
635 // Leave the widget in the pending state if the user canceled the configure.
636 return;
637 }
638
Sunny Goyalaad90582015-07-09 14:22:50 -0700639 if (requestCode == REQUEST_CREATE_SHORTCUT) {
640 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700641 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
642 completeAdd(requestCode, data, -1, requestArgs);
643 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
644 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
645
Sunny Goyalaad90582015-07-09 14:22:50 -0700646 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700647 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
648 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800652 }
653
654 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700655 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800656 final int requestCode, final int resultCode, final Intent data) {
657 handleActivityResult(requestCode, resultCode, data);
658 if (mLauncherCallbacks != null) {
659 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 }
662
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700663 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700664 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600665 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 PendingRequestArgs pendingArgs = mPendingRequestArgs;
667 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
668 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
669 setWaitingForResult(null);
670
Sunny Goyal28c6b962015-10-12 11:42:05 -0700671 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700673 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700675 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 Intent intent = pendingArgs.getPendingIntent();
677
Sunny Goyal28c6b962015-10-12 11:42:05 -0700678 if (grantResults.length > 0
679 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700680 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 } else {
682 // TODO: Show a snack bar with link to settings
683 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700684 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 }
686 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600687 if (mLauncherCallbacks != null) {
688 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
689 grantResults);
690 }
691 }
692
Adam Cohendb364c32014-05-20 14:23:40 -0700693 /**
694 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
695 *
696 * @param screenId the screen id to check
697 * @return the new screen, or screenId if it exists
698 */
699 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800700 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700701 if (dropLayout == null) {
702 // it's possible that the add screen was removed because it was
703 // empty and a re-bind occurred
704 mWorkspace.addExtraEmptyScreen();
705 return mWorkspace.commitExtraEmptyScreen();
706 } else {
707 return screenId;
708 }
709 }
710
Sunny Goyal2100c782016-08-22 16:00:03 -0700711 @Thunk void completeTwoStageWidgetDrop(
712 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
713 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800714 Runnable onCompleteRunnable = null;
715 int animationType = 0;
716
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700717 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 if (resultCode == RESULT_OK) {
719 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
720 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800721 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700722 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800723 onCompleteRunnable = new Runnable() {
724 @Override
725 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700727 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 }
729 };
730 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800731 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700734 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700735 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700736 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
737 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700739 // The animated view may be null in the case of a rotation during widget configuration
740 onCompleteRunnable.run();
741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743
744 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700745 protected void onStop() {
746 super.onStop();
747 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700748
749 if (mLauncherCallbacks != null) {
750 mLauncherCallbacks.onStop();
751 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800752 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800753
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700754 if (!mAppLaunchSuccess) {
755 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700756 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700757 }
Tony Wickham010d2552017-01-20 08:15:28 -0800758 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700759 }
760
761 @Override
762 protected void onStart() {
763 super.onStart();
764 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700765
766 if (mLauncherCallbacks != null) {
767 mLauncherCallbacks.onStart();
768 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800769 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800770 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700771
Jon Miranda7fda2852017-07-19 16:07:01 -0700772 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700773 if (mScrimAnimator != null) {
774 mScrimAnimator.cancel();
775 }
776 mDragLayer.getBackground().setAlpha(0);
777 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
778 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
779 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
780 @Override
781 public void onAnimationEnd(Animator animation) {
782 mScrimAnimator = null;
783 }
784 });
785 mScrimAnimator.setDuration(600);
786 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
787 mScrimAnimator.start();
788 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700789 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700790 }
791
792 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700794 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700796 TraceHelper.partitionSection("ON_RESUME", "superCall");
797
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700798 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700799 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700800 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700801 // Process any items that were added while Launcher was away.
802 InstallShortcutReceiver.disableAndFlushInstallQueue(
803 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700804
Sunny Goyala474a9b2017-05-04 16:47:11 -0700805 // Refresh shortcuts if the permission changed.
806 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700807
Sunny Goyal7ede6112017-12-05 15:11:21 -0800808 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700809 if (mLauncherCallbacks != null) {
810 mLauncherCallbacks.onResume();
811 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800812
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700813 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700814 }
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700817 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700818 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700819 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700820
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700821 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800822 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700823 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700824
Adam Cohen9211d422014-10-07 18:14:53 -0700825 if (mLauncherCallbacks != null) {
826 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700827 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700828 }
829
Adam Cohenc2d6e892014-10-16 09:49:24 -0700830 public interface LauncherOverlay {
831
832 /**
833 * Touch interaction leading to overscroll has begun
834 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700835 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700836
837 /**
838 * Touch interaction related to overscroll has ended
839 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700840 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700841
842 /**
843 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
844 * screen (or in the case of RTL, the rightmost screen).
845 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700846 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700847
848 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800849 * Called when the launcher is ready to use the overlay
850 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700851 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700852 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700853 }
854
855 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700856 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700857 }
858
859 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
860
Sunny Goyalc86df472016-02-25 09:19:38 -0800861 public void onScrollChanged(float progress) {
862 if (mWorkspace != null) {
863 mWorkspace.onOverlayScrollChanged(progress);
864 }
865 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700866 }
867
Sunny Goyal16764582017-11-06 16:00:34 -0800868 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700869 if (mLauncherCallbacks != null) {
870 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700871 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800872 // On O and above we there is always some setting present settings (add icon to
873 // home screen or icon badging). On earlier APIs we will have the allow rotation
874 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700875 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700876 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800877 }
878
Sunny Goyalf9403d92017-10-18 10:55:56 -0700879 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700880 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700881 }
882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 /**
884 * Restores the previous state, if it exists.
885 *
886 * @param savedState The previous state.
887 */
888 private void restoreState(Bundle savedState) {
889 if (savedState == null) {
890 return;
891 }
892
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700893 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700894 LauncherState[] stateValues = LauncherState.values();
895 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800896 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700897 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800898 }
899
Sunny Goyal2100c782016-08-22 16:00:03 -0700900 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
901 if (requestArgs != null) {
902 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700904
905 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700906
907 SparseArray<Parcelable> widgetsState =
908 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
909 if (widgetsState != null) {
910 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 }
913
914 /**
915 * Finds all the views we need and configure them properly.
916 */
917 private void setupViews() {
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800918 mDragLayer = findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700919 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700920 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700921 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800922 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700923
Sunny Goyal784f9c32016-03-23 16:56:54 -0700924 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
925 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
926 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Winson Chung4c98d922011-05-31 16:50:48 -0700928 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800929 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700930
Winson Chung3d503fb2011-07-13 17:25:49 -0700931 // Setup the hotseat
932 mHotseat = (Hotseat) findViewById(R.id.hotseat);
933 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700934 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700935 }
936
Winson Chung4c98d922011-05-31 16:50:48 -0700937 // Setup the workspace
938 mWorkspace.setHapticFeedbackEnabled(false);
939 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700940 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700941 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
942 // default state, otherwise we will update to the wrong offsets in RTL
943 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700944 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700945 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700946
Tony Wickham34d2c912015-09-11 09:27:58 -0700947 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700948 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700949
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700950 // Setup Apps
951 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700952
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700954 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700955 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400956
Hyunyoung Songd725f642017-08-17 22:26:35 -0700957 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
960 /**
961 * Creates a view representing a shortcut.
962 *
963 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800965 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700966 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 /**
970 * Creates a view representing a shortcut inflated from the specified resource.
971 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 * @param parent The group the shortcut belongs to.
973 * @param info The data structure describing the shortcut.
974 *
975 * @return A View inflated from layoutResId.
976 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700977 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800978 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
979 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800980 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700982 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 return favorite;
984 }
985
986 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700987 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 *
989 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700992 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700993 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
994 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700995 return;
996 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700997
Jon Mirandac476d6e2017-05-04 16:30:01 -0700998 int[] cellXY = mTmpAddItemCellCoordinates;
999 CellLayout layout = getCellLayout(container, screenId);
1000
Sunny Goyal782f0c92017-01-19 10:27:54 -08001001 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001002 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001003 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1004 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001005 }
1006
1007 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001008 // Legacy shortcuts are only supported for primary profile.
1009 info = Process.myUserHandle().equals(args.user)
1010 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001011
Sunny Goyalb57645f2017-03-20 13:57:28 -07001012 if (info == null) {
1013 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1014 return;
1015 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001016 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001017 // The app is trying to add a shortcut without sufficient permissions
1018 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1019 return;
1020 }
1021 }
1022
Jon Mirandac476d6e2017-05-04 16:30:01 -07001023 if (container < 0) {
1024 // Adding a shortcut to the Workspace.
1025 final View view = createShortcut(info);
1026 boolean foundCellSpan = false;
1027 // First we check if we already know the exact location where we want to add this item.
1028 if (cellX >= 0 && cellY >= 0) {
1029 cellXY[0] = cellX;
1030 cellXY[1] = cellY;
1031 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001032
Jon Mirandac476d6e2017-05-04 16:30:01 -07001033 // If appropriate, either create a folder or add to an existing folder
1034 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001035 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001036 return;
1037 }
1038 DragObject dragObject = new DragObject();
1039 dragObject.dragInfo = info;
1040 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1041 true)) {
1042 return;
1043 }
1044 } else {
1045 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1046 }
1047
1048 if (!foundCellSpan) {
1049 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001050 return;
1051 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001052
1053 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1054 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001055 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001056 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001057 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001058 if (folderIcon != null) {
1059 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1060 folderInfo.add(info, args.rank, false);
1061 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001062 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001063 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 }
1066
Sunny Goyale29897f2017-07-20 10:09:42 -07001067 public FolderIcon findFolderIcon(final long folderIconId) {
1068 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1069 @Override
1070 public boolean evaluate(ItemInfo info, View view) {
1071 return info != null && info.id == folderIconId;
1072 }
1073 });
1074 }
1075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001079 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001081 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001082 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1083
Adam Cohened66b2b2012-01-23 17:28:51 -08001084 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001085 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001086 }
Romain Guycbb89e42009-06-08 15:52:54 -07001087
Adam Cohen59400422014-03-05 18:07:04 -08001088 LauncherAppWidgetInfo launcherInfo;
1089 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001090 launcherInfo.spanX = itemInfo.spanX;
1091 launcherInfo.spanY = itemInfo.spanY;
1092 launcherInfo.minSpanX = itemInfo.minSpanX;
1093 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001094 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001096 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001097 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098
Sunny Goyal2100c782016-08-22 16:00:03 -07001099 if (hostView == null) {
1100 // Perform actual inflation because we're live
1101 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001103 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301104 prepareAppWidget(hostView, launcherInfo);
1105 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 }
Romain Guycbb89e42009-06-08 15:52:54 -07001107
Sunny Goyald5462aa2016-11-22 16:52:39 +05301108 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001109 hostView.setTag(item);
1110 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001111 hostView.setFocusable(true);
1112 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001113 }
1114
Adam Cohended9f8d2010-11-03 13:25:16 -07001115 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1116 @Override
1117 public void onReceive(Context context, Intent intent) {
1118 final String action = intent.getAction();
1119 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001120 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001121 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001122 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001123 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001124 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001125 mShouldFadeInScrim = true;
1126 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1127 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1128 // the user unlocked and the Launcher is not in the foreground.
1129 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001130 }
1131 }
1132 };
1133
Tony Wickham010d2552017-01-20 08:15:28 -08001134 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001135 mWorkspace.updateIconBadges(updatedBadges);
1136 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001137
Sunny Goyal29947f02017-12-18 13:49:44 -08001138 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1139 if (popup != null) {
1140 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001141 }
1142 }
1143
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 @Override
1145 public void onAttachedToWindow() {
1146 super.onAttachedToWindow();
1147
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001148 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001149 if (mLauncherCallbacks != null) {
1150 mLauncherCallbacks.onAttachedToWindow();
1151 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001152 }
1153
1154 @Override
1155 public void onDetachedFromWindow() {
1156 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001157
1158 if (mLauncherCallbacks != null) {
1159 mLauncherCallbacks.onDetachedFromWindow();
1160 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001161 }
1162
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001163 public AllAppsTransitionController getAllAppsController() {
1164 return mAllAppsController;
1165 }
1166
Winson Chunga6945242014-01-08 14:04:34 -08001167 public DragLayer getDragLayer() {
1168 return mDragLayer;
1169 }
1170
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001171 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001172 return mAppsView;
1173 }
1174
Winson Chunga6945242014-01-08 14:04:34 -08001175 public Workspace getWorkspace() {
1176 return mWorkspace;
1177 }
1178
1179 public Hotseat getHotseat() {
1180 return mHotseat;
1181 }
1182
Winson Chung8ae41982017-11-10 11:42:13 -08001183 public <T extends ViewGroup> T getOverviewPanel() {
1184 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001185 }
1186
Sunny Goyal47328fd2016-05-25 18:56:41 -07001187 public DropTargetBar getDropTargetBar() {
1188 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001189 }
1190
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191 public LauncherAppWidgetHost getAppWidgetHost() {
1192 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 }
Romain Guycbb89e42009-06-08 15:52:54 -07001194
Winson Chunga9abd0e2010-10-27 17:18:37 -07001195 public LauncherModel getModel() {
1196 return mModel;
1197 }
1198
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001199 public ModelWriter getModelWriter() {
1200 return mModelWriter;
1201 }
1202
Adam Cohen79d90c52016-04-22 13:29:20 -07001203 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001204 return mSharedPrefs;
1205 }
1206
Jon Miranda6bed3502017-09-19 14:43:17 -07001207 public int getOrientation() { return mOrientation; }
1208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 @Override
1210 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001211 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 super.onNewIntent(intent);
1213
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001214 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001215 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1216 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001217
1218 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001219 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001220 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001221 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001222 boolean internalStateHandled = InternalStateHandler
1223 .handleNewIntent(this, intent, alreadyOnHome);
1224
Winson15f8b172015-08-19 11:02:39 -07001225 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001226 if (!internalStateHandled) {
1227 // Note: There should be at most one log per method call. This is enforced
1228 // implicitly by using if-else statements.
1229 UserEventDispatcher ued = getUserEventDispatcher();
1230 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1231 if (topOpenView != null) {
1232 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1233 } else if (alreadyOnHome) {
1234 Target target = newContainerTarget(mStateManager.getState().containerType);
1235 target.pageIndex = mWorkspace.getCurrentPage();
1236 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1237 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001238
Sunny Goyald3864fa2017-11-14 15:06:36 -08001239 // In all these cases, only animate if we're already on home
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001240 AbstractFloatingView.closeAllOpenViews(this, isStarted());
Jon Mirandafeba90f2016-10-06 10:53:29 -07001241
Sunny Goyalcc96aa12018-01-11 09:56:07 -08001242 mStateManager.goToState(NORMAL);
Sunny Goyald3864fa2017-11-14 15:06:36 -08001243
1244 // Reset the apps view
1245 if (!alreadyOnHome && mAppsView != null) {
1246 mAppsView.reset();
1247 }
1248
1249 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1250 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1251 }
1252 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001253
1254 final View v = getWindow().peekDecorView();
1255 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001256 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001257 }
1258
Adam Cohen9211d422014-10-07 18:14:53 -07001259 if (mLauncherCallbacks != null) {
1260 mLauncherCallbacks.onHomeIntent();
1261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
Winson15f8b172015-08-19 11:02:39 -07001263
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001264 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001268 public void onRestoreInstanceState(Bundle state) {
1269 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001270 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
1273 @Override
1274 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001275 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001276 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001277
Adam Cohen21cd0022013-10-09 18:57:02 -07001278 }
Sunny Goyalea609262017-10-25 15:47:38 -07001279 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001280
1281
1282 AbstractFloatingView widgets = AbstractFloatingView
1283 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1284 if (widgets != null) {
1285 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1286 widgets.saveHierarchyState(widgetsState);
1287 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1288 } else {
1289 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1290 }
1291
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001292 // We close any open folders and shortcut containers since they will not be re-opened,
1293 // and we need to make sure this state is reflected.
1294 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001295
Sunny Goyal2100c782016-08-22 16:00:03 -07001296 if (mPendingRequestArgs != null) {
1297 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1298 }
1299 if (mPendingActivityResult != null) {
1300 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001303 super.onSaveInstanceState(outState);
1304
Adam Cohen9211d422014-10-07 18:14:53 -07001305 if (mLauncherCallbacks != null) {
1306 mLauncherCallbacks.onSaveInstanceState(outState);
1307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 @Override
1311 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001313
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001314 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001315 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001316
Winson Chungcd2b0142011-06-08 16:02:26 -07001317 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001318 // It's possible to receive onDestroy after a new Launcher activity has
1319 // been created. In this case, don't interfere with the new Launcher.
1320 if (mModel.isCurrentCallbacks(this)) {
1321 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001322 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001323 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001324
Sunny Goyal745bad92016-05-02 10:54:12 -07001325 if (mRotationPrefChangeHandler != null) {
1326 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1327 }
1328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001330 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001332 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001336 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1337
Michael Jurka2ecf9952012-06-18 12:52:28 -07001338 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001339
Tony2917a8b2017-07-31 23:29:42 -07001340 clearPendingBinds();
1341
Adam Cohen9211d422014-10-07 18:14:53 -07001342 if (mLauncherCallbacks != null) {
1343 mLauncherCallbacks.onDestroy();
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
Sunny Goyalae502842016-06-17 08:43:56 -07001347 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1348 return mAccessibilityDelegate;
1349 }
1350
Adam Cohena9cf38f2011-05-02 15:36:58 -07001351 public DragController getDragController() {
1352 return mDragController;
1353 }
1354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001356 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001357 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001358 }
1359
1360 @Override
1361 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1362 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001363 try {
1364 super.startIntentSenderForResult(intent, requestCode,
1365 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1366 } catch (IntentSender.SendIntentException e) {
1367 throw new ActivityNotFoundException();
1368 }
1369 }
1370
Winson Chung70d72102011-08-12 11:24:35 -07001371 /**
1372 * Indicates that we want global search for this activity by setting the globalSearch
1373 * argument for {@link #startSearch} to true.
1374 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001376 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 Bundle appSearchData, boolean globalSearch) {
1378 if (appSearchData == null) {
1379 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001380 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001382
Sunny Goyal6f28e712016-09-13 14:19:29 -07001383 if (mLauncherCallbacks == null ||
1384 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1385 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001386 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001387 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001388
1389 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001390 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001391 }
1392
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001394 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 }
1396
Adam Cohen517a7f52014-03-01 12:12:59 -08001397 public boolean isWorkspaceLoading() {
1398 return mWorkspaceLoading;
1399 }
1400
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001401 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001402 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001403 }
1404
Sunny Goyal04a324a2017-01-20 21:08:59 -08001405 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001406 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001407 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001408
Sunny Goyal2100c782016-08-22 16:00:03 -07001409 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001410 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001411 if (LOGD) {
1412 Log.d(TAG, "Adding widget from drop");
1413 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001414 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001415 }
1416
Sunny Goyal2100c782016-08-22 16:00:03 -07001417 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001418 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1419 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1420 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001421
Adam Cohenad4e15c2013-10-17 16:21:35 -07001422 Runnable onComplete = new Runnable() {
1423 @Override
1424 public void run() {
1425 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001426 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001427 }
1428 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001429 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001430 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
1432 }
Romain Guycbb89e42009-06-08 15:52:54 -07001433
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001434 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1435 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001436 info.container = container;
1437 info.screenId = screenId;
1438 if (cell != null) {
1439 info.cellX = cell[0];
1440 info.cellY = cell[1];
1441 }
1442 info.spanX = spanX;
1443 info.spanY = spanY;
1444
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001445 switch (info.itemType) {
1446 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1447 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001448 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001449 break;
1450 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001451 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001452 break;
1453 default:
1454 throw new IllegalStateException("Unknown item type: " + info.itemType);
1455 }
1456 }
1457
Adam Cohenfbba09b2011-07-18 21:43:05 -07001458 /**
1459 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001460 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001461 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001462 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1463 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001464 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1465 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1466 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001467 }
1468
Adam Cohenfbba09b2011-07-18 21:43:05 -07001469 /**
1470 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001471 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001472 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001473 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001474 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001475 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001476 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001477 // In the case where we've prebound the widget, we remove it from the DragLayer
1478 if (LOGD) {
1479 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1480 }
1481 getDragLayer().removeView(hostView);
1482
Adam Cohened66b2b2012-01-23 17:28:51 -08001483 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001484 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001485
1486 // Clear the boundWidget so that it doesn't get destroyed.
1487 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001488 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001489 // In this case, we either need to start an activity to get permission to bind
1490 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001491 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1492 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1493 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1494 this, info.componentName);
1495 } else {
1496 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1497 }
Adam Cohendd70d662012-10-04 16:53:44 -07001498 Bundle options = info.bindOptions;
1499
Sunny Goyalffe83f12014-08-14 17:39:34 -07001500 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1501 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001502 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001503 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001504 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001505 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001506 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001507 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 }
1509
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001512 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 folderInfo.title = getText(R.string.folder_name);
1514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001516 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517
1518 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001519 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301520 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001521 // Force measure the new folder icon
1522 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1523 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001524 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
Romain Guycbb89e42009-06-08 15:52:54 -07001526
Winsonc0b52fe2015-09-09 16:38:15 -07001527 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001528 * Unbinds the view for the specified item, and removes the item and all its children.
1529 *
1530 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001531 * @param itemInfo the {@link ItemInfo} for this view.
1532 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001533 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001534 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001535 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001536 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001537 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1538 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001539 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001540 } else {
1541 mWorkspace.removeWorkspaceItem(v);
1542 }
Winsonc0b52fe2015-09-09 16:38:15 -07001543 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001544 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001545 }
1546 } else if (itemInfo instanceof FolderInfo) {
1547 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001548 if (v instanceof FolderIcon) {
1549 ((FolderIcon) v).removeListeners();
1550 }
Winsonc0b52fe2015-09-09 16:38:15 -07001551 mWorkspace.removeWorkspaceItem(v);
1552 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001553 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001554 }
1555 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1556 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001557 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001558 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001559 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001560 }
1561 } else {
1562 return false;
1563 }
1564 return true;
1565 }
1566
1567 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001568 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001569 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001570 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001571 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001572 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001573 // Deleting an app widget ID is a void call but writes to disk before returning
1574 // to the caller...
1575 new AsyncTask<Void, Void, Void>() {
1576 public Void doInBackground(Void ... args) {
1577 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1578 return null;
1579 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001580 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001581 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001582 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001583 }
1584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 @Override
1586 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001587 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
1589
Joe Onorato88ec0992009-11-19 13:16:06 -08001590 @Override
1591 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001592 if (finishAutoCancelActionMode()) {
1593 return;
1594 }
Adam Cohen9211d422014-10-07 18:14:53 -07001595 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1596 return;
1597 }
1598
Sunny Goyal45478022015-06-08 16:52:41 -07001599 if (mDragController.isDragging()) {
1600 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001601 return;
1602 }
1603
Jon Mirandafeba90f2016-10-06 10:53:29 -07001604 // Note: There should be at most one log per method call. This is enforced implicitly
1605 // by using if-else statements.
1606 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001607 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1608 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001609 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001610 } else if (!isInState(NORMAL)) {
Sunny Goyal49bcf342018-01-11 13:59:53 -08001611 LauncherState lastState = mStateManager.getLastState();
Sunny Goyalea609262017-10-25 15:47:38 -07001612 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal49bcf342018-01-11 13:59:53 -08001613 mStateManager.goToState(lastState);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001614 } else {
1615 // Back button is a no-op here, but give at least some feedback for the button press
1616 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001617 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 * Launches the intent referred by the clicked shortcut.
1622 *
1623 * @param v The view representing the clicked shortcut.
1624 */
1625 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001626 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1627 // view has detached (it's possible for this to happen if the view is removed mid touch).
1628 if (v.getWindowToken() == null) {
1629 return;
1630 }
1631
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001632 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001633 return;
1634 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001635
Adam Cohen1697b792013-09-17 19:08:21 -07001636 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001637 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001638 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1639 LauncherLogProto.Action.Direction.NONE,
1640 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001641 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001642 }
1643 return;
1644 }
1645
1646 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001647 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001648 int page = mWorkspace.indexOfChild(v);
1649 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1650 LauncherLogProto.Action.Direction.NONE,
1651 LauncherLogProto.ContainerType.OVERVIEW, page);
1652 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001653 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001654 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001655 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001656 }
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001659 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001660 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001662 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001663 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001664 }
Sunny Goyal508da152014-08-14 10:53:27 -07001665 } else if (tag instanceof AppInfo) {
1666 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001667 } else if (tag instanceof LauncherAppWidgetInfo) {
1668 if (v instanceof PendingAppWidgetHostView) {
1669 onClickPendingWidget((PendingAppWidgetHostView) v);
1670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
1672 }
1673
Sunny Goyal70660032015-05-14 00:07:08 -07001674 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001675 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001676 return false;
1677 }
1678
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001680 * Event handler for the app widget view which has not fully restored.
1681 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001682 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001683 if (mIsSafeModeEnabled) {
1684 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1685 return;
1686 }
1687
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001688 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001689 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001690 LauncherAppWidgetProviderInfo appWidgetInfo =
1691 mAppWidgetManager.findProvider(info.providerName, info.user);
1692 if (appWidgetInfo == null) {
1693 return;
1694 }
1695 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1696
Sunny Goyald478c832016-04-01 12:04:16 -07001697 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1698 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1699 // This should not happen, as we make sure that an Id is allocated during bind.
1700 return;
1701 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001702 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1703 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001704 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001705 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001706 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001707 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001708 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001709 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001710 }
1711 }
1712
Sunny Goyale6e72002017-01-12 16:55:36 -08001713 private void onClickPendingAppItem(final View v, final String packageName,
1714 boolean downloadStarted) {
1715 if (downloadStarted) {
1716 // If the download has started, simply direct to the market app.
1717 startMarketIntentForPackage(v, packageName);
1718 return;
1719 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001720 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001721 .setTitle(R.string.abandoned_promises_title)
1722 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001723 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1724 @Override
1725 public void onClick(DialogInterface dialogInterface, int i) {
1726 startMarketIntentForPackage(v, packageName);
1727 }
1728 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001729 .setNeutralButton(R.string.abandoned_clean_this,
1730 new DialogInterface.OnClickListener() {
1731 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001732 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001733 mWorkspace.removeAbandonedPromise(packageName, user);
1734 }
1735 })
1736 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001737 }
1738
1739 private void startMarketIntentForPackage(View v, String packageName) {
1740 ItemInfo item = (ItemInfo) v.getTag();
1741 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001742 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001743 }
1744
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001745 /**
1746 * Event handler for an app shortcut click.
1747 *
1748 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1749 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001750 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001751 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1752 Object tag = v.getTag();
1753 if (!(tag instanceof ShortcutInfo)) {
1754 throw new IllegalArgumentException("Input must be a Shortcut");
1755 }
1756
1757 // Open shortcut
1758 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001759
Sunny Goyal076839c2017-10-30 13:52:20 -07001760 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001761 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1762 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001763 ~FLAG_DISABLED_SUSPENDED &
1764 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001765 // If the app is only disabled because of the above flags, launch activity anyway.
1766 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001767 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001768 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1769 // Use a message specific to this shortcut, if it has one.
1770 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1771 return;
1772 }
1773 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001774 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001775 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001776 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001777 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1778 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001779 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001780 }
1781 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1782 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001783 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001784 }
1785
Chris Wren40c5ed32014-06-24 18:24:23 -04001786 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001787 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001788 String packageName = shortcut.intent.getComponent() != null ?
1789 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1790 if (!TextUtils.isEmpty(packageName)) {
1791 onClickPendingAppItem(v, packageName,
1792 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1793 return;
1794 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001795 }
1796
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001797 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001798 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001799 }
1800
Sunny Goyala7ce1662016-05-31 15:01:35 -07001801 private void startAppShortcutOrInfoActivity(View v) {
1802 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001803 Intent intent;
1804 if (item instanceof PromiseAppInfo) {
1805 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
1806 intent = promiseAppInfo.getMarketIntent();
1807 } else {
1808 intent = item.getIntent();
1809 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001810 if (intent == null) {
1811 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001812 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001813 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001814 }
1815
1816 /**
1817 * Event handler for a folder icon click.
1818 *
1819 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1820 */
1821 protected void onClickFolderIcon(View v) {
1822 if (LOGD) Log.d(TAG, "onClickFolder");
1823 if (!(v instanceof FolderIcon)){
1824 throw new IllegalArgumentException("Input must be a FolderIcon");
1825 }
1826
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001827 Folder folder = ((FolderIcon) v).getFolder();
1828 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001829 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001830 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001831 }
Michael Jurka5130e402011-10-13 04:55:35 -07001832 }
1833
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001834 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001835 * Event handler for the wallpaper picker button that appears after a long press
1836 * on the home screen.
1837 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001838 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001839 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001840 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1841 return;
1842 }
1843
Tony Wickham785f7a52015-08-31 17:28:32 -07001844 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1845 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001846 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001847 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001848 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001849
1850 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001851 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1852 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001853 intent.setPackage(pickerPackage);
1854 }
1855
Sunny Goyala5ace712017-11-15 17:12:51 -08001856 final Bundle launchOptions;
1857 if (v != null) {
1858 intent.setSourceBounds(getViewBounds(v));
1859 // If there is no target package, use the default intent chooser animation
1860 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
1861 } else {
1862 launchOptions = null;
1863 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001864 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001865 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001866 } catch (ActivityNotFoundException e) {
1867 setWaitingForResult(null);
1868 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1869 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001870 }
1871
Sunny Goyala7ce1662016-05-31 15:01:35 -07001872 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001874 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1875 try {
1876 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1877 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1878 // is enabled by default on NYC.
1879 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1880 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001881
1882 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1883 String id = ((ShortcutInfo) info).getDeepShortcutId();
1884 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301885 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001886 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001887 } else {
1888 // Could be launching some bookkeeping activity
1889 startActivity(intent, optsBundle);
1890 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001891 } finally {
1892 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001895 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1896 // corresponding permission. Show the appropriate permission prompt if that
1897 // is the case.
1898 if (intent.getComponent() == null
1899 && Intent.ACTION_CALL.equals(intent.getAction())
1900 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1901 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001902
1903 setWaitingForResult(PendingRequestArgs
1904 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001905 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1906 REQUEST_PERMISSION_CALL_PHONE);
1907 } else {
1908 // No idea why this was thrown.
1909 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001913
Sunny Goyalfe770c92016-09-27 14:38:58 -07001914 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07001915 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001916 if (Utilities.ATLEAST_MARSHMALLOW) {
1917 int left = 0, top = 0;
1918 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001919 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001920 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001921 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07001922 if (icon != null) {
1923 Rect bounds = icon.getBounds();
1924 left = (width - bounds.width()) / 2;
1925 top = v.getPaddingTop();
1926 width = bounds.width();
1927 height = bounds.height();
1928 }
1929 }
1930 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
1931 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
1932 // On L devices, we use the device default slide-up transition.
1933 // On L MR1 devices, we use a custom version of the slide-up transition which
1934 // doesn't have the delay present in the device default.
1935 return ActivityOptions.makeCustomAnimation(
1936 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
1937 }
1938 return null;
1939 }
1940
Tonye3c59252017-05-02 21:32:27 -07001941 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001942 int[] pos = new int[2];
1943 v.getLocationOnScreen(pos);
1944 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1945 }
1946
Sunny Goyala7ce1662016-05-31 15:01:35 -07001947 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001948 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001949 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1950 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001951 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001952 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001953 // Only launch using the new animation if the shortcut has not opted out (this is a
1954 // private contract between launcher and may be ignored in the future).
1955 boolean useLaunchAnimation = (v != null) &&
1956 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1957 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
1958
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001959 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001960
1961 // Prepare intent
1962 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1963 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001964 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001965 }
1966 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001967 if (Utilities.ATLEAST_MARSHMALLOW
1968 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001969 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001970 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08001971 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001972 // Shortcuts need some special checks due to legacy reasons.
1973 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001974 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001975 // Could be launching some bookkeeping activity
1976 startActivity(intent, optsBundle);
1977 } else {
1978 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1979 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1980 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001981
1982 if (v instanceof BubbleTextView) {
1983 // This is set to the view that launched the activity that navigated the user away
1984 // from launcher. Since there is no callback for when the activity has finished
1985 // launching, enable the press state and keep this reference to reset the press
1986 // state when we return to launcher.
1987 BubbleTextView btv = (BubbleTextView) v;
1988 btv.setStayPressed(true);
1989 setOnResumeCallback(btv);
1990 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001991 mAppLaunchSuccess = true;
1992 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07001993 } catch (ActivityNotFoundException|SecurityException e) {
1994 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1995 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
1996 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001997 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07001998 }
1999
Tony Wickhamdadb3042016-02-24 11:07:00 -08002000 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002001 public boolean dispatchTouchEvent(MotionEvent ev) {
2002 mLastDispatchTouchEventX = ev.getX();
2003 return super.dispatchTouchEvent(ev);
2004 }
2005
2006 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002008 if (!isDraggingEnabled()) return false;
2009 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002010 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011
Jon Miranda51f037d2016-11-07 08:37:20 -08002012 boolean ignoreLongPressToOverview =
2013 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002014
Adam Cohen1697b792013-09-17 19:08:21 -07002015 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002016 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002017 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302018 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2019 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002020 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002021 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002022 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2023 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2024 return true;
2025 } else {
2026 return false;
2027 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002028 } else {
2029 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002030 }
Adam Cohen1697b792013-09-17 19:08:21 -07002031 }
2032
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002033 CellLayout.CellInfo longClickCellInfo = null;
2034 View itemUnderLongClick = null;
2035 if (v.getTag() instanceof ItemInfo) {
2036 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002037 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002038 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002039 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
2041
Winson Chung3d503fb2011-07-13 17:25:49 -07002042 // The hotseat touch handling does not go through Workspace, and we always allow long press
2043 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002044 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002045 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002046 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002047 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002048 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302049 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2050 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002051 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002052 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002053 return false;
2054 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302055 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2056 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002057 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002058 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002059 }
Jon Miranda379198e2016-09-23 08:54:40 -07002060 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2061 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002063 final boolean isAllAppsButton =
2064 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2065 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2066 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002067 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002069 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071 }
2072 }
2073 return true;
2074 }
2075
Winson Chung3d503fb2011-07-13 17:25:49 -07002076 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002077 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002078 return mHotseat != null && layout != null &&
2079 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2080 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002081
Winson Chung3d503fb2011-07-13 17:25:49 -07002082 /**
2083 * Returns the CellLayout of the specified container at the specified screen.
2084 */
Sunny Goyal92820592015-03-02 11:31:35 -08002085 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2087 if (mHotseat != null) {
2088 return mHotseat.getLayout();
2089 } else {
2090 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002091 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002092 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002093 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002094 }
2095 }
2096
Michael Jurkae326f182011-11-21 14:05:46 -08002097 @Override
2098 public void onTrimMemory(int level) {
2099 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002100 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2101 // The widget preview db can result in holding onto over
2102 // 3MB of memory for caching which isn't necessary.
2103 SQLiteDatabase.releaseMemory();
2104
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002105 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002106 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002107 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002108 if (mLauncherCallbacks != null) {
2109 mLauncherCallbacks.onTrimMemory(level);
2110 }
Michael Jurkae326f182011-11-21 14:05:46 -08002111 }
2112
Michael Jurkad7c28052012-04-27 15:43:36 -07002113 @Override
2114 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002115 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002116 final List<CharSequence> text = event.getText();
2117 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002118 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002119 // TODO: When can workspace be null?
2120 text.add(mWorkspace == null
2121 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002122 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002123 return result;
2124 }
2125
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002126 public void setOnResumeCallback(OnResumeCallback callback) {
2127 if (mOnResumeCallback != null) {
2128 mOnResumeCallback.onLauncherResume();
2129 }
2130 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002131 }
2132
Michael Jurka7607c2f2013-04-03 14:33:19 -07002133 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002134 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002136 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002138 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002139 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002140 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002141 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002142 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002144
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002146 * Clear any pending bind callbacks. This is called when is loader is planning to
2147 * perform a full rebind from scratch.
2148 */
2149 @Override
2150 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002151 if (mPendingExecutor != null) {
2152 mPendingExecutor.markCompleted();
2153 mPendingExecutor = null;
2154 }
2155 }
2156
2157 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 * Refreshes the shortcuts shown on the workspace.
2159 *
2160 * Implementation of the method from LauncherModel.Callbacks.
2161 */
2162 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002163 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002164 // Floating panels (except the full widget sheet) are associated with individual icons. If
2165 // we are starting a fresh bind, close all such panels as all the icons are about
2166 // to go away.
2167 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002168 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002169
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002170 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002171
Winson Chungd64d1762013-08-20 14:37:16 -07002172 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002173 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002174 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002175
Winson Chung3d503fb2011-07-13 17:25:49 -07002176 if (mHotseat != null) {
2177 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002179 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 }
2181
Adam Cohendcd297f2013-06-18 13:13:40 -07002182 @Override
2183 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002184 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002185 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2186 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002187 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2188 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002189 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002190 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2191 // If there are no screens, we need to have an empty screen
2192 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002193 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002194 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002195
Winsonc7d2e832016-07-28 12:24:55 -07002196 // After we have added all the screens, if the wallpaper was locked to the default state,
2197 // then notify to indicate that it can be released and a proper wallpaper offset can be
2198 // computed before the next layout
2199 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002200 }
2201
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002202 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 int count = orderedScreenIds.size();
2204 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002205 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002206 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002207 // No need to bind the first screen, as its always bound.
2208 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2209 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002210 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 }
2212
Sunny Goyalb23980c2017-08-17 07:45:25 -07002213 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002214 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2215 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002216 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002217 if (newScreens != null) {
2218 bindAddScreens(newScreens);
2219 }
Winson Chungd64d1762013-08-20 14:37:16 -07002220
2221 // We add the items without animation on non-visible pages, and with
2222 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002223 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002224 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002225 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002226 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002227 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002228 }
Winson Chungc58497e2013-09-03 17:48:37 -07002229
Winson Chung87412982013-10-03 18:34:14 -07002230 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002231 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002232 }
2233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002235 * Bind the items start-end from the list.
2236 *
2237 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002239 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002240 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002241 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002242 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002243 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002244 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002245 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002246 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002247 int end = items.size();
2248 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002249 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002250
2251 // Short circuit if we are loading dock items for a configuration which has no dock
2252 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2253 mHotseat == null) {
2254 continue;
2255 }
2256
Sunny Goyal639e9062015-08-19 19:17:06 -07002257 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 switch (item.itemType) {
2259 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2260 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002261 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002262 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002263 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002264 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002265 }
2266 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002267 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002268 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002269 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002270 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002271 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002272 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2273 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002274 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002275 if (view == null) {
2276 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002277 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002278 break;
2279 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002280 default:
2281 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002282 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002283
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002284 /*
2285 * Remove colliding items.
2286 */
2287 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2288 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2289 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2290 View v = cl.getChildAt(item.cellX, item.cellY);
2291 Object tag = v.getTag();
2292 String desc = "Collision while binding workspace item: " + item
2293 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002294 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002295 throw (new RuntimeException(desc));
2296 } else {
2297 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002298 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002299 continue;
2300 }
2301 }
2302 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302303 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002304 if (animateIcons) {
2305 // Animate all the applications up now
2306 view.setAlpha(0f);
2307 view.setScaleX(0f);
2308 view.setScaleY(0f);
2309 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002310 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002313
Winson Chung997a9232013-07-24 15:33:46 -07002314 if (animateIcons) {
2315 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002316 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002317 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002318 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002319 final Runnable startBounceAnimRunnable = new Runnable() {
2320 public void run() {
2321 anim.playTogether(bounceAnims);
2322 anim.start();
2323 }
2324 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002325 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002326 // We post the animation slightly delayed to prevent slowdowns
2327 // when we are loading right after we return to launcher.
2328 mWorkspace.postDelayed(new Runnable() {
2329 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002330 if (mWorkspace != null) {
2331 mWorkspace.snapToPage(newScreenIndex);
2332 mWorkspace.postDelayed(startBounceAnimRunnable,
2333 NEW_APPS_ANIMATION_DELAY);
2334 }
Winson Chung94d67682013-09-25 16:29:40 -07002335 }
2336 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002337 } else {
2338 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002339 }
2340 }
Winson Chung64359a52013-07-08 17:17:08 -07002341 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 }
2344
Joe Onorato9c1289c2009-08-17 11:03:03 -04002345 /**
2346 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002347 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002348 public void bindAppWidget(LauncherAppWidgetInfo item) {
2349 View view = inflateAppWidget(item);
2350 if (view != null) {
2351 mWorkspace.addInScreen(view, item);
2352 mWorkspace.requestLayout();
2353 }
2354 }
2355
2356 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002357 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302358 PendingAppWidgetHostView view =
2359 new PendingAppWidgetHostView(this, item, mIconCache, true);
2360 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002361 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002362 }
2363
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002364 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002365
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002366 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002367
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002368 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2369 // If the provider is not ready, bind as a pending widget.
2370 appWidgetInfo = null;
2371 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2372 // The widget id is not valid. Try to find the widget based on the provider info.
2373 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2374 } else {
2375 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2376 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002377
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002378 // If the provider is ready, but the width is not yet restored, try to restore it.
2379 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2380 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002381 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002382 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2383 + " belongs to component " + item.providerName
2384 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002385 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002386 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002387 }
Sunny Goyalff572272014-07-23 13:58:07 -07002388
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002389 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002390 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002391 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2392 // Id has not been allocated yet. Allocate a new id.
2393 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2394 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002395
Sunny Goyald478c832016-04-01 12:04:16 -07002396 // Also try to bind the widget. If the bind fails, the user will be shown
2397 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002398 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002399 pendingInfo.spanX = item.spanX;
2400 pendingInfo.spanY = item.spanY;
2401 pendingInfo.minSpanX = item.minSpanX;
2402 pendingInfo.minSpanY = item.minSpanY;
2403 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002404
2405 boolean isDirectConfig =
2406 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2407 if (isDirectConfig && item.bindOptions != null) {
2408 Bundle newOptions = item.bindOptions.getExtras();
2409 if (options != null) {
2410 newOptions.putAll(options);
2411 }
2412 options = newOptions;
2413 }
Sunny Goyald478c832016-04-01 12:04:16 -07002414 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2415 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002416
Sunny Goyal86df1382016-08-10 15:03:22 -07002417 // We tried to bind once. If we were not able to bind, we would need to
2418 // go through the permission dialog, which means we cannot skip the config
2419 // activity.
2420 item.bindOptions = null;
2421 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2422
Sunny Goyald478c832016-04-01 12:04:16 -07002423 // Bind succeeded
2424 if (success) {
2425 // If the widget has a configure activity, it is still needs to set it up,
2426 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002427 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002428 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2429 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002430 }
Sunny Goyald478c832016-04-01 12:04:16 -07002431
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002432 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002433 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002434 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002435 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002436 // The widget was marked as UI not ready, but there is no configure activity to
2437 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002438 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002439 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002440 }
Sunny Goyalff572272014-07-23 13:58:07 -07002441 }
2442
Sunny Goyald5462aa2016-11-22 16:52:39 +05302443 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002444 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002445 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002446 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002447 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002448 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002449 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002450 }
2451
Sunny Goyal233ee962015-08-03 13:05:01 -07002452 item.minSpanX = appWidgetInfo.minSpanX;
2453 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302454 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002455 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302456 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002457 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302458 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002459
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002460 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002461 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002462 }
2463
Sunny Goyalff572272014-07-23 13:58:07 -07002464 /**
2465 * Restores a pending widget.
2466 *
2467 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002468 */
Sunny Goyald478c832016-04-01 12:04:16 -07002469 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002470 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2471 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2472 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002473 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002474 }
2475
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002476 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002477 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002478 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2479 info.pendingItemInfo = null;
2480 }
Sunny Goyalff572272014-07-23 13:58:07 -07002481
Sunny Goyalba47faa2017-10-04 16:50:57 -07002482 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002483 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002484 }
2485
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002486 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002487 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002488 }
2489
Adam Cohen1462de32012-07-24 22:34:36 -07002490 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002491 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002492 }
2493
Sunny Goyal527c7d32015-08-28 15:19:36 -07002494 @Override
2495 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2496 if (mPendingExecutor != null) {
2497 mPendingExecutor.markCompleted();
2498 }
2499 mPendingExecutor = executor;
2500 executor.attachTo(this);
2501 }
2502
2503 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2504 if (mPendingExecutor == executor) {
2505 mPendingExecutor = null;
2506 }
2507 }
2508
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002509 @Override
2510 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002511 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002512 mDragLayer.animate().alpha(1).withEndAction(
2513 executor == null ? null : executor::onLoadAnimationCompleted).start();
2514 } else if (executor != null) {
2515 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002516 }
2517 }
2518
Joe Onorato9c1289c2009-08-17 11:03:03 -04002519 /**
2520 * Callback saying that there aren't any more items to bind.
2521 *
2522 * Implementation of the method from LauncherModel.Callbacks.
2523 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002524 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002525 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002526 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002527
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002528 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002529
Sunny Goyal2100c782016-08-22 16:00:03 -07002530 if (mPendingActivityResult != null) {
2531 handleActivityResult(mPendingActivityResult.requestCode,
2532 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2533 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002534 }
2535
Sunny Goyala474a9b2017-05-04 16:47:11 -07002536 InstallShortcutReceiver.disableAndFlushInstallQueue(
2537 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002538
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002539 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002540 }
2541
Winson Chunga2413752012-04-03 14:22:34 -07002542 private boolean canRunNewAppsAnimation() {
2543 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002544 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002545 }
2546
Winson Chungc9168342013-06-26 14:54:55 -07002547 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002548 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002549 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2550 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002551 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002552 return bounceAnim;
2553 }
2554
Adam Cohen27772732013-11-11 14:00:56 +00002555 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002556 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002557 }
2558
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002559 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002560 * Add the icons for all apps.
2561 *
2562 * Implementation of the method from LauncherModel.Callbacks.
2563 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002564 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002565 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002566 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002567 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002568 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002569 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002570 return;
2571 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002572
Winson Chungb745afb2015-03-02 11:51:23 -08002573 mAppsView.setApps(apps);
2574 }
Adam Cohen9211d422014-10-07 18:14:53 -07002575 if (mLauncherCallbacks != null) {
2576 mLauncherCallbacks.bindAllApplications(apps);
2577 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578 }
2579
2580 /**
Tony2917a8b2017-07-31 23:29:42 -07002581 * Returns an Executor that will run after the launcher is first drawn (including after the
2582 * initial fade in animation). Returns null if the first draw has already occurred.
2583 */
2584 public @Nullable Executor getPendingExecutor() {
2585 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2586 }
2587
2588 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002589 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2590 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2591 */
2592 @Override
2593 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002594 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002595 }
2596
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002597 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002598 * A package was updated.
2599 *
2600 * Implementation of the method from LauncherModel.Callbacks.
2601 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002602 @Override
2603 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002604 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002605 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002607 }
2608
Sunny Goyal4390ace2014-10-13 11:33:11 -07002609 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002610 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002611 if (mAppsView != null) {
2612 mAppsView.updatePromiseAppProgress(app);
2613 }
2614 }
2615
2616 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002617 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002618 mWorkspace.widgetsRestored(widgets);
2619 }
2620
Joe Onorato9c1289c2009-08-17 11:03:03 -04002621 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002622 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002623 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002624 *
2625 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002626 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002627 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002628 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002629 if (!updated.isEmpty()) {
2630 mWorkspace.updateShortcuts(updated);
2631 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002632 }
2633
2634 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002635 * Update the state of a package, typically related to install state.
2636 *
2637 * Implementation of the method from LauncherModel.Callbacks.
2638 */
Sunny Goyale755d462014-07-22 13:48:29 -07002639 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002640 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002641 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002642 }
2643
2644 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002645 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002646 * in addition to specific applications to remove, the reason being that
2647 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002648 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002649 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002650 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002651 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002652 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002653 mWorkspace.removeItemsByMatcher(matcher);
2654 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002655 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002656
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002657 @Override
2658 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002659 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002660 if (mAppsView != null) {
2661 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002662 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002663 }
Joe Onoratobe386092009-11-17 17:32:16 -08002664
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002665 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002666 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2667 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002668 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2669 if (topView != null) {
2670 topView.onWidgetsBound();
2671 }
2672 }
2673
Tony Wickham86222d22017-03-29 15:30:43 -07002674 /**
2675 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2676 * refreshes the widgets and shortcuts associated with the given package/user
2677 */
2678 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002679 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002680 }
2681
Sunny Goyalc99cb172017-10-19 16:15:09 -07002682 public boolean isRotationEnabled () {
2683 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002684 }
2685
Winson Chung80baf5a2010-08-09 16:03:15 -07002686 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002687 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002688 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002689 @Override
2690 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2691 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002692
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002693 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2694 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002695 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002696 writer.println(prefix + " Homescreen " + i);
2697
2698 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2699 for (int j = 0; j < layout.getChildCount(); j++) {
2700 Object tag = layout.getChildAt(j).getTag();
2701 if (tag != null) {
2702 writer.println(prefix + " " + tag.toString());
2703 }
2704 }
2705 }
2706
2707 writer.println(prefix + " Hotseat");
2708 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002709 for (int j = 0; j < layout.getChildCount(); j++) {
2710 Object tag = layout.getChildAt(j).getTag();
2711 if (tag != null) {
2712 writer.println(prefix + " " + tag.toString());
2713 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002714 }
Sunny Goyala1365452015-10-01 15:46:24 -07002715
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002716 try {
2717 FileLog.flushAll(writer);
2718 } catch (Exception e) {
2719 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002720 }
2721 }
2722
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002723 writer.println(prefix + "Misc:");
2724 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2725 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2726 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2727
2728 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002729
Adam Cohen9211d422014-10-07 18:14:53 -07002730 if (mLauncherCallbacks != null) {
2731 mLauncherCallbacks.dump(prefix, fd, writer, args);
2732 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002733 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002734
Sunny Goyal66b24572016-09-21 15:57:55 -07002735 @Override
2736 @TargetApi(Build.VERSION_CODES.N)
2737 public void onProvideKeyboardShortcuts(
2738 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2739
2740 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002741 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002742 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2743 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2744 }
2745 View currentFocus = getCurrentFocus();
2746 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2747 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2748 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2749 }
Tony18c4aa42017-05-10 21:23:57 -05002750 if (currentFocus.getTag() instanceof ItemInfo
2751 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002752 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2753 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2754 }
2755 if (!shortcutInfos.isEmpty()) {
2756 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2757 }
2758
2759 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2760 }
2761
2762 @Override
2763 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2764 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2765 switch (keyCode) {
2766 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002767 if (isInState(NORMAL)) {
2768 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002769 return true;
2770 }
2771 break;
2772 case KeyEvent.KEYCODE_S: {
2773 View focusedView = getCurrentFocus();
2774 if (focusedView instanceof BubbleTextView
2775 && focusedView.getTag() instanceof ItemInfo
2776 && mAccessibilityDelegate.performAction(focusedView,
2777 (ItemInfo) focusedView.getTag(),
2778 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002779 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002780 return true;
2781 }
2782 break;
2783 }
2784 case KeyEvent.KEYCODE_O:
2785 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2786 return true;
2787 }
2788 break;
2789 }
2790 }
2791 return super.onKeyShortcut(keyCode, event);
2792 }
2793
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002794 public static Launcher getLauncher(Context context) {
2795 if (context instanceof Launcher) {
2796 return (Launcher) context;
2797 }
2798 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2799 }
2800
Sunny Goyal5a81c382017-03-20 15:08:06 -07002801 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002802
2803 @Override
2804 public void onSharedPreferenceChanged(
2805 SharedPreferences sharedPreferences, String key) {
2806 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002807 // Recreate the activity so that it initializes the rotation preference again.
2808 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002809 }
2810 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002811 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002812
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002813 @Override
2814 public void onActionModeStarted(ActionMode mode) {
2815 super.onActionModeStarted(mode);
2816 mCurrentActionMode = mode;
2817 }
2818
2819 @Override
2820 public void onActionModeFinished(ActionMode mode) {
2821 super.onActionModeFinished(mode);
2822 mCurrentActionMode = null;
2823 }
2824
2825 public boolean finishAutoCancelActionMode() {
2826 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2827 mCurrentActionMode.finish();
2828 return true;
2829 }
2830 return false;
2831 }
2832
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002833 /**
2834 * Callback for listening for onResume
2835 */
2836 public interface OnResumeCallback {
2837
2838 void onLauncherResume();
2839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840}