blob: 553d8d0e578e756666fca05b0d5f4c026e480786 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
33import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
34
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070053import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040054import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070056import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070057import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070058import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070059import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070065import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020066import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080069import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070070import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070071import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070072import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070075import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070077import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070078import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.HapticFeedbackConstants;
81import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070082import android.view.KeyboardShortcutGroup;
83import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080084import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080088import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070097import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070098import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700100import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700103import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700108import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700109import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800110import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700111import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700114import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700115import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700116import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700120import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700121import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800122import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800123import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700124import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700125import com.android.launcher3.states.AllAppsState;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800126import com.android.launcher3.states.InternalStateHandler;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800127import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530128import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
129import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
130import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700131import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700132import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700133import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700134import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700135import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700136import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800137import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700138import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700139import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700140import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700141import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700142import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700143import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700144import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700145import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800146import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700147import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800148import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700149import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700150import com.android.launcher3.widget.WidgetListRowEntry;
151import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700152import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700153
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700154import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700155import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700156import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700157import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700158import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700159import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800160import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700161import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163/**
164 * Default launcher application.
165 */
Sunny Goyal27835952017-01-13 12:15:53 -0800166public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700167 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
168 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700169 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700170 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700171 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chunga2413752012-04-03 14:22:34 -0700173 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700179 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Michael Jurka8b805b12012-04-18 14:23:14 -0700181 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700182 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
183 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700184
Jon Mirandac476d6e2017-05-04 16:30:01 -0700185 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700186
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700187 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
188
Mathew Inwood876a8462013-06-14 14:12:41 +0100189 /**
190 * IntentStarter uses request codes starting with this. This must be greater than all activity
191 * request codes used internally.
192 */
193 protected static final int REQUEST_LAST = 100;
194
Winson Chung2672ff92012-05-04 16:22:30 -0700195 // The Intent extra that defines whether to ignore the launch animation
196 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400197 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 // Type: int
200 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700201 // Type: int
202 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700203 // Type: PendingRequestArgs
204 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
205 // Type: ActivityResultInfo
206 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700207 // Type: SparseArray<Parcelable>
208 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700210 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
219 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700225
Sunny Goyalffe83f12014-08-14 17:39:34 -0700226 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700229 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700230
Sunny Goyal316490e2015-06-02 09:38:28 -0700231 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700232
Michael Jurka838a4ca2011-02-07 13:33:06 -0800233 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700234
Sunny Goyal47328fd2016-05-25 18:56:41 -0700235 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700236
237 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700238 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700239 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700240
Winson Chung8ae41982017-11-10 11:42:13 -0800241 // UI and state for the overview panel
242 private ViewGroup mOverviewPanel;
243
Jon Miranda6bed3502017-09-19 14:43:17 -0700244 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
245 // that results in widgets being inflated in the wrong orientation.
246 private int mOrientation;
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private SpannableStringBuilder mDefaultKeySsb = null;
249
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800252 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700253 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700255 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700256 private OnResumeCallback mOnResumeCallback;
257
Sunny Goyal527c7d32015-08-28 15:19:36 -0700258 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700259
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800261 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800262 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700263 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700264 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Jon Miranda2d89ea82017-05-04 11:47:53 -0700266 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700267 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700268
Tony Wickham010d2552017-01-20 08:15:28 -0800269 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700270
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700271 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700272
Winson Chung46353de2012-02-16 14:05:10 -0800273 // We only want to get the SharedPreferences once since it does an FS stat each time we get
274 // it from the context.
275 private SharedPreferences mSharedPrefs;
276
Sunny Goyal2100c782016-08-22 16:00:03 -0700277 // Activity result which needs to be processed after workspace has loaded.
278 private ActivityResultInfo mPendingActivityResult;
279 /**
280 * Holds extra information required to handle a result from an external call, like
281 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
282 */
283 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800284
Jon Miranda379198e2016-09-23 08:54:40 -0700285 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700286
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700287 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700288 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700289 private boolean mAppLaunchSuccess;
290
Sunny Goyal745bad92016-05-02 10:54:12 -0700291 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 @Override
294 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700295 if (DEBUG_STRICT_MODE) {
296 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
297 .detectDiskReads()
298 .detectDiskWrites()
299 .detectNetwork() // or .detectAll() for all detectable problems
300 .penaltyLog()
301 .build());
302 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
303 .detectLeakedSqlLiteObjects()
304 .detectLeakedClosableObjects()
305 .penaltyLog()
306 .penaltyDeath()
307 .build());
308 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700309 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700310
Adam Cohen9211d422014-10-07 18:14:53 -0700311 if (mLauncherCallbacks != null) {
312 mLauncherCallbacks.preOnCreate();
313 }
314
Mario Bertschler27288382017-05-24 15:35:09 -0700315 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
316 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700317 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700320 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400321
Sunny Goyal87f784c2017-01-11 10:48:34 -0800322 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700323
Adam Cohen2e6da152015-05-06 11:42:25 -0700324 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800325 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700326 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700327 Display display = getWindowManager().getDefaultDisplay();
328 Point mwSize = new Point();
329 display.getSize(mwSize);
330 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
331 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700332
Jon Miranda6bed3502017-09-19 14:43:17 -0700333 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700334 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700335 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800336 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800337 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800338 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700339 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700340
Joe Onorato41a12d22009-10-31 18:30:00 -0400341 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700342 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800343 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700344
Sunny Goyalffe83f12014-08-14 17:39:34 -0700345 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700346
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700347 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700348 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700349
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700350 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
351 // this also ensures that any synchronous binding below doesn't re-trigger another
352 // LauncherModel load.
353 mPaused = false;
354
Sunny Goyal60820d72017-05-09 12:40:11 -0700355 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 setupViews();
Winson1f064272016-07-18 17:18:02 -0700358 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359
Tony Wickham010d2552017-01-20 08:15:28 -0800360 mPopupDataProvider = new PopupDataProvider(this);
361
Sunny Goyalfe770c92016-09-27 14:38:58 -0700362 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363
Winson Chungb63b44c2017-11-10 17:54:44 -0800364 InternalStateHandler.handleCreate(this, getIntent());
365
Sunny Goyal2100c782016-08-22 16:00:03 -0700366 // We only load the page synchronously if the user rotates (or triggers a
367 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700368 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
369 if (savedInstanceState != null) {
370 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
371 }
372 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700373 // If we are not binding synchronously, show a fade in animation when
374 // the first page bind completes.
375 mDragLayer.setAlpha(0);
376 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700377 // Pages bound synchronously.
378 mWorkspace.setCurrentPage(currentScreen);
379
Sunny Goyal2100c782016-08-22 16:00:03 -0700380 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
382
383 // For handling default keys
384 mDefaultKeySsb = new SpannableStringBuilder();
385 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800386
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800387 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700388 // In case we are on a device with locked rotation, we should look at preferences to check
389 // if the user has specifically allowed rotation.
390 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700391 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700392 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
393 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700394 }
395
Sunny Goyal5a81c382017-03-20 15:08:06 -0700396 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
397 // Temporarily enable the rotation
398 mRotationEnabled = true;
399 }
400
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700401 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
402 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700403 setRequestedOrientation(mRotationEnabled
404 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700405
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800406 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700407
Jon Miranda7fda2852017-07-19 16:07:01 -0700408 // Listen for broadcasts
409 IntentFilter filter = new IntentFilter();
410 filter.addAction(Intent.ACTION_SCREEN_OFF);
411 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
412 registerReceiver(mReceiver, filter);
413 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700414
Sunny Goyal8392c822017-06-20 10:03:56 -0700415 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
416 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700417
418 if (mLauncherCallbacks != null) {
419 mLauncherCallbacks.onCreate(savedInstanceState);
420 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700421
422 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700423 }
424
Sunny Goyal7779d622015-06-11 16:18:39 -0700425 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700426 public void onThemeChanged() {
427 recreate();
428 }
429
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700430 public LauncherStateManager getStateManager() {
431 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700432 }
433
Mario Bertschlera6936942017-05-31 14:48:19 -0700434 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700435 if (isDark) {
436 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700437 } else if (supportsDarkText) {
438 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700439 }
440 }
441
442 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700443 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800444 return mLauncherView.findViewById(id);
445 }
446
447 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700448 public void onAppWidgetHostReset() {
449 if (mAppWidgetHost != null) {
450 mAppWidgetHost.startListening();
451 }
452 }
453
Adam Cohen9211d422014-10-07 18:14:53 -0700454 private LauncherCallbacks mLauncherCallbacks;
455
Winson1f064272016-07-18 17:18:02 -0700456 public void onInsetsChanged(Rect insets) {
457 mDeviceProfile.updateInsets(insets);
458 mDeviceProfile.layout(this, true /* notifyListeners */);
459 }
460
Sunny Goyal32554d12015-12-03 15:31:25 -0800461 /**
462 * Call this after onCreate to set or clear overlay.
463 */
464 public void setLauncherOverlay(LauncherOverlay overlay) {
465 if (overlay != null) {
466 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
467 }
468 mWorkspace.setLauncherOverlay(overlay);
469 }
470
Adam Cohen9211d422014-10-07 18:14:53 -0700471 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
472 mLauncherCallbacks = callbacks;
473 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700474 }
475
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700476 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700477 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700478 if (mLauncherCallbacks != null) {
479 mLauncherCallbacks.onLauncherProviderChange();
480 }
481 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700482
Hyunyoung Song3f471442015-04-08 19:01:34 -0700483 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700484 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
485 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700486 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700487 }
488
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000489 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700490 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
491 // This cast is safe as long as the id < 0x00FFFFFF
492 // Since we jail all the dynamically generated views, there should be no clashes
493 // with any other views.
494 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000495 }
496
Tony Wickham010d2552017-01-20 08:15:28 -0800497 public PopupDataProvider getPopupDataProvider() {
498 return mPopupDataProvider;
499 }
500
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000501 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700502 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
503 * a configuration step, this allows the proper animations to run after other transitions.
504 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 private long completeAdd(
506 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
507 long screenId = info.screenId;
508 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700509 // When the screen id represents an actual screen (as opposed to a rank) we make sure
510 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700512 }
Adam Cohendb364c32014-05-20 14:23:40 -0700513
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700516 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700520 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700521 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700523 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700524 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 int widgetId = appWidgetId;
526 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700527 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700529 // Since the view was just bound, also launch the configure activity if needed
530 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
531 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800532 if (provider != null) {
533 new WidgetAddFlowHandler(provider)
534 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700535 }
536 }
537 break;
538 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700540
Adam Cohendb364c32014-05-20 14:23:40 -0700541 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800542 }
543
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800544 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700545 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 if (isWorkspaceLoading()) {
547 // process the result once the workspace has loaded.
548 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
549 return;
550 }
551 mPendingActivityResult = null;
552
Winson Chunge341d302013-08-16 14:31:00 -0700553 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 final PendingRequestArgs requestArgs = mPendingRequestArgs;
555 setWaitingForResult(null);
556 if (requestArgs == null) {
557 return;
558 }
559
560 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700561
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 Runnable exitSpringLoaded = new Runnable() {
563 @Override
564 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700565 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700566 }
567 };
568
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700570 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
573 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700575 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 addAppWidgetImpl(
579 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800580 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700581 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700582 }
583 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200584 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700585 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700586 // User could have free-scrolled between pages before picking a wallpaper; make sure
587 // we move to the closest one now.
588 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700589 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200590 }
591 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700592 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200593
Adam Cohened66b2b2012-01-23 17:28:51 -0800594 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700595 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700596
Adam Cohened66b2b2012-01-23 17:28:51 -0800597 // We have special handling for widgets
598 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800599 final int appWidgetId;
600 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
601 : -1;
602 if (widgetId < 0) {
603 appWidgetId = pendingAddWidgetId;
604 } else {
605 appWidgetId = widgetId;
606 }
607
Adam Cohenad4e15c2013-10-17 16:21:35 -0700608 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800609 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700610 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
611 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700612 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700614 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 @Override
616 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700617 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700618 }
619 };
Adam Cohendb364c32014-05-20 14:23:40 -0700620
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
622 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
623 } else {
624 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
625 // When the screen id represents an actual screen (as opposed to a rank)
626 // we make sure that the drop page actually exists.
627 requestArgs.screenId =
628 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700629 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 final CellLayout dropLayout =
631 mWorkspace.getScreenWithId(requestArgs.screenId);
632
633 dropLayout.setDropPending(true);
634 final Runnable onComplete = new Runnable() {
635 @Override
636 public void run() {
637 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
638 dropLayout.setDropPending(false);
639 }
640 };
641 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
642 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700643 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 return;
645 }
646
Sunny Goyald478c832016-04-01 12:04:16 -0700647 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
648 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700649 if (resultCode == RESULT_OK) {
650 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700652 }
653 // Leave the widget in the pending state if the user canceled the configure.
654 return;
655 }
656
Sunny Goyalaad90582015-07-09 14:22:50 -0700657 if (requestCode == REQUEST_CREATE_SHORTCUT) {
658 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
660 completeAdd(requestCode, data, -1, requestArgs);
661 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
662 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
663
Sunny Goyalaad90582015-07-09 14:22:50 -0700664 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700665 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
666 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800670 }
671
672 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700673 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800674 final int requestCode, final int resultCode, final Intent data) {
675 handleActivityResult(requestCode, resultCode, data);
676 if (mLauncherCallbacks != null) {
677 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
678 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 }
680
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700681 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700682 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600683 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 PendingRequestArgs pendingArgs = mPendingRequestArgs;
685 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
686 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
687 setWaitingForResult(null);
688
Sunny Goyal28c6b962015-10-12 11:42:05 -0700689 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700693 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700694 Intent intent = pendingArgs.getPendingIntent();
695
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 if (grantResults.length > 0
697 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700698 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 } else {
700 // TODO: Show a snack bar with link to settings
701 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700702 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 }
704 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600705 if (mLauncherCallbacks != null) {
706 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
707 grantResults);
708 }
709 }
710
Adam Cohendb364c32014-05-20 14:23:40 -0700711 /**
712 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
713 *
714 * @param screenId the screen id to check
715 * @return the new screen, or screenId if it exists
716 */
717 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800718 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700719 if (dropLayout == null) {
720 // it's possible that the add screen was removed because it was
721 // empty and a re-bind occurred
722 mWorkspace.addExtraEmptyScreen();
723 return mWorkspace.commitExtraEmptyScreen();
724 } else {
725 return screenId;
726 }
727 }
728
Sunny Goyal2100c782016-08-22 16:00:03 -0700729 @Thunk void completeTwoStageWidgetDrop(
730 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
731 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 Runnable onCompleteRunnable = null;
733 int animationType = 0;
734
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 if (resultCode == RESULT_OK) {
737 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
738 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800739 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 onCompleteRunnable = new Runnable() {
742 @Override
743 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700744 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700745 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 }
747 };
748 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700753 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
755 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700757 // The animated view may be null in the case of a rotation during widget configuration
758 onCompleteRunnable.run();
759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
762 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700763 protected void onStop() {
764 super.onStop();
765 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700766
767 if (mLauncherCallbacks != null) {
768 mLauncherCallbacks.onStop();
769 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800770 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800771
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700772 if (!mAppLaunchSuccess) {
773 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700774 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700775 }
Tony Wickham010d2552017-01-20 08:15:28 -0800776 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700777 }
778
779 @Override
780 protected void onStart() {
781 super.onStart();
782 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700783
784 if (mLauncherCallbacks != null) {
785 mLauncherCallbacks.onStart();
786 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800787 mAppWidgetHost.setListenIfResumed(true);
Tony Wickham010d2552017-01-20 08:15:28 -0800788
789 if (!isWorkspaceLoading()) {
790 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
791 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700792
Jon Miranda7fda2852017-07-19 16:07:01 -0700793 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700794 if (mScrimAnimator != null) {
795 mScrimAnimator.cancel();
796 }
797 mDragLayer.getBackground().setAlpha(0);
798 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
799 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
800 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
801 @Override
802 public void onAnimationEnd(Animator animation) {
803 mScrimAnimator = null;
804 }
805 });
806 mScrimAnimator.setDuration(600);
807 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
808 mScrimAnimator.start();
809 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700810 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700811 }
812
813 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700815 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700817 TraceHelper.partitionSection("ON_RESUME", "superCall");
818
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700819 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700820 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800821 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700822 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700823 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700824 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700825 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700827 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200828 // We might have postponed some bind calls until onResume (see waitUntilResume) --
829 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700830 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
831 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200832 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700833 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200834 }
Winson Chunge4e50662012-01-23 14:45:13 -0800835
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700836 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700837 // Process any items that were added while Launcher was away.
838 InstallShortcutReceiver.disableAndFlushInstallQueue(
839 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700840
Sunny Goyala474a9b2017-05-04 16:47:11 -0700841 // Refresh shortcuts if the permission changed.
842 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700843
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700844 if (shouldShowDiscoveryBounce()) {
845 mAllAppsController.showDiscoveryBounce();
846 }
Adam Cohen9211d422014-10-07 18:14:53 -0700847 if (mLauncherCallbacks != null) {
848 mLauncherCallbacks.onResume();
849 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800850
Sunny Goyala502aa32017-10-02 16:04:06 -0700851 clearTypedText();
852
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700853 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700854 }
855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700857 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700858 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700859 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700860
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700861 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700862 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800863 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700864 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700865
Adam Cohen9211d422014-10-07 18:14:53 -0700866 if (mLauncherCallbacks != null) {
867 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700868 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700869 }
870
Adam Cohenc2d6e892014-10-16 09:49:24 -0700871 public interface LauncherOverlay {
872
873 /**
874 * Touch interaction leading to overscroll has begun
875 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700876 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700877
878 /**
879 * Touch interaction related to overscroll has ended
880 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700881 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700882
883 /**
884 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
885 * screen (or in the case of RTL, the rightmost screen).
886 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700887 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888
889 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800890 * Called when the launcher is ready to use the overlay
891 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700892 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700893 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894 }
895
896 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700897 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 }
899
900 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
901
Sunny Goyalc86df472016-02-25 09:19:38 -0800902 public void onScrollChanged(float progress) {
903 if (mWorkspace != null) {
904 mWorkspace.onOverlayScrollChanged(progress);
905 }
906 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700907 }
908
Sunny Goyal16764582017-11-06 16:00:34 -0800909 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700910 if (mLauncherCallbacks != null) {
911 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700912 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800913 // On O and above we there is always some setting present settings (add icon to
914 // home screen or icon badging). On earlier APIs we will have the allow rotation
915 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700916 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700917 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800918 }
919
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700920 @Override
921 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700923 if (mModel.isCurrentCallbacks(this)) {
924 mModel.stopLoader();
925 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700926 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
927
Romain Guy13c2e7b2010-03-10 19:45:00 -0800928 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700929 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700930
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800931 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800932 @Override
933 public void onWindowFocusChanged(boolean hasFocus) {
934 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700935 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700936
937 if (mLauncherCallbacks != null) {
938 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
939 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800940 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 private boolean acceptFilter() {
943 final InputMethodManager inputManager = (InputMethodManager)
944 getSystemService(Context.INPUT_METHOD_SERVICE);
945 return !inputManager.isFullscreenMode();
946 }
947
948 @Override
949 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700950 final int uniChar = event.getUnicodeChar();
951 final boolean handled = super.onKeyDown(keyCode, event);
952 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
953 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
955 keyCode, event);
956 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700957 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700958 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700959 // showSearchDialog()
960 // If there are multiple keystrokes before the search dialog takes focus,
961 // onSearchRequested() will be called for every keystroke,
962 // but it is idempotent, so it's fine.
963 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965 }
966
Joe Onorato8a9625e2010-01-28 15:55:35 -0800967 // Eat the long press event so the keyboard doesn't come up.
968 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
969 return true;
970 }
971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 return handled;
973 }
974
Karl Rosaen138a0412009-04-23 19:00:21 -0700975 private String getTypedText() {
976 return mDefaultKeySsb.toString();
977 }
978
Sunny Goyal6f28e712016-09-13 14:19:29 -0700979 @Override
980 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700981 mDefaultKeySsb.clear();
982 mDefaultKeySsb.clearSpans();
983 Selection.setSelection(mDefaultKeySsb, 0);
984 }
985
Sunny Goyalf9403d92017-10-18 10:55:56 -0700986 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700987 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700988 }
989
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 /**
991 * Restores the previous state, if it exists.
992 *
993 * @param savedState The previous state.
994 */
995 private void restoreState(Bundle savedState) {
996 if (savedState == null) {
997 return;
998 }
999
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001000 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001001 LauncherState[] stateValues = LauncherState.values();
1002 LauncherState state = stateValues[stateOrdinal];
1003 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001004 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001005 }
1006
Sunny Goyal2100c782016-08-22 16:00:03 -07001007 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1008 if (requestArgs != null) {
1009 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001011
1012 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001013
1014 SparseArray<Parcelable> widgetsState =
1015 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1016 if (widgetsState != null) {
1017 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 }
1020
1021 /**
1022 * Finds all the views we need and configure them properly.
1023 */
1024 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001025 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001026 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001027 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001028 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001029 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001030
Sunny Goyal784f9c32016-03-23 16:56:54 -07001031 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1032 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1033 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034
Winson Chung4c98d922011-05-31 16:50:48 -07001035 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001036 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001037
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 // Setup the hotseat
1039 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1040 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001041 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001042 }
1043
Winson Chung4c98d922011-05-31 16:50:48 -07001044 // Setup the workspace
1045 mWorkspace.setHapticFeedbackEnabled(false);
1046 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001047 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001048 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1049 // default state, otherwise we will update to the wrong offsets in RTL
1050 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001051 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001052 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001053
Tony Wickham34d2c912015-09-11 09:27:58 -07001054 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001055 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001056
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001057 // Setup Apps
1058 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001059
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001061 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001062 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001063
Hyunyoung Songd725f642017-08-17 22:26:35 -07001064 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 }
1066
1067 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001068 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001069 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001070 */
1071 public void setAllAppsButton(View allAppsButton) {
1072 mAllAppsButton = allAppsButton;
1073 }
1074
Anjali Koppal5ad44842014-03-10 20:34:39 -07001075 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 * Creates a view representing a shortcut.
1077 *
1078 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001080 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001081 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 }
1083
1084 /**
1085 * Creates a view representing a shortcut inflated from the specified resource.
1086 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 * @param parent The group the shortcut belongs to.
1088 * @param info The data structure describing the shortcut.
1089 *
1090 * @return A View inflated from layoutResId.
1091 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001092 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001093 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1094 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001095 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001097 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 return favorite;
1099 }
1100
1101 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001102 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 *
1104 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001106 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001107 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001108 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1109 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001110 return;
1111 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001112
Jon Mirandac476d6e2017-05-04 16:30:01 -07001113 int[] cellXY = mTmpAddItemCellCoordinates;
1114 CellLayout layout = getCellLayout(container, screenId);
1115
Sunny Goyal782f0c92017-01-19 10:27:54 -08001116 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001117 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001118 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1119 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001120 }
1121
1122 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001123 // Legacy shortcuts are only supported for primary profile.
1124 info = Process.myUserHandle().equals(args.user)
1125 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001126
Sunny Goyalb57645f2017-03-20 13:57:28 -07001127 if (info == null) {
1128 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1129 return;
1130 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001131 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001132 // The app is trying to add a shortcut without sufficient permissions
1133 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1134 return;
1135 }
1136 }
1137
Jon Mirandac476d6e2017-05-04 16:30:01 -07001138 if (container < 0) {
1139 // Adding a shortcut to the Workspace.
1140 final View view = createShortcut(info);
1141 boolean foundCellSpan = false;
1142 // First we check if we already know the exact location where we want to add this item.
1143 if (cellX >= 0 && cellY >= 0) {
1144 cellXY[0] = cellX;
1145 cellXY[1] = cellY;
1146 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001147
Jon Mirandac476d6e2017-05-04 16:30:01 -07001148 // If appropriate, either create a folder or add to an existing folder
1149 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001150 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001151 return;
1152 }
1153 DragObject dragObject = new DragObject();
1154 dragObject.dragInfo = info;
1155 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1156 true)) {
1157 return;
1158 }
1159 } else {
1160 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1161 }
1162
1163 if (!foundCellSpan) {
1164 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001165 return;
1166 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001167
1168 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1169 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001170 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001171 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001172 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001173 if (folderIcon != null) {
1174 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1175 folderInfo.add(info, args.rank, false);
1176 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001177 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001178 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
1181
Sunny Goyale29897f52017-07-20 10:09:42 -07001182 public FolderIcon findFolderIcon(final long folderIconId) {
1183 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1184 @Override
1185 public boolean evaluate(ItemInfo info, View view) {
1186 return info != null && info.id == folderIconId;
1187 }
1188 });
1189 }
1190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001194 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001196 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001197 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1198
Adam Cohened66b2b2012-01-23 17:28:51 -08001199 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001200 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001201 }
Romain Guycbb89e42009-06-08 15:52:54 -07001202
Adam Cohen59400422014-03-05 18:07:04 -08001203 LauncherAppWidgetInfo launcherInfo;
1204 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001205 launcherInfo.spanX = itemInfo.spanX;
1206 launcherInfo.spanY = itemInfo.spanY;
1207 launcherInfo.minSpanX = itemInfo.minSpanX;
1208 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001209 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001211 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001212 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
Sunny Goyal2100c782016-08-22 16:00:03 -07001214 if (hostView == null) {
1215 // Perform actual inflation because we're live
1216 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001218 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301219 prepareAppWidget(hostView, launcherInfo);
1220 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
Romain Guycbb89e42009-06-08 15:52:54 -07001222
Sunny Goyald5462aa2016-11-22 16:52:39 +05301223 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001224 hostView.setTag(item);
1225 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001226 hostView.setFocusable(true);
1227 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001228 }
1229
Adam Cohended9f8d2010-11-03 13:25:16 -07001230 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1231 @Override
1232 public void onReceive(Context context, Intent intent) {
1233 final String action = intent.getAction();
1234 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001235 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001236 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001237 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001238 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001239 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001240 mShouldFadeInScrim = true;
1241 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1242 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1243 // the user unlocked and the Launcher is not in the foreground.
1244 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 }
1246 }
1247 };
1248
Tony Wickham010d2552017-01-20 08:15:28 -08001249 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1250 Runnable r = new Runnable() {
1251 @Override
1252 public void run() {
1253 mWorkspace.updateIconBadges(updatedBadges);
1254 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001255
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001256 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1257 if (popup instanceof PopupContainerWithArrow) {
1258 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001259 }
Tony Wickham010d2552017-01-20 08:15:28 -08001260 }
1261 };
1262 if (!waitUntilResume(r)) {
1263 r.run();
1264 }
1265 }
1266
Adam Cohended9f8d2010-11-03 13:25:16 -07001267 @Override
1268 public void onAttachedToWindow() {
1269 super.onAttachedToWindow();
1270
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001271 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001272 if (mLauncherCallbacks != null) {
1273 mLauncherCallbacks.onAttachedToWindow();
1274 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001275 }
1276
1277 @Override
1278 public void onDetachedFromWindow() {
1279 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001280
1281 if (mLauncherCallbacks != null) {
1282 mLauncherCallbacks.onDetachedFromWindow();
1283 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 }
1285
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001286 public AllAppsTransitionController getAllAppsController() {
1287 return mAllAppsController;
1288 }
1289
Winson Chunga6945242014-01-08 14:04:34 -08001290 public DragLayer getDragLayer() {
1291 return mDragLayer;
1292 }
1293
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001294 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001295 return mAppsView;
1296 }
1297
Winson Chunga6945242014-01-08 14:04:34 -08001298 public Workspace getWorkspace() {
1299 return mWorkspace;
1300 }
1301
1302 public Hotseat getHotseat() {
1303 return mHotseat;
1304 }
1305
Winson Chung8ae41982017-11-10 11:42:13 -08001306 public <T extends ViewGroup> T getOverviewPanel() {
1307 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001308 }
1309
Sunny Goyal47328fd2016-05-25 18:56:41 -07001310 public DropTargetBar getDropTargetBar() {
1311 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001312 }
1313
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001314 public LauncherAppWidgetHost getAppWidgetHost() {
1315 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
Romain Guycbb89e42009-06-08 15:52:54 -07001317
Winson Chunga9abd0e2010-10-27 17:18:37 -07001318 public LauncherModel getModel() {
1319 return mModel;
1320 }
1321
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001322 public ModelWriter getModelWriter() {
1323 return mModelWriter;
1324 }
1325
Adam Cohen79d90c52016-04-22 13:29:20 -07001326 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001327 return mSharedPrefs;
1328 }
1329
Jon Miranda6bed3502017-09-19 14:43:17 -07001330 public int getOrientation() { return mOrientation; }
1331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 @Override
1333 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001334 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 super.onNewIntent(intent);
1336
Winson15f8b172015-08-19 11:02:39 -07001337 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1338 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1339 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001340
1341 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001342 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001343 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001344 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1345 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001346 if (mWorkspace == null) {
1347 // Can be cases where mWorkspace is null, this prevents a NPE
1348 return;
1349 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001350
1351 // Note: There should be at most one log per method call. This is enforced implicitly
1352 // by using if-else statements.
1353 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001354 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001355 if (topOpenView != null) {
1356 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001357 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001358 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001359 target.pageIndex = mWorkspace.getCurrentPage();
1360 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001361 }
1362
1363 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001364 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001365 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001366
1367 final View v = getWindow().peekDecorView();
1368 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001369 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001370 }
1371
Winson Chungb745afb2015-03-02 11:51:23 -08001372 // Reset the apps view
1373 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001374 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001375 }
1376
Adam Cohen9211d422014-10-07 18:14:53 -07001377 if (mLauncherCallbacks != null) {
1378 mLauncherCallbacks.onHomeIntent();
1379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001381 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001382
Adam Cohen9211d422014-10-07 18:14:53 -07001383 if (mLauncherCallbacks != null) {
1384 mLauncherCallbacks.onNewIntent(intent);
1385 }
Winson15f8b172015-08-19 11:02:39 -07001386
1387 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1388 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1389 // animation.
1390 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001391 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001392
Winson15f8b172015-08-19 11:02:39 -07001393 mWorkspace.post(new Runnable() {
1394 @Override
1395 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001396 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001397 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001398 }
Winson15f8b172015-08-19 11:02:39 -07001399 }
1400 });
1401 }
1402 }
Winson Chungb63b44c2017-11-10 17:54:44 -08001403 InternalStateHandler.handleNewIntent(this, intent, alreadyOnHome);
Winson15f8b172015-08-19 11:02:39 -07001404
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001405 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001406 }
1407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001409 public void onRestoreInstanceState(Bundle state) {
1410 super.onRestoreInstanceState(state);
1411 for (int page: mSynchronouslyBoundPages) {
1412 mWorkspace.restoreInstanceStateForChild(page);
1413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
1416 @Override
1417 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001418 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001419 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001420
Adam Cohen21cd0022013-10-09 18:57:02 -07001421 }
Sunny Goyalea609262017-10-25 15:47:38 -07001422 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001423
1424
1425 AbstractFloatingView widgets = AbstractFloatingView
1426 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1427 if (widgets != null) {
1428 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1429 widgets.saveHierarchyState(widgetsState);
1430 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1431 } else {
1432 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1433 }
1434
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001435 // We close any open folders and shortcut containers since they will not be re-opened,
1436 // and we need to make sure this state is reflected.
1437 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001438
Sunny Goyal2100c782016-08-22 16:00:03 -07001439 if (mPendingRequestArgs != null) {
1440 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1441 }
1442 if (mPendingActivityResult != null) {
1443 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001446 super.onSaveInstanceState(outState);
1447
Adam Cohen9211d422014-10-07 18:14:53 -07001448 if (mLauncherCallbacks != null) {
1449 mLauncherCallbacks.onSaveInstanceState(outState);
1450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452
1453 @Override
1454 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001456
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001457 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001458 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001459
Winson Chungcd2b0142011-06-08 16:02:26 -07001460 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001461 // It's possible to receive onDestroy after a new Launcher activity has
1462 // been created. In this case, don't interfere with the new Launcher.
1463 if (mModel.isCurrentCallbacks(this)) {
1464 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001465 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001466 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001467
Sunny Goyal745bad92016-05-02 10:54:12 -07001468 if (mRotationPrefChangeHandler != null) {
1469 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1470 }
1471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001473 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001475 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001477 mAppWidgetHost = null;
1478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001480 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1481
Michael Jurka2ecf9952012-06-18 12:52:28 -07001482 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001483
Tony2917a8b2017-07-31 23:29:42 -07001484 clearPendingBinds();
1485
Adam Cohen9211d422014-10-07 18:14:53 -07001486 if (mLauncherCallbacks != null) {
1487 mLauncherCallbacks.onDestroy();
1488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
Sunny Goyalae502842016-06-17 08:43:56 -07001491 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1492 return mAccessibilityDelegate;
1493 }
1494
Adam Cohena9cf38f2011-05-02 15:36:58 -07001495 public DragController getDragController() {
1496 return mDragController;
1497 }
1498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001500 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001501 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001502 }
1503
1504 @Override
1505 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1506 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001507 try {
1508 super.startIntentSenderForResult(intent, requestCode,
1509 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1510 } catch (IntentSender.SendIntentException e) {
1511 throw new ActivityNotFoundException();
1512 }
1513 }
1514
Winson Chung70d72102011-08-12 11:24:35 -07001515 /**
1516 * Indicates that we want global search for this activity by setting the globalSearch
1517 * argument for {@link #startSearch} to true.
1518 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001520 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001522
Karl Rosaen138a0412009-04-23 19:00:21 -07001523 if (initialQuery == null) {
1524 // Use any text typed in the launcher as the initial query
1525 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001526 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 if (appSearchData == null) {
1528 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001529 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001531
Sunny Goyal6f28e712016-09-13 14:19:29 -07001532 if (mLauncherCallbacks == null ||
1533 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1534 // Starting search from the callbacks failed. Start the default global search.
1535 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001536 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001537
1538 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001539 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001540 }
1541
Ian Parkinson047036e2014-09-01 15:40:53 +01001542 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001543 * Starts the global search activity. This code is a copied from SearchManager
1544 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001545 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001546 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001547 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001548 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1549 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1550 if (globalSearchActivity == null) {
1551 Log.w(TAG, "No global search activity found.");
1552 return;
1553 }
1554 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1555 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1556 intent.setComponent(globalSearchActivity);
1557 // Make sure that we have a Bundle to put source in
1558 if (appSearchData == null) {
1559 appSearchData = new Bundle();
1560 } else {
1561 appSearchData = new Bundle(appSearchData);
1562 }
Adam Cohen9211d422014-10-07 18:14:53 -07001563 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001564 if (!appSearchData.containsKey("source")) {
1565 appSearchData.putString("source", getPackageName());
1566 }
1567 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1568 if (!TextUtils.isEmpty(initialQuery)) {
1569 intent.putExtra(SearchManager.QUERY, initialQuery);
1570 }
1571 if (selectInitialQuery) {
1572 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1573 }
1574 intent.setSourceBounds(sourceBounds);
1575 try {
1576 startActivity(intent);
1577 } catch (ActivityNotFoundException ex) {
1578 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581
Winson Chung70d72102011-08-12 11:24:35 -07001582 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001583 public boolean onPrepareOptionsMenu(Menu menu) {
1584 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001585 if (mLauncherCallbacks != null) {
1586 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1587 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001588 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001591 @Override
1592 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001593 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001594 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001595 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001596 }
1597
Joe Onorato9c1289c2009-08-17 11:03:03 -04001598 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001599 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001600 }
1601
Adam Cohen517a7f52014-03-01 12:12:59 -08001602 public boolean isWorkspaceLoading() {
1603 return mWorkspaceLoading;
1604 }
1605
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001606 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001607 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001608 }
1609
Sunny Goyal04a324a2017-01-20 21:08:59 -08001610 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001611 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001612 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001613
Sunny Goyal2100c782016-08-22 16:00:03 -07001614 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001615 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001616 if (LOGD) {
1617 Log.d(TAG, "Adding widget from drop");
1618 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001619 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001620 }
1621
Sunny Goyal2100c782016-08-22 16:00:03 -07001622 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001623 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1624 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1625 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001626
Adam Cohenad4e15c2013-10-17 16:21:35 -07001627 Runnable onComplete = new Runnable() {
1628 @Override
1629 public void run() {
1630 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001631 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001632 }
1633 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001634 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001635 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 }
1637 }
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001639 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1640 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001641 info.container = container;
1642 info.screenId = screenId;
1643 if (cell != null) {
1644 info.cellX = cell[0];
1645 info.cellY = cell[1];
1646 }
1647 info.spanX = spanX;
1648 info.spanY = spanY;
1649
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001650 switch (info.itemType) {
1651 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1652 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001653 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001654 break;
1655 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001656 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001657 break;
1658 default:
1659 throw new IllegalStateException("Unknown item type: " + info.itemType);
1660 }
1661 }
1662
Adam Cohenfbba09b2011-07-18 21:43:05 -07001663 /**
1664 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001665 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001666 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001667 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1668 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001669 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1670 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1671 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001672 }
1673
Adam Cohenfbba09b2011-07-18 21:43:05 -07001674 /**
1675 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001676 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001677 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001678 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001679 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001680 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001681 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001682 // In the case where we've prebound the widget, we remove it from the DragLayer
1683 if (LOGD) {
1684 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1685 }
1686 getDragLayer().removeView(hostView);
1687
Adam Cohened66b2b2012-01-23 17:28:51 -08001688 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001689 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001690
1691 // Clear the boundWidget so that it doesn't get destroyed.
1692 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001693 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001694 // In this case, we either need to start an activity to get permission to bind
1695 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001696 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1697 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1698 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1699 this, info.componentName);
1700 } else {
1701 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1702 }
Adam Cohendd70d662012-10-04 16:53:44 -07001703 Bundle options = info.bindOptions;
1704
Sunny Goyalffe83f12014-08-14 17:39:34 -07001705 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1706 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001707 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001708 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001709 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001710 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001711 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001712 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001713 }
1714
Adam Cohendcd297f2013-06-18 13:13:40 -07001715 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001716 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001717 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 folderInfo.title = getText(R.string.folder_name);
1719
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001721 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722
1723 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001724 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301725 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001726 // Force measure the new folder icon
1727 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1728 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001729 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 }
Romain Guycbb89e42009-06-08 15:52:54 -07001731
Winsonc0b52fe2015-09-09 16:38:15 -07001732 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001733 * Unbinds the view for the specified item, and removes the item and all its children.
1734 *
1735 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001736 * @param itemInfo the {@link ItemInfo} for this view.
1737 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001738 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001739 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001740 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001741 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001742 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1743 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001744 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001745 } else {
1746 mWorkspace.removeWorkspaceItem(v);
1747 }
Winsonc0b52fe2015-09-09 16:38:15 -07001748 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001749 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001750 }
1751 } else if (itemInfo instanceof FolderInfo) {
1752 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001753 if (v instanceof FolderIcon) {
1754 ((FolderIcon) v).removeListeners();
1755 }
Winsonc0b52fe2015-09-09 16:38:15 -07001756 mWorkspace.removeWorkspaceItem(v);
1757 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001758 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001759 }
1760 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1761 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001762 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001763 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001764 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001765 }
1766 } else {
1767 return false;
1768 }
1769 return true;
1770 }
1771
1772 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001773 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001774 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001775 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001776 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001777 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001778 // Deleting an app widget ID is a void call but writes to disk before returning
1779 // to the caller...
1780 new AsyncTask<Void, Void, Void>() {
1781 public Void doInBackground(Void ... args) {
1782 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1783 return null;
1784 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001785 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001786 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001787 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001788 }
1789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 @Override
1791 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001792 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 }
1794
Joe Onorato88ec0992009-11-19 13:16:06 -08001795 @Override
1796 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001797 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1798 return;
1799 }
1800
Sunny Goyal45478022015-06-08 16:52:41 -07001801 if (mDragController.isDragging()) {
1802 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001803 return;
1804 }
1805
Jon Mirandafeba90f2016-10-06 10:53:29 -07001806 // Note: There should be at most one log per method call. This is enforced implicitly
1807 // by using if-else statements.
1808 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001809 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1810 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001811 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001812 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001813 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001814 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001815 } else {
1816 // Back button is a no-op here, but give at least some feedback for the button press
1817 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001818 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 * Launches the intent referred by the clicked shortcut.
1823 *
1824 * @param v The view representing the clicked shortcut.
1825 */
1826 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001827 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1828 // view has detached (it's possible for this to happen if the view is removed mid touch).
1829 if (v.getWindowToken() == null) {
1830 return;
1831 }
1832
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001833 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001834 return;
1835 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001836
Adam Cohen1697b792013-09-17 19:08:21 -07001837 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001838 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001839 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1840 LauncherLogProto.Action.Direction.NONE,
1841 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001842 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001843 }
1844 return;
1845 }
1846
1847 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001848 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001849 int page = mWorkspace.indexOfChild(v);
1850 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1851 LauncherLogProto.Action.Direction.NONE,
1852 LauncherLogProto.ContainerType.OVERVIEW, page);
1853 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001854 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001855 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001856 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001857 }
1858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001860 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001861 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001863 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001864 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001865 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001866 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001867 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001868 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001869 } else if (tag instanceof AppInfo) {
1870 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001871 } else if (tag instanceof LauncherAppWidgetInfo) {
1872 if (v instanceof PendingAppWidgetHostView) {
1873 onClickPendingWidget((PendingAppWidgetHostView) v);
1874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 }
1876 }
1877
Sunny Goyal70660032015-05-14 00:07:08 -07001878 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001879 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001880 return false;
1881 }
1882
Michael Jurkaaf442092010-06-10 17:01:57 -07001883 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001884 * Event handler for the app widget view which has not fully restored.
1885 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001886 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001887 if (mIsSafeModeEnabled) {
1888 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1889 return;
1890 }
1891
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001892 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001893 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001894 LauncherAppWidgetProviderInfo appWidgetInfo =
1895 mAppWidgetManager.findProvider(info.providerName, info.user);
1896 if (appWidgetInfo == null) {
1897 return;
1898 }
1899 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1900
Sunny Goyald478c832016-04-01 12:04:16 -07001901 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1902 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1903 // This should not happen, as we make sure that an Id is allocated during bind.
1904 return;
1905 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001906 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1907 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001908 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001909 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001910 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001911 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001912 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001913 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001914 }
1915 }
1916
1917 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001918 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1919 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1920 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001921 *
1922 * @param v The view that was clicked.
1923 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001924 protected void onClickAllAppsButton(View v) {
1925 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001926 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301927 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1928 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001929 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001930 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001931 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001932 }
1933 }
1934
Sunny Goyale6e72002017-01-12 16:55:36 -08001935 private void onClickPendingAppItem(final View v, final String packageName,
1936 boolean downloadStarted) {
1937 if (downloadStarted) {
1938 // If the download has started, simply direct to the market app.
1939 startMarketIntentForPackage(v, packageName);
1940 return;
1941 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001942 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001943 .setTitle(R.string.abandoned_promises_title)
1944 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001945 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1946 @Override
1947 public void onClick(DialogInterface dialogInterface, int i) {
1948 startMarketIntentForPackage(v, packageName);
1949 }
1950 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001951 .setNeutralButton(R.string.abandoned_clean_this,
1952 new DialogInterface.OnClickListener() {
1953 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001954 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001955 mWorkspace.removeAbandonedPromise(packageName, user);
1956 }
1957 })
1958 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001959 }
1960
1961 private void startMarketIntentForPackage(View v, String packageName) {
1962 ItemInfo item = (ItemInfo) v.getTag();
1963 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001964 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001965 }
1966
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001967 /**
1968 * Event handler for an app shortcut click.
1969 *
1970 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1971 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001972 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001973 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1974 Object tag = v.getTag();
1975 if (!(tag instanceof ShortcutInfo)) {
1976 throw new IllegalArgumentException("Input must be a Shortcut");
1977 }
1978
1979 // Open shortcut
1980 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001981
Sunny Goyal076839c2017-10-30 13:52:20 -07001982 if (shortcut.isDisabled()) {
1983 if ((shortcut.runtimeStatusFlags &
1984 ~FLAG_DISABLED_SUSPENDED &
1985 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001986 // If the app is only disabled because of the above flags, launch activity anyway.
1987 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001988 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001989 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1990 // Use a message specific to this shortcut, if it has one.
1991 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1992 return;
1993 }
1994 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001995 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001996 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001997 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001998 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1999 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002000 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002001 }
2002 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2003 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002004 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002005 }
2006
Chris Wren40c5ed32014-06-24 18:24:23 -04002007 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002008 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002009 String packageName = shortcut.intent.getComponent() != null ?
2010 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2011 if (!TextUtils.isEmpty(packageName)) {
2012 onClickPendingAppItem(v, packageName,
2013 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2014 return;
2015 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002016 }
2017
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002018 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002019 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002020 }
2021
Sunny Goyala7ce1662016-05-31 15:01:35 -07002022 private void startAppShortcutOrInfoActivity(View v) {
2023 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002024 Intent intent;
2025 if (item instanceof PromiseAppInfo) {
2026 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2027 intent = promiseAppInfo.getMarketIntent();
2028 } else {
2029 intent = item.getIntent();
2030 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002031 if (intent == null) {
2032 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002033 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002034 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002035 }
2036
2037 /**
2038 * Event handler for a folder icon click.
2039 *
2040 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2041 */
2042 protected void onClickFolderIcon(View v) {
2043 if (LOGD) Log.d(TAG, "onClickFolder");
2044 if (!(v instanceof FolderIcon)){
2045 throw new IllegalArgumentException("Input must be a FolderIcon");
2046 }
2047
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002048 Folder folder = ((FolderIcon) v).getFolder();
2049 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002050 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002051 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002052 }
Michael Jurka5130e402011-10-13 04:55:35 -07002053 }
2054
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002055 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002056 * Event handler for the wallpaper picker button that appears after a long press
2057 * on the home screen.
2058 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002059 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002060 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002061 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2062 return;
2063 }
2064
Tony Wickham785f7a52015-08-31 17:28:32 -07002065 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2066 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002067 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002068 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002069 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002070
2071 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002072 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2073 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002074 intent.setPackage(pickerPackage);
2075 }
2076
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002077 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002078 try {
2079 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2080 // If there is no target package, use the default intent chooser animation
2081 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2082 } catch (ActivityNotFoundException e) {
2083 setWaitingForResult(null);
2084 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2085 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002086 }
2087
Sunny Goyala7ce1662016-05-31 15:01:35 -07002088 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002090 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2091 try {
2092 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2093 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2094 // is enabled by default on NYC.
2095 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2096 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002097
2098 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2099 String id = ((ShortcutInfo) info).getDeepShortcutId();
2100 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302101 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002102 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002103 } else {
2104 // Could be launching some bookkeeping activity
2105 startActivity(intent, optsBundle);
2106 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002107 } finally {
2108 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002111 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2112 // corresponding permission. Show the appropriate permission prompt if that
2113 // is the case.
2114 if (intent.getComponent() == null
2115 && Intent.ACTION_CALL.equals(intent.getAction())
2116 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2117 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002118
2119 setWaitingForResult(PendingRequestArgs
2120 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002121 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2122 REQUEST_PERMISSION_CALL_PHONE);
2123 } else {
2124 // No idea why this was thrown.
2125 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 }
2128 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002129
Sunny Goyalfe770c92016-09-27 14:38:58 -07002130 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002131 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002132 if (Utilities.ATLEAST_MARSHMALLOW) {
2133 int left = 0, top = 0;
2134 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002135 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002136 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002137 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002138 if (icon != null) {
2139 Rect bounds = icon.getBounds();
2140 left = (width - bounds.width()) / 2;
2141 top = v.getPaddingTop();
2142 width = bounds.width();
2143 height = bounds.height();
2144 }
2145 }
2146 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2147 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2148 // On L devices, we use the device default slide-up transition.
2149 // On L MR1 devices, we use a custom version of the slide-up transition which
2150 // doesn't have the delay present in the device default.
2151 return ActivityOptions.makeCustomAnimation(
2152 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2153 }
2154 return null;
2155 }
2156
Tonye3c59252017-05-02 21:32:27 -07002157 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002158 int[] pos = new int[2];
2159 v.getLocationOnScreen(pos);
2160 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2161 }
2162
Sunny Goyala7ce1662016-05-31 15:01:35 -07002163 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002164 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002165 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2166 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002167 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002168 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002169 // Only launch using the new animation if the shortcut has not opted out (this is a
2170 // private contract between launcher and may be ignored in the future).
2171 boolean useLaunchAnimation = (v != null) &&
2172 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2173 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2174
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002175 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002176
2177 // Prepare intent
2178 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2179 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002180 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002181 }
2182 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002183 if (Utilities.ATLEAST_MARSHMALLOW
2184 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002185 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002186 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002187 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002188 // Shortcuts need some special checks due to legacy reasons.
2189 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002190 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002191 // Could be launching some bookkeeping activity
2192 startActivity(intent, optsBundle);
2193 } else {
2194 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2195 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2196 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002197
2198 if (v instanceof BubbleTextView) {
2199 // This is set to the view that launched the activity that navigated the user away
2200 // from launcher. Since there is no callback for when the activity has finished
2201 // launching, enable the press state and keep this reference to reset the press
2202 // state when we return to launcher.
2203 BubbleTextView btv = (BubbleTextView) v;
2204 btv.setStayPressed(true);
2205 setOnResumeCallback(btv);
2206 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002207 mAppLaunchSuccess = true;
2208 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002209 } catch (ActivityNotFoundException|SecurityException e) {
2210 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2211 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2212 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002213 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002214 }
2215
Tony Wickhamdadb3042016-02-24 11:07:00 -08002216 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002217 public boolean dispatchTouchEvent(MotionEvent ev) {
2218 mLastDispatchTouchEventX = ev.getX();
2219 return super.dispatchTouchEvent(ev);
2220 }
2221
2222 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002224 if (!isDraggingEnabled()) return false;
2225 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002226 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227
Jon Miranda51f037d2016-11-07 08:37:20 -08002228 boolean ignoreLongPressToOverview =
2229 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002230
Adam Cohen1697b792013-09-17 19:08:21 -07002231 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002232 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002233 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302234 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2235 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002236 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002237 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002238 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2239 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2240 return true;
2241 } else {
2242 return false;
2243 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002244 } else {
2245 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002246 }
Adam Cohen1697b792013-09-17 19:08:21 -07002247 }
2248
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002249 CellLayout.CellInfo longClickCellInfo = null;
2250 View itemUnderLongClick = null;
2251 if (v.getTag() instanceof ItemInfo) {
2252 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002253 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002254 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002255 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 }
2257
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 // The hotseat touch handling does not go through Workspace, and we always allow long press
2259 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002260 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002262 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002263 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002264 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302265 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2266 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002267 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002268 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002269 return false;
2270 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302271 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2272 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002273 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002274 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002275 }
Jon Miranda379198e2016-09-23 08:54:40 -07002276 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2277 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002279 final boolean isAllAppsButton =
2280 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2281 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2282 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002283 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002285 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 }
2287 }
2288 }
2289 return true;
2290 }
2291
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002293 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002294 return mHotseat != null && layout != null &&
2295 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2296 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002297
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 /**
2299 * Returns the CellLayout of the specified container at the specified screen.
2300 */
Sunny Goyal92820592015-03-02 11:31:35 -08002301 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002302 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2303 if (mHotseat != null) {
2304 return mHotseat.getLayout();
2305 } else {
2306 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002307 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002308 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002309 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002310 }
2311 }
2312
Michael Jurkae326f182011-11-21 14:05:46 -08002313 @Override
2314 public void onTrimMemory(int level) {
2315 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002316 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2317 // The widget preview db can result in holding onto over
2318 // 3MB of memory for caching which isn't necessary.
2319 SQLiteDatabase.releaseMemory();
2320
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002321 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002322 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002323 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002324 if (mLauncherCallbacks != null) {
2325 mLauncherCallbacks.onTrimMemory(level);
2326 }
Michael Jurkae326f182011-11-21 14:05:46 -08002327 }
2328
Michael Jurkad7c28052012-04-27 15:43:36 -07002329 @Override
2330 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002331 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002332 final List<CharSequence> text = event.getText();
2333 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002334 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002335 // TODO: When can workspace be null?
2336 text.add(mWorkspace == null
2337 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002338 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002339 return result;
2340 }
2341
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002342 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002343 * If the activity is currently paused, signal that we need to run the passed Runnable
2344 * in onResume.
2345 *
2346 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002347 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2348 * wrong when we're not running, and if the activity comes back to what the configuration was
2349 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002350 *
2351 * Implementation of the method from LauncherModel.Callbacks.
2352 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002353 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002354 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002355 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002356 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002357 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002358 if (run instanceof RunnableWithId) {
2359 // Remove any runnables which have the same id
2360 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002361 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002362 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002363 return true;
2364 } else {
2365 return false;
2366 }
2367 }
2368
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002369 public void setOnResumeCallback(OnResumeCallback callback) {
2370 if (mOnResumeCallback != null) {
2371 mOnResumeCallback.onLauncherResume();
2372 }
2373 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002374 }
2375
Michael Jurka7607c2f2013-04-03 14:33:19 -07002376 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002377 * If the activity is currently paused, signal that we need to re-run the loader
2378 * in onResume.
2379 *
2380 * This needs to be called from incoming places where resources might have been loaded
2381 * while we are paused. That is becaues the Configuration might be wrong
2382 * when we're not running, and if it comes back to what it was when we
2383 * were paused, we are not restarted.
2384 *
2385 * Implementation of the method from LauncherModel.Callbacks.
2386 *
2387 * @return true if we are currently paused. The caller might be able to
2388 * skip some work in that case since we will come back again.
2389 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002390 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002391 public boolean setLoadOnResume() {
2392 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002393 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002394 mOnResumeNeedsLoad = true;
2395 return true;
2396 } else {
2397 return false;
2398 }
2399 }
2400
2401 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002402 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002404 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002405 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002406 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002407 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002408 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002409 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002410 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002411 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002412
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002414 * Clear any pending bind callbacks. This is called when is loader is planning to
2415 * perform a full rebind from scratch.
2416 */
2417 @Override
2418 public void clearPendingBinds() {
2419 mBindOnResumeCallbacks.clear();
2420 if (mPendingExecutor != null) {
2421 mPendingExecutor.markCompleted();
2422 mPendingExecutor = null;
2423 }
2424 }
2425
2426 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002427 * Refreshes the shortcuts shown on the workspace.
2428 *
2429 * Implementation of the method from LauncherModel.Callbacks.
2430 */
2431 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002432 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002433 // Floating panels (except the full widget sheet) are associated with individual icons. If
2434 // we are starting a fresh bind, close all such panels as all the icons are about
2435 // to go away.
2436 AbstractFloatingView.closeOpenViews(this, true,
2437 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002438
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002439 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002440
Winson Chungd64d1762013-08-20 14:37:16 -07002441 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002442 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002443 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002444
Winson Chung3d503fb2011-07-13 17:25:49 -07002445 if (mHotseat != null) {
2446 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002448 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 }
2450
Adam Cohendcd297f2013-06-18 13:13:40 -07002451 @Override
2452 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002453 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002454 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2455 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002456 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2457 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002458 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002459 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2460 // If there are no screens, we need to have an empty screen
2461 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002462 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002463 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002464
Winsonc7d2e832016-07-28 12:24:55 -07002465 // After we have added all the screens, if the wallpaper was locked to the default state,
2466 // then notify to indicate that it can be released and a proper wallpaper offset can be
2467 // computed before the next layout
2468 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002469 }
2470
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002471 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002472 int count = orderedScreenIds.size();
2473 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002474 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002475 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002476 // No need to bind the first screen, as its always bound.
2477 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2478 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002479 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002480 }
2481
Sunny Goyalb23980c2017-08-17 07:45:25 -07002482 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002483 public void bindAppsAdded(final ArrayList<Long> newScreens,
2484 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002485 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002486 Runnable r = new Runnable() {
2487 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002488 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002489 }
2490 };
2491 if (waitUntilResume(r)) {
2492 return;
2493 }
2494
Winson Chungd64d1762013-08-20 14:37:16 -07002495 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002496 if (newScreens != null) {
2497 bindAddScreens(newScreens);
2498 }
Winson Chungd64d1762013-08-20 14:37:16 -07002499
2500 // We add the items without animation on non-visible pages, and with
2501 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002502 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002503 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002504 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002505 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002506 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002507 }
Winson Chungc58497e2013-09-03 17:48:37 -07002508
Winson Chung87412982013-10-03 18:34:14 -07002509 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002510 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002511 }
2512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002514 * Bind the items start-end from the list.
2515 *
2516 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002518 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002519 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002520 Runnable r = new Runnable() {
2521 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002522 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002523 }
2524 };
2525 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002526 return;
2527 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002528
Sunny Goyal3be633b2016-12-08 09:59:25 -08002529 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002530 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002531 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002532 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002533 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002534 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002535 int end = items.size();
2536 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002537 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002538
2539 // Short circuit if we are loading dock items for a configuration which has no dock
2540 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2541 mHotseat == null) {
2542 continue;
2543 }
2544
Sunny Goyal639e9062015-08-19 19:17:06 -07002545 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002546 switch (item.itemType) {
2547 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2548 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002549 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002550 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002551 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002553 }
2554 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002555 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002556 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002557 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002558 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002559 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002560 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2561 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002562 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002563 if (view == null) {
2564 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002565 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002566 break;
2567 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002568 default:
2569 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002570 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002571
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002572 /*
2573 * Remove colliding items.
2574 */
2575 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2576 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2577 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2578 View v = cl.getChildAt(item.cellX, item.cellY);
2579 Object tag = v.getTag();
2580 String desc = "Collision while binding workspace item: " + item
2581 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002582 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002583 throw (new RuntimeException(desc));
2584 } else {
2585 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002586 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002587 continue;
2588 }
2589 }
2590 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302591 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002592 if (animateIcons) {
2593 // Animate all the applications up now
2594 view.setAlpha(0f);
2595 view.setScaleX(0f);
2596 view.setScaleY(0f);
2597 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002598 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002601
Winson Chung997a9232013-07-24 15:33:46 -07002602 if (animateIcons) {
2603 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002604 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002605 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002606 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002607 final Runnable startBounceAnimRunnable = new Runnable() {
2608 public void run() {
2609 anim.playTogether(bounceAnims);
2610 anim.start();
2611 }
2612 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002613 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002614 // We post the animation slightly delayed to prevent slowdowns
2615 // when we are loading right after we return to launcher.
2616 mWorkspace.postDelayed(new Runnable() {
2617 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002618 if (mWorkspace != null) {
2619 mWorkspace.snapToPage(newScreenIndex);
2620 mWorkspace.postDelayed(startBounceAnimRunnable,
2621 NEW_APPS_ANIMATION_DELAY);
2622 }
Winson Chung94d67682013-09-25 16:29:40 -07002623 }
2624 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002625 } else {
2626 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002627 }
2628 }
Winson Chung64359a52013-07-08 17:17:08 -07002629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 }
2632
Joe Onorato9c1289c2009-08-17 11:03:03 -04002633 /**
2634 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002635 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002636 public void bindAppWidget(LauncherAppWidgetInfo item) {
2637 View view = inflateAppWidget(item);
2638 if (view != null) {
2639 mWorkspace.addInScreen(view, item);
2640 mWorkspace.requestLayout();
2641 }
2642 }
2643
2644 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002645 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302646 PendingAppWidgetHostView view =
2647 new PendingAppWidgetHostView(this, item, mIconCache, true);
2648 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002649 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002650 }
2651
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002652 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002653
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002654 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002655
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002656 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2657 // If the provider is not ready, bind as a pending widget.
2658 appWidgetInfo = null;
2659 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2660 // The widget id is not valid. Try to find the widget based on the provider info.
2661 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2662 } else {
2663 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2664 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002665
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002666 // If the provider is ready, but the width is not yet restored, try to restore it.
2667 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2668 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002669 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002670 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2671 + " belongs to component " + item.providerName
2672 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002673 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002674 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002675 }
Sunny Goyalff572272014-07-23 13:58:07 -07002676
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002677 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002678 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002679 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2680 // Id has not been allocated yet. Allocate a new id.
2681 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2682 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002683
Sunny Goyald478c832016-04-01 12:04:16 -07002684 // Also try to bind the widget. If the bind fails, the user will be shown
2685 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002686 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002687 pendingInfo.spanX = item.spanX;
2688 pendingInfo.spanY = item.spanY;
2689 pendingInfo.minSpanX = item.minSpanX;
2690 pendingInfo.minSpanY = item.minSpanY;
2691 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002692
2693 boolean isDirectConfig =
2694 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2695 if (isDirectConfig && item.bindOptions != null) {
2696 Bundle newOptions = item.bindOptions.getExtras();
2697 if (options != null) {
2698 newOptions.putAll(options);
2699 }
2700 options = newOptions;
2701 }
Sunny Goyald478c832016-04-01 12:04:16 -07002702 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2703 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002704
Sunny Goyal86df1382016-08-10 15:03:22 -07002705 // We tried to bind once. If we were not able to bind, we would need to
2706 // go through the permission dialog, which means we cannot skip the config
2707 // activity.
2708 item.bindOptions = null;
2709 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2710
Sunny Goyald478c832016-04-01 12:04:16 -07002711 // Bind succeeded
2712 if (success) {
2713 // If the widget has a configure activity, it is still needs to set it up,
2714 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002715 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002716 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2717 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002718 }
Sunny Goyald478c832016-04-01 12:04:16 -07002719
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002720 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002721 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002722 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002723 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002724 // The widget was marked as UI not ready, but there is no configure activity to
2725 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002726 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002727 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002728 }
Sunny Goyalff572272014-07-23 13:58:07 -07002729 }
2730
Sunny Goyald5462aa2016-11-22 16:52:39 +05302731 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002732 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002733 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002734 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002735 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002736 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002737 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002738 }
2739
Sunny Goyal233ee962015-08-03 13:05:01 -07002740 item.minSpanX = appWidgetInfo.minSpanX;
2741 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302742 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002743 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302744 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002745 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302746 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002747
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002748 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002749 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002750 }
2751
Sunny Goyalff572272014-07-23 13:58:07 -07002752 /**
2753 * Restores a pending widget.
2754 *
2755 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002756 */
Sunny Goyald478c832016-04-01 12:04:16 -07002757 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002758 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2759 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2760 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002761 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002762 }
2763
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002764 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002765 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002766 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2767 info.pendingItemInfo = null;
2768 }
Sunny Goyalff572272014-07-23 13:58:07 -07002769
Sunny Goyalba47faa2017-10-04 16:50:57 -07002770 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2771 view.reinflate();
2772 }
2773
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002774 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002775 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002776 }
2777
Adam Cohen1462de32012-07-24 22:34:36 -07002778 public void onPageBoundSynchronously(int page) {
2779 mSynchronouslyBoundPages.add(page);
2780 }
2781
Sunny Goyal527c7d32015-08-28 15:19:36 -07002782 @Override
2783 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2784 if (mPendingExecutor != null) {
2785 mPendingExecutor.markCompleted();
2786 }
2787 mPendingExecutor = executor;
2788 executor.attachTo(this);
2789 }
2790
2791 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2792 if (mPendingExecutor == executor) {
2793 mPendingExecutor = null;
2794 }
2795 }
2796
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002797 @Override
2798 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2799 Runnable r = new Runnable() {
2800 public void run() {
2801 finishFirstPageBind(executor);
2802 }
2803 };
2804 if (waitUntilResume(r)) {
2805 return;
2806 }
2807
2808 Runnable onComplete = new Runnable() {
2809 @Override
2810 public void run() {
2811 if (executor != null) {
2812 executor.onLoadAnimationCompleted();
2813 }
2814 }
2815 };
2816 if (mDragLayer.getAlpha() < 1) {
2817 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2818 } else {
2819 onComplete.run();
2820 }
2821 }
2822
Joe Onorato9c1289c2009-08-17 11:03:03 -04002823 /**
2824 * Callback saying that there aren't any more items to bind.
2825 *
2826 * Implementation of the method from LauncherModel.Callbacks.
2827 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002828 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002829 Runnable r = new Runnable() {
2830 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002831 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002832 }
2833 };
2834 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002835 return;
2836 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002837 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002838 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002840 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002841
Sunny Goyal2100c782016-08-22 16:00:03 -07002842 if (mPendingActivityResult != null) {
2843 handleActivityResult(mPendingActivityResult.requestCode,
2844 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2845 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002846 }
2847
Sunny Goyala474a9b2017-05-04 16:47:11 -07002848 InstallShortcutReceiver.disableAndFlushInstallQueue(
2849 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002850
Tony Wickham010d2552017-01-20 08:15:28 -08002851 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002852 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002853 }
2854
Winson Chunga2413752012-04-03 14:22:34 -07002855 private boolean canRunNewAppsAnimation() {
2856 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002857 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002858 }
2859
Winson Chungc9168342013-06-26 14:54:55 -07002860 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002861 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002862 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2863 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002864 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002865 return bounceAnim;
2866 }
2867
Adam Cohen27772732013-11-11 14:00:56 +00002868 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002869 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002870 }
2871
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002872 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 * Add the icons for all apps.
2874 *
2875 * Implementation of the method from LauncherModel.Callbacks.
2876 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002877 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002878 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2879 public void run() {
2880 bindAllApplications(apps);
2881 }
2882 };
2883 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002884 return;
2885 }
2886
Winson Chungb745afb2015-03-02 11:51:23 -08002887 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002888 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002889 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002890 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002891 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002892 return;
2893 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002894
Winson Chungb745afb2015-03-02 11:51:23 -08002895 mAppsView.setApps(apps);
2896 }
Adam Cohen9211d422014-10-07 18:14:53 -07002897 if (mLauncherCallbacks != null) {
2898 mLauncherCallbacks.bindAllApplications(apps);
2899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 }
2901
2902 /**
Tony2917a8b2017-07-31 23:29:42 -07002903 * Returns an Executor that will run after the launcher is first drawn (including after the
2904 * initial fade in animation). Returns null if the first draw has already occurred.
2905 */
2906 public @Nullable Executor getPendingExecutor() {
2907 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2908 }
2909
2910 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002911 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2912 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2913 */
2914 @Override
2915 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002916 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002917 }
2918
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002919 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 * A package was updated.
2921 *
2922 * Implementation of the method from LauncherModel.Callbacks.
2923 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002924 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002925 Runnable r = new Runnable() {
2926 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002927 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002928 }
2929 };
2930 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002931 return;
2932 }
2933
Winson Chungb745afb2015-03-02 11:51:23 -08002934 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002935 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002937 }
2938
Sunny Goyal4390ace2014-10-13 11:33:11 -07002939 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002940 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2941 Runnable r = new Runnable() {
2942 public void run() {
2943 bindPromiseAppProgressUpdated(app);
2944 }
2945 };
2946 if (waitUntilResume(r)) {
2947 return;
2948 }
2949
2950 if (mAppsView != null) {
2951 mAppsView.updatePromiseAppProgress(app);
2952 }
2953 }
2954
2955 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002956 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2957 Runnable r = new Runnable() {
2958 public void run() {
2959 bindWidgetsRestored(widgets);
2960 }
2961 };
2962 if (waitUntilResume(r)) {
2963 return;
2964 }
2965 mWorkspace.widgetsRestored(widgets);
2966 }
2967
Joe Onorato9c1289c2009-08-17 11:03:03 -04002968 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002969 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002970 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002971 *
2972 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002973 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002974 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002975 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002976 Runnable r = new Runnable() {
2977 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002978 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002979 }
2980 };
2981 if (waitUntilResume(r)) {
2982 return;
2983 }
2984
Sunny Goyal4390ace2014-10-13 11:33:11 -07002985 if (!updated.isEmpty()) {
2986 mWorkspace.updateShortcuts(updated);
2987 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002988 }
2989
2990 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002991 * Update the state of a package, typically related to install state.
2992 *
2993 * Implementation of the method from LauncherModel.Callbacks.
2994 */
Sunny Goyale755d462014-07-22 13:48:29 -07002995 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002996 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
2997 Runnable r = new Runnable() {
2998 public void run() {
2999 bindRestoreItemsChange(updates);
3000 }
3001 };
3002 if (waitUntilResume(r)) {
3003 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003004 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003005
Sunny Goyal756adbc2015-04-16 15:20:51 -07003006 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003007 }
3008
3009 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003010 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003011 * in addition to specific applications to remove, the reason being that
3012 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003013 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003014 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003016 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003017 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003018 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003019 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003020 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003021 }
Winson Chungd64d1762013-08-20 14:37:16 -07003022 };
3023 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003024 return;
3025 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003026 mWorkspace.removeItemsByMatcher(matcher);
3027 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003028 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003029
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003030 @Override
3031 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3032 Runnable r = new Runnable() {
3033 public void run() {
3034 bindAppInfosRemoved(appInfos);
3035 }
3036 };
3037 if (waitUntilResume(r)) {
3038 return;
Joe Onorato36115782010-06-17 13:28:48 -04003039 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003040
Winson Chungdf95eb12013-10-16 14:57:07 -07003041 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003042 if (mAppsView != null) {
3043 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003045 }
Joe Onoratobe386092009-11-17 17:32:16 -08003046
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003047 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003048 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3049 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003050 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3051 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003052 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003053 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003054 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003055 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003056 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003057 return;
3058 }
3059
Tony Wickham26b17462017-03-20 17:12:24 -07003060 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3061 if (topView != null) {
3062 topView.onWidgetsBound();
3063 }
3064 }
3065
Tony Wickham86222d22017-03-29 15:30:43 -07003066 /**
3067 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3068 * refreshes the widgets and shortcuts associated with the given package/user
3069 */
3070 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003071 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003072 }
3073
Sunny Goyalc99cb172017-10-19 16:15:09 -07003074 public boolean isRotationEnabled () {
3075 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003076 }
3077
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003078 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003079 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003080 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003081 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003082 }
3083
Winson Chung80baf5a2010-08-09 16:03:15 -07003084 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003085 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003086 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003087 @Override
3088 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3089 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003090
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003091 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3092 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003093 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003094 writer.println(prefix + " Homescreen " + i);
3095
3096 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3097 for (int j = 0; j < layout.getChildCount(); j++) {
3098 Object tag = layout.getChildAt(j).getTag();
3099 if (tag != null) {
3100 writer.println(prefix + " " + tag.toString());
3101 }
3102 }
3103 }
3104
3105 writer.println(prefix + " Hotseat");
3106 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003107 for (int j = 0; j < layout.getChildCount(); j++) {
3108 Object tag = layout.getChildAt(j).getTag();
3109 if (tag != null) {
3110 writer.println(prefix + " " + tag.toString());
3111 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003112 }
Sunny Goyala1365452015-10-01 15:46:24 -07003113
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003114 try {
3115 FileLog.flushAll(writer);
3116 } catch (Exception e) {
3117 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003118 }
3119 }
3120
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003121 writer.println(prefix + "Misc:");
3122 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3123 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3124 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3125
3126 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003127
Adam Cohen9211d422014-10-07 18:14:53 -07003128 if (mLauncherCallbacks != null) {
3129 mLauncherCallbacks.dump(prefix, fd, writer, args);
3130 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003131 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003132
Sunny Goyal66b24572016-09-21 15:57:55 -07003133 @Override
3134 @TargetApi(Build.VERSION_CODES.N)
3135 public void onProvideKeyboardShortcuts(
3136 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3137
3138 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003139 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003140 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3141 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3142 }
3143 View currentFocus = getCurrentFocus();
3144 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3145 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3146 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3147 }
Tony18c4aa42017-05-10 21:23:57 -05003148 if (currentFocus.getTag() instanceof ItemInfo
3149 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003150 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3151 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3152 }
3153 if (!shortcutInfos.isEmpty()) {
3154 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3155 }
3156
3157 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3158 }
3159
3160 @Override
3161 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3162 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3163 switch (keyCode) {
3164 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003165 if (isInState(NORMAL)) {
3166 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003167 return true;
3168 }
3169 break;
3170 case KeyEvent.KEYCODE_S: {
3171 View focusedView = getCurrentFocus();
3172 if (focusedView instanceof BubbleTextView
3173 && focusedView.getTag() instanceof ItemInfo
3174 && mAccessibilityDelegate.performAction(focusedView,
3175 (ItemInfo) focusedView.getTag(),
3176 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003177 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003178 return true;
3179 }
3180 break;
3181 }
3182 case KeyEvent.KEYCODE_O:
3183 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3184 return true;
3185 }
3186 break;
3187 }
3188 }
3189 return super.onKeyShortcut(keyCode, event);
3190 }
3191
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003192 public static Launcher getLauncher(Context context) {
3193 if (context instanceof Launcher) {
3194 return (Launcher) context;
3195 }
3196 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3197 }
3198
Sunny Goyal5a81c382017-03-20 15:08:06 -07003199 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003200
3201 @Override
3202 public void onSharedPreferenceChanged(
3203 SharedPreferences sharedPreferences, String key) {
3204 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003205 // Recreate the activity so that it initializes the rotation preference again.
3206 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003207 }
3208 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003209 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003210
3211 /**
3212 * Callback for listening for onResume
3213 */
3214 public interface OnResumeCallback {
3215
3216 void onLauncherResume();
3217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218}