blob: 46eb263c635f48fa1b9c83c18c91cd1c4e2b04e5 [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 Goyalaeb16432017-10-16 11:46:41 -070019import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_NEXT_FRAME;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070020import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyalaeb16432017-10-16 11:46:41 -070021import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070022import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
23import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
24
Sunny Goyal28c6b962015-10-12 11:42:05 -070025import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070027import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070029import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070031import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000032import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070033import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040034import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080036import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080039import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080040import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040041import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070043import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070046import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070047import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070048import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070049import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070053import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070056import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020057import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070060import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080061import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070063import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070064import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.Selection;
66import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070067import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070070import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.HapticFeedbackConstants;
73import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070074import android.view.KeyboardShortcutGroup;
75import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080076import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070083import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070084import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070087
Sunny Goyal651077b2014-06-30 14:15:31 -070088import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070089import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070090import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070091import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070092import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070093import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070094import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010095import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070096import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -070097import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080098import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070099import com.android.launcher3.dragndrop.DragController;
100import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700101import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700102import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800103import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700104import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000105import com.android.launcher3.folder.Folder;
106import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700107import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700108import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700109import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700110import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700113import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700114import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800115import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800116import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700117import com.android.launcher3.shortcuts.DeepShortcutManager;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530119import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
121import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700122import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700124import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700125import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700126import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700127import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800128import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700129import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700130import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700131import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700132import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700133import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700134import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700135import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700136import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700137import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800138import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700139import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800140import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700141import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142import com.android.launcher3.widget.WidgetListRowEntry;
143import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700144import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700145
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700147import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700148import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700149import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700150import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700151import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800152import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700153import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155/**
156 * Default launcher application.
157 */
Sunny Goyal27835952017-01-13 12:15:53 -0800158public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700159 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
160 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700161 AccessibilityManager.AccessibilityStateChangeListener,
162 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700163 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700164 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Winson Chunga2413752012-04-03 14:22:34 -0700166 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700170
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700171 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700172 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Michael Jurka8b805b12012-04-18 14:23:14 -0700174 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700175 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
176 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700177
Jon Mirandac476d6e2017-05-04 16:30:01 -0700178 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700179
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700180 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
181
Mathew Inwood876a8462013-06-14 14:12:41 +0100182 /**
183 * IntentStarter uses request codes starting with this. This must be greater than all activity
184 * request codes used internally.
185 */
186 protected static final int REQUEST_LAST = 100;
187
Winson Chung2672ff92012-05-04 16:22:30 -0700188 // The Intent extra that defines whether to ignore the launch animation
189 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400190 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 // Type: int
193 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700194 // Type: int
195 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700196 // Type: PendingRequestArgs
197 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
198 // Type: ActivityResultInfo
199 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700200 // Type: SparseArray<Parcelable>
201 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700203 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
213 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
214 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700215
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700217 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800219 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700220
221 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700222
Sunny Goyalffe83f12014-08-14 17:39:34 -0700223 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700224 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700225
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700226 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700227
Sunny Goyal316490e2015-06-02 09:38:28 -0700228 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800229 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700230
Michael Jurka838a4ca2011-02-07 13:33:06 -0800231 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700232
Sunny Goyal47328fd2016-05-25 18:56:41 -0700233 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
235 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700236 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700237 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700238
Jon Miranda6bed3502017-09-19 14:43:17 -0700239 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
240 // that results in widgets being inflated in the wrong orientation.
241 private int mOrientation;
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private SpannableStringBuilder mDefaultKeySsb = null;
244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800247 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700248 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700250 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700251 private OnResumeCallback mOnResumeCallback;
252
Sunny Goyal527c7d32015-08-28 15:19:36 -0700253 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700254
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800256 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800257 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700258 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700259 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700260 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261
Jon Miranda2d89ea82017-05-04 11:47:53 -0700262 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700263 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700264
Tony Wickham010d2552017-01-20 08:15:28 -0800265 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700266
Winson Chung400438b2011-01-16 17:53:48 -0800267 // Determines how long to wait after a rotation before restoring the screen orientation to
268 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700269 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800270
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700271 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700272
Winson Chung46353de2012-02-16 14:05:10 -0800273 // We only want to get the SharedPreferences once since it does an FS stat each time we get
274 // it from the context.
275 private SharedPreferences mSharedPrefs;
276
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700277 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
278 // state transition. If another state transition happened during this delay,
279 // simply unregister this runnable.
280 private Runnable mExitSpringLoadedModeRunnable;
281
Sunny Goyal2100c782016-08-22 16:00:03 -0700282 // Activity result which needs to be processed after workspace has loaded.
283 private ActivityResultInfo mPendingActivityResult;
284 /**
285 * Holds extra information required to handle a result from an external call, like
286 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
287 */
288 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800289
Jon Miranda379198e2016-09-23 08:54:40 -0700290 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700291
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700292 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700293 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400294
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700295 private boolean mAppLaunchSuccess;
296
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400297 @Thunk void setOrientation() {
298 if (mRotationEnabled) {
299 unlockScreenOrientation(true);
300 } else {
301 setRequestedOrientation(
302 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700303 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400304 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700305
Sunny Goyal745bad92016-05-02 10:54:12 -0700306 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 @Override
309 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700310 if (DEBUG_STRICT_MODE) {
311 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
312 .detectDiskReads()
313 .detectDiskWrites()
314 .detectNetwork() // or .detectAll() for all detectable problems
315 .penaltyLog()
316 .build());
317 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
318 .detectLeakedSqlLiteObjects()
319 .detectLeakedClosableObjects()
320 .penaltyLog()
321 .penaltyDeath()
322 .build());
323 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700324 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700325
Adam Cohen9211d422014-10-07 18:14:53 -0700326 if (mLauncherCallbacks != null) {
327 mLauncherCallbacks.preOnCreate();
328 }
329
Mario Bertschler27288382017-05-24 15:35:09 -0700330 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
331 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700332 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700335 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400336
Sunny Goyal87f784c2017-01-11 10:48:34 -0800337 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700338
Adam Cohen2e6da152015-05-06 11:42:25 -0700339 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800340 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700341 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700342 Display display = getWindowManager().getDefaultDisplay();
343 Point mwSize = new Point();
344 display.getSize(mwSize);
345 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
346 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700347
Jon Miranda6bed3502017-09-19 14:43:17 -0700348 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700349 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700350 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800351 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800352 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800353 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700354 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700355
Joe Onorato41a12d22009-10-31 18:30:00 -0400356 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700357 mAllAppsController = new AllAppsTransitionController(this);
358 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700359
Sunny Goyalffe83f12014-08-14 17:39:34 -0700360 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700361
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700362 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700363 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700364
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700365 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
366 // this also ensures that any synchronous binding below doesn't re-trigger another
367 // LauncherModel load.
368 mPaused = false;
369
Sunny Goyal60820d72017-05-09 12:40:11 -0700370 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 setupViews();
Winson1f064272016-07-18 17:18:02 -0700373 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374
Tony Wickham010d2552017-01-20 08:15:28 -0800375 mPopupDataProvider = new PopupDataProvider(this);
376
Tony Wickhame2217252016-03-22 16:34:23 -0700377 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
378 .addAccessibilityStateChangeListener(this);
379
Sunny Goyalfe770c92016-09-27 14:38:58 -0700380 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381
Sunny Goyal2100c782016-08-22 16:00:03 -0700382 // We only load the page synchronously if the user rotates (or triggers a
383 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700384 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
385 if (savedInstanceState != null) {
386 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
387 }
388 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700389 // If we are not binding synchronously, show a fade in animation when
390 // the first page bind completes.
391 mDragLayer.setAlpha(0);
392 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700393 // Pages bound synchronously.
394 mWorkspace.setCurrentPage(currentScreen);
395
Sunny Goyal2100c782016-08-22 16:00:03 -0700396 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
398
399 // For handling default keys
400 mDefaultKeySsb = new SpannableStringBuilder();
401 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800402
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800403 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700404 // In case we are on a device with locked rotation, we should look at preferences to check
405 // if the user has specifically allowed rotation.
406 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700407 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700408 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
409 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700410 }
411
Sunny Goyal5a81c382017-03-20 15:08:06 -0700412 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
413 // Temporarily enable the rotation
414 mRotationEnabled = true;
415 }
416
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700417 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
418 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400419 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700420
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800421 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700422
Jon Miranda7fda2852017-07-19 16:07:01 -0700423 // Listen for broadcasts
424 IntentFilter filter = new IntentFilter();
425 filter.addAction(Intent.ACTION_SCREEN_OFF);
426 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
427 registerReceiver(mReceiver, filter);
428 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700429
Sunny Goyal8392c822017-06-20 10:03:56 -0700430 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
431 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700432
433 if (mLauncherCallbacks != null) {
434 mLauncherCallbacks.onCreate(savedInstanceState);
435 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700436
437 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700438 }
439
Sunny Goyal7779d622015-06-11 16:18:39 -0700440 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700441 public void onThemeChanged() {
442 recreate();
443 }
444
Mario Bertschlera6936942017-05-31 14:48:19 -0700445 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700446 if (isDark) {
447 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700448 } else if (supportsDarkText) {
449 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700450 }
451 }
452
453 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700454 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800455 return mLauncherView.findViewById(id);
456 }
457
458 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700459 public void onAppWidgetHostReset() {
460 if (mAppWidgetHost != null) {
461 mAppWidgetHost.startListening();
462 }
463 }
464
Adam Cohen9211d422014-10-07 18:14:53 -0700465 private LauncherCallbacks mLauncherCallbacks;
466
Winson1f064272016-07-18 17:18:02 -0700467 public void onInsetsChanged(Rect insets) {
468 mDeviceProfile.updateInsets(insets);
469 mDeviceProfile.layout(this, true /* notifyListeners */);
470 }
471
Sunny Goyal32554d12015-12-03 15:31:25 -0800472 /**
473 * Call this after onCreate to set or clear overlay.
474 */
475 public void setLauncherOverlay(LauncherOverlay overlay) {
476 if (overlay != null) {
477 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
478 }
479 mWorkspace.setLauncherOverlay(overlay);
480 }
481
Adam Cohen9211d422014-10-07 18:14:53 -0700482 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
483 mLauncherCallbacks = callbacks;
484 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700485 }
486
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700487 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700488 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700489 if (mLauncherCallbacks != null) {
490 mLauncherCallbacks.onLauncherProviderChange();
491 }
492 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700493
Hyunyoung Song3f471442015-04-08 19:01:34 -0700494 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700495 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
496 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700497 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700498 }
499
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000500 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700501 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
502 // This cast is safe as long as the id < 0x00FFFFFF
503 // Since we jail all the dynamically generated views, there should be no clashes
504 // with any other views.
505 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000506 }
507
Tony Wickham010d2552017-01-20 08:15:28 -0800508 public PopupDataProvider getPopupDataProvider() {
509 return mPopupDataProvider;
510 }
511
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000512 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700513 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
514 * a configuration step, this allows the proper animations to run after other transitions.
515 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 private long completeAdd(
517 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
518 long screenId = info.screenId;
519 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700520 // When the screen id represents an actual screen (as opposed to a rank) we make sure
521 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700523 }
Adam Cohendb364c32014-05-20 14:23:40 -0700524
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800526 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700527 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700528 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800529 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700531 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700532 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700534 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700535 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700536 int widgetId = appWidgetId;
537 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700538 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700539 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700540 // Since the view was just bound, also launch the configure activity if needed
541 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
542 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800543 if (provider != null) {
544 new WidgetAddFlowHandler(provider)
545 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700546 }
547 }
548 break;
549 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800550 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700551
Adam Cohendb364c32014-05-20 14:23:40 -0700552 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800553 }
554
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800555 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700557 if (isWorkspaceLoading()) {
558 // process the result once the workspace has loaded.
559 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
560 return;
561 }
562 mPendingActivityResult = null;
563
Winson Chunge341d302013-08-16 14:31:00 -0700564 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700565 final PendingRequestArgs requestArgs = mPendingRequestArgs;
566 setWaitingForResult(null);
567 if (requestArgs == null) {
568 return;
569 }
570
571 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700572
Adam Cohenad4e15c2013-10-17 16:21:35 -0700573 Runnable exitSpringLoaded = new Runnable() {
574 @Override
575 public void run() {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700576 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700577 }
578 };
579
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700581 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700582 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
584 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700585 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700586 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700587 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700589 addAppWidgetImpl(
590 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800591 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700592 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700593 }
594 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200595 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyalf9403d92017-10-18 10:55:56 -0700596 if (resultCode == RESULT_OK && isInState(LauncherState.OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700597 // User could have free-scrolled between pages before picking a wallpaper; make sure
598 // we move to the closest one now.
599 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700600 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200601 }
602 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700603 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200604
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700606 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700607
Adam Cohened66b2b2012-01-23 17:28:51 -0800608 // We have special handling for widgets
609 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800610 final int appWidgetId;
611 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
612 : -1;
613 if (widgetId < 0) {
614 appWidgetId = pendingAddWidgetId;
615 } else {
616 appWidgetId = widgetId;
617 }
618
Adam Cohenad4e15c2013-10-17 16:21:35 -0700619 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800620 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700621 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
622 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700623 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700624 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700625 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700626 @Override
627 public void run() {
Sunny Goyalaeb16432017-10-16 11:46:41 -0700628 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_NEXT_FRAME);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700629 }
630 };
Adam Cohendb364c32014-05-20 14:23:40 -0700631
Sunny Goyal2100c782016-08-22 16:00:03 -0700632 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
633 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
634 } else {
635 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
636 // When the screen id represents an actual screen (as opposed to a rank)
637 // we make sure that the drop page actually exists.
638 requestArgs.screenId =
639 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700640 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700641 final CellLayout dropLayout =
642 mWorkspace.getScreenWithId(requestArgs.screenId);
643
644 dropLayout.setDropPending(true);
645 final Runnable onComplete = new Runnable() {
646 @Override
647 public void run() {
648 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
649 dropLayout.setDropPending(false);
650 }
651 };
652 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
653 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700654 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800655 return;
656 }
657
Sunny Goyald478c832016-04-01 12:04:16 -0700658 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
659 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700660 if (resultCode == RESULT_OK) {
661 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700663 }
664 // Leave the widget in the pending state if the user canceled the configure.
665 return;
666 }
667
Sunny Goyalaad90582015-07-09 14:22:50 -0700668 if (requestCode == REQUEST_CREATE_SHORTCUT) {
669 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
671 completeAdd(requestCode, data, -1, requestArgs);
672 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
673 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
674
Sunny Goyalaad90582015-07-09 14:22:50 -0700675 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700676 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
677 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800680 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800681 }
682
683 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700684 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800685 final int requestCode, final int resultCode, final Intent data) {
686 handleActivityResult(requestCode, resultCode, data);
687 if (mLauncherCallbacks != null) {
688 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
689 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 }
691
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700692 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700693 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600694 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 PendingRequestArgs pendingArgs = mPendingRequestArgs;
696 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
697 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
698 setWaitingForResult(null);
699
Sunny Goyal28c6b962015-10-12 11:42:05 -0700700 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700701 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700702 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700703 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700704 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700705 Intent intent = pendingArgs.getPendingIntent();
706
Sunny Goyal28c6b962015-10-12 11:42:05 -0700707 if (grantResults.length > 0
708 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700709 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700710 } else {
711 // TODO: Show a snack bar with link to settings
712 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700713 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700714 }
715 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600716 if (mLauncherCallbacks != null) {
717 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
718 grantResults);
719 }
720 }
721
Adam Cohendb364c32014-05-20 14:23:40 -0700722 /**
723 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
724 *
725 * @param screenId the screen id to check
726 * @return the new screen, or screenId if it exists
727 */
728 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800729 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700730 if (dropLayout == null) {
731 // it's possible that the add screen was removed because it was
732 // empty and a re-bind occurred
733 mWorkspace.addExtraEmptyScreen();
734 return mWorkspace.commitExtraEmptyScreen();
735 } else {
736 return screenId;
737 }
738 }
739
Sunny Goyal2100c782016-08-22 16:00:03 -0700740 @Thunk void completeTwoStageWidgetDrop(
741 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
742 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 Runnable onCompleteRunnable = null;
744 int animationType = 0;
745
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 if (resultCode == RESULT_OK) {
748 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
749 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800750 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700751 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800752 onCompleteRunnable = new Runnable() {
753 @Override
754 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700755 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700756 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 }
758 };
759 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800760 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800761 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700763 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700764 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700765 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
766 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700768 // The animated view may be null in the case of a rotation during widget configuration
769 onCompleteRunnable.run();
770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 }
772
773 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700774 protected void onStop() {
775 super.onStop();
776 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700777
778 if (mLauncherCallbacks != null) {
779 mLauncherCallbacks.onStop();
780 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700781
Sunny Goyalf5e37442016-11-02 10:31:24 -0700782 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700783 mAppWidgetHost.stopListening();
784 }
Tony Wickham010d2552017-01-20 08:15:28 -0800785
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700786 if (!mAppLaunchSuccess) {
787 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
788 mWorkspace.getState().containerType);
789 }
Tony Wickham010d2552017-01-20 08:15:28 -0800790 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700791 }
792
793 @Override
794 protected void onStart() {
795 super.onStart();
796 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700797
798 if (mLauncherCallbacks != null) {
799 mLauncherCallbacks.onStart();
800 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700801
Sunny Goyalf5e37442016-11-02 10:31:24 -0700802 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700803 mAppWidgetHost.startListening();
804 }
Tony Wickham010d2552017-01-20 08:15:28 -0800805
806 if (!isWorkspaceLoading()) {
807 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
808 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700809
Jon Miranda7fda2852017-07-19 16:07:01 -0700810 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700811 if (mScrimAnimator != null) {
812 mScrimAnimator.cancel();
813 }
814 mDragLayer.getBackground().setAlpha(0);
815 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
816 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
817 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
818 @Override
819 public void onAnimationEnd(Animator animation) {
820 mScrimAnimator = null;
821 }
822 });
823 mScrimAnimator.setDuration(600);
824 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
825 mScrimAnimator.start();
826 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700827 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700828 }
829
830 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700832 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700834 TraceHelper.partitionSection("ON_RESUME", "superCall");
835
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700836 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700837 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800838 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700839 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700840 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700841 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700842 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700844 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200845 // We might have postponed some bind calls until onResume (see waitUntilResume) --
846 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700847 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
848 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200849 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700850 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200851 }
Winson Chunge4e50662012-01-23 14:45:13 -0800852
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700853 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700854 // Process any items that were added while Launcher was away.
855 InstallShortcutReceiver.disableAndFlushInstallQueue(
856 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700857
Sunny Goyala474a9b2017-05-04 16:47:11 -0700858 // Refresh shortcuts if the permission changed.
859 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700860
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700861 if (shouldShowDiscoveryBounce()) {
862 mAllAppsController.showDiscoveryBounce();
863 }
Adam Cohen9211d422014-10-07 18:14:53 -0700864 if (mLauncherCallbacks != null) {
865 mLauncherCallbacks.onResume();
866 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800867
Sunny Goyala502aa32017-10-02 16:04:06 -0700868 clearTypedText();
869
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700870 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700871 }
872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700874 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700875 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700876 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700877
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700878 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700879 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800880 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700881 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700882
Adam Cohen9211d422014-10-07 18:14:53 -0700883 if (mLauncherCallbacks != null) {
884 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700885 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700886 }
887
Adam Cohenc2d6e892014-10-16 09:49:24 -0700888 public interface LauncherOverlay {
889
890 /**
891 * Touch interaction leading to overscroll has begun
892 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700893 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700894
895 /**
896 * Touch interaction related to overscroll has ended
897 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700898 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700899
900 /**
901 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
902 * screen (or in the case of RTL, the rightmost screen).
903 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700904 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700905
906 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800907 * Called when the launcher is ready to use the overlay
908 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700909 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700910 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700911 }
912
913 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700914 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700915 }
916
917 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
918
Sunny Goyalc86df472016-02-25 09:19:38 -0800919 public void onScrollChanged(float progress) {
920 if (mWorkspace != null) {
921 mWorkspace.onOverlayScrollChanged(progress);
922 }
923 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700924 }
925
Jorim Jaggid017f882014-01-14 17:08:48 -0800926 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700927 if (mLauncherCallbacks != null) {
928 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700929 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800930 // On O and above we there is always some setting present settings (add icon to
931 // home screen or icon badging). On earlier APIs we will have the allow rotation
932 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700933 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700934 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800935 }
936
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700937 @Override
938 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700940 if (mModel.isCurrentCallbacks(this)) {
941 mModel.stopLoader();
942 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700943 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
944
Romain Guy13c2e7b2010-03-10 19:45:00 -0800945 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700946 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700947
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800948 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800949 @Override
950 public void onWindowFocusChanged(boolean hasFocus) {
951 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700952 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700953
954 if (mLauncherCallbacks != null) {
955 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
956 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800957 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 private boolean acceptFilter() {
960 final InputMethodManager inputManager = (InputMethodManager)
961 getSystemService(Context.INPUT_METHOD_SERVICE);
962 return !inputManager.isFullscreenMode();
963 }
964
965 @Override
966 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700967 final int uniChar = event.getUnicodeChar();
968 final boolean handled = super.onKeyDown(keyCode, event);
969 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
970 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
972 keyCode, event);
973 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700975 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 // showSearchDialog()
977 // If there are multiple keystrokes before the search dialog takes focus,
978 // onSearchRequested() will be called for every keystroke,
979 // but it is idempotent, so it's fine.
980 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982 }
983
Joe Onorato8a9625e2010-01-28 15:55:35 -0800984 // Eat the long press event so the keyboard doesn't come up.
985 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
986 return true;
987 }
988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 return handled;
990 }
991
Winson46163472015-09-22 17:31:56 -0700992 @Override
993 public boolean onKeyUp(int keyCode, KeyEvent event) {
994 if (keyCode == KeyEvent.KEYCODE_MENU) {
995 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700996 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700997 // Close any open floating view
998 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -0700999
Winson46163472015-09-22 17:31:56 -07001000 // Show the overview mode if we are on the workspace
Sunny Goyalf9403d92017-10-18 10:55:56 -07001001 if (isInState(LauncherState.NORMAL) && !mWorkspace.isSwitchingState()) {
Winson46163472015-09-22 17:31:56 -07001002 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001003 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001004 }
1005 }
1006 return true;
1007 }
1008 return super.onKeyUp(keyCode, event);
1009 }
1010
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 private String getTypedText() {
1012 return mDefaultKeySsb.toString();
1013 }
1014
Sunny Goyal6f28e712016-09-13 14:19:29 -07001015 @Override
1016 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001017 mDefaultKeySsb.clear();
1018 mDefaultKeySsb.clearSpans();
1019 Selection.setSelection(mDefaultKeySsb, 0);
1020 }
1021
Sunny Goyalf9403d92017-10-18 10:55:56 -07001022 public boolean isInState(LauncherState state) {
1023 return mWorkspace.getState() == state;
1024 }
1025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 /**
1027 * Restores the previous state, if it exists.
1028 *
1029 * @param savedState The previous state.
1030 */
1031 private void restoreState(Bundle savedState) {
1032 if (savedState == null) {
1033 return;
1034 }
1035
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07001036 int stateOrdinal = savedState.getInt(RUNTIME_STATE, LauncherState.NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001037 LauncherState[] stateValues = LauncherState.values();
1038 LauncherState state = stateValues[stateOrdinal];
1039 if (!state.doNotRestore) {
1040 if (state == LauncherState.ALL_APPS) {
1041 showAppsView(false /* animated */);
1042 } else {
1043 // TODO: Add logic for other states
1044 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001045 }
1046
Sunny Goyal2100c782016-08-22 16:00:03 -07001047 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1048 if (requestArgs != null) {
1049 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001051
1052 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001053
1054 SparseArray<Parcelable> widgetsState =
1055 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1056 if (widgetsState != null) {
1057 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
1061 /**
1062 * Finds all the views we need and configure them properly.
1063 */
1064 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001065 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001066 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001067 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001068 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001069
Sunny Goyal784f9c32016-03-23 16:56:54 -07001070 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1071 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1072 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073
Winson Chung4c98d922011-05-31 16:50:48 -07001074 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001075 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001076
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 // Setup the hotseat
1078 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1079 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001080 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001081 }
1082
Winsone9f27272015-10-13 10:47:51 -07001083 // Setup the overview panel
1084 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001085
Winson Chung4c98d922011-05-31 16:50:48 -07001086 // Setup the workspace
1087 mWorkspace.setHapticFeedbackEnabled(false);
1088 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001089 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001090 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1091 // default state, otherwise we will update to the wrong offsets in RTL
1092 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001093 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001094 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001095
Tony Wickham34d2c912015-09-11 09:27:58 -07001096 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001097 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001098
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001099 // Setup Apps
1100 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001101
Winson Chung3d503fb2011-07-13 17:25:49 -07001102 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001103 mDragController.setMoveTarget(mWorkspace);
1104 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001105 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001106
Hyunyoung Songd725f642017-08-17 22:26:35 -07001107 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001108
Sunny Goyal322d5562015-06-25 19:35:49 -07001109 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1110 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113
Winsone9f27272015-10-13 10:47:51 -07001114 private void setupOverviewPanel() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001115 mOverviewPanel = findViewById(R.id.overview_panel);
Winsone9f27272015-10-13 10:47:51 -07001116
Winsone9f27272015-10-13 10:47:51 -07001117 // Bind wallpaper button actions
1118 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301119 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001120 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001121 public void handleViewClick(View view) {
1122 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001123 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001124 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001125
1126 // Bind widget button actions
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301127 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001128 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001129 public void handleViewClick(View view) {
1130 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001131 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001132 }.attachTo(findViewById(R.id.widget_button));
Winsone9f27272015-10-13 10:47:51 -07001133
1134 // Bind settings actions
1135 View settingsButton = findViewById(R.id.settings_button);
1136 boolean hasSettings = hasSettings();
1137 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301138 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001139 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001140 public void handleViewClick(View view) {
1141 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001142 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001143 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001144 } else {
1145 settingsButton.setVisibility(View.GONE);
1146 }
1147
1148 mOverviewPanel.setAlpha(0f);
1149 }
1150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001152 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001153 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001154 */
1155 public void setAllAppsButton(View allAppsButton) {
1156 mAllAppsButton = allAppsButton;
1157 }
1158
Anjali Koppal5ad44842014-03-10 20:34:39 -07001159 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 * Creates a view representing a shortcut.
1161 *
1162 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001164 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001165 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167
1168 /**
1169 * Creates a view representing a shortcut inflated from the specified resource.
1170 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 * @param parent The group the shortcut belongs to.
1172 * @param info The data structure describing the shortcut.
1173 *
1174 * @return A View inflated from layoutResId.
1175 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001176 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001177 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1178 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001179 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001181 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 return favorite;
1183 }
1184
1185 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001186 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 *
1188 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001190 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001191 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001192 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1193 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001194 return;
1195 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001196
Jon Mirandac476d6e2017-05-04 16:30:01 -07001197 int[] cellXY = mTmpAddItemCellCoordinates;
1198 CellLayout layout = getCellLayout(container, screenId);
1199
Sunny Goyal782f0c92017-01-19 10:27:54 -08001200 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001201 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001202 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1203 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001204 }
1205
1206 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001207 // Legacy shortcuts are only supported for primary profile.
1208 info = Process.myUserHandle().equals(args.user)
1209 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001210
Sunny Goyalb57645f2017-03-20 13:57:28 -07001211 if (info == null) {
1212 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1213 return;
1214 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001215 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001216 // The app is trying to add a shortcut without sufficient permissions
1217 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1218 return;
1219 }
1220 }
1221
Jon Mirandac476d6e2017-05-04 16:30:01 -07001222 if (container < 0) {
1223 // Adding a shortcut to the Workspace.
1224 final View view = createShortcut(info);
1225 boolean foundCellSpan = false;
1226 // First we check if we already know the exact location where we want to add this item.
1227 if (cellX >= 0 && cellY >= 0) {
1228 cellXY[0] = cellX;
1229 cellXY[1] = cellY;
1230 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001231
Jon Mirandac476d6e2017-05-04 16:30:01 -07001232 // If appropriate, either create a folder or add to an existing folder
1233 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001234 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001235 return;
1236 }
1237 DragObject dragObject = new DragObject();
1238 dragObject.dragInfo = info;
1239 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1240 true)) {
1241 return;
1242 }
1243 } else {
1244 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1245 }
1246
1247 if (!foundCellSpan) {
1248 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001249 return;
1250 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001251
1252 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1253 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001254 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001255 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001256 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001257 if (folderIcon != null) {
1258 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1259 folderInfo.add(info, args.rank, false);
1260 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001261 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001262 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265
Sunny Goyale29897f52017-07-20 10:09:42 -07001266 public FolderIcon findFolderIcon(final long folderIconId) {
1267 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1268 @Override
1269 public boolean evaluate(ItemInfo info, View view) {
1270 return info != null && info.id == folderIconId;
1271 }
1272 });
1273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001278 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001280 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001281 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1282
Adam Cohened66b2b2012-01-23 17:28:51 -08001283 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001284 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001285 }
Romain Guycbb89e42009-06-08 15:52:54 -07001286
Adam Cohen59400422014-03-05 18:07:04 -08001287 LauncherAppWidgetInfo launcherInfo;
1288 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001289 launcherInfo.spanX = itemInfo.spanX;
1290 launcherInfo.spanY = itemInfo.spanY;
1291 launcherInfo.minSpanX = itemInfo.minSpanX;
1292 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001293 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001294
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001295 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001296 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297
Sunny Goyal2100c782016-08-22 16:00:03 -07001298 if (hostView == null) {
1299 // Perform actual inflation because we're live
1300 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001302 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301303 prepareAppWidget(hostView, launcherInfo);
1304 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
Romain Guycbb89e42009-06-08 15:52:54 -07001306
Sunny Goyald5462aa2016-11-22 16:52:39 +05301307 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001308 hostView.setTag(item);
1309 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001310 hostView.setFocusable(true);
1311 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001312 }
1313
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1315 @Override
1316 public void onReceive(Context context, Intent intent) {
1317 final String action = intent.getAction();
1318 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001319 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001320 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001321 if (mAppsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001322 if (!showWorkspace(false)) {
1323 // If we are already on the workspace, then manually reset all apps
1324 mAppsView.reset();
1325 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001326 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001327 mShouldFadeInScrim = true;
1328 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1329 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1330 // the user unlocked and the Launcher is not in the foreground.
1331 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 }
1333 }
1334 };
1335
Tony Wickham010d2552017-01-20 08:15:28 -08001336 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1337 Runnable r = new Runnable() {
1338 @Override
1339 public void run() {
1340 mWorkspace.updateIconBadges(updatedBadges);
1341 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001342
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001343 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1344 if (popup instanceof PopupContainerWithArrow) {
1345 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001346 }
Tony Wickham010d2552017-01-20 08:15:28 -08001347 }
1348 };
1349 if (!waitUntilResume(r)) {
1350 r.run();
1351 }
1352 }
1353
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 @Override
1355 public void onAttachedToWindow() {
1356 super.onAttachedToWindow();
1357
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001358 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001359 if (mLauncherCallbacks != null) {
1360 mLauncherCallbacks.onAttachedToWindow();
1361 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001362 }
1363
1364 @Override
1365 public void onDetachedFromWindow() {
1366 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001367
1368 if (mLauncherCallbacks != null) {
1369 mLauncherCallbacks.onDetachedFromWindow();
1370 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001371 }
1372
Winson Chunga6945242014-01-08 14:04:34 -08001373 public DragLayer getDragLayer() {
1374 return mDragLayer;
1375 }
1376
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001377 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001378 return mAppsView;
1379 }
1380
Winson Chunga6945242014-01-08 14:04:34 -08001381 public Workspace getWorkspace() {
1382 return mWorkspace;
1383 }
1384
1385 public Hotseat getHotseat() {
1386 return mHotseat;
1387 }
1388
Jorim Jaggid017f882014-01-14 17:08:48 -08001389 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001390 return mOverviewPanel;
1391 }
1392
Sunny Goyal47328fd2016-05-25 18:56:41 -07001393 public DropTargetBar getDropTargetBar() {
1394 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001395 }
1396
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001397 public LauncherAppWidgetHost getAppWidgetHost() {
1398 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
Romain Guycbb89e42009-06-08 15:52:54 -07001400
Winson Chunga9abd0e2010-10-27 17:18:37 -07001401 public LauncherModel getModel() {
1402 return mModel;
1403 }
1404
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001405 public ModelWriter getModelWriter() {
1406 return mModelWriter;
1407 }
1408
Adam Cohen79d90c52016-04-22 13:29:20 -07001409 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001410 return mSharedPrefs;
1411 }
1412
Jon Miranda6bed3502017-09-19 14:43:17 -07001413 public int getOrientation() { return mOrientation; }
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 @Override
1416 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001417 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 super.onNewIntent(intent);
1419
Winson15f8b172015-08-19 11:02:39 -07001420 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1421 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1422 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001423
1424 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyalf9403d92017-10-18 10:55:56 -07001425 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(LauncherState.NORMAL)
1426 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001427
Winson15f8b172015-08-19 11:02:39 -07001428 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1429 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001430 if (mWorkspace == null) {
1431 // Can be cases where mWorkspace is null, this prevents a NPE
1432 return;
1433 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001434
1435 // Note: There should be at most one log per method call. This is enforced implicitly
1436 // by using if-else statements.
1437 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001438 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001439 if (topOpenView != null) {
1440 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001441 } else if (alreadyOnHome) {
Sunny Goyalaeb16432017-10-16 11:46:41 -07001442 Target target = newContainerTarget(mWorkspace.getState().containerType);
1443 target.pageIndex = mWorkspace.getCurrentPage();
1444 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001445 }
1446
1447 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001448 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal53b99362017-10-05 14:58:56 -07001449 showWorkspace(alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001450
1451 final View v = getWindow().peekDecorView();
1452 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001453 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001454 }
1455
Winson Chungb745afb2015-03-02 11:51:23 -08001456 // Reset the apps view
1457 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001458 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001459 }
1460
Adam Cohen9211d422014-10-07 18:14:53 -07001461 if (mLauncherCallbacks != null) {
1462 mLauncherCallbacks.onHomeIntent();
1463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001465 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001466
Adam Cohen9211d422014-10-07 18:14:53 -07001467 if (mLauncherCallbacks != null) {
1468 mLauncherCallbacks.onNewIntent(intent);
1469 }
Winson15f8b172015-08-19 11:02:39 -07001470
1471 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1472 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1473 // animation.
1474 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001475 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001476
Winson15f8b172015-08-19 11:02:39 -07001477 mWorkspace.post(new Runnable() {
1478 @Override
1479 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001480 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001481 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001482 }
Winson15f8b172015-08-19 11:02:39 -07001483 }
1484 });
1485 }
1486 }
1487
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001488 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001489 }
1490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001492 public void onRestoreInstanceState(Bundle state) {
1493 super.onRestoreInstanceState(state);
1494 for (int page: mSynchronouslyBoundPages) {
1495 mWorkspace.restoreInstanceStateForChild(page);
1496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498
1499 @Override
1500 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001501 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001502 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001503
Adam Cohen21cd0022013-10-09 18:57:02 -07001504 }
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07001505 outState.putInt(RUNTIME_STATE, mWorkspace.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001506
1507
1508 AbstractFloatingView widgets = AbstractFloatingView
1509 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1510 if (widgets != null) {
1511 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1512 widgets.saveHierarchyState(widgetsState);
1513 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1514 } else {
1515 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1516 }
1517
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001518 // We close any open folders and shortcut containers since they will not be re-opened,
1519 // and we need to make sure this state is reflected.
1520 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001521
Sunny Goyal2100c782016-08-22 16:00:03 -07001522 if (mPendingRequestArgs != null) {
1523 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1524 }
1525 if (mPendingActivityResult != null) {
1526 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 }
1528
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001529 super.onSaveInstanceState(outState);
1530
Adam Cohen9211d422014-10-07 18:14:53 -07001531 if (mLauncherCallbacks != null) {
1532 mLauncherCallbacks.onSaveInstanceState(outState);
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535
1536 @Override
1537 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001539
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001540 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001541 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001542
Winson Chungcd2b0142011-06-08 16:02:26 -07001543 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001544 // It's possible to receive onDestroy after a new Launcher activity has
1545 // been created. In this case, don't interfere with the new Launcher.
1546 if (mModel.isCurrentCallbacks(this)) {
1547 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001548 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001549 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001550
Sunny Goyal745bad92016-05-02 10:54:12 -07001551 if (mRotationPrefChangeHandler != null) {
1552 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1553 }
1554
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001556 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001558 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001560 mAppWidgetHost = null;
1561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 TextKeyListener.getInstance().release();
1563
Tony Wickhame2217252016-03-22 16:34:23 -07001564 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1565 .removeAccessibilityStateChangeListener(this);
1566
Mario Bertschler27288382017-05-24 15:35:09 -07001567 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1568
Michael Jurka2ecf9952012-06-18 12:52:28 -07001569 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001570
Tony2917a8b2017-07-31 23:29:42 -07001571 clearPendingBinds();
1572
Adam Cohen9211d422014-10-07 18:14:53 -07001573 if (mLauncherCallbacks != null) {
1574 mLauncherCallbacks.onDestroy();
1575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
1577
Sunny Goyalae502842016-06-17 08:43:56 -07001578 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1579 return mAccessibilityDelegate;
1580 }
1581
Adam Cohena9cf38f2011-05-02 15:36:58 -07001582 public DragController getDragController() {
1583 return mDragController;
1584 }
1585
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001587 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001588 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001589 }
1590
1591 @Override
1592 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1593 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001594 try {
1595 super.startIntentSenderForResult(intent, requestCode,
1596 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1597 } catch (IntentSender.SendIntentException e) {
1598 throw new ActivityNotFoundException();
1599 }
1600 }
1601
Winson Chung70d72102011-08-12 11:24:35 -07001602 /**
1603 * Indicates that we want global search for this activity by setting the globalSearch
1604 * argument for {@link #startSearch} to true.
1605 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001607 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001609
Karl Rosaen138a0412009-04-23 19:00:21 -07001610 if (initialQuery == null) {
1611 // Use any text typed in the launcher as the initial query
1612 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 if (appSearchData == null) {
1615 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001616 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001618
Sunny Goyal6f28e712016-09-13 14:19:29 -07001619 if (mLauncherCallbacks == null ||
1620 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1621 // Starting search from the callbacks failed. Start the default global search.
1622 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001623 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001624
1625 // We need to show the workspace after starting the search
1626 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001627 }
1628
Ian Parkinson047036e2014-09-01 15:40:53 +01001629 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001630 * Starts the global search activity. This code is a copied from SearchManager
1631 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001632 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001633 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001634 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001635 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1636 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1637 if (globalSearchActivity == null) {
1638 Log.w(TAG, "No global search activity found.");
1639 return;
1640 }
1641 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1642 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1643 intent.setComponent(globalSearchActivity);
1644 // Make sure that we have a Bundle to put source in
1645 if (appSearchData == null) {
1646 appSearchData = new Bundle();
1647 } else {
1648 appSearchData = new Bundle(appSearchData);
1649 }
Adam Cohen9211d422014-10-07 18:14:53 -07001650 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001651 if (!appSearchData.containsKey("source")) {
1652 appSearchData.putString("source", getPackageName());
1653 }
1654 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1655 if (!TextUtils.isEmpty(initialQuery)) {
1656 intent.putExtra(SearchManager.QUERY, initialQuery);
1657 }
1658 if (selectInitialQuery) {
1659 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1660 }
1661 intent.setSourceBounds(sourceBounds);
1662 try {
1663 startActivity(intent);
1664 } catch (ActivityNotFoundException ex) {
1665 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
1668
Winson Chung70d72102011-08-12 11:24:35 -07001669 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001670 public boolean onPrepareOptionsMenu(Menu menu) {
1671 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001672 if (mLauncherCallbacks != null) {
1673 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1674 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001675 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001678 @Override
1679 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001680 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001681 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001682 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001683 }
1684
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001686 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 }
1688
Adam Cohen517a7f52014-03-01 12:12:59 -08001689 public boolean isWorkspaceLoading() {
1690 return mWorkspaceLoading;
1691 }
1692
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001693 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001694 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001695 }
1696
Sunny Goyal04a324a2017-01-20 21:08:59 -08001697 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001698 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001699 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001700
Sunny Goyal2100c782016-08-22 16:00:03 -07001701 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001702 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001703 if (LOGD) {
1704 Log.d(TAG, "Adding widget from drop");
1705 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001706 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001707 }
1708
Sunny Goyal2100c782016-08-22 16:00:03 -07001709 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001710 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1711 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1712 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001713
Adam Cohenad4e15c2013-10-17 16:21:35 -07001714 Runnable onComplete = new Runnable() {
1715 @Override
1716 public void run() {
1717 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal4c7f2152017-10-17 17:17:16 -07001718 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001719 }
1720 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001721 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001722 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724 }
Romain Guycbb89e42009-06-08 15:52:54 -07001725
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001726 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1727 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001728 info.container = container;
1729 info.screenId = screenId;
1730 if (cell != null) {
1731 info.cellX = cell[0];
1732 info.cellY = cell[1];
1733 }
1734 info.spanX = spanX;
1735 info.spanY = spanY;
1736
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001737 switch (info.itemType) {
1738 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1739 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001740 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001741 break;
1742 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001743 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001744 break;
1745 default:
1746 throw new IllegalStateException("Unknown item type: " + info.itemType);
1747 }
1748 }
1749
Adam Cohenfbba09b2011-07-18 21:43:05 -07001750 /**
1751 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001752 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001753 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001754 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1755 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001756 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1757 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1758 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001759 }
1760
Adam Cohenfbba09b2011-07-18 21:43:05 -07001761 /**
1762 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001763 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001764 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001765 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001766 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001767 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001768 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001769 // In the case where we've prebound the widget, we remove it from the DragLayer
1770 if (LOGD) {
1771 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1772 }
1773 getDragLayer().removeView(hostView);
1774
Adam Cohened66b2b2012-01-23 17:28:51 -08001775 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001776 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001777
1778 // Clear the boundWidget so that it doesn't get destroyed.
1779 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001780 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001781 // In this case, we either need to start an activity to get permission to bind
1782 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001783 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1784 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1785 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1786 this, info.componentName);
1787 } else {
1788 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1789 }
Adam Cohendd70d662012-10-04 16:53:44 -07001790 Bundle options = info.bindOptions;
1791
Sunny Goyalffe83f12014-08-14 17:39:34 -07001792 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1793 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001794 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001795 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001796 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001797 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001798 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001799 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001800 }
1801
Adam Cohendcd297f2013-06-18 13:13:40 -07001802 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001803 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001804 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 folderInfo.title = getText(R.string.folder_name);
1806
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001808 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809
1810 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001811 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301812 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001813 // Force measure the new folder icon
1814 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1815 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001816 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Winsonc0b52fe2015-09-09 16:38:15 -07001819 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001820 * Unbinds the view for the specified item, and removes the item and all its children.
1821 *
1822 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001823 * @param itemInfo the {@link ItemInfo} for this view.
1824 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001825 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001826 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001827 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001828 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001829 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1830 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001831 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001832 } else {
1833 mWorkspace.removeWorkspaceItem(v);
1834 }
Winsonc0b52fe2015-09-09 16:38:15 -07001835 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001836 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001837 }
1838 } else if (itemInfo instanceof FolderInfo) {
1839 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001840 if (v instanceof FolderIcon) {
1841 ((FolderIcon) v).removeListeners();
1842 }
Winsonc0b52fe2015-09-09 16:38:15 -07001843 mWorkspace.removeWorkspaceItem(v);
1844 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001845 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001846 }
1847 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1848 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001849 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001850 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001851 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001852 }
1853 } else {
1854 return false;
1855 }
1856 return true;
1857 }
1858
1859 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001860 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001861 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001862 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001863 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001864 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001865 // Deleting an app widget ID is a void call but writes to disk before returning
1866 // to the caller...
1867 new AsyncTask<Void, Void, Void>() {
1868 public Void doInBackground(Void ... args) {
1869 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1870 return null;
1871 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001872 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001873 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001874 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001875 }
1876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 @Override
1878 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001879 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
1881
Joe Onorato88ec0992009-11-19 13:16:06 -08001882 @Override
1883 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001884 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1885 return;
1886 }
1887
Sunny Goyal45478022015-06-08 16:52:41 -07001888 if (mDragController.isDragging()) {
1889 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001890 return;
1891 }
1892
Jon Mirandafeba90f2016-10-06 10:53:29 -07001893 // Note: There should be at most one log per method call. This is enforced implicitly
1894 // by using if-else statements.
1895 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001896 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1897 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001898 topView.onBackPressed();
Sunny Goyalf9403d92017-10-18 10:55:56 -07001899 } else if (isInState(LauncherState.ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301900 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001901 showWorkspace(true);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001902 } else if (isInState(LauncherState.OVERVIEW)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301903 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001904 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001905 } else {
1906 // Back button is a no-op here, but give at least some feedback for the button press
1907 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001908 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001909 }
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 * Launches the intent referred by the clicked shortcut.
1913 *
1914 * @param v The view representing the clicked shortcut.
1915 */
1916 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001917 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1918 // view has detached (it's possible for this to happen if the view is removed mid touch).
1919 if (v.getWindowToken() == null) {
1920 return;
1921 }
1922
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001923 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001924 return;
1925 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001926
Adam Cohen1697b792013-09-17 19:08:21 -07001927 if (v instanceof Workspace) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07001928 if (isInState(LauncherState.OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001929 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1930 LauncherLogProto.Action.Direction.NONE,
1931 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07001932 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001933 }
1934 return;
1935 }
1936
1937 if (v instanceof CellLayout) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07001938 if (isInState(LauncherState.OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001939 int page = mWorkspace.indexOfChild(v);
1940 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1941 LauncherLogProto.Action.Direction.NONE,
1942 LauncherLogProto.ContainerType.OVERVIEW, page);
1943 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001944 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001945 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001946 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001947 }
1948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001950 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001951 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001953 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001954 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001955 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001956 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001957 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001958 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001959 } else if (tag instanceof AppInfo) {
1960 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001961 } else if (tag instanceof LauncherAppWidgetInfo) {
1962 if (v instanceof PendingAppWidgetHostView) {
1963 onClickPendingWidget((PendingAppWidgetHostView) v);
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966 }
1967
Sunny Goyal70660032015-05-14 00:07:08 -07001968 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001969 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001970 return false;
1971 }
1972
Michael Jurkaaf442092010-06-10 17:01:57 -07001973 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001974 * Event handler for the app widget view which has not fully restored.
1975 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001976 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001977 if (mIsSafeModeEnabled) {
1978 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1979 return;
1980 }
1981
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001982 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001983 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001984 LauncherAppWidgetProviderInfo appWidgetInfo =
1985 mAppWidgetManager.findProvider(info.providerName, info.user);
1986 if (appWidgetInfo == null) {
1987 return;
1988 }
1989 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1990
Sunny Goyald478c832016-04-01 12:04:16 -07001991 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1992 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1993 // This should not happen, as we make sure that an Id is allocated during bind.
1994 return;
1995 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001996 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1997 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001998 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001999 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002000 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002001 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002002 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002003 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002004 }
2005 }
2006
2007 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002008 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2009 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2010 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002011 *
2012 * @param v The view that was clicked.
2013 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002014 protected void onClickAllAppsButton(View v) {
2015 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyalf9403d92017-10-18 10:55:56 -07002016 if (!isInState(LauncherState.ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302017 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2018 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002019 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002020 } else {
2021 showWorkspace(true);
2022 }
2023 }
2024
Sunny Goyale6e72002017-01-12 16:55:36 -08002025 private void onClickPendingAppItem(final View v, final String packageName,
2026 boolean downloadStarted) {
2027 if (downloadStarted) {
2028 // If the download has started, simply direct to the market app.
2029 startMarketIntentForPackage(v, packageName);
2030 return;
2031 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002032 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002033 .setTitle(R.string.abandoned_promises_title)
2034 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002035 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2036 @Override
2037 public void onClick(DialogInterface dialogInterface, int i) {
2038 startMarketIntentForPackage(v, packageName);
2039 }
2040 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002041 .setNeutralButton(R.string.abandoned_clean_this,
2042 new DialogInterface.OnClickListener() {
2043 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002044 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002045 mWorkspace.removeAbandonedPromise(packageName, user);
2046 }
2047 })
2048 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002049 }
2050
2051 private void startMarketIntentForPackage(View v, String packageName) {
2052 ItemInfo item = (ItemInfo) v.getTag();
2053 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002054 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002055 }
2056
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002057 /**
2058 * Event handler for an app shortcut click.
2059 *
2060 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2061 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002062 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002063 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2064 Object tag = v.getTag();
2065 if (!(tag instanceof ShortcutInfo)) {
2066 throw new IllegalArgumentException("Input must be a Shortcut");
2067 }
2068
2069 // Open shortcut
2070 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002071
2072 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002073 if ((shortcut.isDisabled &
2074 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2075 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2076 // If the app is only disabled because of the above flags, launch activity anyway.
2077 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002078 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002079 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2080 // Use a message specific to this shortcut, if it has one.
2081 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2082 return;
2083 }
2084 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002085 int error = R.string.activity_not_available;
2086 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2087 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002088 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2089 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002090 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002091 }
2092 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2093 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002094 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002095 }
2096
Chris Wren40c5ed32014-06-24 18:24:23 -04002097 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002098 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002099 String packageName = shortcut.intent.getComponent() != null ?
2100 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2101 if (!TextUtils.isEmpty(packageName)) {
2102 onClickPendingAppItem(v, packageName,
2103 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2104 return;
2105 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002106 }
2107
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002108 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002109 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002110 }
2111
Sunny Goyala7ce1662016-05-31 15:01:35 -07002112 private void startAppShortcutOrInfoActivity(View v) {
2113 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002114 Intent intent;
2115 if (item instanceof PromiseAppInfo) {
2116 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2117 intent = promiseAppInfo.getMarketIntent();
2118 } else {
2119 intent = item.getIntent();
2120 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002121 if (intent == null) {
2122 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002123 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002124 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002125 }
2126
2127 /**
2128 * Event handler for a folder icon click.
2129 *
2130 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2131 */
2132 protected void onClickFolderIcon(View v) {
2133 if (LOGD) Log.d(TAG, "onClickFolder");
2134 if (!(v instanceof FolderIcon)){
2135 throw new IllegalArgumentException("Input must be a FolderIcon");
2136 }
2137
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002138 Folder folder = ((FolderIcon) v).getFolder();
2139 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002140 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002141 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002142 }
Michael Jurka5130e402011-10-13 04:55:35 -07002143 }
2144
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002145 /**
2146 * Event handler for the (Add) Widgets button that appears after a long press
2147 * on the home screen.
2148 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002149 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002150 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002151 if (mIsSafeModeEnabled) {
2152 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2153 } else {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002154 WidgetsFullSheet.show(this, true /* animated */);
Adam Cohen9211d422014-10-07 18:14:53 -07002155 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002156 }
2157
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002158 /**
2159 * Event handler for the wallpaper picker button that appears after a long press
2160 * on the home screen.
2161 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002162 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002163 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002164 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2165 return;
2166 }
2167
Tony Wickham785f7a52015-08-31 17:28:32 -07002168 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2169 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002170 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002171 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002172 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002173
2174 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002175 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2176 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002177 intent.setPackage(pickerPackage);
2178 }
2179
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002180 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002181 try {
2182 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2183 // If there is no target package, use the default intent chooser animation
2184 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2185 } catch (ActivityNotFoundException e) {
2186 setWaitingForResult(null);
2187 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2188 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002189 }
2190
2191 /**
2192 * Event handler for a click on the settings button that appears after a long press
2193 * on the home screen.
2194 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002195 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002196 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002197 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2198 .setPackage(getPackageName());
2199 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002200 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002201 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002202 }
2203
Tony Wickhame2217252016-03-22 16:34:23 -07002204 @Override
2205 public void onAccessibilityStateChanged(boolean enabled) {
2206 mDragLayer.onAccessibilityStateChanged(enabled);
2207 }
2208
Sunny Goyala7ce1662016-05-31 15:01:35 -07002209 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002211 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2212 try {
2213 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2214 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2215 // is enabled by default on NYC.
2216 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2217 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002218
2219 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2220 String id = ((ShortcutInfo) info).getDeepShortcutId();
2221 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302222 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002223 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002224 } else {
2225 // Could be launching some bookkeeping activity
2226 startActivity(intent, optsBundle);
2227 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002228 } finally {
2229 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002232 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2233 // corresponding permission. Show the appropriate permission prompt if that
2234 // is the case.
2235 if (intent.getComponent() == null
2236 && Intent.ACTION_CALL.equals(intent.getAction())
2237 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2238 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002239
2240 setWaitingForResult(PendingRequestArgs
2241 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002242 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2243 REQUEST_PERMISSION_CALL_PHONE);
2244 } else {
2245 // No idea why this was thrown.
2246 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002250
Sunny Goyalfe770c92016-09-27 14:38:58 -07002251 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002252 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002253 if (Utilities.ATLEAST_MARSHMALLOW) {
2254 int left = 0, top = 0;
2255 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002256 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002257 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002258 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002259 if (icon != null) {
2260 Rect bounds = icon.getBounds();
2261 left = (width - bounds.width()) / 2;
2262 top = v.getPaddingTop();
2263 width = bounds.width();
2264 height = bounds.height();
2265 }
2266 }
2267 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2268 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2269 // On L devices, we use the device default slide-up transition.
2270 // On L MR1 devices, we use a custom version of the slide-up transition which
2271 // doesn't have the delay present in the device default.
2272 return ActivityOptions.makeCustomAnimation(
2273 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2274 }
2275 return null;
2276 }
2277
Tonye3c59252017-05-02 21:32:27 -07002278 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002279 int[] pos = new int[2];
2280 v.getLocationOnScreen(pos);
2281 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2282 }
2283
Sunny Goyala7ce1662016-05-31 15:01:35 -07002284 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002285 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002286 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2287 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002288 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002289 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002290 // Only launch using the new animation if the shortcut has not opted out (this is a
2291 // private contract between launcher and may be ignored in the future).
2292 boolean useLaunchAnimation = (v != null) &&
2293 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2294 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2295
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002296 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002297
2298 // Prepare intent
2299 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2300 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002301 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002302 }
2303 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002304 if (Utilities.ATLEAST_MARSHMALLOW
2305 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002306 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002307 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002308 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002309 // Shortcuts need some special checks due to legacy reasons.
2310 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002311 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002312 // Could be launching some bookkeeping activity
2313 startActivity(intent, optsBundle);
2314 } else {
2315 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2316 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2317 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002318
2319 if (v instanceof BubbleTextView) {
2320 // This is set to the view that launched the activity that navigated the user away
2321 // from launcher. Since there is no callback for when the activity has finished
2322 // launching, enable the press state and keep this reference to reset the press
2323 // state when we return to launcher.
2324 BubbleTextView btv = (BubbleTextView) v;
2325 btv.setStayPressed(true);
2326 setOnResumeCallback(btv);
2327 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002328 mAppLaunchSuccess = true;
2329 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002330 } catch (ActivityNotFoundException|SecurityException e) {
2331 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2332 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2333 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002334 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002335 }
2336
Tony Wickhamdadb3042016-02-24 11:07:00 -08002337 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002338 public boolean dispatchTouchEvent(MotionEvent ev) {
2339 mLastDispatchTouchEventX = ev.getX();
2340 return super.dispatchTouchEvent(ev);
2341 }
2342
2343 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002345 if (!isDraggingEnabled()) return false;
2346 if (isWorkspaceLocked()) return false;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07002347 if (!isInState(LauncherState.NORMAL) && !isInState(LauncherState.OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348
Jon Miranda51f037d2016-11-07 08:37:20 -08002349 boolean ignoreLongPressToOverview =
2350 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002351
Adam Cohen1697b792013-09-17 19:08:21 -07002352 if (v instanceof Workspace) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002353 if (!isInState(LauncherState.OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002354 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302355 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2356 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002357 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002358 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002359 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2360 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2361 return true;
2362 } else {
2363 return false;
2364 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002365 } else {
2366 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002367 }
Adam Cohen1697b792013-09-17 19:08:21 -07002368 }
2369
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002370 CellLayout.CellInfo longClickCellInfo = null;
2371 View itemUnderLongClick = null;
2372 if (v.getTag() instanceof ItemInfo) {
2373 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002374 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002375 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002376 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 }
2378
Winson Chung3d503fb2011-07-13 17:25:49 -07002379 // The hotseat touch handling does not go through Workspace, and we always allow long press
2380 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002381 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002382 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002383 // User long pressed on empty space
Sunny Goyalf9403d92017-10-18 10:55:56 -07002384 if (isInState(LauncherState.OVERVIEW)) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002385 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302386 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2387 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002388 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002389 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002390 return false;
2391 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302392 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2393 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002394 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002395 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002396 }
Jon Miranda379198e2016-09-23 08:54:40 -07002397 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2398 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002400 final boolean isAllAppsButton =
2401 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2402 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2403 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002404 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002406 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 }
2408 }
2409 }
2410 return true;
2411 }
2412
Winson Chung3d503fb2011-07-13 17:25:49 -07002413 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002414 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 return mHotseat != null && layout != null &&
2416 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2417 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002418
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 /**
2420 * Returns the CellLayout of the specified container at the specified screen.
2421 */
Sunny Goyal92820592015-03-02 11:31:35 -08002422 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2424 if (mHotseat != null) {
2425 return mHotseat.getLayout();
2426 } else {
2427 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002428 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002430 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002431 }
2432 }
2433
Michael Jurkae326f182011-11-21 14:05:46 -08002434 @Override
2435 public void onTrimMemory(int level) {
2436 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002437 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2438 // The widget preview db can result in holding onto over
2439 // 3MB of memory for caching which isn't necessary.
2440 SQLiteDatabase.releaseMemory();
2441
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002442 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002443 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002444 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002445 if (mLauncherCallbacks != null) {
2446 mLauncherCallbacks.onTrimMemory(level);
2447 }
Michael Jurkae326f182011-11-21 14:05:46 -08002448 }
2449
Winson5c6bdbb2015-09-03 11:36:19 -07002450 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002451 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002452 }
2453
Winson5c6bdbb2015-09-03 11:36:19 -07002454 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002455 boolean changed = !isInState(LauncherState.NORMAL);
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002456 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002457 mWorkspace.setVisibility(View.VISIBLE);
Sunny Goyalf9403d92017-10-18 10:55:56 -07002458 mStateTransitionAnimation.startAnimationToWorkspace(
Sunny Goyal4c7f2152017-10-17 17:17:16 -07002459 LauncherState.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002460
Michael Jurkab3e22d92011-10-31 15:58:33 -07002461 // Set focus to the AppsCustomize button
2462 if (mAllAppsButton != null) {
2463 mAllAppsButton.requestFocus();
2464 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002465 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002466
Winson Chung0f785722015-04-08 10:27:49 -07002467 if (changed) {
2468 // Send an accessibility event to announce the context change
2469 getWindow().getDecorView()
2470 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002471 }
Winson5c6bdbb2015-09-03 11:36:19 -07002472 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002473 }
2474
Winsone9f27272015-10-13 10:47:51 -07002475 /**
2476 * Shows the overview button.
2477 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002478 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002479 showOverviewMode(animated, false);
2480 }
2481
2482 /**
2483 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2484 * onto one of the overview panel buttons.
2485 */
2486 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2487 Runnable postAnimRunnable = null;
2488 if (requestButtonFocus) {
2489 postAnimRunnable = new Runnable() {
2490 @Override
2491 public void run() {
2492 // Hitting the menu button when in touch mode does not trigger touch mode to
2493 // be disabled, so if requested, force focus on one of the overview panel
2494 // buttons.
2495 mOverviewPanel.requestFocusFromTouch();
2496 }
2497 };
2498 }
Adam Cohened307df2013-10-02 09:37:31 -07002499 mWorkspace.setVisibility(View.VISIBLE);
Sunny Goyalf9403d92017-10-18 10:55:56 -07002500 mStateTransitionAnimation.startAnimationToWorkspace(
Sunny Goyal4c7f2152017-10-17 17:17:16 -07002501 LauncherState.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002502
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002503 // If animated from long press, then don't allow any of the controller in the drag
2504 // layer to intercept any remaining touch.
2505 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002506 }
2507
Winson Chungb745afb2015-03-02 11:51:23 -08002508 /**
2509 * Shows the apps view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002510 *
2511 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002512 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002513 // TODO: calling method should use the return value so that when {@code false} is returned
2514 // the workspace transition doesn't fall into invalid state.
Sunny Goyalaeb16432017-10-16 11:46:41 -07002515 public boolean showAppsView(boolean animated) {
2516 markAppsViewShown();
2517
Sunny Goyalf9403d92017-10-18 10:55:56 -07002518 if (!(isInState(LauncherState.NORMAL) ||
2519 (isInState(LauncherState.ALL_APPS) && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002520 return false;
2521 }
Winson Chungb745afb2015-03-02 11:51:23 -08002522
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002523 // This is a safe and supported transition to bypass spring_loaded mode.
2524 if (mExitSpringLoadedModeRunnable != null) {
2525 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2526 mExitSpringLoadedModeRunnable = null;
2527 }
2528
Sunny Goyalaeb16432017-10-16 11:46:41 -07002529 mStateTransitionAnimation.startAnimationToAllApps(animated);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002530
Michael Jurkab3e22d92011-10-31 15:58:33 -07002531 // Change the state *after* we've called all the transition code
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002532 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002533
2534 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002535 getWindow().getDecorView()
2536 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002537 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002538 }
2539
Hyunyoung Song3f471442015-04-08 19:01:34 -07002540 public void enterSpringLoadedDragMode() {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002541 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07002542 mWorkspace.getState().ordinal));
Sunny Goyalf9403d92017-10-18 10:55:56 -07002543 if (isInState(LauncherState.SPRING_LOADED)) {
Winson Chungb745afb2015-03-02 11:51:23 -08002544 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002545 }
Winson Chungb745afb2015-03-02 11:51:23 -08002546
Sunny Goyal1797af42017-10-06 13:29:57 -07002547 // Lock the orientation:
2548 if (mRotationEnabled) {
2549 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
2550 }
2551
2552 // Prevent any Un/InstallShortcutReceivers from updating the db while we are
2553 // in spring loaded mode
2554 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_DRAG_AND_DROP);
2555
Sunny Goyalf9403d92017-10-18 10:55:56 -07002556 mStateTransitionAnimation.startAnimationToWorkspace(
Sunny Goyal4c7f2152017-10-17 17:17:16 -07002557 LauncherState.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002558 null /* onCompleteRunnable */);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002559 }
Adam Cohen7777d962011-08-18 18:58:38 -07002560
Sunny Goyalaeb16432017-10-16 11:46:41 -07002561 public void exitSpringLoadedDragMode(int delay) {
2562 exitSpringLoadedDragMode(delay, null);
Sunny Goyal1797af42017-10-06 13:29:57 -07002563 }
2564
Sunny Goyalaeb16432017-10-16 11:46:41 -07002565 public void exitSpringLoadedDragMode(int delay, final Runnable onCompleteRunnable) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002566 if (!isInState(LauncherState.SPRING_LOADED)) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002567
Sunny Goyal1797af42017-10-06 13:29:57 -07002568 // Unlock rotation lock
2569 unlockScreenOrientation(false);
2570
2571 // Re-enable any Un/InstallShortcutReceiver and now process any queued items
2572 InstallShortcutReceiver.disableAndFlushInstallQueue(
2573 InstallShortcutReceiver.FLAG_DRAG_AND_DROP, this);
2574
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002575 if (mExitSpringLoadedModeRunnable != null) {
2576 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2577 }
2578 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002579 @Override
2580 public void run() {
Sunny Goyalaeb16432017-10-16 11:46:41 -07002581 showWorkspace(true, onCompleteRunnable);
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002582 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002583 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002584 };
2585 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002586 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002587
Michael Jurkad7c28052012-04-27 15:43:36 -07002588 @Override
2589 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002590 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002591 final List<CharSequence> text = event.getText();
2592 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002593 // Populate event with a fake title based on the current state.
Sunny Goyalf9403d92017-10-18 10:55:56 -07002594 if (isInState(LauncherState.ALL_APPS)) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002595 text.add(getString(R.string.all_apps_button_label));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002596 } else if (mWorkspace != null) {
2597 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002598 } else {
2599 text.add(getString(R.string.all_apps_home_button_label));
2600 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002601 return result;
2602 }
2603
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002604 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002605 * If the activity is currently paused, signal that we need to run the passed Runnable
2606 * in onResume.
2607 *
2608 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002609 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2610 * wrong when we're not running, and if the activity comes back to what the configuration was
2611 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002612 *
2613 * Implementation of the method from LauncherModel.Callbacks.
2614 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002615 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002616 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002617 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002618 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002619 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002620 if (run instanceof RunnableWithId) {
2621 // Remove any runnables which have the same id
2622 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002623 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002624 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002625 return true;
2626 } else {
2627 return false;
2628 }
2629 }
2630
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002631 public void setOnResumeCallback(OnResumeCallback callback) {
2632 if (mOnResumeCallback != null) {
2633 mOnResumeCallback.onLauncherResume();
2634 }
2635 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002636 }
2637
Michael Jurka7607c2f2013-04-03 14:33:19 -07002638 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002639 * If the activity is currently paused, signal that we need to re-run the loader
2640 * in onResume.
2641 *
2642 * This needs to be called from incoming places where resources might have been loaded
2643 * while we are paused. That is becaues the Configuration might be wrong
2644 * when we're not running, and if it comes back to what it was when we
2645 * were paused, we are not restarted.
2646 *
2647 * Implementation of the method from LauncherModel.Callbacks.
2648 *
2649 * @return true if we are currently paused. The caller might be able to
2650 * skip some work in that case since we will come back again.
2651 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002652 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002653 public boolean setLoadOnResume() {
2654 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002655 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002656 mOnResumeNeedsLoad = true;
2657 return true;
2658 } else {
2659 return false;
2660 }
2661 }
2662
2663 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002664 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002666 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002667 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002668 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002669 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002670 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002671 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002672 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002673 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002674
Joe Onorato9c1289c2009-08-17 11:03:03 -04002675 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002676 * Clear any pending bind callbacks. This is called when is loader is planning to
2677 * perform a full rebind from scratch.
2678 */
2679 @Override
2680 public void clearPendingBinds() {
2681 mBindOnResumeCallbacks.clear();
2682 if (mPendingExecutor != null) {
2683 mPendingExecutor.markCompleted();
2684 mPendingExecutor = null;
2685 }
2686 }
2687
2688 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002689 * Refreshes the shortcuts shown on the workspace.
2690 *
2691 * Implementation of the method from LauncherModel.Callbacks.
2692 */
2693 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002694 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002695 // Floating panels (except the full widget sheet) are associated with individual icons. If
2696 // we are starting a fresh bind, close all such panels as all the icons are about
2697 // to go away.
2698 AbstractFloatingView.closeOpenViews(this, true,
2699 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002700
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002701 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002702
Winson Chungd64d1762013-08-20 14:37:16 -07002703 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002704 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002705 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002706
Winson Chung3d503fb2011-07-13 17:25:49 -07002707 if (mHotseat != null) {
2708 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002710 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 }
2712
Adam Cohendcd297f2013-06-18 13:13:40 -07002713 @Override
2714 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002715 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002716 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2717 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002718 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2719 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002720 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002721 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2722 // If there are no screens, we need to have an empty screen
2723 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002724 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002725 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002726
Winsonc7d2e832016-07-28 12:24:55 -07002727 // After we have added all the screens, if the wallpaper was locked to the default state,
2728 // then notify to indicate that it can be released and a proper wallpaper offset can be
2729 // computed before the next layout
2730 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002731 }
2732
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002733 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002734 int count = orderedScreenIds.size();
2735 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002736 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002737 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002738 // No need to bind the first screen, as its always bound.
2739 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2740 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002741 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002742 }
2743
Sunny Goyalb23980c2017-08-17 07:45:25 -07002744 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002745 public void bindAppsAdded(final ArrayList<Long> newScreens,
2746 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002747 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002748 Runnable r = new Runnable() {
2749 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002750 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002751 }
2752 };
2753 if (waitUntilResume(r)) {
2754 return;
2755 }
2756
Winson Chungd64d1762013-08-20 14:37:16 -07002757 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002758 if (newScreens != null) {
2759 bindAddScreens(newScreens);
2760 }
Winson Chungd64d1762013-08-20 14:37:16 -07002761
2762 // We add the items without animation on non-visible pages, and with
2763 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002764 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002765 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002766 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002767 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002768 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002769 }
Winson Chungc58497e2013-09-03 17:48:37 -07002770
Winson Chung87412982013-10-03 18:34:14 -07002771 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002772 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002773 }
2774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002776 * Bind the items start-end from the list.
2777 *
2778 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002780 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002781 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002782 Runnable r = new Runnable() {
2783 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002784 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002785 }
2786 };
2787 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002788 return;
2789 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002790
Sunny Goyal3be633b2016-12-08 09:59:25 -08002791 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002792 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002793 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002794 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002795 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002796 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002797 int end = items.size();
2798 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002799 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002800
2801 // Short circuit if we are loading dock items for a configuration which has no dock
2802 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2803 mHotseat == null) {
2804 continue;
2805 }
2806
Sunny Goyal639e9062015-08-19 19:17:06 -07002807 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 switch (item.itemType) {
2809 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2810 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002811 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002812 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002813 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002815 }
2816 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002817 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002818 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002819 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002820 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002821 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002822 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2823 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002824 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002825 if (view == null) {
2826 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002827 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002828 break;
2829 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002830 default:
2831 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002833
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002834 /*
2835 * Remove colliding items.
2836 */
2837 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2838 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2839 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2840 View v = cl.getChildAt(item.cellX, item.cellY);
2841 Object tag = v.getTag();
2842 String desc = "Collision while binding workspace item: " + item
2843 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002844 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002845 throw (new RuntimeException(desc));
2846 } else {
2847 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002848 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002849 continue;
2850 }
2851 }
2852 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302853 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002854 if (animateIcons) {
2855 // Animate all the applications up now
2856 view.setAlpha(0f);
2857 view.setScaleX(0f);
2858 view.setScaleY(0f);
2859 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002860 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002863
Winson Chung997a9232013-07-24 15:33:46 -07002864 if (animateIcons) {
2865 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002866 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002867 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002868 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002869 final Runnable startBounceAnimRunnable = new Runnable() {
2870 public void run() {
2871 anim.playTogether(bounceAnims);
2872 anim.start();
2873 }
2874 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002875 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002876 // We post the animation slightly delayed to prevent slowdowns
2877 // when we are loading right after we return to launcher.
2878 mWorkspace.postDelayed(new Runnable() {
2879 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002880 if (mWorkspace != null) {
2881 mWorkspace.snapToPage(newScreenIndex);
2882 mWorkspace.postDelayed(startBounceAnimRunnable,
2883 NEW_APPS_ANIMATION_DELAY);
2884 }
Winson Chung94d67682013-09-25 16:29:40 -07002885 }
2886 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002887 } else {
2888 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002889 }
2890 }
Winson Chung64359a52013-07-08 17:17:08 -07002891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 }
2894
Joe Onorato9c1289c2009-08-17 11:03:03 -04002895 /**
2896 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002898 public void bindAppWidget(LauncherAppWidgetInfo item) {
2899 View view = inflateAppWidget(item);
2900 if (view != null) {
2901 mWorkspace.addInScreen(view, item);
2902 mWorkspace.requestLayout();
2903 }
2904 }
2905
2906 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002907 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302908 PendingAppWidgetHostView view =
2909 new PendingAppWidgetHostView(this, item, mIconCache, true);
2910 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002911 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002912 }
2913
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002914 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002916 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002917
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002918 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2919 // If the provider is not ready, bind as a pending widget.
2920 appWidgetInfo = null;
2921 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2922 // The widget id is not valid. Try to find the widget based on the provider info.
2923 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2924 } else {
2925 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2926 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002927
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002928 // If the provider is ready, but the width is not yet restored, try to restore it.
2929 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2930 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002931 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002932 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2933 + " belongs to component " + item.providerName
2934 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002935 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002936 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002937 }
Sunny Goyalff572272014-07-23 13:58:07 -07002938
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002939 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002940 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002941 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2942 // Id has not been allocated yet. Allocate a new id.
2943 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2944 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002945
Sunny Goyald478c832016-04-01 12:04:16 -07002946 // Also try to bind the widget. If the bind fails, the user will be shown
2947 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002948 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002949 pendingInfo.spanX = item.spanX;
2950 pendingInfo.spanY = item.spanY;
2951 pendingInfo.minSpanX = item.minSpanX;
2952 pendingInfo.minSpanY = item.minSpanY;
2953 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002954
2955 boolean isDirectConfig =
2956 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2957 if (isDirectConfig && item.bindOptions != null) {
2958 Bundle newOptions = item.bindOptions.getExtras();
2959 if (options != null) {
2960 newOptions.putAll(options);
2961 }
2962 options = newOptions;
2963 }
Sunny Goyald478c832016-04-01 12:04:16 -07002964 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2965 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002966
Sunny Goyal86df1382016-08-10 15:03:22 -07002967 // We tried to bind once. If we were not able to bind, we would need to
2968 // go through the permission dialog, which means we cannot skip the config
2969 // activity.
2970 item.bindOptions = null;
2971 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2972
Sunny Goyald478c832016-04-01 12:04:16 -07002973 // Bind succeeded
2974 if (success) {
2975 // If the widget has a configure activity, it is still needs to set it up,
2976 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002977 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002978 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2979 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002980 }
Sunny Goyald478c832016-04-01 12:04:16 -07002981
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002982 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002983 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002984 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002985 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002986 // The widget was marked as UI not ready, but there is no configure activity to
2987 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002988 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002989 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002990 }
Sunny Goyalff572272014-07-23 13:58:07 -07002991 }
2992
Sunny Goyald5462aa2016-11-22 16:52:39 +05302993 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002994 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002995 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002996 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002997 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002998 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002999 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003000 }
3001
Sunny Goyal233ee962015-08-03 13:05:01 -07003002 item.minSpanX = appWidgetInfo.minSpanX;
3003 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303004 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003005 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303006 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003007 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303008 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003010 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003011 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003012 }
3013
Sunny Goyalff572272014-07-23 13:58:07 -07003014 /**
3015 * Restores a pending widget.
3016 *
3017 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003018 */
Sunny Goyald478c832016-04-01 12:04:16 -07003019 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003020 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3021 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3022 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003023 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003024 }
3025
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003026 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003027 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003028 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3029 info.pendingItemInfo = null;
3030 }
Sunny Goyalff572272014-07-23 13:58:07 -07003031
Sunny Goyalba47faa2017-10-04 16:50:57 -07003032 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3033 view.reinflate();
3034 }
3035
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003036 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003037 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003038 }
3039
Adam Cohen1462de32012-07-24 22:34:36 -07003040 public void onPageBoundSynchronously(int page) {
3041 mSynchronouslyBoundPages.add(page);
3042 }
3043
Sunny Goyal527c7d32015-08-28 15:19:36 -07003044 @Override
3045 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3046 if (mPendingExecutor != null) {
3047 mPendingExecutor.markCompleted();
3048 }
3049 mPendingExecutor = executor;
3050 executor.attachTo(this);
3051 }
3052
3053 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3054 if (mPendingExecutor == executor) {
3055 mPendingExecutor = null;
3056 }
3057 }
3058
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003059 @Override
3060 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3061 Runnable r = new Runnable() {
3062 public void run() {
3063 finishFirstPageBind(executor);
3064 }
3065 };
3066 if (waitUntilResume(r)) {
3067 return;
3068 }
3069
3070 Runnable onComplete = new Runnable() {
3071 @Override
3072 public void run() {
3073 if (executor != null) {
3074 executor.onLoadAnimationCompleted();
3075 }
3076 }
3077 };
3078 if (mDragLayer.getAlpha() < 1) {
3079 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3080 } else {
3081 onComplete.run();
3082 }
3083 }
3084
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 /**
3086 * Callback saying that there aren't any more items to bind.
3087 *
3088 * Implementation of the method from LauncherModel.Callbacks.
3089 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003090 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003091 Runnable r = new Runnable() {
3092 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003093 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003094 }
3095 };
3096 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003097 return;
3098 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003099 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003100 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003102 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003103
Sunny Goyal2100c782016-08-22 16:00:03 -07003104 if (mPendingActivityResult != null) {
3105 handleActivityResult(mPendingActivityResult.requestCode,
3106 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3107 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003108 }
3109
Sunny Goyala474a9b2017-05-04 16:47:11 -07003110 InstallShortcutReceiver.disableAndFlushInstallQueue(
3111 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003112
Tony Wickham010d2552017-01-20 08:15:28 -08003113 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003114 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003115 }
3116
Winson Chunga2413752012-04-03 14:22:34 -07003117 private boolean canRunNewAppsAnimation() {
3118 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003119 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003120 }
3121
Winson Chungc9168342013-06-26 14:54:55 -07003122 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003123 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003124 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3125 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003126 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003127 return bounceAnim;
3128 }
3129
Adam Cohen27772732013-11-11 14:00:56 +00003130 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003131 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003132 }
3133
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003134 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003135 * Add the icons for all apps.
3136 *
3137 * Implementation of the method from LauncherModel.Callbacks.
3138 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003139 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003140 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3141 public void run() {
3142 bindAllApplications(apps);
3143 }
3144 };
3145 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003146 return;
3147 }
3148
Winson Chungb745afb2015-03-02 11:51:23 -08003149 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003150 Executor pendingExecutor = getPendingExecutor();
Sunny Goyalf9403d92017-10-18 10:55:56 -07003151 if (pendingExecutor != null && !isInState(LauncherState.ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07003152 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003153 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003154 return;
3155 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003156
Winson Chungb745afb2015-03-02 11:51:23 -08003157 mAppsView.setApps(apps);
3158 }
Adam Cohen9211d422014-10-07 18:14:53 -07003159 if (mLauncherCallbacks != null) {
3160 mLauncherCallbacks.bindAllApplications(apps);
3161 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003162 }
3163
3164 /**
Tony2917a8b2017-07-31 23:29:42 -07003165 * Returns an Executor that will run after the launcher is first drawn (including after the
3166 * initial fade in animation). Returns null if the first draw has already occurred.
3167 */
3168 public @Nullable Executor getPendingExecutor() {
3169 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3170 }
3171
3172 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003173 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3174 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3175 */
3176 @Override
3177 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003178 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003179 }
3180
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003181 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 * A package was updated.
3183 *
3184 * Implementation of the method from LauncherModel.Callbacks.
3185 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003186 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003187 Runnable r = new Runnable() {
3188 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003189 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003190 }
3191 };
3192 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003193 return;
3194 }
3195
Winson Chungb745afb2015-03-02 11:51:23 -08003196 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003197 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003199 }
3200
Sunny Goyal4390ace2014-10-13 11:33:11 -07003201 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003202 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3203 Runnable r = new Runnable() {
3204 public void run() {
3205 bindPromiseAppProgressUpdated(app);
3206 }
3207 };
3208 if (waitUntilResume(r)) {
3209 return;
3210 }
3211
3212 if (mAppsView != null) {
3213 mAppsView.updatePromiseAppProgress(app);
3214 }
3215 }
3216
3217 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003218 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3219 Runnable r = new Runnable() {
3220 public void run() {
3221 bindWidgetsRestored(widgets);
3222 }
3223 };
3224 if (waitUntilResume(r)) {
3225 return;
3226 }
3227 mWorkspace.widgetsRestored(widgets);
3228 }
3229
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003231 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003232 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003233 *
3234 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003235 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003236 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003237 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003238 Runnable r = new Runnable() {
3239 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003240 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003241 }
3242 };
3243 if (waitUntilResume(r)) {
3244 return;
3245 }
3246
Sunny Goyal4390ace2014-10-13 11:33:11 -07003247 if (!updated.isEmpty()) {
3248 mWorkspace.updateShortcuts(updated);
3249 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003250 }
3251
3252 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003253 * Update the state of a package, typically related to install state.
3254 *
3255 * Implementation of the method from LauncherModel.Callbacks.
3256 */
Sunny Goyale755d462014-07-22 13:48:29 -07003257 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003258 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3259 Runnable r = new Runnable() {
3260 public void run() {
3261 bindRestoreItemsChange(updates);
3262 }
3263 };
3264 if (waitUntilResume(r)) {
3265 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003266 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003267
Sunny Goyal756adbc2015-04-16 15:20:51 -07003268 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003269 }
3270
3271 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003272 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003273 * in addition to specific applications to remove, the reason being that
3274 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003275 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003276 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003278 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003279 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003280 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003281 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003282 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003283 }
Winson Chungd64d1762013-08-20 14:37:16 -07003284 };
3285 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003286 return;
3287 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003288 mWorkspace.removeItemsByMatcher(matcher);
3289 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003290 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003291
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003292 @Override
3293 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3294 Runnable r = new Runnable() {
3295 public void run() {
3296 bindAppInfosRemoved(appInfos);
3297 }
3298 };
3299 if (waitUntilResume(r)) {
3300 return;
Joe Onorato36115782010-06-17 13:28:48 -04003301 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003302
Winson Chungdf95eb12013-10-16 14:57:07 -07003303 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003304 if (mAppsView != null) {
3305 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003306 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 }
Joe Onoratobe386092009-11-17 17:32:16 -08003308
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003309 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003310 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3311 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003312 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3313 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003314 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003315 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003316 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003317 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003318 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003319 return;
3320 }
3321
Tony Wickham26b17462017-03-20 17:12:24 -07003322 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3323 if (topView != null) {
3324 topView.onWidgetsBound();
3325 }
3326 }
3327
Tony Wickham86222d22017-03-29 15:30:43 -07003328 /**
3329 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3330 * refreshes the widgets and shortcuts associated with the given package/user
3331 */
3332 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003333 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003334 }
3335
Winson Chung4b919f82012-05-01 10:44:08 -07003336 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003337 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003338 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003339 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003340 } else {
3341 mHandler.postDelayed(new Runnable() {
3342 public void run() {
3343 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3344 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003345 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003346 }
Winson Chung4b919f82012-05-01 10:44:08 -07003347 }
Winson Chung400438b2011-01-16 17:53:48 -08003348 }
3349
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003350 private void markAppsViewShown() {
3351 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3352 return;
3353 }
3354 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3355 }
3356
3357 private boolean shouldShowDiscoveryBounce() {
Sunny Goyalf9403d92017-10-18 10:55:56 -07003358 return isInState(LauncherState.NORMAL) && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)
3359 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003360 }
3361
Winson Chung80baf5a2010-08-09 16:03:15 -07003362 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003363 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003364 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003365 @Override
3366 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3367 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003368
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003369 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3370 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003371 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003372 writer.println(prefix + " Homescreen " + i);
3373
3374 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3375 for (int j = 0; j < layout.getChildCount(); j++) {
3376 Object tag = layout.getChildAt(j).getTag();
3377 if (tag != null) {
3378 writer.println(prefix + " " + tag.toString());
3379 }
3380 }
3381 }
3382
3383 writer.println(prefix + " Hotseat");
3384 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003385 for (int j = 0; j < layout.getChildCount(); j++) {
3386 Object tag = layout.getChildAt(j).getTag();
3387 if (tag != null) {
3388 writer.println(prefix + " " + tag.toString());
3389 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003390 }
Sunny Goyala1365452015-10-01 15:46:24 -07003391
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003392 try {
3393 FileLog.flushAll(writer);
3394 } catch (Exception e) {
3395 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003396 }
3397 }
3398
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003399 writer.println(prefix + "Misc:");
3400 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3401 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3402 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3403
3404 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003405
Adam Cohen9211d422014-10-07 18:14:53 -07003406 if (mLauncherCallbacks != null) {
3407 mLauncherCallbacks.dump(prefix, fd, writer, args);
3408 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003409 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003410
Sunny Goyal66b24572016-09-21 15:57:55 -07003411 @Override
3412 @TargetApi(Build.VERSION_CODES.N)
3413 public void onProvideKeyboardShortcuts(
3414 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3415
3416 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyalf9403d92017-10-18 10:55:56 -07003417 if (isInState(LauncherState.NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003418 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3419 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3420 }
3421 View currentFocus = getCurrentFocus();
3422 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3423 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3424 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3425 }
Tony18c4aa42017-05-10 21:23:57 -05003426 if (currentFocus.getTag() instanceof ItemInfo
3427 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003428 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3429 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3430 }
3431 if (!shortcutInfos.isEmpty()) {
3432 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3433 }
3434
3435 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3436 }
3437
3438 @Override
3439 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3440 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3441 switch (keyCode) {
3442 case KeyEvent.KEYCODE_A:
Sunny Goyalf9403d92017-10-18 10:55:56 -07003443 if (isInState(LauncherState.NORMAL)) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003444 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003445 return true;
3446 }
3447 break;
3448 case KeyEvent.KEYCODE_S: {
3449 View focusedView = getCurrentFocus();
3450 if (focusedView instanceof BubbleTextView
3451 && focusedView.getTag() instanceof ItemInfo
3452 && mAccessibilityDelegate.performAction(focusedView,
3453 (ItemInfo) focusedView.getTag(),
3454 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003455 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003456 return true;
3457 }
3458 break;
3459 }
3460 case KeyEvent.KEYCODE_O:
3461 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3462 return true;
3463 }
3464 break;
3465 }
3466 }
3467 return super.onKeyShortcut(keyCode, event);
3468 }
3469
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003470 public static Launcher getLauncher(Context context) {
3471 if (context instanceof Launcher) {
3472 return (Launcher) context;
3473 }
3474 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3475 }
3476
Sunny Goyal5a81c382017-03-20 15:08:06 -07003477 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003478
3479 @Override
3480 public void onSharedPreferenceChanged(
3481 SharedPreferences sharedPreferences, String key) {
3482 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003483 // Recreate the activity so that it initializes the rotation preference again.
3484 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003485 }
3486 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003487 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003488
3489 /**
3490 * Callback for listening for onResume
3491 */
3492 public interface OnResumeCallback {
3493
3494 void onLauncherResume();
3495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003496}