blob: 2593c49405b0f299e163464bdc4260bb05b627d8 [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 Goyal4c7f2152017-10-17 17:17:16 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070023import static com.android.launcher3.LauncherState.ALL_APPS;
24import static com.android.launcher3.LauncherState.NORMAL;
25import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070026import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070027import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
28import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
29
Sunny Goyal28c6b962015-10-12 11:42:05 -070030import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070032import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070034import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070035import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070036import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000037import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070038import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040039import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080041import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070042import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080044import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080045import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040046import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070048import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040049import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070051import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070052import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070053import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070054import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070056import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070057import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070063import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080064import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070066import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070067import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070073import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070077import android.view.KeyboardShortcutGroup;
78import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080079import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070086import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070090
Sunny Goyal651077b2014-06-30 14:15:31 -070091import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070092import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070093import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070094import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070095import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070096import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070099import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700100import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800101import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700102import com.android.launcher3.dragndrop.DragController;
103import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700104import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800106import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700107import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000108import com.android.launcher3.folder.Folder;
109import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700110import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700111import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700112import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700113import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700116import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700117import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800118import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700120import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700121import com.android.launcher3.states.AllAppsState;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800122import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530123import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
124import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
125import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700126import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700128import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700129import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700130import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700131import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800132import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700133import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700135import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700136import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700137import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700138import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700139import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700140import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700141import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800142import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700143import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800144import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700145import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700146import com.android.launcher3.widget.WidgetListRowEntry;
147import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700148import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700149
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700150import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700151import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700152import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700153import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700154import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700155import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800156import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700157import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159/**
160 * Default launcher application.
161 */
Sunny Goyal27835952017-01-13 12:15:53 -0800162public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700163 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
164 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700165 AccessibilityManager.AccessibilityStateChangeListener,
166 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700167 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700168 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Winson Chunga2413752012-04-03 14:22:34 -0700170 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700176 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Michael Jurka8b805b12012-04-18 14:23:14 -0700178 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700179 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
180 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700181
Jon Mirandac476d6e2017-05-04 16:30:01 -0700182 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700183
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700184 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
185
Mathew Inwood876a8462013-06-14 14:12:41 +0100186 /**
187 * IntentStarter uses request codes starting with this. This must be greater than all activity
188 * request codes used internally.
189 */
190 protected static final int REQUEST_LAST = 100;
191
Winson Chung2672ff92012-05-04 16:22:30 -0700192 // The Intent extra that defines whether to ignore the launch animation
193 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400194 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 // Type: int
197 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700198 // Type: int
199 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700200 // Type: PendingRequestArgs
201 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
202 // Type: ActivityResultInfo
203 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700204 // Type: SparseArray<Parcelable>
205 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700207 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700209 private boolean mIsSafeModeEnabled;
210
Adam Cohenad4e15c2013-10-17 16:21:35 -0700211 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chunga2413752012-04-03 14:22:34 -0700213 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700214 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
215 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
216 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700217
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700219 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800221 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700222
223 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
Sunny Goyalffe83f12014-08-14 17:39:34 -0700225 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700228 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700229
Sunny Goyal316490e2015-06-02 09:38:28 -0700230 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800231 private ViewGroup mOverviewPanel;
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
Jon Miranda6bed3502017-09-19 14:43:17 -0700241 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
242 // that results in widgets being inflated in the wrong orientation.
243 private int mOrientation;
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private SpannableStringBuilder mDefaultKeySsb = null;
246
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800249 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700250 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700252 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700253 private OnResumeCallback mOnResumeCallback;
254
Sunny Goyal527c7d32015-08-28 15:19:36 -0700255 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700256
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800258 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800259 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700260 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700261 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262
Jon Miranda2d89ea82017-05-04 11:47:53 -0700263 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700264 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700265
Tony Wickham010d2552017-01-20 08:15:28 -0800266 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700267
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700268 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700269
Winson Chung46353de2012-02-16 14:05:10 -0800270 // We only want to get the SharedPreferences once since it does an FS stat each time we get
271 // it from the context.
272 private SharedPreferences mSharedPrefs;
273
Sunny Goyal2100c782016-08-22 16:00:03 -0700274 // Activity result which needs to be processed after workspace has loaded.
275 private ActivityResultInfo mPendingActivityResult;
276 /**
277 * Holds extra information required to handle a result from an external call, like
278 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
279 */
280 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800281
Jon Miranda379198e2016-09-23 08:54:40 -0700282 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700283
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700284 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700285 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700286 private boolean mAppLaunchSuccess;
287
Sunny Goyal745bad92016-05-02 10:54:12 -0700288 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 @Override
291 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700292 if (DEBUG_STRICT_MODE) {
293 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
294 .detectDiskReads()
295 .detectDiskWrites()
296 .detectNetwork() // or .detectAll() for all detectable problems
297 .penaltyLog()
298 .build());
299 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
300 .detectLeakedSqlLiteObjects()
301 .detectLeakedClosableObjects()
302 .penaltyLog()
303 .penaltyDeath()
304 .build());
305 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700306 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700307
Adam Cohen9211d422014-10-07 18:14:53 -0700308 if (mLauncherCallbacks != null) {
309 mLauncherCallbacks.preOnCreate();
310 }
311
Mario Bertschler27288382017-05-24 15:35:09 -0700312 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
313 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700314 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700317 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400318
Sunny Goyal87f784c2017-01-11 10:48:34 -0800319 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700320
Adam Cohen2e6da152015-05-06 11:42:25 -0700321 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800322 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700323 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700324 Display display = getWindowManager().getDefaultDisplay();
325 Point mwSize = new Point();
326 display.getSize(mwSize);
327 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
328 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700329
Jon Miranda6bed3502017-09-19 14:43:17 -0700330 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700331 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700332 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800333 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800334 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800335 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700336 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700337
Joe Onorato41a12d22009-10-31 18:30:00 -0400338 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700339 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700340 mStateManager = new LauncherStateManager(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700341
Sunny Goyalffe83f12014-08-14 17:39:34 -0700342 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700343
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700344 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700345 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700346
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700347 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
348 // this also ensures that any synchronous binding below doesn't re-trigger another
349 // LauncherModel load.
350 mPaused = false;
351
Sunny Goyal60820d72017-05-09 12:40:11 -0700352 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 setupViews();
Winson1f064272016-07-18 17:18:02 -0700355 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356
Tony Wickham010d2552017-01-20 08:15:28 -0800357 mPopupDataProvider = new PopupDataProvider(this);
358
Tony Wickhame2217252016-03-22 16:34:23 -0700359 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
360 .addAccessibilityStateChangeListener(this);
361
Sunny Goyalfe770c92016-09-27 14:38:58 -0700362 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363
Sunny Goyal2100c782016-08-22 16:00:03 -0700364 // We only load the page synchronously if the user rotates (or triggers a
365 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700366 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
367 if (savedInstanceState != null) {
368 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
369 }
370 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700371 // If we are not binding synchronously, show a fade in animation when
372 // the first page bind completes.
373 mDragLayer.setAlpha(0);
374 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700375 // Pages bound synchronously.
376 mWorkspace.setCurrentPage(currentScreen);
377
Sunny Goyal2100c782016-08-22 16:00:03 -0700378 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
380
381 // For handling default keys
382 mDefaultKeySsb = new SpannableStringBuilder();
383 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800384
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800385 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700386 // In case we are on a device with locked rotation, we should look at preferences to check
387 // if the user has specifically allowed rotation.
388 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700389 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700390 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
391 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700392 }
393
Sunny Goyal5a81c382017-03-20 15:08:06 -0700394 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
395 // Temporarily enable the rotation
396 mRotationEnabled = true;
397 }
398
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700399 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
400 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700401 setRequestedOrientation(mRotationEnabled
402 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700403
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800404 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700405
Jon Miranda7fda2852017-07-19 16:07:01 -0700406 // Listen for broadcasts
407 IntentFilter filter = new IntentFilter();
408 filter.addAction(Intent.ACTION_SCREEN_OFF);
409 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
410 registerReceiver(mReceiver, filter);
411 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700412
Sunny Goyal8392c822017-06-20 10:03:56 -0700413 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
414 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700415
416 if (mLauncherCallbacks != null) {
417 mLauncherCallbacks.onCreate(savedInstanceState);
418 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700419
420 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700421 }
422
Sunny Goyal7779d622015-06-11 16:18:39 -0700423 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700424 public void onThemeChanged() {
425 recreate();
426 }
427
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700428 public LauncherStateManager getStateManager() {
429 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700430 }
431
Mario Bertschlera6936942017-05-31 14:48:19 -0700432 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700433 if (isDark) {
434 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700435 } else if (supportsDarkText) {
436 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700437 }
438 }
439
440 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700441 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800442 return mLauncherView.findViewById(id);
443 }
444
445 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700446 public void onAppWidgetHostReset() {
447 if (mAppWidgetHost != null) {
448 mAppWidgetHost.startListening();
449 }
450 }
451
Adam Cohen9211d422014-10-07 18:14:53 -0700452 private LauncherCallbacks mLauncherCallbacks;
453
Winson1f064272016-07-18 17:18:02 -0700454 public void onInsetsChanged(Rect insets) {
455 mDeviceProfile.updateInsets(insets);
456 mDeviceProfile.layout(this, true /* notifyListeners */);
457 }
458
Sunny Goyal32554d12015-12-03 15:31:25 -0800459 /**
460 * Call this after onCreate to set or clear overlay.
461 */
462 public void setLauncherOverlay(LauncherOverlay overlay) {
463 if (overlay != null) {
464 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
465 }
466 mWorkspace.setLauncherOverlay(overlay);
467 }
468
Adam Cohen9211d422014-10-07 18:14:53 -0700469 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
470 mLauncherCallbacks = callbacks;
471 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700472 }
473
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700474 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700475 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700476 if (mLauncherCallbacks != null) {
477 mLauncherCallbacks.onLauncherProviderChange();
478 }
479 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700480
Hyunyoung Song3f471442015-04-08 19:01:34 -0700481 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700482 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
483 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700484 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700485 }
486
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000487 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700488 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
489 // This cast is safe as long as the id < 0x00FFFFFF
490 // Since we jail all the dynamically generated views, there should be no clashes
491 // with any other views.
492 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000493 }
494
Tony Wickham010d2552017-01-20 08:15:28 -0800495 public PopupDataProvider getPopupDataProvider() {
496 return mPopupDataProvider;
497 }
498
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000499 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700500 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
501 * a configuration step, this allows the proper animations to run after other transitions.
502 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 private long completeAdd(
504 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
505 long screenId = info.screenId;
506 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700507 // When the screen id represents an actual screen (as opposed to a rank) we make sure
508 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700509 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700510 }
Adam Cohendb364c32014-05-20 14:23:40 -0700511
Sunny Goyal2100c782016-08-22 16:00:03 -0700512 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800513 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700514 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700515 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800516 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700518 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700519 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700520 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700521 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700522 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700523 int widgetId = appWidgetId;
524 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700525 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700527 // Since the view was just bound, also launch the configure activity if needed
528 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
529 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800530 if (provider != null) {
531 new WidgetAddFlowHandler(provider)
532 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700533 }
534 }
535 break;
536 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800537 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700538
Adam Cohendb364c32014-05-20 14:23:40 -0700539 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800540 }
541
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800542 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700543 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700544 if (isWorkspaceLoading()) {
545 // process the result once the workspace has loaded.
546 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
547 return;
548 }
549 mPendingActivityResult = null;
550
Winson Chunge341d302013-08-16 14:31:00 -0700551 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700552 final PendingRequestArgs requestArgs = mPendingRequestArgs;
553 setWaitingForResult(null);
554 if (requestArgs == null) {
555 return;
556 }
557
558 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700559
Adam Cohenad4e15c2013-10-17 16:21:35 -0700560 Runnable exitSpringLoaded = new Runnable() {
561 @Override
562 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700563 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700564 }
565 };
566
Michael Jurka8b805b12012-04-18 14:23:14 -0700567 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700568 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700569 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700570 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
571 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700572 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700573 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700576 addAppWidgetImpl(
577 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800578 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 }
581 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700583 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700584 // User could have free-scrolled between pages before picking a wallpaper; make sure
585 // we move to the closest one now.
586 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700587 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200588 }
589 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700590 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200591
Adam Cohened66b2b2012-01-23 17:28:51 -0800592 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700593 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700594
Adam Cohened66b2b2012-01-23 17:28:51 -0800595 // We have special handling for widgets
596 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800597 final int appWidgetId;
598 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
599 : -1;
600 if (widgetId < 0) {
601 appWidgetId = pendingAddWidgetId;
602 } else {
603 appWidgetId = widgetId;
604 }
605
Adam Cohenad4e15c2013-10-17 16:21:35 -0700606 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800607 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700608 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
609 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700612 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700613 @Override
614 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700615 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700616 }
617 };
Adam Cohendb364c32014-05-20 14:23:40 -0700618
Sunny Goyal2100c782016-08-22 16:00:03 -0700619 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
620 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
621 } else {
622 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
623 // When the screen id represents an actual screen (as opposed to a rank)
624 // we make sure that the drop page actually exists.
625 requestArgs.screenId =
626 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700627 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 final CellLayout dropLayout =
629 mWorkspace.getScreenWithId(requestArgs.screenId);
630
631 dropLayout.setDropPending(true);
632 final Runnable onComplete = new Runnable() {
633 @Override
634 public void run() {
635 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
636 dropLayout.setDropPending(false);
637 }
638 };
639 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
640 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700641 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800642 return;
643 }
644
Sunny Goyald478c832016-04-01 12:04:16 -0700645 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
646 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700647 if (resultCode == RESULT_OK) {
648 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700650 }
651 // Leave the widget in the pending state if the user canceled the configure.
652 return;
653 }
654
Sunny Goyalaad90582015-07-09 14:22:50 -0700655 if (requestCode == REQUEST_CREATE_SHORTCUT) {
656 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
658 completeAdd(requestCode, data, -1, requestArgs);
659 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
660 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
661
Sunny Goyalaad90582015-07-09 14:22:50 -0700662 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700663 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
664 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800667 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800668 }
669
670 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700671 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800672 final int requestCode, final int resultCode, final Intent data) {
673 handleActivityResult(requestCode, resultCode, data);
674 if (mLauncherCallbacks != null) {
675 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 }
678
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700679 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700680 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600681 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 PendingRequestArgs pendingArgs = mPendingRequestArgs;
683 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
684 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
685 setWaitingForResult(null);
686
Sunny Goyal28c6b962015-10-12 11:42:05 -0700687 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700689 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 Intent intent = pendingArgs.getPendingIntent();
693
Sunny Goyal28c6b962015-10-12 11:42:05 -0700694 if (grantResults.length > 0
695 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700696 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700697 } else {
698 // TODO: Show a snack bar with link to settings
699 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700700 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 }
702 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600703 if (mLauncherCallbacks != null) {
704 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
705 grantResults);
706 }
707 }
708
Adam Cohendb364c32014-05-20 14:23:40 -0700709 /**
710 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
711 *
712 * @param screenId the screen id to check
713 * @return the new screen, or screenId if it exists
714 */
715 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800716 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700717 if (dropLayout == null) {
718 // it's possible that the add screen was removed because it was
719 // empty and a re-bind occurred
720 mWorkspace.addExtraEmptyScreen();
721 return mWorkspace.commitExtraEmptyScreen();
722 } else {
723 return screenId;
724 }
725 }
726
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 @Thunk void completeTwoStageWidgetDrop(
728 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
729 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 Runnable onCompleteRunnable = null;
731 int animationType = 0;
732
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700733 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 if (resultCode == RESULT_OK) {
735 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
736 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800737 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700738 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 onCompleteRunnable = new Runnable() {
740 @Override
741 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700743 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 }
745 };
746 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800747 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700750 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
753 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700755 // The animated view may be null in the case of a rotation during widget configuration
756 onCompleteRunnable.run();
757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
759
760 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700761 protected void onStop() {
762 super.onStop();
763 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700764
765 if (mLauncherCallbacks != null) {
766 mLauncherCallbacks.onStop();
767 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700768
Sunny Goyalf5e37442016-11-02 10:31:24 -0700769 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700770 mAppWidgetHost.stopListening();
771 }
Tony Wickham010d2552017-01-20 08:15:28 -0800772
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700773 if (!mAppLaunchSuccess) {
774 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700775 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700776 }
Tony Wickham010d2552017-01-20 08:15:28 -0800777 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700778 }
779
780 @Override
781 protected void onStart() {
782 super.onStart();
783 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700784
785 if (mLauncherCallbacks != null) {
786 mLauncherCallbacks.onStart();
787 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700788
Sunny Goyalf5e37442016-11-02 10:31:24 -0700789 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700790 mAppWidgetHost.startListening();
791 }
Tony Wickham010d2552017-01-20 08:15:28 -0800792
793 if (!isWorkspaceLoading()) {
794 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
795 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700796
Jon Miranda7fda2852017-07-19 16:07:01 -0700797 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700798 if (mScrimAnimator != null) {
799 mScrimAnimator.cancel();
800 }
801 mDragLayer.getBackground().setAlpha(0);
802 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
803 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
804 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
805 @Override
806 public void onAnimationEnd(Animator animation) {
807 mScrimAnimator = null;
808 }
809 });
810 mScrimAnimator.setDuration(600);
811 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
812 mScrimAnimator.start();
813 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700814 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700815 }
816
817 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700819 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700821 TraceHelper.partitionSection("ON_RESUME", "superCall");
822
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700823 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700824 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800825 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700826 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700827 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700828 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700829 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700831 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200832 // We might have postponed some bind calls until onResume (see waitUntilResume) --
833 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700834 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
835 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200836 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700837 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200838 }
Winson Chunge4e50662012-01-23 14:45:13 -0800839
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700840 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700841 // Process any items that were added while Launcher was away.
842 InstallShortcutReceiver.disableAndFlushInstallQueue(
843 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700844
Sunny Goyala474a9b2017-05-04 16:47:11 -0700845 // Refresh shortcuts if the permission changed.
846 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700847
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700848 if (shouldShowDiscoveryBounce()) {
849 mAllAppsController.showDiscoveryBounce();
850 }
Adam Cohen9211d422014-10-07 18:14:53 -0700851 if (mLauncherCallbacks != null) {
852 mLauncherCallbacks.onResume();
853 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800854
Sunny Goyala502aa32017-10-02 16:04:06 -0700855 clearTypedText();
856
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700857 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700858 }
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700861 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700862 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700863 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700864
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700865 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700866 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800867 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700868 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700869
Adam Cohen9211d422014-10-07 18:14:53 -0700870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700872 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700873 }
874
Adam Cohenc2d6e892014-10-16 09:49:24 -0700875 public interface LauncherOverlay {
876
877 /**
878 * Touch interaction leading to overscroll has begun
879 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700880 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700881
882 /**
883 * Touch interaction related to overscroll has ended
884 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700885 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700886
887 /**
888 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
889 * screen (or in the case of RTL, the rightmost screen).
890 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700891 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700892
893 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800894 * Called when the launcher is ready to use the overlay
895 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700896 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700897 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 }
899
900 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700901 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700902 }
903
904 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
905
Sunny Goyalc86df472016-02-25 09:19:38 -0800906 public void onScrollChanged(float progress) {
907 if (mWorkspace != null) {
908 mWorkspace.onOverlayScrollChanged(progress);
909 }
910 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700911 }
912
Jorim Jaggid017f882014-01-14 17:08:48 -0800913 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700914 if (mLauncherCallbacks != null) {
915 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700916 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800917 // On O and above we there is always some setting present settings (add icon to
918 // home screen or icon badging). On earlier APIs we will have the allow rotation
919 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700920 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700921 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800922 }
923
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700924 @Override
925 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700927 if (mModel.isCurrentCallbacks(this)) {
928 mModel.stopLoader();
929 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700930 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
931
Romain Guy13c2e7b2010-03-10 19:45:00 -0800932 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700933 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700934
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800935 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800936 @Override
937 public void onWindowFocusChanged(boolean hasFocus) {
938 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700939 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700940
941 if (mLauncherCallbacks != null) {
942 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
943 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800944 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 private boolean acceptFilter() {
947 final InputMethodManager inputManager = (InputMethodManager)
948 getSystemService(Context.INPUT_METHOD_SERVICE);
949 return !inputManager.isFullscreenMode();
950 }
951
952 @Override
953 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700954 final int uniChar = event.getUnicodeChar();
955 final boolean handled = super.onKeyDown(keyCode, event);
956 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
957 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
959 keyCode, event);
960 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700962 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 // showSearchDialog()
964 // If there are multiple keystrokes before the search dialog takes focus,
965 // onSearchRequested() will be called for every keystroke,
966 // but it is idempotent, so it's fine.
967 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969 }
970
Joe Onorato8a9625e2010-01-28 15:55:35 -0800971 // Eat the long press event so the keyboard doesn't come up.
972 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
973 return true;
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 return handled;
977 }
978
Winson46163472015-09-22 17:31:56 -0700979 @Override
980 public boolean onKeyUp(int keyCode, KeyEvent event) {
981 if (keyCode == KeyEvent.KEYCODE_MENU) {
982 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700983 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700984 // Close any open floating view
985 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -0700986
Winson46163472015-09-22 17:31:56 -0700987 // Show the overview mode if we are on the workspace
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700988 if (isInState(NORMAL) && !mWorkspace.isSwitchingState()) {
989 mStateManager.goToState(OVERVIEW, true /* animate */, new Runnable() {
990 @Override
991 public void run() {
992 // Hitting the menu button when in touch mode does not trigger touch
993 // mode to be disabled, so if requested, force focus on one of the
994 // overview panel buttons.
995 mOverviewPanel.requestFocusFromTouch();
996 }
997 });
Winson46163472015-09-22 17:31:56 -0700998 }
999 }
1000 return true;
1001 }
1002 return super.onKeyUp(keyCode, event);
1003 }
1004
Karl Rosaen138a0412009-04-23 19:00:21 -07001005 private String getTypedText() {
1006 return mDefaultKeySsb.toString();
1007 }
1008
Sunny Goyal6f28e712016-09-13 14:19:29 -07001009 @Override
1010 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 mDefaultKeySsb.clear();
1012 mDefaultKeySsb.clearSpans();
1013 Selection.setSelection(mDefaultKeySsb, 0);
1014 }
1015
Sunny Goyalf9403d92017-10-18 10:55:56 -07001016 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -07001017 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -07001018 }
1019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 /**
1021 * Restores the previous state, if it exists.
1022 *
1023 * @param savedState The previous state.
1024 */
1025 private void restoreState(Bundle savedState) {
1026 if (savedState == null) {
1027 return;
1028 }
1029
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001030 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001031 LauncherState[] stateValues = LauncherState.values();
1032 LauncherState state = stateValues[stateOrdinal];
1033 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001034 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001035 }
1036
Sunny Goyal2100c782016-08-22 16:00:03 -07001037 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1038 if (requestArgs != null) {
1039 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001041
1042 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001043
1044 SparseArray<Parcelable> widgetsState =
1045 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1046 if (widgetsState != null) {
1047 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050
1051 /**
1052 * Finds all the views we need and configure them properly.
1053 */
1054 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001055 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001056 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001057 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001058 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001059
Sunny Goyal784f9c32016-03-23 16:56:54 -07001060 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1061 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1062 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063
Winson Chung4c98d922011-05-31 16:50:48 -07001064 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001065 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001066
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 // Setup the hotseat
1068 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1069 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001070 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 }
1072
Winsone9f27272015-10-13 10:47:51 -07001073 // Setup the overview panel
1074 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001075
Winson Chung4c98d922011-05-31 16:50:48 -07001076 // Setup the workspace
1077 mWorkspace.setHapticFeedbackEnabled(false);
1078 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001079 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001080 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1081 // default state, otherwise we will update to the wrong offsets in RTL
1082 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001083 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001084 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001085
Tony Wickham34d2c912015-09-11 09:27:58 -07001086 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001087 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001088
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001089 // Setup Apps
1090 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001091
Winson Chung3d503fb2011-07-13 17:25:49 -07001092 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001093 mDragController.setMoveTarget(mWorkspace);
1094 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001095 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001096
Hyunyoung Songd725f642017-08-17 22:26:35 -07001097 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001098
Sunny Goyal322d5562015-06-25 19:35:49 -07001099 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1100 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103
Winsone9f27272015-10-13 10:47:51 -07001104 private void setupOverviewPanel() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001105 mOverviewPanel = findViewById(R.id.overview_panel);
Winsone9f27272015-10-13 10:47:51 -07001106
Winsone9f27272015-10-13 10:47:51 -07001107 // Bind wallpaper button actions
1108 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301109 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001110 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001111 public void handleViewClick(View view) {
1112 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001113 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001114 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001115
1116 // Bind widget button actions
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301117 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001118 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001119 public void handleViewClick(View view) {
1120 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001121 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001122 }.attachTo(findViewById(R.id.widget_button));
Winsone9f27272015-10-13 10:47:51 -07001123
1124 // Bind settings actions
1125 View settingsButton = findViewById(R.id.settings_button);
1126 boolean hasSettings = hasSettings();
1127 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301128 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001129 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001130 public void handleViewClick(View view) {
1131 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001132 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001133 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001134 } else {
1135 settingsButton.setVisibility(View.GONE);
1136 }
1137
1138 mOverviewPanel.setAlpha(0f);
1139 }
1140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001142 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001143 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001144 */
1145 public void setAllAppsButton(View allAppsButton) {
1146 mAllAppsButton = allAppsButton;
1147 }
1148
Anjali Koppal5ad44842014-03-10 20:34:39 -07001149 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 * Creates a view representing a shortcut.
1151 *
1152 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001154 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001155 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157
1158 /**
1159 * Creates a view representing a shortcut inflated from the specified resource.
1160 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 * @param parent The group the shortcut belongs to.
1162 * @param info The data structure describing the shortcut.
1163 *
1164 * @return A View inflated from layoutResId.
1165 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001166 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001167 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1168 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001169 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001171 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 return favorite;
1173 }
1174
1175 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001176 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 *
1178 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001180 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001181 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001182 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1183 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001184 return;
1185 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001186
Jon Mirandac476d6e2017-05-04 16:30:01 -07001187 int[] cellXY = mTmpAddItemCellCoordinates;
1188 CellLayout layout = getCellLayout(container, screenId);
1189
Sunny Goyal782f0c92017-01-19 10:27:54 -08001190 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001191 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001192 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1193 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001194 }
1195
1196 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001197 // Legacy shortcuts are only supported for primary profile.
1198 info = Process.myUserHandle().equals(args.user)
1199 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001200
Sunny Goyalb57645f2017-03-20 13:57:28 -07001201 if (info == null) {
1202 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1203 return;
1204 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001205 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001206 // The app is trying to add a shortcut without sufficient permissions
1207 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1208 return;
1209 }
1210 }
1211
Jon Mirandac476d6e2017-05-04 16:30:01 -07001212 if (container < 0) {
1213 // Adding a shortcut to the Workspace.
1214 final View view = createShortcut(info);
1215 boolean foundCellSpan = false;
1216 // First we check if we already know the exact location where we want to add this item.
1217 if (cellX >= 0 && cellY >= 0) {
1218 cellXY[0] = cellX;
1219 cellXY[1] = cellY;
1220 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001221
Jon Mirandac476d6e2017-05-04 16:30:01 -07001222 // If appropriate, either create a folder or add to an existing folder
1223 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001224 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001225 return;
1226 }
1227 DragObject dragObject = new DragObject();
1228 dragObject.dragInfo = info;
1229 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1230 true)) {
1231 return;
1232 }
1233 } else {
1234 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1235 }
1236
1237 if (!foundCellSpan) {
1238 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001239 return;
1240 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001241
1242 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1243 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001244 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001245 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001246 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001247 if (folderIcon != null) {
1248 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1249 folderInfo.add(info, args.rank, false);
1250 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001251 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001252 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001253 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
Sunny Goyale29897f52017-07-20 10:09:42 -07001256 public FolderIcon findFolderIcon(final long folderIconId) {
1257 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1258 @Override
1259 public boolean evaluate(ItemInfo info, View view) {
1260 return info != null && info.id == folderIconId;
1261 }
1262 });
1263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001266 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001268 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001270 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001271 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1272
Adam Cohened66b2b2012-01-23 17:28:51 -08001273 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001274 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001275 }
Romain Guycbb89e42009-06-08 15:52:54 -07001276
Adam Cohen59400422014-03-05 18:07:04 -08001277 LauncherAppWidgetInfo launcherInfo;
1278 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001279 launcherInfo.spanX = itemInfo.spanX;
1280 launcherInfo.spanY = itemInfo.spanY;
1281 launcherInfo.minSpanX = itemInfo.minSpanX;
1282 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001283 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001284
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001285 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001286 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287
Sunny Goyal2100c782016-08-22 16:00:03 -07001288 if (hostView == null) {
1289 // Perform actual inflation because we're live
1290 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001292 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301293 prepareAppWidget(hostView, launcherInfo);
1294 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
Romain Guycbb89e42009-06-08 15:52:54 -07001296
Sunny Goyald5462aa2016-11-22 16:52:39 +05301297 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001298 hostView.setTag(item);
1299 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001300 hostView.setFocusable(true);
1301 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001302 }
1303
Adam Cohended9f8d2010-11-03 13:25:16 -07001304 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1305 @Override
1306 public void onReceive(Context context, Intent intent) {
1307 final String action = intent.getAction();
1308 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001309 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001310 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001311 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001312 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001313 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001314 mShouldFadeInScrim = true;
1315 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1316 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1317 // the user unlocked and the Launcher is not in the foreground.
1318 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001319 }
1320 }
1321 };
1322
Tony Wickham010d2552017-01-20 08:15:28 -08001323 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1324 Runnable r = new Runnable() {
1325 @Override
1326 public void run() {
1327 mWorkspace.updateIconBadges(updatedBadges);
1328 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001329
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001330 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1331 if (popup instanceof PopupContainerWithArrow) {
1332 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001333 }
Tony Wickham010d2552017-01-20 08:15:28 -08001334 }
1335 };
1336 if (!waitUntilResume(r)) {
1337 r.run();
1338 }
1339 }
1340
Adam Cohended9f8d2010-11-03 13:25:16 -07001341 @Override
1342 public void onAttachedToWindow() {
1343 super.onAttachedToWindow();
1344
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001345 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001346 if (mLauncherCallbacks != null) {
1347 mLauncherCallbacks.onAttachedToWindow();
1348 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001349 }
1350
1351 @Override
1352 public void onDetachedFromWindow() {
1353 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001354
1355 if (mLauncherCallbacks != null) {
1356 mLauncherCallbacks.onDetachedFromWindow();
1357 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001358 }
1359
Winson Chunga6945242014-01-08 14:04:34 -08001360 public DragLayer getDragLayer() {
1361 return mDragLayer;
1362 }
1363
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001364 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001365 return mAppsView;
1366 }
1367
Winson Chunga6945242014-01-08 14:04:34 -08001368 public Workspace getWorkspace() {
1369 return mWorkspace;
1370 }
1371
1372 public Hotseat getHotseat() {
1373 return mHotseat;
1374 }
1375
Jorim Jaggid017f882014-01-14 17:08:48 -08001376 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001377 return mOverviewPanel;
1378 }
1379
Sunny Goyal47328fd2016-05-25 18:56:41 -07001380 public DropTargetBar getDropTargetBar() {
1381 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001382 }
1383
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001384 public LauncherAppWidgetHost getAppWidgetHost() {
1385 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
Romain Guycbb89e42009-06-08 15:52:54 -07001387
Winson Chunga9abd0e2010-10-27 17:18:37 -07001388 public LauncherModel getModel() {
1389 return mModel;
1390 }
1391
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001392 public ModelWriter getModelWriter() {
1393 return mModelWriter;
1394 }
1395
Adam Cohen79d90c52016-04-22 13:29:20 -07001396 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001397 return mSharedPrefs;
1398 }
1399
Jon Miranda6bed3502017-09-19 14:43:17 -07001400 public int getOrientation() { return mOrientation; }
1401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 @Override
1403 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001404 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 super.onNewIntent(intent);
1406
Winson15f8b172015-08-19 11:02:39 -07001407 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1408 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1409 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001410
1411 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001412 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001413 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001414
Winson15f8b172015-08-19 11:02:39 -07001415 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1416 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001417 if (mWorkspace == null) {
1418 // Can be cases where mWorkspace is null, this prevents a NPE
1419 return;
1420 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001421
1422 // Note: There should be at most one log per method call. This is enforced implicitly
1423 // by using if-else statements.
1424 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001425 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001426 if (topOpenView != null) {
1427 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001428 } else if (alreadyOnHome) {
Sunny Goyalea609262017-10-25 15:47:38 -07001429 Target target = newContainerTarget(mStateManager.getState().containerType);
Sunny Goyalaeb16432017-10-16 11:46:41 -07001430 target.pageIndex = mWorkspace.getCurrentPage();
1431 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001432 }
1433
1434 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001435 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001436 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001437
1438 final View v = getWindow().peekDecorView();
1439 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001440 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001441 }
1442
Winson Chungb745afb2015-03-02 11:51:23 -08001443 // Reset the apps view
1444 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001445 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001446 }
1447
Adam Cohen9211d422014-10-07 18:14:53 -07001448 if (mLauncherCallbacks != null) {
1449 mLauncherCallbacks.onHomeIntent();
1450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001452 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001453
Adam Cohen9211d422014-10-07 18:14:53 -07001454 if (mLauncherCallbacks != null) {
1455 mLauncherCallbacks.onNewIntent(intent);
1456 }
Winson15f8b172015-08-19 11:02:39 -07001457
1458 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1459 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1460 // animation.
1461 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001462 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001463
Winson15f8b172015-08-19 11:02:39 -07001464 mWorkspace.post(new Runnable() {
1465 @Override
1466 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001467 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001468 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001469 }
Winson15f8b172015-08-19 11:02:39 -07001470 }
1471 });
1472 }
1473 }
1474
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001475 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001476 }
1477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001479 public void onRestoreInstanceState(Bundle state) {
1480 super.onRestoreInstanceState(state);
1481 for (int page: mSynchronouslyBoundPages) {
1482 mWorkspace.restoreInstanceStateForChild(page);
1483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
1485
1486 @Override
1487 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001488 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001489 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001490
Adam Cohen21cd0022013-10-09 18:57:02 -07001491 }
Sunny Goyalea609262017-10-25 15:47:38 -07001492 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001493
1494
1495 AbstractFloatingView widgets = AbstractFloatingView
1496 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1497 if (widgets != null) {
1498 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1499 widgets.saveHierarchyState(widgetsState);
1500 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1501 } else {
1502 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1503 }
1504
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001505 // We close any open folders and shortcut containers since they will not be re-opened,
1506 // and we need to make sure this state is reflected.
1507 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001508
Sunny Goyal2100c782016-08-22 16:00:03 -07001509 if (mPendingRequestArgs != null) {
1510 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1511 }
1512 if (mPendingActivityResult != null) {
1513 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001516 super.onSaveInstanceState(outState);
1517
Adam Cohen9211d422014-10-07 18:14:53 -07001518 if (mLauncherCallbacks != null) {
1519 mLauncherCallbacks.onSaveInstanceState(outState);
1520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
1522
1523 @Override
1524 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001526
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001527 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001528 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001529
Winson Chungcd2b0142011-06-08 16:02:26 -07001530 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001531 // It's possible to receive onDestroy after a new Launcher activity has
1532 // been created. In this case, don't interfere with the new Launcher.
1533 if (mModel.isCurrentCallbacks(this)) {
1534 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001535 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001536 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001537
Sunny Goyal745bad92016-05-02 10:54:12 -07001538 if (mRotationPrefChangeHandler != null) {
1539 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001543 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001545 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001547 mAppWidgetHost = null;
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 TextKeyListener.getInstance().release();
1550
Tony Wickhame2217252016-03-22 16:34:23 -07001551 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1552 .removeAccessibilityStateChangeListener(this);
1553
Mario Bertschler27288382017-05-24 15:35:09 -07001554 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1555
Michael Jurka2ecf9952012-06-18 12:52:28 -07001556 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001557
Tony2917a8b2017-07-31 23:29:42 -07001558 clearPendingBinds();
1559
Adam Cohen9211d422014-10-07 18:14:53 -07001560 if (mLauncherCallbacks != null) {
1561 mLauncherCallbacks.onDestroy();
1562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
1564
Sunny Goyalae502842016-06-17 08:43:56 -07001565 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1566 return mAccessibilityDelegate;
1567 }
1568
Adam Cohena9cf38f2011-05-02 15:36:58 -07001569 public DragController getDragController() {
1570 return mDragController;
1571 }
1572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001574 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001575 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001576 }
1577
1578 @Override
1579 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1580 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001581 try {
1582 super.startIntentSenderForResult(intent, requestCode,
1583 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1584 } catch (IntentSender.SendIntentException e) {
1585 throw new ActivityNotFoundException();
1586 }
1587 }
1588
Winson Chung70d72102011-08-12 11:24:35 -07001589 /**
1590 * Indicates that we want global search for this activity by setting the globalSearch
1591 * argument for {@link #startSearch} to true.
1592 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001594 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001596
Karl Rosaen138a0412009-04-23 19:00:21 -07001597 if (initialQuery == null) {
1598 // Use any text typed in the launcher as the initial query
1599 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001600 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 if (appSearchData == null) {
1602 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001603 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001605
Sunny Goyal6f28e712016-09-13 14:19:29 -07001606 if (mLauncherCallbacks == null ||
1607 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1608 // Starting search from the callbacks failed. Start the default global search.
1609 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001610 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001611
1612 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001613 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001614 }
1615
Ian Parkinson047036e2014-09-01 15:40:53 +01001616 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001617 * Starts the global search activity. This code is a copied from SearchManager
1618 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001619 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001620 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001621 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001622 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1623 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1624 if (globalSearchActivity == null) {
1625 Log.w(TAG, "No global search activity found.");
1626 return;
1627 }
1628 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1629 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1630 intent.setComponent(globalSearchActivity);
1631 // Make sure that we have a Bundle to put source in
1632 if (appSearchData == null) {
1633 appSearchData = new Bundle();
1634 } else {
1635 appSearchData = new Bundle(appSearchData);
1636 }
Adam Cohen9211d422014-10-07 18:14:53 -07001637 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001638 if (!appSearchData.containsKey("source")) {
1639 appSearchData.putString("source", getPackageName());
1640 }
1641 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1642 if (!TextUtils.isEmpty(initialQuery)) {
1643 intent.putExtra(SearchManager.QUERY, initialQuery);
1644 }
1645 if (selectInitialQuery) {
1646 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1647 }
1648 intent.setSourceBounds(sourceBounds);
1649 try {
1650 startActivity(intent);
1651 } catch (ActivityNotFoundException ex) {
1652 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 }
1655
Winson Chung70d72102011-08-12 11:24:35 -07001656 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001657 public boolean onPrepareOptionsMenu(Menu menu) {
1658 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001659 if (mLauncherCallbacks != null) {
1660 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1661 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001662 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001665 @Override
1666 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001667 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001668 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001669 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001670 }
1671
Joe Onorato9c1289c2009-08-17 11:03:03 -04001672 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001673 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001674 }
1675
Adam Cohen517a7f52014-03-01 12:12:59 -08001676 public boolean isWorkspaceLoading() {
1677 return mWorkspaceLoading;
1678 }
1679
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001680 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001681 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001682 }
1683
Sunny Goyal04a324a2017-01-20 21:08:59 -08001684 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001685 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001686 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001687
Sunny Goyal2100c782016-08-22 16:00:03 -07001688 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001689 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001690 if (LOGD) {
1691 Log.d(TAG, "Adding widget from drop");
1692 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001693 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001694 }
1695
Sunny Goyal2100c782016-08-22 16:00:03 -07001696 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001697 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1698 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1699 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001700
Adam Cohenad4e15c2013-10-17 16:21:35 -07001701 Runnable onComplete = new Runnable() {
1702 @Override
1703 public void run() {
1704 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001705 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001706 }
1707 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001708 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001709 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 }
1711 }
Romain Guycbb89e42009-06-08 15:52:54 -07001712
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001713 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1714 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001715 info.container = container;
1716 info.screenId = screenId;
1717 if (cell != null) {
1718 info.cellX = cell[0];
1719 info.cellY = cell[1];
1720 }
1721 info.spanX = spanX;
1722 info.spanY = spanY;
1723
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001724 switch (info.itemType) {
1725 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1726 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001727 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001728 break;
1729 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001730 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001731 break;
1732 default:
1733 throw new IllegalStateException("Unknown item type: " + info.itemType);
1734 }
1735 }
1736
Adam Cohenfbba09b2011-07-18 21:43:05 -07001737 /**
1738 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001739 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001740 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001741 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1742 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001743 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1744 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1745 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001746 }
1747
Adam Cohenfbba09b2011-07-18 21:43:05 -07001748 /**
1749 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001750 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001751 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001752 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001753 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001754 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001755 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001756 // In the case where we've prebound the widget, we remove it from the DragLayer
1757 if (LOGD) {
1758 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1759 }
1760 getDragLayer().removeView(hostView);
1761
Adam Cohened66b2b2012-01-23 17:28:51 -08001762 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001763 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001764
1765 // Clear the boundWidget so that it doesn't get destroyed.
1766 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001767 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001768 // In this case, we either need to start an activity to get permission to bind
1769 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001770 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1771 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1772 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1773 this, info.componentName);
1774 } else {
1775 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1776 }
Adam Cohendd70d662012-10-04 16:53:44 -07001777 Bundle options = info.bindOptions;
1778
Sunny Goyalffe83f12014-08-14 17:39:34 -07001779 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1780 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001781 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001782 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001783 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001784 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001785 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001786 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001787 }
1788
Adam Cohendcd297f2013-06-18 13:13:40 -07001789 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001790 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001791 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 folderInfo.title = getText(R.string.folder_name);
1793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001795 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796
1797 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001798 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301799 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001800 // Force measure the new folder icon
1801 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1802 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001803 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
Romain Guycbb89e42009-06-08 15:52:54 -07001805
Winsonc0b52fe2015-09-09 16:38:15 -07001806 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001807 * Unbinds the view for the specified item, and removes the item and all its children.
1808 *
1809 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001810 * @param itemInfo the {@link ItemInfo} for this view.
1811 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001812 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001813 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001814 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001815 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001816 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1817 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001818 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001819 } else {
1820 mWorkspace.removeWorkspaceItem(v);
1821 }
Winsonc0b52fe2015-09-09 16:38:15 -07001822 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001823 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001824 }
1825 } else if (itemInfo instanceof FolderInfo) {
1826 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001827 if (v instanceof FolderIcon) {
1828 ((FolderIcon) v).removeListeners();
1829 }
Winsonc0b52fe2015-09-09 16:38:15 -07001830 mWorkspace.removeWorkspaceItem(v);
1831 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001832 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001833 }
1834 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1835 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001836 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001837 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001838 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001839 }
1840 } else {
1841 return false;
1842 }
1843 return true;
1844 }
1845
1846 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001847 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001848 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001849 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001850 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001851 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001852 // Deleting an app widget ID is a void call but writes to disk before returning
1853 // to the caller...
1854 new AsyncTask<Void, Void, Void>() {
1855 public Void doInBackground(Void ... args) {
1856 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1857 return null;
1858 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001859 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001860 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001861 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001862 }
1863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 @Override
1865 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001866 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
1868
Joe Onorato88ec0992009-11-19 13:16:06 -08001869 @Override
1870 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001871 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1872 return;
1873 }
1874
Sunny Goyal45478022015-06-08 16:52:41 -07001875 if (mDragController.isDragging()) {
1876 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001877 return;
1878 }
1879
Jon Mirandafeba90f2016-10-06 10:53:29 -07001880 // Note: There should be at most one log per method call. This is enforced implicitly
1881 // by using if-else statements.
1882 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001883 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1884 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001885 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001886 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001887 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001888 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001889 } else {
1890 // Back button is a no-op here, but give at least some feedback for the button press
1891 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001892 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001893 }
1894
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 * Launches the intent referred by the clicked shortcut.
1897 *
1898 * @param v The view representing the clicked shortcut.
1899 */
1900 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001901 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1902 // view has detached (it's possible for this to happen if the view is removed mid touch).
1903 if (v.getWindowToken() == null) {
1904 return;
1905 }
1906
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001907 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001908 return;
1909 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001910
Adam Cohen1697b792013-09-17 19:08:21 -07001911 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001912 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001913 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1914 LauncherLogProto.Action.Direction.NONE,
1915 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001916 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001917 }
1918 return;
1919 }
1920
1921 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001922 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001923 int page = mWorkspace.indexOfChild(v);
1924 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1925 LauncherLogProto.Action.Direction.NONE,
1926 LauncherLogProto.ContainerType.OVERVIEW, page);
1927 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001928 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001929 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001930 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001931 }
1932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001934 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001935 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001937 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001938 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001939 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001940 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001941 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001942 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001943 } else if (tag instanceof AppInfo) {
1944 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001945 } else if (tag instanceof LauncherAppWidgetInfo) {
1946 if (v instanceof PendingAppWidgetHostView) {
1947 onClickPendingWidget((PendingAppWidgetHostView) v);
1948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950 }
1951
Sunny Goyal70660032015-05-14 00:07:08 -07001952 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001953 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001954 return false;
1955 }
1956
Michael Jurkaaf442092010-06-10 17:01:57 -07001957 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001958 * Event handler for the app widget view which has not fully restored.
1959 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001960 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001961 if (mIsSafeModeEnabled) {
1962 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1963 return;
1964 }
1965
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001966 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001967 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001968 LauncherAppWidgetProviderInfo appWidgetInfo =
1969 mAppWidgetManager.findProvider(info.providerName, info.user);
1970 if (appWidgetInfo == null) {
1971 return;
1972 }
1973 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1974
Sunny Goyald478c832016-04-01 12:04:16 -07001975 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1976 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1977 // This should not happen, as we make sure that an Id is allocated during bind.
1978 return;
1979 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001980 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1981 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001982 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001983 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001984 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001985 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001986 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001987 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001988 }
1989 }
1990
1991 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001992 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1993 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1994 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001995 *
1996 * @param v The view that was clicked.
1997 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001998 protected void onClickAllAppsButton(View v) {
1999 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002000 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302001 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2002 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002003 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002004 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002005 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002006 }
2007 }
2008
Sunny Goyale6e72002017-01-12 16:55:36 -08002009 private void onClickPendingAppItem(final View v, final String packageName,
2010 boolean downloadStarted) {
2011 if (downloadStarted) {
2012 // If the download has started, simply direct to the market app.
2013 startMarketIntentForPackage(v, packageName);
2014 return;
2015 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002016 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002017 .setTitle(R.string.abandoned_promises_title)
2018 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002019 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2020 @Override
2021 public void onClick(DialogInterface dialogInterface, int i) {
2022 startMarketIntentForPackage(v, packageName);
2023 }
2024 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002025 .setNeutralButton(R.string.abandoned_clean_this,
2026 new DialogInterface.OnClickListener() {
2027 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002028 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002029 mWorkspace.removeAbandonedPromise(packageName, user);
2030 }
2031 })
2032 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002033 }
2034
2035 private void startMarketIntentForPackage(View v, String packageName) {
2036 ItemInfo item = (ItemInfo) v.getTag();
2037 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002038 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002039 }
2040
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002041 /**
2042 * Event handler for an app shortcut click.
2043 *
2044 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2045 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002046 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002047 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2048 Object tag = v.getTag();
2049 if (!(tag instanceof ShortcutInfo)) {
2050 throw new IllegalArgumentException("Input must be a Shortcut");
2051 }
2052
2053 // Open shortcut
2054 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002055
2056 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002057 if ((shortcut.isDisabled &
2058 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2059 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2060 // If the app is only disabled because of the above flags, launch activity anyway.
2061 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002062 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002063 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2064 // Use a message specific to this shortcut, if it has one.
2065 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2066 return;
2067 }
2068 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002069 int error = R.string.activity_not_available;
2070 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2071 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002072 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2073 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002074 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002075 }
2076 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2077 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002078 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002079 }
2080
Chris Wren40c5ed32014-06-24 18:24:23 -04002081 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002082 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002083 String packageName = shortcut.intent.getComponent() != null ?
2084 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2085 if (!TextUtils.isEmpty(packageName)) {
2086 onClickPendingAppItem(v, packageName,
2087 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2088 return;
2089 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002090 }
2091
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002092 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002093 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002094 }
2095
Sunny Goyala7ce1662016-05-31 15:01:35 -07002096 private void startAppShortcutOrInfoActivity(View v) {
2097 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002098 Intent intent;
2099 if (item instanceof PromiseAppInfo) {
2100 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2101 intent = promiseAppInfo.getMarketIntent();
2102 } else {
2103 intent = item.getIntent();
2104 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002105 if (intent == null) {
2106 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002107 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002108 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002109 }
2110
2111 /**
2112 * Event handler for a folder icon click.
2113 *
2114 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2115 */
2116 protected void onClickFolderIcon(View v) {
2117 if (LOGD) Log.d(TAG, "onClickFolder");
2118 if (!(v instanceof FolderIcon)){
2119 throw new IllegalArgumentException("Input must be a FolderIcon");
2120 }
2121
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002122 Folder folder = ((FolderIcon) v).getFolder();
2123 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002124 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002125 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002126 }
Michael Jurka5130e402011-10-13 04:55:35 -07002127 }
2128
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002129 /**
2130 * Event handler for the (Add) Widgets button that appears after a long press
2131 * on the home screen.
2132 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002133 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002134 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002135 if (mIsSafeModeEnabled) {
2136 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2137 } else {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002138 WidgetsFullSheet.show(this, true /* animated */);
Adam Cohen9211d422014-10-07 18:14:53 -07002139 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002140 }
2141
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002142 /**
2143 * Event handler for the wallpaper picker button that appears after a long press
2144 * on the home screen.
2145 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002146 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002147 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002148 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2149 return;
2150 }
2151
Tony Wickham785f7a52015-08-31 17:28:32 -07002152 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2153 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002154 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002155 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002156 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002157
2158 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002159 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2160 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002161 intent.setPackage(pickerPackage);
2162 }
2163
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002164 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002165 try {
2166 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2167 // If there is no target package, use the default intent chooser animation
2168 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2169 } catch (ActivityNotFoundException e) {
2170 setWaitingForResult(null);
2171 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2172 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002173 }
2174
2175 /**
2176 * Event handler for a click on the settings button that appears after a long press
2177 * on the home screen.
2178 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002179 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002180 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002181 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2182 .setPackage(getPackageName());
2183 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002184 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002185 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002186 }
2187
Tony Wickhame2217252016-03-22 16:34:23 -07002188 @Override
2189 public void onAccessibilityStateChanged(boolean enabled) {
2190 mDragLayer.onAccessibilityStateChanged(enabled);
2191 }
2192
Sunny Goyala7ce1662016-05-31 15:01:35 -07002193 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002195 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2196 try {
2197 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2198 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2199 // is enabled by default on NYC.
2200 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2201 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002202
2203 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2204 String id = ((ShortcutInfo) info).getDeepShortcutId();
2205 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302206 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002207 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002208 } else {
2209 // Could be launching some bookkeeping activity
2210 startActivity(intent, optsBundle);
2211 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002212 } finally {
2213 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002216 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2217 // corresponding permission. Show the appropriate permission prompt if that
2218 // is the case.
2219 if (intent.getComponent() == null
2220 && Intent.ACTION_CALL.equals(intent.getAction())
2221 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2222 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002223
2224 setWaitingForResult(PendingRequestArgs
2225 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002226 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2227 REQUEST_PERMISSION_CALL_PHONE);
2228 } else {
2229 // No idea why this was thrown.
2230 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002234
Sunny Goyalfe770c92016-09-27 14:38:58 -07002235 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002236 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002237 if (Utilities.ATLEAST_MARSHMALLOW) {
2238 int left = 0, top = 0;
2239 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002240 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002241 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002242 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002243 if (icon != null) {
2244 Rect bounds = icon.getBounds();
2245 left = (width - bounds.width()) / 2;
2246 top = v.getPaddingTop();
2247 width = bounds.width();
2248 height = bounds.height();
2249 }
2250 }
2251 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2252 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2253 // On L devices, we use the device default slide-up transition.
2254 // On L MR1 devices, we use a custom version of the slide-up transition which
2255 // doesn't have the delay present in the device default.
2256 return ActivityOptions.makeCustomAnimation(
2257 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2258 }
2259 return null;
2260 }
2261
Tonye3c59252017-05-02 21:32:27 -07002262 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002263 int[] pos = new int[2];
2264 v.getLocationOnScreen(pos);
2265 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2266 }
2267
Sunny Goyala7ce1662016-05-31 15:01:35 -07002268 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002269 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002270 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2271 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002272 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002273 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002274 // Only launch using the new animation if the shortcut has not opted out (this is a
2275 // private contract between launcher and may be ignored in the future).
2276 boolean useLaunchAnimation = (v != null) &&
2277 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2278 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2279
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002280 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002281
2282 // Prepare intent
2283 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2284 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002285 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002286 }
2287 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002288 if (Utilities.ATLEAST_MARSHMALLOW
2289 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002290 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002291 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002292 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002293 // Shortcuts need some special checks due to legacy reasons.
2294 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002295 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002296 // Could be launching some bookkeeping activity
2297 startActivity(intent, optsBundle);
2298 } else {
2299 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2300 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2301 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002302
2303 if (v instanceof BubbleTextView) {
2304 // This is set to the view that launched the activity that navigated the user away
2305 // from launcher. Since there is no callback for when the activity has finished
2306 // launching, enable the press state and keep this reference to reset the press
2307 // state when we return to launcher.
2308 BubbleTextView btv = (BubbleTextView) v;
2309 btv.setStayPressed(true);
2310 setOnResumeCallback(btv);
2311 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002312 mAppLaunchSuccess = true;
2313 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002314 } catch (ActivityNotFoundException|SecurityException e) {
2315 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2316 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2317 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002318 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002319 }
2320
Tony Wickhamdadb3042016-02-24 11:07:00 -08002321 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002322 public boolean dispatchTouchEvent(MotionEvent ev) {
2323 mLastDispatchTouchEventX = ev.getX();
2324 return super.dispatchTouchEvent(ev);
2325 }
2326
2327 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002329 if (!isDraggingEnabled()) return false;
2330 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002331 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332
Jon Miranda51f037d2016-11-07 08:37:20 -08002333 boolean ignoreLongPressToOverview =
2334 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002335
Adam Cohen1697b792013-09-17 19:08:21 -07002336 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002337 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002338 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302339 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2340 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002341 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002342 getStateManager().goToState(OVERVIEW);
Adam Cohen93c97562013-09-26 13:48:01 -07002343 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2344 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2345 return true;
2346 } else {
2347 return false;
2348 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002349 } else {
2350 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002351 }
Adam Cohen1697b792013-09-17 19:08:21 -07002352 }
2353
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002354 CellLayout.CellInfo longClickCellInfo = null;
2355 View itemUnderLongClick = null;
2356 if (v.getTag() instanceof ItemInfo) {
2357 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002358 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002359 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002360 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 }
2362
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 // The hotseat touch handling does not go through Workspace, and we always allow long press
2364 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002365 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002366 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002367 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002368 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002369 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302370 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2371 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002372 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002373 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002374 return false;
2375 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302376 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2377 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002378 mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002379 getStateManager().goToState(OVERVIEW);
Adam Cohendedbd962013-07-11 14:21:49 -07002380 }
Jon Miranda379198e2016-09-23 08:54:40 -07002381 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2382 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002384 final boolean isAllAppsButton =
2385 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2386 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2387 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002388 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002390 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 }
2392 }
2393 }
2394 return true;
2395 }
2396
Winson Chung3d503fb2011-07-13 17:25:49 -07002397 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002398 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002399 return mHotseat != null && layout != null &&
2400 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2401 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002402
Winson Chung3d503fb2011-07-13 17:25:49 -07002403 /**
2404 * Returns the CellLayout of the specified container at the specified screen.
2405 */
Sunny Goyal92820592015-03-02 11:31:35 -08002406 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002407 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2408 if (mHotseat != null) {
2409 return mHotseat.getLayout();
2410 } else {
2411 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002412 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002413 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002414 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002415 }
2416 }
2417
Michael Jurkae326f182011-11-21 14:05:46 -08002418 @Override
2419 public void onTrimMemory(int level) {
2420 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002421 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2422 // The widget preview db can result in holding onto over
2423 // 3MB of memory for caching which isn't necessary.
2424 SQLiteDatabase.releaseMemory();
2425
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002426 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002427 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002428 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002429 if (mLauncherCallbacks != null) {
2430 mLauncherCallbacks.onTrimMemory(level);
2431 }
Michael Jurkae326f182011-11-21 14:05:46 -08002432 }
2433
Michael Jurkad7c28052012-04-27 15:43:36 -07002434 @Override
2435 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002436 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002437 final List<CharSequence> text = event.getText();
2438 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002439 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002440 // TODO: When can workspace be null?
2441 text.add(mWorkspace == null
2442 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002443 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002444 return result;
2445 }
2446
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002447 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002448 * If the activity is currently paused, signal that we need to run the passed Runnable
2449 * in onResume.
2450 *
2451 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002452 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2453 * wrong when we're not running, and if the activity comes back to what the configuration was
2454 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002455 *
2456 * Implementation of the method from LauncherModel.Callbacks.
2457 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002458 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002459 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002460 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002461 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002462 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002463 if (run instanceof RunnableWithId) {
2464 // Remove any runnables which have the same id
2465 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002466 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002467 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002468 return true;
2469 } else {
2470 return false;
2471 }
2472 }
2473
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002474 public void setOnResumeCallback(OnResumeCallback callback) {
2475 if (mOnResumeCallback != null) {
2476 mOnResumeCallback.onLauncherResume();
2477 }
2478 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002479 }
2480
Michael Jurka7607c2f2013-04-03 14:33:19 -07002481 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002482 * If the activity is currently paused, signal that we need to re-run the loader
2483 * in onResume.
2484 *
2485 * This needs to be called from incoming places where resources might have been loaded
2486 * while we are paused. That is becaues the Configuration might be wrong
2487 * when we're not running, and if it comes back to what it was when we
2488 * were paused, we are not restarted.
2489 *
2490 * Implementation of the method from LauncherModel.Callbacks.
2491 *
2492 * @return true if we are currently paused. The caller might be able to
2493 * skip some work in that case since we will come back again.
2494 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002495 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002496 public boolean setLoadOnResume() {
2497 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002498 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002499 mOnResumeNeedsLoad = true;
2500 return true;
2501 } else {
2502 return false;
2503 }
2504 }
2505
2506 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002507 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002509 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002511 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002512 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002513 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002514 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002515 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002516 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002517
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002519 * Clear any pending bind callbacks. This is called when is loader is planning to
2520 * perform a full rebind from scratch.
2521 */
2522 @Override
2523 public void clearPendingBinds() {
2524 mBindOnResumeCallbacks.clear();
2525 if (mPendingExecutor != null) {
2526 mPendingExecutor.markCompleted();
2527 mPendingExecutor = null;
2528 }
2529 }
2530
2531 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002532 * Refreshes the shortcuts shown on the workspace.
2533 *
2534 * Implementation of the method from LauncherModel.Callbacks.
2535 */
2536 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002537 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002538 // Floating panels (except the full widget sheet) are associated with individual icons. If
2539 // we are starting a fresh bind, close all such panels as all the icons are about
2540 // to go away.
2541 AbstractFloatingView.closeOpenViews(this, true,
2542 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002543
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002544 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002545
Winson Chungd64d1762013-08-20 14:37:16 -07002546 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002547 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002548 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002549
Winson Chung3d503fb2011-07-13 17:25:49 -07002550 if (mHotseat != null) {
2551 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002553 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 }
2555
Adam Cohendcd297f2013-06-18 13:13:40 -07002556 @Override
2557 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002558 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002559 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2560 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002561 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2562 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002563 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002564 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2565 // If there are no screens, we need to have an empty screen
2566 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002567 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002568 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002569
Winsonc7d2e832016-07-28 12:24:55 -07002570 // After we have added all the screens, if the wallpaper was locked to the default state,
2571 // then notify to indicate that it can be released and a proper wallpaper offset can be
2572 // computed before the next layout
2573 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002574 }
2575
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002576 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002577 int count = orderedScreenIds.size();
2578 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002579 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002580 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002581 // No need to bind the first screen, as its always bound.
2582 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2583 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002584 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002585 }
2586
Sunny Goyalb23980c2017-08-17 07:45:25 -07002587 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002588 public void bindAppsAdded(final ArrayList<Long> newScreens,
2589 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002590 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002591 Runnable r = new Runnable() {
2592 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002593 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002594 }
2595 };
2596 if (waitUntilResume(r)) {
2597 return;
2598 }
2599
Winson Chungd64d1762013-08-20 14:37:16 -07002600 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002601 if (newScreens != null) {
2602 bindAddScreens(newScreens);
2603 }
Winson Chungd64d1762013-08-20 14:37:16 -07002604
2605 // We add the items without animation on non-visible pages, and with
2606 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002607 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002608 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002609 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002610 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002611 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002612 }
Winson Chungc58497e2013-09-03 17:48:37 -07002613
Winson Chung87412982013-10-03 18:34:14 -07002614 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002615 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002616 }
2617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002619 * Bind the items start-end from the list.
2620 *
2621 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002623 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002624 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002625 Runnable r = new Runnable() {
2626 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002627 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002628 }
2629 };
2630 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002631 return;
2632 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002633
Sunny Goyal3be633b2016-12-08 09:59:25 -08002634 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002635 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002636 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002637 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002638 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002639 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002640 int end = items.size();
2641 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002642 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002643
2644 // Short circuit if we are loading dock items for a configuration which has no dock
2645 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2646 mHotseat == null) {
2647 continue;
2648 }
2649
Sunny Goyal639e9062015-08-19 19:17:06 -07002650 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002651 switch (item.itemType) {
2652 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2653 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002654 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002655 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002656 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002657 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002658 }
2659 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002660 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002661 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002662 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002663 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002664 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002665 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2666 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002667 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002668 if (view == null) {
2669 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002670 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002671 break;
2672 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002673 default:
2674 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002675 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002676
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002677 /*
2678 * Remove colliding items.
2679 */
2680 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2681 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2682 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2683 View v = cl.getChildAt(item.cellX, item.cellY);
2684 Object tag = v.getTag();
2685 String desc = "Collision while binding workspace item: " + item
2686 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002687 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002688 throw (new RuntimeException(desc));
2689 } else {
2690 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002691 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002692 continue;
2693 }
2694 }
2695 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302696 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002697 if (animateIcons) {
2698 // Animate all the applications up now
2699 view.setAlpha(0f);
2700 view.setScaleX(0f);
2701 view.setScaleY(0f);
2702 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002703 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002706
Winson Chung997a9232013-07-24 15:33:46 -07002707 if (animateIcons) {
2708 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002709 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002710 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002711 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002712 final Runnable startBounceAnimRunnable = new Runnable() {
2713 public void run() {
2714 anim.playTogether(bounceAnims);
2715 anim.start();
2716 }
2717 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002718 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002719 // We post the animation slightly delayed to prevent slowdowns
2720 // when we are loading right after we return to launcher.
2721 mWorkspace.postDelayed(new Runnable() {
2722 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002723 if (mWorkspace != null) {
2724 mWorkspace.snapToPage(newScreenIndex);
2725 mWorkspace.postDelayed(startBounceAnimRunnable,
2726 NEW_APPS_ANIMATION_DELAY);
2727 }
Winson Chung94d67682013-09-25 16:29:40 -07002728 }
2729 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002730 } else {
2731 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002732 }
2733 }
Winson Chung64359a52013-07-08 17:17:08 -07002734 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002735 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002736 }
2737
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738 /**
2739 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002740 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002741 public void bindAppWidget(LauncherAppWidgetInfo item) {
2742 View view = inflateAppWidget(item);
2743 if (view != null) {
2744 mWorkspace.addInScreen(view, item);
2745 mWorkspace.requestLayout();
2746 }
2747 }
2748
2749 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002750 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302751 PendingAppWidgetHostView view =
2752 new PendingAppWidgetHostView(this, item, mIconCache, true);
2753 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002754 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002755 }
2756
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002757 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002758
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002759 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002760
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002761 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2762 // If the provider is not ready, bind as a pending widget.
2763 appWidgetInfo = null;
2764 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2765 // The widget id is not valid. Try to find the widget based on the provider info.
2766 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2767 } else {
2768 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2769 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002770
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002771 // If the provider is ready, but the width is not yet restored, try to restore it.
2772 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2773 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002774 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002775 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2776 + " belongs to component " + item.providerName
2777 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002778 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002779 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002780 }
Sunny Goyalff572272014-07-23 13:58:07 -07002781
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002782 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002783 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002784 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2785 // Id has not been allocated yet. Allocate a new id.
2786 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2787 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002788
Sunny Goyald478c832016-04-01 12:04:16 -07002789 // Also try to bind the widget. If the bind fails, the user will be shown
2790 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002791 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002792 pendingInfo.spanX = item.spanX;
2793 pendingInfo.spanY = item.spanY;
2794 pendingInfo.minSpanX = item.minSpanX;
2795 pendingInfo.minSpanY = item.minSpanY;
2796 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002797
2798 boolean isDirectConfig =
2799 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2800 if (isDirectConfig && item.bindOptions != null) {
2801 Bundle newOptions = item.bindOptions.getExtras();
2802 if (options != null) {
2803 newOptions.putAll(options);
2804 }
2805 options = newOptions;
2806 }
Sunny Goyald478c832016-04-01 12:04:16 -07002807 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2808 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002809
Sunny Goyal86df1382016-08-10 15:03:22 -07002810 // We tried to bind once. If we were not able to bind, we would need to
2811 // go through the permission dialog, which means we cannot skip the config
2812 // activity.
2813 item.bindOptions = null;
2814 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2815
Sunny Goyald478c832016-04-01 12:04:16 -07002816 // Bind succeeded
2817 if (success) {
2818 // If the widget has a configure activity, it is still needs to set it up,
2819 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002820 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002821 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2822 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002823 }
Sunny Goyald478c832016-04-01 12:04:16 -07002824
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002825 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002826 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002827 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002828 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002829 // The widget was marked as UI not ready, but there is no configure activity to
2830 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002831 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002832 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002833 }
Sunny Goyalff572272014-07-23 13:58:07 -07002834 }
2835
Sunny Goyald5462aa2016-11-22 16:52:39 +05302836 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002837 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002838 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002839 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002840 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002841 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002842 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002843 }
2844
Sunny Goyal233ee962015-08-03 13:05:01 -07002845 item.minSpanX = appWidgetInfo.minSpanX;
2846 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302847 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002848 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302849 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002850 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302851 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002853 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002854 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002855 }
2856
Sunny Goyalff572272014-07-23 13:58:07 -07002857 /**
2858 * Restores a pending widget.
2859 *
2860 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002861 */
Sunny Goyald478c832016-04-01 12:04:16 -07002862 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002863 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2864 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2865 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002866 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002867 }
2868
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002869 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002870 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002871 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2872 info.pendingItemInfo = null;
2873 }
Sunny Goyalff572272014-07-23 13:58:07 -07002874
Sunny Goyalba47faa2017-10-04 16:50:57 -07002875 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2876 view.reinflate();
2877 }
2878
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002879 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002880 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002881 }
2882
Adam Cohen1462de32012-07-24 22:34:36 -07002883 public void onPageBoundSynchronously(int page) {
2884 mSynchronouslyBoundPages.add(page);
2885 }
2886
Sunny Goyal527c7d32015-08-28 15:19:36 -07002887 @Override
2888 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2889 if (mPendingExecutor != null) {
2890 mPendingExecutor.markCompleted();
2891 }
2892 mPendingExecutor = executor;
2893 executor.attachTo(this);
2894 }
2895
2896 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2897 if (mPendingExecutor == executor) {
2898 mPendingExecutor = null;
2899 }
2900 }
2901
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002902 @Override
2903 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2904 Runnable r = new Runnable() {
2905 public void run() {
2906 finishFirstPageBind(executor);
2907 }
2908 };
2909 if (waitUntilResume(r)) {
2910 return;
2911 }
2912
2913 Runnable onComplete = new Runnable() {
2914 @Override
2915 public void run() {
2916 if (executor != null) {
2917 executor.onLoadAnimationCompleted();
2918 }
2919 }
2920 };
2921 if (mDragLayer.getAlpha() < 1) {
2922 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2923 } else {
2924 onComplete.run();
2925 }
2926 }
2927
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 /**
2929 * Callback saying that there aren't any more items to bind.
2930 *
2931 * Implementation of the method from LauncherModel.Callbacks.
2932 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002933 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002934 Runnable r = new Runnable() {
2935 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002936 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002937 }
2938 };
2939 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002940 return;
2941 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002942 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002943 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002944
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002945 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002946
Sunny Goyal2100c782016-08-22 16:00:03 -07002947 if (mPendingActivityResult != null) {
2948 handleActivityResult(mPendingActivityResult.requestCode,
2949 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2950 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002951 }
2952
Sunny Goyala474a9b2017-05-04 16:47:11 -07002953 InstallShortcutReceiver.disableAndFlushInstallQueue(
2954 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002955
Tony Wickham010d2552017-01-20 08:15:28 -08002956 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002957 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002958 }
2959
Winson Chunga2413752012-04-03 14:22:34 -07002960 private boolean canRunNewAppsAnimation() {
2961 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002962 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002963 }
2964
Winson Chungc9168342013-06-26 14:54:55 -07002965 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002966 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002967 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2968 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002969 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002970 return bounceAnim;
2971 }
2972
Adam Cohen27772732013-11-11 14:00:56 +00002973 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002974 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002975 }
2976
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002977 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 * Add the icons for all apps.
2979 *
2980 * Implementation of the method from LauncherModel.Callbacks.
2981 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002982 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002983 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2984 public void run() {
2985 bindAllApplications(apps);
2986 }
2987 };
2988 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002989 return;
2990 }
2991
Winson Chungb745afb2015-03-02 11:51:23 -08002992 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002993 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002994 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002995 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002996 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002997 return;
2998 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002999
Winson Chungb745afb2015-03-02 11:51:23 -08003000 mAppsView.setApps(apps);
3001 }
Adam Cohen9211d422014-10-07 18:14:53 -07003002 if (mLauncherCallbacks != null) {
3003 mLauncherCallbacks.bindAllApplications(apps);
3004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 }
3006
3007 /**
Tony2917a8b2017-07-31 23:29:42 -07003008 * Returns an Executor that will run after the launcher is first drawn (including after the
3009 * initial fade in animation). Returns null if the first draw has already occurred.
3010 */
3011 public @Nullable Executor getPendingExecutor() {
3012 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3013 }
3014
3015 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003016 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3017 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3018 */
3019 @Override
3020 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003021 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003022 }
3023
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003024 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 * A package was updated.
3026 *
3027 * Implementation of the method from LauncherModel.Callbacks.
3028 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003029 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003030 Runnable r = new Runnable() {
3031 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003032 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003033 }
3034 };
3035 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003036 return;
3037 }
3038
Winson Chungb745afb2015-03-02 11:51:23 -08003039 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003040 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003041 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003042 }
3043
Sunny Goyal4390ace2014-10-13 11:33:11 -07003044 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003045 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3046 Runnable r = new Runnable() {
3047 public void run() {
3048 bindPromiseAppProgressUpdated(app);
3049 }
3050 };
3051 if (waitUntilResume(r)) {
3052 return;
3053 }
3054
3055 if (mAppsView != null) {
3056 mAppsView.updatePromiseAppProgress(app);
3057 }
3058 }
3059
3060 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3062 Runnable r = new Runnable() {
3063 public void run() {
3064 bindWidgetsRestored(widgets);
3065 }
3066 };
3067 if (waitUntilResume(r)) {
3068 return;
3069 }
3070 mWorkspace.widgetsRestored(widgets);
3071 }
3072
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003074 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003075 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003076 *
3077 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003078 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003080 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003081 Runnable r = new Runnable() {
3082 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003083 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003084 }
3085 };
3086 if (waitUntilResume(r)) {
3087 return;
3088 }
3089
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 if (!updated.isEmpty()) {
3091 mWorkspace.updateShortcuts(updated);
3092 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003093 }
3094
3095 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003096 * Update the state of a package, typically related to install state.
3097 *
3098 * Implementation of the method from LauncherModel.Callbacks.
3099 */
Sunny Goyale755d462014-07-22 13:48:29 -07003100 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003101 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3102 Runnable r = new Runnable() {
3103 public void run() {
3104 bindRestoreItemsChange(updates);
3105 }
3106 };
3107 if (waitUntilResume(r)) {
3108 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003109 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003110
Sunny Goyal756adbc2015-04-16 15:20:51 -07003111 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003112 }
3113
3114 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003115 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003116 * in addition to specific applications to remove, the reason being that
3117 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003118 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003119 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003121 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003122 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003123 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003124 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003125 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003126 }
Winson Chungd64d1762013-08-20 14:37:16 -07003127 };
3128 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003129 return;
3130 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003131 mWorkspace.removeItemsByMatcher(matcher);
3132 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003133 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003134
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003135 @Override
3136 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3137 Runnable r = new Runnable() {
3138 public void run() {
3139 bindAppInfosRemoved(appInfos);
3140 }
3141 };
3142 if (waitUntilResume(r)) {
3143 return;
Joe Onorato36115782010-06-17 13:28:48 -04003144 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003145
Winson Chungdf95eb12013-10-16 14:57:07 -07003146 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003147 if (mAppsView != null) {
3148 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003149 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 }
Joe Onoratobe386092009-11-17 17:32:16 -08003151
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003152 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003153 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3154 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003155 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3156 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003157 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003158 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003159 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003160 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003161 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003162 return;
3163 }
3164
Tony Wickham26b17462017-03-20 17:12:24 -07003165 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3166 if (topView != null) {
3167 topView.onWidgetsBound();
3168 }
3169 }
3170
Tony Wickham86222d22017-03-29 15:30:43 -07003171 /**
3172 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3173 * refreshes the widgets and shortcuts associated with the given package/user
3174 */
3175 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003176 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003177 }
3178
Sunny Goyalc99cb172017-10-19 16:15:09 -07003179 public boolean isRotationEnabled () {
3180 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003181 }
3182
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003183 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003184 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003185 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003186 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003187 }
3188
Winson Chung80baf5a2010-08-09 16:03:15 -07003189 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003190 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003191 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003192 @Override
3193 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3194 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003195
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003196 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3197 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003198 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003199 writer.println(prefix + " Homescreen " + i);
3200
3201 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3202 for (int j = 0; j < layout.getChildCount(); j++) {
3203 Object tag = layout.getChildAt(j).getTag();
3204 if (tag != null) {
3205 writer.println(prefix + " " + tag.toString());
3206 }
3207 }
3208 }
3209
3210 writer.println(prefix + " Hotseat");
3211 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003212 for (int j = 0; j < layout.getChildCount(); j++) {
3213 Object tag = layout.getChildAt(j).getTag();
3214 if (tag != null) {
3215 writer.println(prefix + " " + tag.toString());
3216 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003217 }
Sunny Goyala1365452015-10-01 15:46:24 -07003218
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003219 try {
3220 FileLog.flushAll(writer);
3221 } catch (Exception e) {
3222 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003223 }
3224 }
3225
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003226 writer.println(prefix + "Misc:");
3227 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3228 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3229 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3230
3231 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003232
Adam Cohen9211d422014-10-07 18:14:53 -07003233 if (mLauncherCallbacks != null) {
3234 mLauncherCallbacks.dump(prefix, fd, writer, args);
3235 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003236 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003237
Sunny Goyal66b24572016-09-21 15:57:55 -07003238 @Override
3239 @TargetApi(Build.VERSION_CODES.N)
3240 public void onProvideKeyboardShortcuts(
3241 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3242
3243 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003244 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003245 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3246 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3247 }
3248 View currentFocus = getCurrentFocus();
3249 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3250 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3251 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3252 }
Tony18c4aa42017-05-10 21:23:57 -05003253 if (currentFocus.getTag() instanceof ItemInfo
3254 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003255 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3256 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3257 }
3258 if (!shortcutInfos.isEmpty()) {
3259 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3260 }
3261
3262 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3263 }
3264
3265 @Override
3266 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3267 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3268 switch (keyCode) {
3269 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003270 if (isInState(NORMAL)) {
3271 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003272 return true;
3273 }
3274 break;
3275 case KeyEvent.KEYCODE_S: {
3276 View focusedView = getCurrentFocus();
3277 if (focusedView instanceof BubbleTextView
3278 && focusedView.getTag() instanceof ItemInfo
3279 && mAccessibilityDelegate.performAction(focusedView,
3280 (ItemInfo) focusedView.getTag(),
3281 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003282 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003283 return true;
3284 }
3285 break;
3286 }
3287 case KeyEvent.KEYCODE_O:
3288 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3289 return true;
3290 }
3291 break;
3292 }
3293 }
3294 return super.onKeyShortcut(keyCode, event);
3295 }
3296
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003297 public static Launcher getLauncher(Context context) {
3298 if (context instanceof Launcher) {
3299 return (Launcher) context;
3300 }
3301 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3302 }
3303
Sunny Goyal5a81c382017-03-20 15:08:06 -07003304 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003305
3306 @Override
3307 public void onSharedPreferenceChanged(
3308 SharedPreferences sharedPreferences, String key) {
3309 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003310 // Recreate the activity so that it initializes the rotation preference again.
3311 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003312 }
3313 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003314 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003315
3316 /**
3317 * Callback for listening for onResume
3318 */
3319 public interface OnResumeCallback {
3320
3321 void onLauncherResume();
3322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323}