blob: b06081b8127daf7771b767c92589af1e16df49d0 [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 Goyalaeb16432017-10-16 11:46:41 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_NEXT_FRAME;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070023import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyalaeb16432017-10-16 11:46:41 -070024import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070025import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
26import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
27
Sunny Goyal28c6b962015-10-12 11:42:05 -070028import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070030import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070032import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070034import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000035import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070036import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040037import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080039import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070040import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080043import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040044import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070046import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040047import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070049import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070050import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070051import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070052import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070054import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070055import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070062import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070065import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070066import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070072import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070076import android.view.KeyboardShortcutGroup;
77import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080078import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070085import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal651077b2014-06-30 14:15:31 -070090import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070091import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070092import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070093import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070096import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010097import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070098import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -070099import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700101import com.android.launcher3.dragndrop.DragController;
102import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700103import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800105import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700106import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000107import com.android.launcher3.folder.Folder;
108import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700109import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700110import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700111import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700112import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700115import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700116import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800117import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700119import com.android.launcher3.shortcuts.DeepShortcutManager;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800120import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530121import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
122import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
123import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700124import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700125import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700126import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700127import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700128import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700129import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800130import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700131import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700132import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700133import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700134import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700135import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700136import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700137import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700138import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700139import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800140import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800142import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700143import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700144import com.android.launcher3.widget.WidgetListRowEntry;
145import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700146import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700147
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700148import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700149import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700150import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700151import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700152import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700153import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800154import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700155import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157/**
158 * Default launcher application.
159 */
Sunny Goyal27835952017-01-13 12:15:53 -0800160public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700161 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
162 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700163 AccessibilityManager.AccessibilityStateChangeListener,
164 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700165 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700166 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chunga2413752012-04-03 14:22:34 -0700168 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700171 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700172
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700174 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700177 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
178 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700179
Jon Mirandac476d6e2017-05-04 16:30:01 -0700180 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700181
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700182 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
183
Mathew Inwood876a8462013-06-14 14:12:41 +0100184 /**
185 * IntentStarter uses request codes starting with this. This must be greater than all activity
186 * request codes used internally.
187 */
188 protected static final int REQUEST_LAST = 100;
189
Winson Chung2672ff92012-05-04 16:22:30 -0700190 // The Intent extra that defines whether to ignore the launch animation
191 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400192 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 // Type: int
195 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700196 // Type: int
197 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700198 // Type: PendingRequestArgs
199 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
200 // Type: ActivityResultInfo
201 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700202 // Type: SparseArray<Parcelable>
203 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700205 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
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;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700261 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700262 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263
Jon Miranda2d89ea82017-05-04 11:47:53 -0700264 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700265 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700266
Tony Wickham010d2552017-01-20 08:15:28 -0800267 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700268
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700269 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700270
Winson Chung46353de2012-02-16 14:05:10 -0800271 // We only want to get the SharedPreferences once since it does an FS stat each time we get
272 // it from the context.
273 private SharedPreferences mSharedPrefs;
274
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700275 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
276 // state transition. If another state transition happened during this delay,
277 // simply unregister this runnable.
278 private Runnable mExitSpringLoadedModeRunnable;
279
Sunny Goyal2100c782016-08-22 16:00:03 -0700280 // Activity result which needs to be processed after workspace has loaded.
281 private ActivityResultInfo mPendingActivityResult;
282 /**
283 * Holds extra information required to handle a result from an external call, like
284 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
285 */
286 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800287
Jon Miranda379198e2016-09-23 08:54:40 -0700288 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700289
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700290 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700291 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700292 private boolean mAppLaunchSuccess;
293
Sunny Goyal745bad92016-05-02 10:54:12 -0700294 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 @Override
297 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700298 if (DEBUG_STRICT_MODE) {
299 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
300 .detectDiskReads()
301 .detectDiskWrites()
302 .detectNetwork() // or .detectAll() for all detectable problems
303 .penaltyLog()
304 .build());
305 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
306 .detectLeakedSqlLiteObjects()
307 .detectLeakedClosableObjects()
308 .penaltyLog()
309 .penaltyDeath()
310 .build());
311 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700312 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700313
Adam Cohen9211d422014-10-07 18:14:53 -0700314 if (mLauncherCallbacks != null) {
315 mLauncherCallbacks.preOnCreate();
316 }
317
Mario Bertschler27288382017-05-24 15:35:09 -0700318 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
319 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700320 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700321
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700323 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400324
Sunny Goyal87f784c2017-01-11 10:48:34 -0800325 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700326
Adam Cohen2e6da152015-05-06 11:42:25 -0700327 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800328 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700329 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700330 Display display = getWindowManager().getDefaultDisplay();
331 Point mwSize = new Point();
332 display.getSize(mwSize);
333 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
334 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700335
Jon Miranda6bed3502017-09-19 14:43:17 -0700336 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700337 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700338 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800339 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800340 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800341 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700342 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700343
Joe Onorato41a12d22009-10-31 18:30:00 -0400344 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700345 mAllAppsController = new AllAppsTransitionController(this);
346 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700347
Sunny Goyalffe83f12014-08-14 17:39:34 -0700348 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700349
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700350 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700351 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700352
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700353 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
354 // this also ensures that any synchronous binding below doesn't re-trigger another
355 // LauncherModel load.
356 mPaused = false;
357
Sunny Goyal60820d72017-05-09 12:40:11 -0700358 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 setupViews();
Winson1f064272016-07-18 17:18:02 -0700361 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362
Tony Wickham010d2552017-01-20 08:15:28 -0800363 mPopupDataProvider = new PopupDataProvider(this);
364
Tony Wickhame2217252016-03-22 16:34:23 -0700365 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
366 .addAccessibilityStateChangeListener(this);
367
Sunny Goyalfe770c92016-09-27 14:38:58 -0700368 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369
Sunny Goyal2100c782016-08-22 16:00:03 -0700370 // We only load the page synchronously if the user rotates (or triggers a
371 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700372 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
373 if (savedInstanceState != null) {
374 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
375 }
376 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700377 // If we are not binding synchronously, show a fade in animation when
378 // the first page bind completes.
379 mDragLayer.setAlpha(0);
380 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700381 // Pages bound synchronously.
382 mWorkspace.setCurrentPage(currentScreen);
383
Sunny Goyal2100c782016-08-22 16:00:03 -0700384 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386
387 // For handling default keys
388 mDefaultKeySsb = new SpannableStringBuilder();
389 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800390
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800391 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700392 // In case we are on a device with locked rotation, we should look at preferences to check
393 // if the user has specifically allowed rotation.
394 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700395 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700396 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
397 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700398 }
399
Sunny Goyal5a81c382017-03-20 15:08:06 -0700400 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
401 // Temporarily enable the rotation
402 mRotationEnabled = true;
403 }
404
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700405 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
406 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700407 setRequestedOrientation(mRotationEnabled
408 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700409
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800410 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700411
Jon Miranda7fda2852017-07-19 16:07:01 -0700412 // Listen for broadcasts
413 IntentFilter filter = new IntentFilter();
414 filter.addAction(Intent.ACTION_SCREEN_OFF);
415 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
416 registerReceiver(mReceiver, filter);
417 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700418
Sunny Goyal8392c822017-06-20 10:03:56 -0700419 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
420 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700421
422 if (mLauncherCallbacks != null) {
423 mLauncherCallbacks.onCreate(savedInstanceState);
424 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700425
426 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700427 }
428
Sunny Goyal7779d622015-06-11 16:18:39 -0700429 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700430 public void onThemeChanged() {
431 recreate();
432 }
433
Mario Bertschlera6936942017-05-31 14:48:19 -0700434 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700435 if (isDark) {
436 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700437 } else if (supportsDarkText) {
438 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700439 }
440 }
441
442 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700443 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800444 return mLauncherView.findViewById(id);
445 }
446
447 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700448 public void onAppWidgetHostReset() {
449 if (mAppWidgetHost != null) {
450 mAppWidgetHost.startListening();
451 }
452 }
453
Adam Cohen9211d422014-10-07 18:14:53 -0700454 private LauncherCallbacks mLauncherCallbacks;
455
Winson1f064272016-07-18 17:18:02 -0700456 public void onInsetsChanged(Rect insets) {
457 mDeviceProfile.updateInsets(insets);
458 mDeviceProfile.layout(this, true /* notifyListeners */);
459 }
460
Sunny Goyal32554d12015-12-03 15:31:25 -0800461 /**
462 * Call this after onCreate to set or clear overlay.
463 */
464 public void setLauncherOverlay(LauncherOverlay overlay) {
465 if (overlay != null) {
466 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
467 }
468 mWorkspace.setLauncherOverlay(overlay);
469 }
470
Adam Cohen9211d422014-10-07 18:14:53 -0700471 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
472 mLauncherCallbacks = callbacks;
473 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700474 }
475
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700476 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700477 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700478 if (mLauncherCallbacks != null) {
479 mLauncherCallbacks.onLauncherProviderChange();
480 }
481 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700482
Hyunyoung Song3f471442015-04-08 19:01:34 -0700483 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700484 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
485 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700486 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700487 }
488
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000489 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700490 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
491 // This cast is safe as long as the id < 0x00FFFFFF
492 // Since we jail all the dynamically generated views, there should be no clashes
493 // with any other views.
494 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000495 }
496
Tony Wickham010d2552017-01-20 08:15:28 -0800497 public PopupDataProvider getPopupDataProvider() {
498 return mPopupDataProvider;
499 }
500
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000501 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700502 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
503 * a configuration step, this allows the proper animations to run after other transitions.
504 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700505 private long completeAdd(
506 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
507 long screenId = info.screenId;
508 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700509 // When the screen id represents an actual screen (as opposed to a rank) we make sure
510 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700512 }
Adam Cohendb364c32014-05-20 14:23:40 -0700513
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700516 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700520 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700521 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700523 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700524 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 int widgetId = appWidgetId;
526 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700527 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700529 // Since the view was just bound, also launch the configure activity if needed
530 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
531 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800532 if (provider != null) {
533 new WidgetAddFlowHandler(provider)
534 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700535 }
536 }
537 break;
538 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700540
Adam Cohendb364c32014-05-20 14:23:40 -0700541 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800542 }
543
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800544 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700545 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 if (isWorkspaceLoading()) {
547 // process the result once the workspace has loaded.
548 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
549 return;
550 }
551 mPendingActivityResult = null;
552
Winson Chunge341d302013-08-16 14:31:00 -0700553 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 final PendingRequestArgs requestArgs = mPendingRequestArgs;
555 setWaitingForResult(null);
556 if (requestArgs == null) {
557 return;
558 }
559
560 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700561
Adam Cohenad4e15c2013-10-17 16:21:35 -0700562 Runnable exitSpringLoaded = new Runnable() {
563 @Override
564 public void run() {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700565 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700566 }
567 };
568
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700570 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700571 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
573 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700575 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700576 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 addAppWidgetImpl(
579 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800580 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700581 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700582 }
583 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200584 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyalf9403d92017-10-18 10:55:56 -0700585 if (resultCode == RESULT_OK && isInState(LauncherState.OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700586 // User could have free-scrolled between pages before picking a wallpaper; make sure
587 // we move to the closest one now.
588 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700589 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200590 }
591 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700592 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200593
Adam Cohened66b2b2012-01-23 17:28:51 -0800594 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700595 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700596
Adam Cohened66b2b2012-01-23 17:28:51 -0800597 // We have special handling for widgets
598 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800599 final int appWidgetId;
600 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
601 : -1;
602 if (widgetId < 0) {
603 appWidgetId = pendingAddWidgetId;
604 } else {
605 appWidgetId = widgetId;
606 }
607
Adam Cohenad4e15c2013-10-17 16:21:35 -0700608 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800609 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700610 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
611 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700612 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700614 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 @Override
616 public void run() {
Sunny Goyalaeb16432017-10-16 11:46:41 -0700617 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_NEXT_FRAME);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700618 }
619 };
Adam Cohendb364c32014-05-20 14:23:40 -0700620
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
622 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
623 } else {
624 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
625 // When the screen id represents an actual screen (as opposed to a rank)
626 // we make sure that the drop page actually exists.
627 requestArgs.screenId =
628 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700629 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700630 final CellLayout dropLayout =
631 mWorkspace.getScreenWithId(requestArgs.screenId);
632
633 dropLayout.setDropPending(true);
634 final Runnable onComplete = new Runnable() {
635 @Override
636 public void run() {
637 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
638 dropLayout.setDropPending(false);
639 }
640 };
641 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
642 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700643 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 return;
645 }
646
Sunny Goyald478c832016-04-01 12:04:16 -0700647 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
648 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700649 if (resultCode == RESULT_OK) {
650 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700652 }
653 // Leave the widget in the pending state if the user canceled the configure.
654 return;
655 }
656
Sunny Goyalaad90582015-07-09 14:22:50 -0700657 if (requestCode == REQUEST_CREATE_SHORTCUT) {
658 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
660 completeAdd(requestCode, data, -1, requestArgs);
661 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
662 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
663
Sunny Goyalaad90582015-07-09 14:22:50 -0700664 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700665 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
666 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800670 }
671
672 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700673 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800674 final int requestCode, final int resultCode, final Intent data) {
675 handleActivityResult(requestCode, resultCode, data);
676 if (mLauncherCallbacks != null) {
677 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
678 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 }
680
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700681 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700682 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600683 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 PendingRequestArgs pendingArgs = mPendingRequestArgs;
685 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
686 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
687 setWaitingForResult(null);
688
Sunny Goyal28c6b962015-10-12 11:42:05 -0700689 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700693 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700694 Intent intent = pendingArgs.getPendingIntent();
695
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 if (grantResults.length > 0
697 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700698 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 } else {
700 // TODO: Show a snack bar with link to settings
701 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700702 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700703 }
704 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600705 if (mLauncherCallbacks != null) {
706 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
707 grantResults);
708 }
709 }
710
Adam Cohendb364c32014-05-20 14:23:40 -0700711 /**
712 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
713 *
714 * @param screenId the screen id to check
715 * @return the new screen, or screenId if it exists
716 */
717 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800718 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700719 if (dropLayout == null) {
720 // it's possible that the add screen was removed because it was
721 // empty and a re-bind occurred
722 mWorkspace.addExtraEmptyScreen();
723 return mWorkspace.commitExtraEmptyScreen();
724 } else {
725 return screenId;
726 }
727 }
728
Sunny Goyal2100c782016-08-22 16:00:03 -0700729 @Thunk void completeTwoStageWidgetDrop(
730 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
731 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 Runnable onCompleteRunnable = null;
733 int animationType = 0;
734
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 if (resultCode == RESULT_OK) {
737 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
738 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800739 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 onCompleteRunnable = new Runnable() {
742 @Override
743 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700744 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700745 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 }
747 };
748 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700753 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700754 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
755 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700757 // The animated view may be null in the case of a rotation during widget configuration
758 onCompleteRunnable.run();
759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
762 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700763 protected void onStop() {
764 super.onStop();
765 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700766
767 if (mLauncherCallbacks != null) {
768 mLauncherCallbacks.onStop();
769 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700770
Sunny Goyalf5e37442016-11-02 10:31:24 -0700771 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700772 mAppWidgetHost.stopListening();
773 }
Tony Wickham010d2552017-01-20 08:15:28 -0800774
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700775 if (!mAppLaunchSuccess) {
776 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
777 mWorkspace.getState().containerType);
778 }
Tony Wickham010d2552017-01-20 08:15:28 -0800779 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700780 }
781
782 @Override
783 protected void onStart() {
784 super.onStart();
785 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700786
787 if (mLauncherCallbacks != null) {
788 mLauncherCallbacks.onStart();
789 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700790
Sunny Goyalf5e37442016-11-02 10:31:24 -0700791 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700792 mAppWidgetHost.startListening();
793 }
Tony Wickham010d2552017-01-20 08:15:28 -0800794
795 if (!isWorkspaceLoading()) {
796 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
797 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700798
Jon Miranda7fda2852017-07-19 16:07:01 -0700799 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700800 if (mScrimAnimator != null) {
801 mScrimAnimator.cancel();
802 }
803 mDragLayer.getBackground().setAlpha(0);
804 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
805 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
806 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
807 @Override
808 public void onAnimationEnd(Animator animation) {
809 mScrimAnimator = null;
810 }
811 });
812 mScrimAnimator.setDuration(600);
813 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
814 mScrimAnimator.start();
815 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700816 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700817 }
818
819 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700821 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700823 TraceHelper.partitionSection("ON_RESUME", "superCall");
824
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700825 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700826 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800827 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700828 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700829 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700830 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700831 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700833 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200834 // We might have postponed some bind calls until onResume (see waitUntilResume) --
835 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700836 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
837 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200838 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700839 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200840 }
Winson Chunge4e50662012-01-23 14:45:13 -0800841
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700842 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700843 // Process any items that were added while Launcher was away.
844 InstallShortcutReceiver.disableAndFlushInstallQueue(
845 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700846
Sunny Goyala474a9b2017-05-04 16:47:11 -0700847 // Refresh shortcuts if the permission changed.
848 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700849
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700850 if (shouldShowDiscoveryBounce()) {
851 mAllAppsController.showDiscoveryBounce();
852 }
Adam Cohen9211d422014-10-07 18:14:53 -0700853 if (mLauncherCallbacks != null) {
854 mLauncherCallbacks.onResume();
855 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800856
Sunny Goyala502aa32017-10-02 16:04:06 -0700857 clearTypedText();
858
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700859 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700860 }
861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700863 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700864 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700865 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700866
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700867 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700868 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800869 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700870 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700871
Adam Cohen9211d422014-10-07 18:14:53 -0700872 if (mLauncherCallbacks != null) {
873 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700874 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700875 }
876
Adam Cohenc2d6e892014-10-16 09:49:24 -0700877 public interface LauncherOverlay {
878
879 /**
880 * Touch interaction leading to overscroll has begun
881 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700882 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700883
884 /**
885 * Touch interaction related to overscroll has ended
886 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700887 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888
889 /**
890 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
891 * screen (or in the case of RTL, the rightmost screen).
892 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700893 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894
895 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800896 * Called when the launcher is ready to use the overlay
897 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700898 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700899 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900 }
901
902 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700903 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700904 }
905
906 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
907
Sunny Goyalc86df472016-02-25 09:19:38 -0800908 public void onScrollChanged(float progress) {
909 if (mWorkspace != null) {
910 mWorkspace.onOverlayScrollChanged(progress);
911 }
912 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700913 }
914
Jorim Jaggid017f882014-01-14 17:08:48 -0800915 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700916 if (mLauncherCallbacks != null) {
917 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700918 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800919 // On O and above we there is always some setting present settings (add icon to
920 // home screen or icon badging). On earlier APIs we will have the allow rotation
921 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700922 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700923 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800924 }
925
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700926 @Override
927 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700929 if (mModel.isCurrentCallbacks(this)) {
930 mModel.stopLoader();
931 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700932 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
933
Romain Guy13c2e7b2010-03-10 19:45:00 -0800934 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700935 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700936
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800937 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800938 @Override
939 public void onWindowFocusChanged(boolean hasFocus) {
940 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700941 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700942
943 if (mLauncherCallbacks != null) {
944 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
945 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800946 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 private boolean acceptFilter() {
949 final InputMethodManager inputManager = (InputMethodManager)
950 getSystemService(Context.INPUT_METHOD_SERVICE);
951 return !inputManager.isFullscreenMode();
952 }
953
954 @Override
955 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700956 final int uniChar = event.getUnicodeChar();
957 final boolean handled = super.onKeyDown(keyCode, event);
958 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
959 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
961 keyCode, event);
962 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700964 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 // showSearchDialog()
966 // If there are multiple keystrokes before the search dialog takes focus,
967 // onSearchRequested() will be called for every keystroke,
968 // but it is idempotent, so it's fine.
969 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971 }
972
Joe Onorato8a9625e2010-01-28 15:55:35 -0800973 // Eat the long press event so the keyboard doesn't come up.
974 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
975 return true;
976 }
977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 return handled;
979 }
980
Winson46163472015-09-22 17:31:56 -0700981 @Override
982 public boolean onKeyUp(int keyCode, KeyEvent event) {
983 if (keyCode == KeyEvent.KEYCODE_MENU) {
984 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700985 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700986 // Close any open floating view
987 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -0700988
Winson46163472015-09-22 17:31:56 -0700989 // Show the overview mode if we are on the workspace
Sunny Goyalf9403d92017-10-18 10:55:56 -0700990 if (isInState(LauncherState.NORMAL) && !mWorkspace.isSwitchingState()) {
Winson46163472015-09-22 17:31:56 -0700991 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -0700992 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -0700993 }
994 }
995 return true;
996 }
997 return super.onKeyUp(keyCode, event);
998 }
999
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 private String getTypedText() {
1001 return mDefaultKeySsb.toString();
1002 }
1003
Sunny Goyal6f28e712016-09-13 14:19:29 -07001004 @Override
1005 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001006 mDefaultKeySsb.clear();
1007 mDefaultKeySsb.clearSpans();
1008 Selection.setSelection(mDefaultKeySsb, 0);
1009 }
1010
Sunny Goyalf9403d92017-10-18 10:55:56 -07001011 public boolean isInState(LauncherState state) {
1012 return mWorkspace.getState() == state;
1013 }
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 /**
1016 * Restores the previous state, if it exists.
1017 *
1018 * @param savedState The previous state.
1019 */
1020 private void restoreState(Bundle savedState) {
1021 if (savedState == null) {
1022 return;
1023 }
1024
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07001025 int stateOrdinal = savedState.getInt(RUNTIME_STATE, LauncherState.NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001026 LauncherState[] stateValues = LauncherState.values();
1027 LauncherState state = stateValues[stateOrdinal];
1028 if (!state.doNotRestore) {
1029 if (state == LauncherState.ALL_APPS) {
1030 showAppsView(false /* animated */);
1031 } else {
1032 // TODO: Add logic for other states
1033 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001034 }
1035
Sunny Goyal2100c782016-08-22 16:00:03 -07001036 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1037 if (requestArgs != null) {
1038 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001040
1041 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001042
1043 SparseArray<Parcelable> widgetsState =
1044 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1045 if (widgetsState != null) {
1046 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 }
1049
1050 /**
1051 * Finds all the views we need and configure them properly.
1052 */
1053 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001054 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001055 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001056 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001057 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001058
Sunny Goyal784f9c32016-03-23 16:56:54 -07001059 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1060 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1061 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062
Winson Chung4c98d922011-05-31 16:50:48 -07001063 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001064 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001065
Winson Chung3d503fb2011-07-13 17:25:49 -07001066 // Setup the hotseat
1067 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1068 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001069 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001070 }
1071
Winsone9f27272015-10-13 10:47:51 -07001072 // Setup the overview panel
1073 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001074
Winson Chung4c98d922011-05-31 16:50:48 -07001075 // Setup the workspace
1076 mWorkspace.setHapticFeedbackEnabled(false);
1077 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001078 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001079 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1080 // default state, otherwise we will update to the wrong offsets in RTL
1081 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001082 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001083 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001084
Tony Wickham34d2c912015-09-11 09:27:58 -07001085 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001086 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001087
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001088 // Setup Apps
1089 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001090
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001092 mDragController.setMoveTarget(mWorkspace);
1093 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001094 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001095
Hyunyoung Songd725f642017-08-17 22:26:35 -07001096 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001097
Sunny Goyal322d5562015-06-25 19:35:49 -07001098 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1099 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
1102
Winsone9f27272015-10-13 10:47:51 -07001103 private void setupOverviewPanel() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001104 mOverviewPanel = findViewById(R.id.overview_panel);
Winsone9f27272015-10-13 10:47:51 -07001105
Winsone9f27272015-10-13 10:47:51 -07001106 // Bind wallpaper button actions
1107 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301108 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001109 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001110 public void handleViewClick(View view) {
1111 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001112 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001113 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001114
1115 // Bind widget button actions
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301116 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001117 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001118 public void handleViewClick(View view) {
1119 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001120 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001121 }.attachTo(findViewById(R.id.widget_button));
Winsone9f27272015-10-13 10:47:51 -07001122
1123 // Bind settings actions
1124 View settingsButton = findViewById(R.id.settings_button);
1125 boolean hasSettings = hasSettings();
1126 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301127 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001128 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001129 public void handleViewClick(View view) {
1130 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001131 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001132 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001133 } else {
1134 settingsButton.setVisibility(View.GONE);
1135 }
1136
1137 mOverviewPanel.setAlpha(0f);
1138 }
1139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001141 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001142 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001143 */
1144 public void setAllAppsButton(View allAppsButton) {
1145 mAllAppsButton = allAppsButton;
1146 }
1147
Anjali Koppal5ad44842014-03-10 20:34:39 -07001148 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 * Creates a view representing a shortcut.
1150 *
1151 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001153 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001154 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156
1157 /**
1158 * Creates a view representing a shortcut inflated from the specified resource.
1159 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 * @param parent The group the shortcut belongs to.
1161 * @param info The data structure describing the shortcut.
1162 *
1163 * @return A View inflated from layoutResId.
1164 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001165 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001166 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1167 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001168 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001170 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 return favorite;
1172 }
1173
1174 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001175 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 *
1177 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001179 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001180 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001181 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1182 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001183 return;
1184 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001185
Jon Mirandac476d6e2017-05-04 16:30:01 -07001186 int[] cellXY = mTmpAddItemCellCoordinates;
1187 CellLayout layout = getCellLayout(container, screenId);
1188
Sunny Goyal782f0c92017-01-19 10:27:54 -08001189 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001190 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001191 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1192 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001193 }
1194
1195 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001196 // Legacy shortcuts are only supported for primary profile.
1197 info = Process.myUserHandle().equals(args.user)
1198 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001199
Sunny Goyalb57645f2017-03-20 13:57:28 -07001200 if (info == null) {
1201 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1202 return;
1203 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001204 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001205 // The app is trying to add a shortcut without sufficient permissions
1206 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1207 return;
1208 }
1209 }
1210
Jon Mirandac476d6e2017-05-04 16:30:01 -07001211 if (container < 0) {
1212 // Adding a shortcut to the Workspace.
1213 final View view = createShortcut(info);
1214 boolean foundCellSpan = false;
1215 // First we check if we already know the exact location where we want to add this item.
1216 if (cellX >= 0 && cellY >= 0) {
1217 cellXY[0] = cellX;
1218 cellXY[1] = cellY;
1219 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001220
Jon Mirandac476d6e2017-05-04 16:30:01 -07001221 // If appropriate, either create a folder or add to an existing folder
1222 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001223 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001224 return;
1225 }
1226 DragObject dragObject = new DragObject();
1227 dragObject.dragInfo = info;
1228 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1229 true)) {
1230 return;
1231 }
1232 } else {
1233 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1234 }
1235
1236 if (!foundCellSpan) {
1237 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001238 return;
1239 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001240
1241 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1242 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001243 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001244 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001245 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001246 if (folderIcon != null) {
1247 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1248 folderInfo.add(info, args.rank, false);
1249 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001250 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001251 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254
Sunny Goyale29897f2017-07-20 10:09:42 -07001255 public FolderIcon findFolderIcon(final long folderIconId) {
1256 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1257 @Override
1258 public boolean evaluate(ItemInfo info, View view) {
1259 return info != null && info.id == folderIconId;
1260 }
1261 });
1262 }
1263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001265 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001267 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001269 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001270 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1271
Adam Cohened66b2b2012-01-23 17:28:51 -08001272 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001273 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001274 }
Romain Guycbb89e42009-06-08 15:52:54 -07001275
Adam Cohen59400422014-03-05 18:07:04 -08001276 LauncherAppWidgetInfo launcherInfo;
1277 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001278 launcherInfo.spanX = itemInfo.spanX;
1279 launcherInfo.spanY = itemInfo.spanY;
1280 launcherInfo.minSpanX = itemInfo.minSpanX;
1281 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001282 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001283
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001284 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001285 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286
Sunny Goyal2100c782016-08-22 16:00:03 -07001287 if (hostView == null) {
1288 // Perform actual inflation because we're live
1289 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001291 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301292 prepareAppWidget(hostView, launcherInfo);
1293 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
Romain Guycbb89e42009-06-08 15:52:54 -07001295
Sunny Goyald5462aa2016-11-22 16:52:39 +05301296 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001297 hostView.setTag(item);
1298 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001299 hostView.setFocusable(true);
1300 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001301 }
1302
Adam Cohended9f8d2010-11-03 13:25:16 -07001303 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1304 @Override
1305 public void onReceive(Context context, Intent intent) {
1306 final String action = intent.getAction();
1307 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001308 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001309 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001310 if (mAppsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001311 if (!showWorkspace(false)) {
1312 // If we are already on the workspace, then manually reset all apps
1313 mAppsView.reset();
1314 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001315 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001316 mShouldFadeInScrim = true;
1317 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1318 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1319 // the user unlocked and the Launcher is not in the foreground.
1320 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001321 }
1322 }
1323 };
1324
Tony Wickham010d2552017-01-20 08:15:28 -08001325 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1326 Runnable r = new Runnable() {
1327 @Override
1328 public void run() {
1329 mWorkspace.updateIconBadges(updatedBadges);
1330 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001331
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001332 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1333 if (popup instanceof PopupContainerWithArrow) {
1334 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001335 }
Tony Wickham010d2552017-01-20 08:15:28 -08001336 }
1337 };
1338 if (!waitUntilResume(r)) {
1339 r.run();
1340 }
1341 }
1342
Adam Cohended9f8d2010-11-03 13:25:16 -07001343 @Override
1344 public void onAttachedToWindow() {
1345 super.onAttachedToWindow();
1346
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001347 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001348 if (mLauncherCallbacks != null) {
1349 mLauncherCallbacks.onAttachedToWindow();
1350 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001351 }
1352
1353 @Override
1354 public void onDetachedFromWindow() {
1355 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001356
1357 if (mLauncherCallbacks != null) {
1358 mLauncherCallbacks.onDetachedFromWindow();
1359 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001360 }
1361
Winson Chunga6945242014-01-08 14:04:34 -08001362 public DragLayer getDragLayer() {
1363 return mDragLayer;
1364 }
1365
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001366 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001367 return mAppsView;
1368 }
1369
Winson Chunga6945242014-01-08 14:04:34 -08001370 public Workspace getWorkspace() {
1371 return mWorkspace;
1372 }
1373
1374 public Hotseat getHotseat() {
1375 return mHotseat;
1376 }
1377
Jorim Jaggid017f882014-01-14 17:08:48 -08001378 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001379 return mOverviewPanel;
1380 }
1381
Sunny Goyal47328fd2016-05-25 18:56:41 -07001382 public DropTargetBar getDropTargetBar() {
1383 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001384 }
1385
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001386 public LauncherAppWidgetHost getAppWidgetHost() {
1387 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
Romain Guycbb89e42009-06-08 15:52:54 -07001389
Winson Chunga9abd0e2010-10-27 17:18:37 -07001390 public LauncherModel getModel() {
1391 return mModel;
1392 }
1393
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001394 public ModelWriter getModelWriter() {
1395 return mModelWriter;
1396 }
1397
Adam Cohen79d90c52016-04-22 13:29:20 -07001398 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001399 return mSharedPrefs;
1400 }
1401
Jon Miranda6bed3502017-09-19 14:43:17 -07001402 public int getOrientation() { return mOrientation; }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 @Override
1405 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001406 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 super.onNewIntent(intent);
1408
Winson15f8b172015-08-19 11:02:39 -07001409 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1410 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1411 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001412
1413 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyalf9403d92017-10-18 10:55:56 -07001414 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(LauncherState.NORMAL)
1415 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001416
Winson15f8b172015-08-19 11:02:39 -07001417 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1418 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001419 if (mWorkspace == null) {
1420 // Can be cases where mWorkspace is null, this prevents a NPE
1421 return;
1422 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001423
1424 // Note: There should be at most one log per method call. This is enforced implicitly
1425 // by using if-else statements.
1426 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001427 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001428 if (topOpenView != null) {
1429 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001430 } else if (alreadyOnHome) {
Sunny Goyalaeb16432017-10-16 11:46:41 -07001431 Target target = newContainerTarget(mWorkspace.getState().containerType);
1432 target.pageIndex = mWorkspace.getCurrentPage();
1433 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001434 }
1435
1436 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001437 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal53b99362017-10-05 14:58:56 -07001438 showWorkspace(alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001439
1440 final View v = getWindow().peekDecorView();
1441 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001442 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001443 }
1444
Winson Chungb745afb2015-03-02 11:51:23 -08001445 // Reset the apps view
1446 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001447 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001448 }
1449
Adam Cohen9211d422014-10-07 18:14:53 -07001450 if (mLauncherCallbacks != null) {
1451 mLauncherCallbacks.onHomeIntent();
1452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001454 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001455
Adam Cohen9211d422014-10-07 18:14:53 -07001456 if (mLauncherCallbacks != null) {
1457 mLauncherCallbacks.onNewIntent(intent);
1458 }
Winson15f8b172015-08-19 11:02:39 -07001459
1460 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1461 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1462 // animation.
1463 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001464 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001465
Winson15f8b172015-08-19 11:02:39 -07001466 mWorkspace.post(new Runnable() {
1467 @Override
1468 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001469 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001470 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001471 }
Winson15f8b172015-08-19 11:02:39 -07001472 }
1473 });
1474 }
1475 }
1476
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001477 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001478 }
1479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001481 public void onRestoreInstanceState(Bundle state) {
1482 super.onRestoreInstanceState(state);
1483 for (int page: mSynchronouslyBoundPages) {
1484 mWorkspace.restoreInstanceStateForChild(page);
1485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487
1488 @Override
1489 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001490 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001491 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001492
Adam Cohen21cd0022013-10-09 18:57:02 -07001493 }
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07001494 outState.putInt(RUNTIME_STATE, mWorkspace.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001495
1496
1497 AbstractFloatingView widgets = AbstractFloatingView
1498 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1499 if (widgets != null) {
1500 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1501 widgets.saveHierarchyState(widgetsState);
1502 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1503 } else {
1504 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1505 }
1506
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001507 // We close any open folders and shortcut containers since they will not be re-opened,
1508 // and we need to make sure this state is reflected.
1509 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001510
Sunny Goyal2100c782016-08-22 16:00:03 -07001511 if (mPendingRequestArgs != null) {
1512 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1513 }
1514 if (mPendingActivityResult != null) {
1515 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001518 super.onSaveInstanceState(outState);
1519
Adam Cohen9211d422014-10-07 18:14:53 -07001520 if (mLauncherCallbacks != null) {
1521 mLauncherCallbacks.onSaveInstanceState(outState);
1522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
1525 @Override
1526 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001528
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001529 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001530 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001531
Winson Chungcd2b0142011-06-08 16:02:26 -07001532 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001533 // It's possible to receive onDestroy after a new Launcher activity has
1534 // been created. In this case, don't interfere with the new Launcher.
1535 if (mModel.isCurrentCallbacks(this)) {
1536 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001537 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001538 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001539
Sunny Goyal745bad92016-05-02 10:54:12 -07001540 if (mRotationPrefChangeHandler != null) {
1541 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1542 }
1543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001545 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001547 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001549 mAppWidgetHost = null;
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 TextKeyListener.getInstance().release();
1552
Tony Wickhame2217252016-03-22 16:34:23 -07001553 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1554 .removeAccessibilityStateChangeListener(this);
1555
Mario Bertschler27288382017-05-24 15:35:09 -07001556 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1557
Michael Jurka2ecf9952012-06-18 12:52:28 -07001558 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001559
Tony2917a8b2017-07-31 23:29:42 -07001560 clearPendingBinds();
1561
Adam Cohen9211d422014-10-07 18:14:53 -07001562 if (mLauncherCallbacks != null) {
1563 mLauncherCallbacks.onDestroy();
1564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566
Sunny Goyalae502842016-06-17 08:43:56 -07001567 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1568 return mAccessibilityDelegate;
1569 }
1570
Adam Cohena9cf38f2011-05-02 15:36:58 -07001571 public DragController getDragController() {
1572 return mDragController;
1573 }
1574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001576 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001577 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001578 }
1579
1580 @Override
1581 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1582 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001583 try {
1584 super.startIntentSenderForResult(intent, requestCode,
1585 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1586 } catch (IntentSender.SendIntentException e) {
1587 throw new ActivityNotFoundException();
1588 }
1589 }
1590
Winson Chung70d72102011-08-12 11:24:35 -07001591 /**
1592 * Indicates that we want global search for this activity by setting the globalSearch
1593 * argument for {@link #startSearch} to true.
1594 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001596 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001598
Karl Rosaen138a0412009-04-23 19:00:21 -07001599 if (initialQuery == null) {
1600 // Use any text typed in the launcher as the initial query
1601 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 if (appSearchData == null) {
1604 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001605 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001607
Sunny Goyal6f28e712016-09-13 14:19:29 -07001608 if (mLauncherCallbacks == null ||
1609 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1610 // Starting search from the callbacks failed. Start the default global search.
1611 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001612 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001613
1614 // We need to show the workspace after starting the search
1615 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001616 }
1617
Ian Parkinson047036e2014-09-01 15:40:53 +01001618 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001619 * Starts the global search activity. This code is a copied from SearchManager
1620 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001621 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001622 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001623 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001624 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1625 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1626 if (globalSearchActivity == null) {
1627 Log.w(TAG, "No global search activity found.");
1628 return;
1629 }
1630 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1631 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1632 intent.setComponent(globalSearchActivity);
1633 // Make sure that we have a Bundle to put source in
1634 if (appSearchData == null) {
1635 appSearchData = new Bundle();
1636 } else {
1637 appSearchData = new Bundle(appSearchData);
1638 }
Adam Cohen9211d422014-10-07 18:14:53 -07001639 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001640 if (!appSearchData.containsKey("source")) {
1641 appSearchData.putString("source", getPackageName());
1642 }
1643 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1644 if (!TextUtils.isEmpty(initialQuery)) {
1645 intent.putExtra(SearchManager.QUERY, initialQuery);
1646 }
1647 if (selectInitialQuery) {
1648 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1649 }
1650 intent.setSourceBounds(sourceBounds);
1651 try {
1652 startActivity(intent);
1653 } catch (ActivityNotFoundException ex) {
1654 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657
Winson Chung70d72102011-08-12 11:24:35 -07001658 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001659 public boolean onPrepareOptionsMenu(Menu menu) {
1660 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001661 if (mLauncherCallbacks != null) {
1662 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1663 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001664 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001667 @Override
1668 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001669 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001670 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001671 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001672 }
1673
Joe Onorato9c1289c2009-08-17 11:03:03 -04001674 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001675 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001676 }
1677
Adam Cohen517a7f52014-03-01 12:12:59 -08001678 public boolean isWorkspaceLoading() {
1679 return mWorkspaceLoading;
1680 }
1681
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001682 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001683 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001684 }
1685
Sunny Goyal04a324a2017-01-20 21:08:59 -08001686 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001687 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001688 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001689
Sunny Goyal2100c782016-08-22 16:00:03 -07001690 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001691 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001692 if (LOGD) {
1693 Log.d(TAG, "Adding widget from drop");
1694 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001695 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001696 }
1697
Sunny Goyal2100c782016-08-22 16:00:03 -07001698 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001699 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1700 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1701 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001702
Adam Cohenad4e15c2013-10-17 16:21:35 -07001703 Runnable onComplete = new Runnable() {
1704 @Override
1705 public void run() {
1706 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal4c7f2152017-10-17 17:17:16 -07001707 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001708 }
1709 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001710 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001711 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 }
1713 }
Romain Guycbb89e42009-06-08 15:52:54 -07001714
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001715 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1716 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001717 info.container = container;
1718 info.screenId = screenId;
1719 if (cell != null) {
1720 info.cellX = cell[0];
1721 info.cellY = cell[1];
1722 }
1723 info.spanX = spanX;
1724 info.spanY = spanY;
1725
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001726 switch (info.itemType) {
1727 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1728 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001729 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001730 break;
1731 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001732 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001733 break;
1734 default:
1735 throw new IllegalStateException("Unknown item type: " + info.itemType);
1736 }
1737 }
1738
Adam Cohenfbba09b2011-07-18 21:43:05 -07001739 /**
1740 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001741 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001742 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001743 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1744 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001745 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1746 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1747 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001748 }
1749
Adam Cohenfbba09b2011-07-18 21:43:05 -07001750 /**
1751 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001752 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001753 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001754 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001755 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001756 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001757 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001758 // In the case where we've prebound the widget, we remove it from the DragLayer
1759 if (LOGD) {
1760 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1761 }
1762 getDragLayer().removeView(hostView);
1763
Adam Cohened66b2b2012-01-23 17:28:51 -08001764 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001765 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001766
1767 // Clear the boundWidget so that it doesn't get destroyed.
1768 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001769 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001770 // In this case, we either need to start an activity to get permission to bind
1771 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001772 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1773 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1774 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1775 this, info.componentName);
1776 } else {
1777 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1778 }
Adam Cohendd70d662012-10-04 16:53:44 -07001779 Bundle options = info.bindOptions;
1780
Sunny Goyalffe83f12014-08-14 17:39:34 -07001781 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1782 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001783 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001784 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001785 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001786 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001787 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001788 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001789 }
1790
Adam Cohendcd297f2013-06-18 13:13:40 -07001791 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001792 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001793 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 folderInfo.title = getText(R.string.folder_name);
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001797 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798
1799 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001800 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301801 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001802 // Force measure the new folder icon
1803 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1804 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001805 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 }
Romain Guycbb89e42009-06-08 15:52:54 -07001807
Winsonc0b52fe2015-09-09 16:38:15 -07001808 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001809 * Unbinds the view for the specified item, and removes the item and all its children.
1810 *
1811 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001812 * @param itemInfo the {@link ItemInfo} for this view.
1813 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001814 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001815 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001816 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001817 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001818 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1819 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001820 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001821 } else {
1822 mWorkspace.removeWorkspaceItem(v);
1823 }
Winsonc0b52fe2015-09-09 16:38:15 -07001824 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001825 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001826 }
1827 } else if (itemInfo instanceof FolderInfo) {
1828 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001829 if (v instanceof FolderIcon) {
1830 ((FolderIcon) v).removeListeners();
1831 }
Winsonc0b52fe2015-09-09 16:38:15 -07001832 mWorkspace.removeWorkspaceItem(v);
1833 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001834 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001835 }
1836 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1837 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001838 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001839 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001840 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001841 }
1842 } else {
1843 return false;
1844 }
1845 return true;
1846 }
1847
1848 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001849 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001850 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001851 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001852 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001853 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001854 // Deleting an app widget ID is a void call but writes to disk before returning
1855 // to the caller...
1856 new AsyncTask<Void, Void, Void>() {
1857 public Void doInBackground(Void ... args) {
1858 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1859 return null;
1860 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001861 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001862 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001863 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 @Override
1867 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001868 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 }
1870
Joe Onorato88ec0992009-11-19 13:16:06 -08001871 @Override
1872 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001873 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1874 return;
1875 }
1876
Sunny Goyal45478022015-06-08 16:52:41 -07001877 if (mDragController.isDragging()) {
1878 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001879 return;
1880 }
1881
Jon Mirandafeba90f2016-10-06 10:53:29 -07001882 // Note: There should be at most one log per method call. This is enforced implicitly
1883 // by using if-else statements.
1884 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001885 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1886 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001887 topView.onBackPressed();
Sunny Goyalc99cb172017-10-19 16:15:09 -07001888 } else if (!isInState(LauncherState.NORMAL)) {
1889 ued.logActionCommand(Action.Command.BACK, mWorkspace.getState().containerType);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001890 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001891 } else {
1892 // Back button is a no-op here, but give at least some feedback for the button press
1893 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001894 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001895 }
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 * Launches the intent referred by the clicked shortcut.
1899 *
1900 * @param v The view representing the clicked shortcut.
1901 */
1902 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001903 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1904 // view has detached (it's possible for this to happen if the view is removed mid touch).
1905 if (v.getWindowToken() == null) {
1906 return;
1907 }
1908
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001909 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001910 return;
1911 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001912
Adam Cohen1697b792013-09-17 19:08:21 -07001913 if (v instanceof Workspace) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07001914 if (isInState(LauncherState.OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001915 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1916 LauncherLogProto.Action.Direction.NONE,
1917 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07001918 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001919 }
1920 return;
1921 }
1922
1923 if (v instanceof CellLayout) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07001924 if (isInState(LauncherState.OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001925 int page = mWorkspace.indexOfChild(v);
1926 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1927 LauncherLogProto.Action.Direction.NONE,
1928 LauncherLogProto.ContainerType.OVERVIEW, page);
1929 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001930 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001931 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001932 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001933 }
1934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001936 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001937 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001939 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001940 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001941 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001942 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001943 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001944 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001945 } else if (tag instanceof AppInfo) {
1946 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001947 } else if (tag instanceof LauncherAppWidgetInfo) {
1948 if (v instanceof PendingAppWidgetHostView) {
1949 onClickPendingWidget((PendingAppWidgetHostView) v);
1950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952 }
1953
Sunny Goyal70660032015-05-14 00:07:08 -07001954 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001955 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001956 return false;
1957 }
1958
Michael Jurkaaf442092010-06-10 17:01:57 -07001959 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001960 * Event handler for the app widget view which has not fully restored.
1961 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001962 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001963 if (mIsSafeModeEnabled) {
1964 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1965 return;
1966 }
1967
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001968 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001969 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001970 LauncherAppWidgetProviderInfo appWidgetInfo =
1971 mAppWidgetManager.findProvider(info.providerName, info.user);
1972 if (appWidgetInfo == null) {
1973 return;
1974 }
1975 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1976
Sunny Goyald478c832016-04-01 12:04:16 -07001977 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1978 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1979 // This should not happen, as we make sure that an Id is allocated during bind.
1980 return;
1981 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001982 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1983 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001984 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001985 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001986 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001987 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001988 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001989 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001990 }
1991 }
1992
1993 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001994 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1995 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1996 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001997 *
1998 * @param v The view that was clicked.
1999 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002000 protected void onClickAllAppsButton(View v) {
2001 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyalf9403d92017-10-18 10:55:56 -07002002 if (!isInState(LauncherState.ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302003 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2004 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002005 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002006 } else {
2007 showWorkspace(true);
2008 }
2009 }
2010
Sunny Goyale6e72002017-01-12 16:55:36 -08002011 private void onClickPendingAppItem(final View v, final String packageName,
2012 boolean downloadStarted) {
2013 if (downloadStarted) {
2014 // If the download has started, simply direct to the market app.
2015 startMarketIntentForPackage(v, packageName);
2016 return;
2017 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002018 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002019 .setTitle(R.string.abandoned_promises_title)
2020 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002021 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2022 @Override
2023 public void onClick(DialogInterface dialogInterface, int i) {
2024 startMarketIntentForPackage(v, packageName);
2025 }
2026 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002027 .setNeutralButton(R.string.abandoned_clean_this,
2028 new DialogInterface.OnClickListener() {
2029 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002030 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002031 mWorkspace.removeAbandonedPromise(packageName, user);
2032 }
2033 })
2034 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002035 }
2036
2037 private void startMarketIntentForPackage(View v, String packageName) {
2038 ItemInfo item = (ItemInfo) v.getTag();
2039 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002040 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002041 }
2042
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002043 /**
2044 * Event handler for an app shortcut click.
2045 *
2046 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2047 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002048 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002049 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2050 Object tag = v.getTag();
2051 if (!(tag instanceof ShortcutInfo)) {
2052 throw new IllegalArgumentException("Input must be a Shortcut");
2053 }
2054
2055 // Open shortcut
2056 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002057
2058 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002059 if ((shortcut.isDisabled &
2060 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2061 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2062 // If the app is only disabled because of the above flags, launch activity anyway.
2063 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002064 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002065 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2066 // Use a message specific to this shortcut, if it has one.
2067 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2068 return;
2069 }
2070 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002071 int error = R.string.activity_not_available;
2072 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2073 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002074 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2075 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002076 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002077 }
2078 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2079 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002080 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002081 }
2082
Chris Wren40c5ed32014-06-24 18:24:23 -04002083 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002084 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002085 String packageName = shortcut.intent.getComponent() != null ?
2086 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2087 if (!TextUtils.isEmpty(packageName)) {
2088 onClickPendingAppItem(v, packageName,
2089 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2090 return;
2091 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002092 }
2093
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002094 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002095 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002096 }
2097
Sunny Goyala7ce1662016-05-31 15:01:35 -07002098 private void startAppShortcutOrInfoActivity(View v) {
2099 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002100 Intent intent;
2101 if (item instanceof PromiseAppInfo) {
2102 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2103 intent = promiseAppInfo.getMarketIntent();
2104 } else {
2105 intent = item.getIntent();
2106 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002107 if (intent == null) {
2108 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002109 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002110 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002111 }
2112
2113 /**
2114 * Event handler for a folder icon click.
2115 *
2116 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2117 */
2118 protected void onClickFolderIcon(View v) {
2119 if (LOGD) Log.d(TAG, "onClickFolder");
2120 if (!(v instanceof FolderIcon)){
2121 throw new IllegalArgumentException("Input must be a FolderIcon");
2122 }
2123
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002124 Folder folder = ((FolderIcon) v).getFolder();
2125 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002126 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002127 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002128 }
Michael Jurka5130e402011-10-13 04:55:35 -07002129 }
2130
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002131 /**
2132 * Event handler for the (Add) Widgets button that appears after a long press
2133 * on the home screen.
2134 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002135 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002136 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002137 if (mIsSafeModeEnabled) {
2138 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2139 } else {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002140 WidgetsFullSheet.show(this, true /* animated */);
Adam Cohen9211d422014-10-07 18:14:53 -07002141 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002142 }
2143
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002144 /**
2145 * Event handler for the wallpaper picker button that appears after a long press
2146 * on the home screen.
2147 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002148 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002149 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002150 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2151 return;
2152 }
2153
Tony Wickham785f7a52015-08-31 17:28:32 -07002154 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2155 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002156 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002157 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002158 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002159
2160 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002161 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2162 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002163 intent.setPackage(pickerPackage);
2164 }
2165
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002166 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002167 try {
2168 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2169 // If there is no target package, use the default intent chooser animation
2170 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2171 } catch (ActivityNotFoundException e) {
2172 setWaitingForResult(null);
2173 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2174 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002175 }
2176
2177 /**
2178 * Event handler for a click on the settings button that appears after a long press
2179 * on the home screen.
2180 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002181 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002182 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002183 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2184 .setPackage(getPackageName());
2185 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002186 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002187 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002188 }
2189
Tony Wickhame2217252016-03-22 16:34:23 -07002190 @Override
2191 public void onAccessibilityStateChanged(boolean enabled) {
2192 mDragLayer.onAccessibilityStateChanged(enabled);
2193 }
2194
Sunny Goyala7ce1662016-05-31 15:01:35 -07002195 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002197 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2198 try {
2199 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2200 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2201 // is enabled by default on NYC.
2202 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2203 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002204
2205 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2206 String id = ((ShortcutInfo) info).getDeepShortcutId();
2207 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302208 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002209 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002210 } else {
2211 // Could be launching some bookkeeping activity
2212 startActivity(intent, optsBundle);
2213 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002214 } finally {
2215 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002218 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2219 // corresponding permission. Show the appropriate permission prompt if that
2220 // is the case.
2221 if (intent.getComponent() == null
2222 && Intent.ACTION_CALL.equals(intent.getAction())
2223 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2224 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002225
2226 setWaitingForResult(PendingRequestArgs
2227 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002228 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2229 REQUEST_PERMISSION_CALL_PHONE);
2230 } else {
2231 // No idea why this was thrown.
2232 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 }
2235 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002236
Sunny Goyalfe770c92016-09-27 14:38:58 -07002237 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002238 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002239 if (Utilities.ATLEAST_MARSHMALLOW) {
2240 int left = 0, top = 0;
2241 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002242 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002243 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002244 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002245 if (icon != null) {
2246 Rect bounds = icon.getBounds();
2247 left = (width - bounds.width()) / 2;
2248 top = v.getPaddingTop();
2249 width = bounds.width();
2250 height = bounds.height();
2251 }
2252 }
2253 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2254 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2255 // On L devices, we use the device default slide-up transition.
2256 // On L MR1 devices, we use a custom version of the slide-up transition which
2257 // doesn't have the delay present in the device default.
2258 return ActivityOptions.makeCustomAnimation(
2259 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2260 }
2261 return null;
2262 }
2263
Tonye3c59252017-05-02 21:32:27 -07002264 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002265 int[] pos = new int[2];
2266 v.getLocationOnScreen(pos);
2267 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2268 }
2269
Sunny Goyala7ce1662016-05-31 15:01:35 -07002270 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002271 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002272 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2273 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002274 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002275 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002276 // Only launch using the new animation if the shortcut has not opted out (this is a
2277 // private contract between launcher and may be ignored in the future).
2278 boolean useLaunchAnimation = (v != null) &&
2279 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2280 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2281
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002282 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002283
2284 // Prepare intent
2285 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2286 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002287 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002288 }
2289 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002290 if (Utilities.ATLEAST_MARSHMALLOW
2291 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002292 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002293 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002294 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002295 // Shortcuts need some special checks due to legacy reasons.
2296 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002297 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002298 // Could be launching some bookkeeping activity
2299 startActivity(intent, optsBundle);
2300 } else {
2301 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2302 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2303 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002304
2305 if (v instanceof BubbleTextView) {
2306 // This is set to the view that launched the activity that navigated the user away
2307 // from launcher. Since there is no callback for when the activity has finished
2308 // launching, enable the press state and keep this reference to reset the press
2309 // state when we return to launcher.
2310 BubbleTextView btv = (BubbleTextView) v;
2311 btv.setStayPressed(true);
2312 setOnResumeCallback(btv);
2313 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002314 mAppLaunchSuccess = true;
2315 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002316 } catch (ActivityNotFoundException|SecurityException e) {
2317 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2318 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2319 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002320 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002321 }
2322
Tony Wickhamdadb3042016-02-24 11:07:00 -08002323 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002324 public boolean dispatchTouchEvent(MotionEvent ev) {
2325 mLastDispatchTouchEventX = ev.getX();
2326 return super.dispatchTouchEvent(ev);
2327 }
2328
2329 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002331 if (!isDraggingEnabled()) return false;
2332 if (isWorkspaceLocked()) return false;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07002333 if (!isInState(LauncherState.NORMAL) && !isInState(LauncherState.OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334
Jon Miranda51f037d2016-11-07 08:37:20 -08002335 boolean ignoreLongPressToOverview =
2336 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002337
Adam Cohen1697b792013-09-17 19:08:21 -07002338 if (v instanceof Workspace) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002339 if (!isInState(LauncherState.OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002340 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302341 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2342 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002343 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002344 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002345 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2346 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2347 return true;
2348 } else {
2349 return false;
2350 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002351 } else {
2352 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002353 }
Adam Cohen1697b792013-09-17 19:08:21 -07002354 }
2355
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002356 CellLayout.CellInfo longClickCellInfo = null;
2357 View itemUnderLongClick = null;
2358 if (v.getTag() instanceof ItemInfo) {
2359 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002360 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002361 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002362 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 }
2364
Winson Chung3d503fb2011-07-13 17:25:49 -07002365 // The hotseat touch handling does not go through Workspace, and we always allow long press
2366 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002367 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002368 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002369 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002370 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002371 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302372 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2373 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002374 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002375 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002376 return false;
2377 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302378 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2379 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002380 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002381 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002382 }
Jon Miranda379198e2016-09-23 08:54:40 -07002383 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2384 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002386 final boolean isAllAppsButton =
2387 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2388 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2389 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002390 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002392 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 }
2394 }
2395 }
2396 return true;
2397 }
2398
Winson Chung3d503fb2011-07-13 17:25:49 -07002399 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002400 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002401 return mHotseat != null && layout != null &&
2402 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2403 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002404
Winson Chung3d503fb2011-07-13 17:25:49 -07002405 /**
2406 * Returns the CellLayout of the specified container at the specified screen.
2407 */
Sunny Goyal92820592015-03-02 11:31:35 -08002408 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002409 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2410 if (mHotseat != null) {
2411 return mHotseat.getLayout();
2412 } else {
2413 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002414 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002416 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002417 }
2418 }
2419
Michael Jurkae326f182011-11-21 14:05:46 -08002420 @Override
2421 public void onTrimMemory(int level) {
2422 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002423 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2424 // The widget preview db can result in holding onto over
2425 // 3MB of memory for caching which isn't necessary.
2426 SQLiteDatabase.releaseMemory();
2427
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002428 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002429 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002430 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002431 if (mLauncherCallbacks != null) {
2432 mLauncherCallbacks.onTrimMemory(level);
2433 }
Michael Jurkae326f182011-11-21 14:05:46 -08002434 }
2435
Winson5c6bdbb2015-09-03 11:36:19 -07002436 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002437 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002438 }
2439
Winson5c6bdbb2015-09-03 11:36:19 -07002440 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002441 boolean changed = !isInState(LauncherState.NORMAL);
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002442 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002443 mWorkspace.setVisibility(View.VISIBLE);
Sunny Goyalf9403d92017-10-18 10:55:56 -07002444 mStateTransitionAnimation.startAnimationToWorkspace(
Sunny Goyal4c7f2152017-10-17 17:17:16 -07002445 LauncherState.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002446
Michael Jurkab3e22d92011-10-31 15:58:33 -07002447 // Set focus to the AppsCustomize button
2448 if (mAllAppsButton != null) {
2449 mAllAppsButton.requestFocus();
2450 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002451 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002452
Winson Chung0f785722015-04-08 10:27:49 -07002453 if (changed) {
2454 // Send an accessibility event to announce the context change
2455 getWindow().getDecorView()
2456 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002457 }
Winson5c6bdbb2015-09-03 11:36:19 -07002458 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002459 }
2460
Winsone9f27272015-10-13 10:47:51 -07002461 /**
2462 * Shows the overview button.
2463 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002464 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002465 showOverviewMode(animated, false);
2466 }
2467
2468 /**
2469 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2470 * onto one of the overview panel buttons.
2471 */
2472 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2473 Runnable postAnimRunnable = null;
2474 if (requestButtonFocus) {
2475 postAnimRunnable = new Runnable() {
2476 @Override
2477 public void run() {
2478 // Hitting the menu button when in touch mode does not trigger touch mode to
2479 // be disabled, so if requested, force focus on one of the overview panel
2480 // buttons.
2481 mOverviewPanel.requestFocusFromTouch();
2482 }
2483 };
2484 }
Adam Cohened307df2013-10-02 09:37:31 -07002485 mWorkspace.setVisibility(View.VISIBLE);
Sunny Goyalf9403d92017-10-18 10:55:56 -07002486 mStateTransitionAnimation.startAnimationToWorkspace(
Sunny Goyal4c7f2152017-10-17 17:17:16 -07002487 LauncherState.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002488
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002489 // If animated from long press, then don't allow any of the controller in the drag
2490 // layer to intercept any remaining touch.
2491 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002492 }
2493
Winson Chungb745afb2015-03-02 11:51:23 -08002494 /**
2495 * Shows the apps view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002496 *
2497 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002498 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002499 // TODO: calling method should use the return value so that when {@code false} is returned
2500 // the workspace transition doesn't fall into invalid state.
Sunny Goyalaeb16432017-10-16 11:46:41 -07002501 public boolean showAppsView(boolean animated) {
2502 markAppsViewShown();
2503
Sunny Goyalf9403d92017-10-18 10:55:56 -07002504 if (!(isInState(LauncherState.NORMAL) ||
2505 (isInState(LauncherState.ALL_APPS) && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002506 return false;
2507 }
Winson Chungb745afb2015-03-02 11:51:23 -08002508
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002509 // This is a safe and supported transition to bypass spring_loaded mode.
2510 if (mExitSpringLoadedModeRunnable != null) {
2511 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2512 mExitSpringLoadedModeRunnable = null;
2513 }
2514
Sunny Goyalaeb16432017-10-16 11:46:41 -07002515 mStateTransitionAnimation.startAnimationToAllApps(animated);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002516
Michael Jurkab3e22d92011-10-31 15:58:33 -07002517 // Change the state *after* we've called all the transition code
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002518 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002519
2520 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002521 getWindow().getDecorView()
2522 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002523 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002524 }
2525
Hyunyoung Song3f471442015-04-08 19:01:34 -07002526 public void enterSpringLoadedDragMode() {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002527 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07002528 mWorkspace.getState().ordinal));
Sunny Goyalf9403d92017-10-18 10:55:56 -07002529 if (isInState(LauncherState.SPRING_LOADED)) {
Winson Chungb745afb2015-03-02 11:51:23 -08002530 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002531 }
Winson Chungb745afb2015-03-02 11:51:23 -08002532
Sunny Goyalf9403d92017-10-18 10:55:56 -07002533 mStateTransitionAnimation.startAnimationToWorkspace(
Sunny Goyal4c7f2152017-10-17 17:17:16 -07002534 LauncherState.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002535 null /* onCompleteRunnable */);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002536 }
Adam Cohen7777d962011-08-18 18:58:38 -07002537
Sunny Goyalaeb16432017-10-16 11:46:41 -07002538 public void exitSpringLoadedDragMode(int delay) {
2539 exitSpringLoadedDragMode(delay, null);
Sunny Goyal1797af42017-10-06 13:29:57 -07002540 }
2541
Sunny Goyalaeb16432017-10-16 11:46:41 -07002542 public void exitSpringLoadedDragMode(int delay, final Runnable onCompleteRunnable) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002543 if (!isInState(LauncherState.SPRING_LOADED)) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002544
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002545 if (mExitSpringLoadedModeRunnable != null) {
2546 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2547 }
2548 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002549 @Override
2550 public void run() {
Sunny Goyalaeb16432017-10-16 11:46:41 -07002551 showWorkspace(true, onCompleteRunnable);
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002552 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002553 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002554 };
2555 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002556 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002557
Michael Jurkad7c28052012-04-27 15:43:36 -07002558 @Override
2559 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002560 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002561 final List<CharSequence> text = event.getText();
2562 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002563 // Populate event with a fake title based on the current state.
Sunny Goyalf9403d92017-10-18 10:55:56 -07002564 if (isInState(LauncherState.ALL_APPS)) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002565 text.add(getString(R.string.all_apps_button_label));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002566 } else if (mWorkspace != null) {
2567 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002568 } else {
2569 text.add(getString(R.string.all_apps_home_button_label));
2570 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002571 return result;
2572 }
2573
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002574 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002575 * If the activity is currently paused, signal that we need to run the passed Runnable
2576 * in onResume.
2577 *
2578 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002579 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2580 * wrong when we're not running, and if the activity comes back to what the configuration was
2581 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002582 *
2583 * Implementation of the method from LauncherModel.Callbacks.
2584 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002585 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002586 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002587 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002588 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002589 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002590 if (run instanceof RunnableWithId) {
2591 // Remove any runnables which have the same id
2592 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002593 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002594 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002595 return true;
2596 } else {
2597 return false;
2598 }
2599 }
2600
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002601 public void setOnResumeCallback(OnResumeCallback callback) {
2602 if (mOnResumeCallback != null) {
2603 mOnResumeCallback.onLauncherResume();
2604 }
2605 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002606 }
2607
Michael Jurka7607c2f2013-04-03 14:33:19 -07002608 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002609 * If the activity is currently paused, signal that we need to re-run the loader
2610 * in onResume.
2611 *
2612 * This needs to be called from incoming places where resources might have been loaded
2613 * while we are paused. That is becaues the Configuration might be wrong
2614 * when we're not running, and if it comes back to what it was when we
2615 * were paused, we are not restarted.
2616 *
2617 * Implementation of the method from LauncherModel.Callbacks.
2618 *
2619 * @return true if we are currently paused. The caller might be able to
2620 * skip some work in that case since we will come back again.
2621 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002622 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002623 public boolean setLoadOnResume() {
2624 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002625 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002626 mOnResumeNeedsLoad = true;
2627 return true;
2628 } else {
2629 return false;
2630 }
2631 }
2632
2633 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002634 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002636 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002637 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002638 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002639 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002640 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002641 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002642 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002643 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002644
Joe Onorato9c1289c2009-08-17 11:03:03 -04002645 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002646 * Clear any pending bind callbacks. This is called when is loader is planning to
2647 * perform a full rebind from scratch.
2648 */
2649 @Override
2650 public void clearPendingBinds() {
2651 mBindOnResumeCallbacks.clear();
2652 if (mPendingExecutor != null) {
2653 mPendingExecutor.markCompleted();
2654 mPendingExecutor = null;
2655 }
2656 }
2657
2658 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002659 * Refreshes the shortcuts shown on the workspace.
2660 *
2661 * Implementation of the method from LauncherModel.Callbacks.
2662 */
2663 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002664 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002665 // Floating panels (except the full widget sheet) are associated with individual icons. If
2666 // we are starting a fresh bind, close all such panels as all the icons are about
2667 // to go away.
2668 AbstractFloatingView.closeOpenViews(this, true,
2669 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002670
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002671 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002672
Winson Chungd64d1762013-08-20 14:37:16 -07002673 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002674 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002675 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002676
Winson Chung3d503fb2011-07-13 17:25:49 -07002677 if (mHotseat != null) {
2678 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002680 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 }
2682
Adam Cohendcd297f2013-06-18 13:13:40 -07002683 @Override
2684 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002685 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002686 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2687 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002688 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2689 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002690 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002691 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2692 // If there are no screens, we need to have an empty screen
2693 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002694 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002695 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002696
Winsonc7d2e832016-07-28 12:24:55 -07002697 // After we have added all the screens, if the wallpaper was locked to the default state,
2698 // then notify to indicate that it can be released and a proper wallpaper offset can be
2699 // computed before the next layout
2700 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002701 }
2702
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002703 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002704 int count = orderedScreenIds.size();
2705 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002706 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002707 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002708 // No need to bind the first screen, as its always bound.
2709 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2710 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002711 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002712 }
2713
Sunny Goyalb23980c2017-08-17 07:45:25 -07002714 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002715 public void bindAppsAdded(final ArrayList<Long> newScreens,
2716 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002717 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002718 Runnable r = new Runnable() {
2719 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002720 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002721 }
2722 };
2723 if (waitUntilResume(r)) {
2724 return;
2725 }
2726
Winson Chungd64d1762013-08-20 14:37:16 -07002727 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002728 if (newScreens != null) {
2729 bindAddScreens(newScreens);
2730 }
Winson Chungd64d1762013-08-20 14:37:16 -07002731
2732 // We add the items without animation on non-visible pages, and with
2733 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002734 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002735 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002736 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002737 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002738 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002739 }
Winson Chungc58497e2013-09-03 17:48:37 -07002740
Winson Chung87412982013-10-03 18:34:14 -07002741 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002742 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002743 }
2744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002746 * Bind the items start-end from the list.
2747 *
2748 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002750 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002751 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002752 Runnable r = new Runnable() {
2753 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002754 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002755 }
2756 };
2757 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002758 return;
2759 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002760
Sunny Goyal3be633b2016-12-08 09:59:25 -08002761 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002762 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002763 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002764 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002765 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002766 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002767 int end = items.size();
2768 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002769 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002770
2771 // Short circuit if we are loading dock items for a configuration which has no dock
2772 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2773 mHotseat == null) {
2774 continue;
2775 }
2776
Sunny Goyal639e9062015-08-19 19:17:06 -07002777 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002778 switch (item.itemType) {
2779 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2780 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002781 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002782 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002783 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002784 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002785 }
2786 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002787 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002788 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002789 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002790 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002791 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002792 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2793 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002794 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002795 if (view == null) {
2796 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002797 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002798 break;
2799 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002800 default:
2801 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002802 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002803
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002804 /*
2805 * Remove colliding items.
2806 */
2807 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2808 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2809 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2810 View v = cl.getChildAt(item.cellX, item.cellY);
2811 Object tag = v.getTag();
2812 String desc = "Collision while binding workspace item: " + item
2813 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002814 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002815 throw (new RuntimeException(desc));
2816 } else {
2817 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002818 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002819 continue;
2820 }
2821 }
2822 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302823 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002824 if (animateIcons) {
2825 // Animate all the applications up now
2826 view.setAlpha(0f);
2827 view.setScaleX(0f);
2828 view.setScaleY(0f);
2829 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002830 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002833
Winson Chung997a9232013-07-24 15:33:46 -07002834 if (animateIcons) {
2835 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002836 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002837 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002838 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002839 final Runnable startBounceAnimRunnable = new Runnable() {
2840 public void run() {
2841 anim.playTogether(bounceAnims);
2842 anim.start();
2843 }
2844 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002845 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002846 // We post the animation slightly delayed to prevent slowdowns
2847 // when we are loading right after we return to launcher.
2848 mWorkspace.postDelayed(new Runnable() {
2849 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002850 if (mWorkspace != null) {
2851 mWorkspace.snapToPage(newScreenIndex);
2852 mWorkspace.postDelayed(startBounceAnimRunnable,
2853 NEW_APPS_ANIMATION_DELAY);
2854 }
Winson Chung94d67682013-09-25 16:29:40 -07002855 }
2856 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002857 } else {
2858 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002859 }
2860 }
Winson Chung64359a52013-07-08 17:17:08 -07002861 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002863 }
2864
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 /**
2866 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002868 public void bindAppWidget(LauncherAppWidgetInfo item) {
2869 View view = inflateAppWidget(item);
2870 if (view != null) {
2871 mWorkspace.addInScreen(view, item);
2872 mWorkspace.requestLayout();
2873 }
2874 }
2875
2876 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002877 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302878 PendingAppWidgetHostView view =
2879 new PendingAppWidgetHostView(this, item, mIconCache, true);
2880 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002881 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002882 }
2883
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002884 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002886 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002887
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002888 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2889 // If the provider is not ready, bind as a pending widget.
2890 appWidgetInfo = null;
2891 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2892 // The widget id is not valid. Try to find the widget based on the provider info.
2893 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2894 } else {
2895 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2896 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002897
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002898 // If the provider is ready, but the width is not yet restored, try to restore it.
2899 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2900 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002901 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002902 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2903 + " belongs to component " + item.providerName
2904 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002905 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002906 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002907 }
Sunny Goyalff572272014-07-23 13:58:07 -07002908
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002909 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002910 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002911 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2912 // Id has not been allocated yet. Allocate a new id.
2913 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2914 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002915
Sunny Goyald478c832016-04-01 12:04:16 -07002916 // Also try to bind the widget. If the bind fails, the user will be shown
2917 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002918 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002919 pendingInfo.spanX = item.spanX;
2920 pendingInfo.spanY = item.spanY;
2921 pendingInfo.minSpanX = item.minSpanX;
2922 pendingInfo.minSpanY = item.minSpanY;
2923 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002924
2925 boolean isDirectConfig =
2926 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2927 if (isDirectConfig && item.bindOptions != null) {
2928 Bundle newOptions = item.bindOptions.getExtras();
2929 if (options != null) {
2930 newOptions.putAll(options);
2931 }
2932 options = newOptions;
2933 }
Sunny Goyald478c832016-04-01 12:04:16 -07002934 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2935 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002936
Sunny Goyal86df1382016-08-10 15:03:22 -07002937 // We tried to bind once. If we were not able to bind, we would need to
2938 // go through the permission dialog, which means we cannot skip the config
2939 // activity.
2940 item.bindOptions = null;
2941 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2942
Sunny Goyald478c832016-04-01 12:04:16 -07002943 // Bind succeeded
2944 if (success) {
2945 // If the widget has a configure activity, it is still needs to set it up,
2946 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002947 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002948 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2949 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002950 }
Sunny Goyald478c832016-04-01 12:04:16 -07002951
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002952 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002953 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002954 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002955 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002956 // The widget was marked as UI not ready, but there is no configure activity to
2957 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002958 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002959 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002960 }
Sunny Goyalff572272014-07-23 13:58:07 -07002961 }
2962
Sunny Goyald5462aa2016-11-22 16:52:39 +05302963 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002964 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002965 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002966 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002967 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002968 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002969 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002970 }
2971
Sunny Goyal233ee962015-08-03 13:05:01 -07002972 item.minSpanX = appWidgetInfo.minSpanX;
2973 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302974 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002975 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302976 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002977 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302978 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002980 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002981 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002982 }
2983
Sunny Goyalff572272014-07-23 13:58:07 -07002984 /**
2985 * Restores a pending widget.
2986 *
2987 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002988 */
Sunny Goyald478c832016-04-01 12:04:16 -07002989 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002990 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2991 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2992 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002993 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002994 }
2995
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002996 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002997 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002998 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2999 info.pendingItemInfo = null;
3000 }
Sunny Goyalff572272014-07-23 13:58:07 -07003001
Sunny Goyalba47faa2017-10-04 16:50:57 -07003002 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3003 view.reinflate();
3004 }
3005
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003006 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003007 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003008 }
3009
Adam Cohen1462de32012-07-24 22:34:36 -07003010 public void onPageBoundSynchronously(int page) {
3011 mSynchronouslyBoundPages.add(page);
3012 }
3013
Sunny Goyal527c7d32015-08-28 15:19:36 -07003014 @Override
3015 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3016 if (mPendingExecutor != null) {
3017 mPendingExecutor.markCompleted();
3018 }
3019 mPendingExecutor = executor;
3020 executor.attachTo(this);
3021 }
3022
3023 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3024 if (mPendingExecutor == executor) {
3025 mPendingExecutor = null;
3026 }
3027 }
3028
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003029 @Override
3030 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3031 Runnable r = new Runnable() {
3032 public void run() {
3033 finishFirstPageBind(executor);
3034 }
3035 };
3036 if (waitUntilResume(r)) {
3037 return;
3038 }
3039
3040 Runnable onComplete = new Runnable() {
3041 @Override
3042 public void run() {
3043 if (executor != null) {
3044 executor.onLoadAnimationCompleted();
3045 }
3046 }
3047 };
3048 if (mDragLayer.getAlpha() < 1) {
3049 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3050 } else {
3051 onComplete.run();
3052 }
3053 }
3054
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 /**
3056 * Callback saying that there aren't any more items to bind.
3057 *
3058 * Implementation of the method from LauncherModel.Callbacks.
3059 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003060 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003061 Runnable r = new Runnable() {
3062 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003063 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003064 }
3065 };
3066 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003067 return;
3068 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003069 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003070 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003071
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003072 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003073
Sunny Goyal2100c782016-08-22 16:00:03 -07003074 if (mPendingActivityResult != null) {
3075 handleActivityResult(mPendingActivityResult.requestCode,
3076 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3077 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003078 }
3079
Sunny Goyala474a9b2017-05-04 16:47:11 -07003080 InstallShortcutReceiver.disableAndFlushInstallQueue(
3081 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003082
Tony Wickham010d2552017-01-20 08:15:28 -08003083 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003084 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003085 }
3086
Winson Chunga2413752012-04-03 14:22:34 -07003087 private boolean canRunNewAppsAnimation() {
3088 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003089 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003090 }
3091
Winson Chungc9168342013-06-26 14:54:55 -07003092 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003093 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003094 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3095 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003096 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003097 return bounceAnim;
3098 }
3099
Adam Cohen27772732013-11-11 14:00:56 +00003100 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003101 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003102 }
3103
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003104 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003105 * Add the icons for all apps.
3106 *
3107 * Implementation of the method from LauncherModel.Callbacks.
3108 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003109 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003110 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3111 public void run() {
3112 bindAllApplications(apps);
3113 }
3114 };
3115 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003116 return;
3117 }
3118
Winson Chungb745afb2015-03-02 11:51:23 -08003119 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003120 Executor pendingExecutor = getPendingExecutor();
Sunny Goyalf9403d92017-10-18 10:55:56 -07003121 if (pendingExecutor != null && !isInState(LauncherState.ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07003122 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003123 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003124 return;
3125 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003126
Winson Chungb745afb2015-03-02 11:51:23 -08003127 mAppsView.setApps(apps);
3128 }
Adam Cohen9211d422014-10-07 18:14:53 -07003129 if (mLauncherCallbacks != null) {
3130 mLauncherCallbacks.bindAllApplications(apps);
3131 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 }
3133
3134 /**
Tony2917a8b2017-07-31 23:29:42 -07003135 * Returns an Executor that will run after the launcher is first drawn (including after the
3136 * initial fade in animation). Returns null if the first draw has already occurred.
3137 */
3138 public @Nullable Executor getPendingExecutor() {
3139 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3140 }
3141
3142 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003143 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3144 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3145 */
3146 @Override
3147 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003148 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003149 }
3150
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003151 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 * A package was updated.
3153 *
3154 * Implementation of the method from LauncherModel.Callbacks.
3155 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003156 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003157 Runnable r = new Runnable() {
3158 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003159 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003160 }
3161 };
3162 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003163 return;
3164 }
3165
Winson Chungb745afb2015-03-02 11:51:23 -08003166 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003167 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003168 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 }
3170
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003172 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3173 Runnable r = new Runnable() {
3174 public void run() {
3175 bindPromiseAppProgressUpdated(app);
3176 }
3177 };
3178 if (waitUntilResume(r)) {
3179 return;
3180 }
3181
3182 if (mAppsView != null) {
3183 mAppsView.updatePromiseAppProgress(app);
3184 }
3185 }
3186
3187 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003188 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3189 Runnable r = new Runnable() {
3190 public void run() {
3191 bindWidgetsRestored(widgets);
3192 }
3193 };
3194 if (waitUntilResume(r)) {
3195 return;
3196 }
3197 mWorkspace.widgetsRestored(widgets);
3198 }
3199
Joe Onorato9c1289c2009-08-17 11:03:03 -04003200 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003201 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003202 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003203 *
3204 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003205 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003206 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003207 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003208 Runnable r = new Runnable() {
3209 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003210 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003211 }
3212 };
3213 if (waitUntilResume(r)) {
3214 return;
3215 }
3216
Sunny Goyal4390ace2014-10-13 11:33:11 -07003217 if (!updated.isEmpty()) {
3218 mWorkspace.updateShortcuts(updated);
3219 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003220 }
3221
3222 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003223 * Update the state of a package, typically related to install state.
3224 *
3225 * Implementation of the method from LauncherModel.Callbacks.
3226 */
Sunny Goyale755d462014-07-22 13:48:29 -07003227 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003228 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3229 Runnable r = new Runnable() {
3230 public void run() {
3231 bindRestoreItemsChange(updates);
3232 }
3233 };
3234 if (waitUntilResume(r)) {
3235 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003236 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003237
Sunny Goyal756adbc2015-04-16 15:20:51 -07003238 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003239 }
3240
3241 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003242 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003243 * in addition to specific applications to remove, the reason being that
3244 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003245 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003246 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003247 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003248 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003249 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003250 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003251 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003252 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003253 }
Winson Chungd64d1762013-08-20 14:37:16 -07003254 };
3255 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003256 return;
3257 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003258 mWorkspace.removeItemsByMatcher(matcher);
3259 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003260 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003261
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003262 @Override
3263 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3264 Runnable r = new Runnable() {
3265 public void run() {
3266 bindAppInfosRemoved(appInfos);
3267 }
3268 };
3269 if (waitUntilResume(r)) {
3270 return;
Joe Onorato36115782010-06-17 13:28:48 -04003271 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003272
Winson Chungdf95eb12013-10-16 14:57:07 -07003273 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003274 if (mAppsView != null) {
3275 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003276 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 }
Joe Onoratobe386092009-11-17 17:32:16 -08003278
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003279 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003280 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3281 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003282 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3283 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003284 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003285 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003286 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003287 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003288 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003289 return;
3290 }
3291
Tony Wickham26b17462017-03-20 17:12:24 -07003292 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3293 if (topView != null) {
3294 topView.onWidgetsBound();
3295 }
3296 }
3297
Tony Wickham86222d22017-03-29 15:30:43 -07003298 /**
3299 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3300 * refreshes the widgets and shortcuts associated with the given package/user
3301 */
3302 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003303 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003304 }
3305
Sunny Goyalc99cb172017-10-19 16:15:09 -07003306 public boolean isRotationEnabled () {
3307 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003308 }
3309
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003310 private void markAppsViewShown() {
3311 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3312 return;
3313 }
3314 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3315 }
3316
3317 private boolean shouldShowDiscoveryBounce() {
Sunny Goyalf9403d92017-10-18 10:55:56 -07003318 return isInState(LauncherState.NORMAL) && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)
3319 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003320 }
3321
Winson Chung80baf5a2010-08-09 16:03:15 -07003322 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003323 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003324 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003325 @Override
3326 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3327 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003328
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003329 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3330 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003331 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003332 writer.println(prefix + " Homescreen " + i);
3333
3334 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3335 for (int j = 0; j < layout.getChildCount(); j++) {
3336 Object tag = layout.getChildAt(j).getTag();
3337 if (tag != null) {
3338 writer.println(prefix + " " + tag.toString());
3339 }
3340 }
3341 }
3342
3343 writer.println(prefix + " Hotseat");
3344 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003345 for (int j = 0; j < layout.getChildCount(); j++) {
3346 Object tag = layout.getChildAt(j).getTag();
3347 if (tag != null) {
3348 writer.println(prefix + " " + tag.toString());
3349 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003350 }
Sunny Goyala1365452015-10-01 15:46:24 -07003351
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003352 try {
3353 FileLog.flushAll(writer);
3354 } catch (Exception e) {
3355 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003356 }
3357 }
3358
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003359 writer.println(prefix + "Misc:");
3360 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3361 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3362 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3363
3364 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003365
Adam Cohen9211d422014-10-07 18:14:53 -07003366 if (mLauncherCallbacks != null) {
3367 mLauncherCallbacks.dump(prefix, fd, writer, args);
3368 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003369 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003370
Sunny Goyal66b24572016-09-21 15:57:55 -07003371 @Override
3372 @TargetApi(Build.VERSION_CODES.N)
3373 public void onProvideKeyboardShortcuts(
3374 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3375
3376 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyalf9403d92017-10-18 10:55:56 -07003377 if (isInState(LauncherState.NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003378 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3379 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3380 }
3381 View currentFocus = getCurrentFocus();
3382 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3383 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3384 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3385 }
Tony18c4aa42017-05-10 21:23:57 -05003386 if (currentFocus.getTag() instanceof ItemInfo
3387 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003388 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3389 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3390 }
3391 if (!shortcutInfos.isEmpty()) {
3392 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3393 }
3394
3395 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3396 }
3397
3398 @Override
3399 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3400 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3401 switch (keyCode) {
3402 case KeyEvent.KEYCODE_A:
Sunny Goyalf9403d92017-10-18 10:55:56 -07003403 if (isInState(LauncherState.NORMAL)) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003404 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003405 return true;
3406 }
3407 break;
3408 case KeyEvent.KEYCODE_S: {
3409 View focusedView = getCurrentFocus();
3410 if (focusedView instanceof BubbleTextView
3411 && focusedView.getTag() instanceof ItemInfo
3412 && mAccessibilityDelegate.performAction(focusedView,
3413 (ItemInfo) focusedView.getTag(),
3414 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003415 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003416 return true;
3417 }
3418 break;
3419 }
3420 case KeyEvent.KEYCODE_O:
3421 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3422 return true;
3423 }
3424 break;
3425 }
3426 }
3427 return super.onKeyShortcut(keyCode, event);
3428 }
3429
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003430 public static Launcher getLauncher(Context context) {
3431 if (context instanceof Launcher) {
3432 return (Launcher) context;
3433 }
3434 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3435 }
3436
Sunny Goyal5a81c382017-03-20 15:08:06 -07003437 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003438
3439 @Override
3440 public void onSharedPreferenceChanged(
3441 SharedPreferences sharedPreferences, String key) {
3442 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003443 // Recreate the activity so that it initializes the rotation preference again.
3444 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003445 }
3446 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003447 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003448
3449 /**
3450 * Callback for listening for onResume
3451 */
3452 public interface OnResumeCallback {
3453
3454 void onLauncherResume();
3455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003456}