blob: 8702660d2c018b53c2fdd426250c0de064954589 [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 Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070027import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080030import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080033import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080034import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040035import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070037import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040038import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070040import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070041import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070043import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070046import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070047import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070050import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020051import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080054import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070055import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040056import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070057import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070058import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070059import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.text.Selection;
61import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070062import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070064import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.HapticFeedbackConstants;
67import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070068import android.view.KeyboardShortcutGroup;
69import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080070import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080074import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.ViewGroup;
76import android.view.ViewTreeObserver;
Mario Bertschleracbf5702017-03-03 10:58:06 -080077import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080078import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070079import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070080import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070083
Sunny Goyal651077b2014-06-30 14:15:31 -070084import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070085import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070086import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070087import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070088import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070089import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalb5e65c82016-10-26 18:32:38 -070090import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyalffe83f12014-08-14 17:39:34 -070091import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010092import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070093import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -070094import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080095import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070096import com.android.launcher3.dragndrop.DragController;
97import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070098import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070099import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800100import com.android.launcher3.dragndrop.PinItemDragListener;
Tony Wickham827cef22016-03-17 15:39:39 -0700101import com.android.launcher3.dynamicui.ExtractedColors;
Mario Bertschler27288382017-05-24 15:35:09 -0700102import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000103import com.android.launcher3.folder.Folder;
104import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700105import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700106import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700107import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700108import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.model.ModelWriter;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700110import com.android.launcher3.model.PackageItemInfo;
111import com.android.launcher3.model.WidgetItem;
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;
Tony Wickham540913e2017-01-23 11:47:51 -0800114import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700116import com.android.launcher3.shortcuts.DeepShortcutManager;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800117import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530118import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
119import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700121import com.android.launcher3.util.ActivityResultInfo;
Sunny Goyal49fc3f72017-08-18 10:17:06 -0700122import com.android.launcher3.util.RunnableWithId;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700124import com.android.launcher3.util.ComponentKeyMapper;
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 Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700131import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700132import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700133import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700134import com.android.launcher3.util.TraceHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800136import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800138import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700139import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700141import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700142
Adam Cohen39933482017-09-29 16:43:51 -0700143
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700146import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700147import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700148import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700149import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800150import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700151import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700152
Sunny Goyal49fc3f72017-08-18 10:17:06 -0700153import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
154import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156/**
157 * Default launcher application.
158 */
Sunny Goyal27835952017-01-13 12:15:53 -0800159public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700160 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
161 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700162 AccessibilityManager.AccessibilityStateChangeListener,
163 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700164 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700165 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Winson Chunga2413752012-04-03 14:22:34 -0700167 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700171
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700173 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
177 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700178
Jon Mirandac476d6e2017-05-04 16:30:01 -0700179 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700180
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700181 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
182
Mathew Inwood876a8462013-06-14 14:12:41 +0100183 /**
184 * IntentStarter uses request codes starting with this. This must be greater than all activity
185 * request codes used internally.
186 */
187 protected static final int REQUEST_LAST = 100;
188
Mario Bertschleracbf5702017-03-03 10:58:06 -0800189 private static final int SOFT_INPUT_MODE_DEFAULT =
Jon Mirandafd516322017-03-09 13:14:50 -0800190 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800191 private static final int SOFT_INPUT_MODE_ALL_APPS =
Jon Mirandafd516322017-03-09 13:14:50 -0800192 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800193
Winson Chung2672ff92012-05-04 16:22:30 -0700194 // The Intent extra that defines whether to ignore the launch animation
195 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400196 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 // Type: int
199 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700200 // Type: int
201 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700202 // Type: PendingRequestArgs
203 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
204 // Type: ActivityResultInfo
205 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700207 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800208
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700210 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
211 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700212
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700216 private boolean mIsSafeModeEnabled;
217
Adam Cohen4f8071b2016-02-13 16:06:05 -0800218 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700219 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
Winson Chunga2413752012-04-03 14:22:34 -0700221 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700222 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
223 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
224 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700225
Sunny Goyal60820d72017-05-09 12:40:11 -0700226 private final ExtractedColors mExtractedColors = new ExtractedColors();
227
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700232
233 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalffe83f12014-08-14 17:39:34 -0700235 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700238 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239
Sunny Goyal316490e2015-06-02 09:38:28 -0700240 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800241 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700242
Michael Jurka838a4ca2011-02-07 13:33:06 -0800243 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700244 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700245
Sunny Goyal47328fd2016-05-25 18:56:41 -0700246 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700247
248 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700249 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700250 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700251
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700252 // Main container view and the model for the widget tray screen.
253 @Thunk WidgetsContainerView mWidgetsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700254
Jon Miranda6bed3502017-09-19 14:43:17 -0700255 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
256 // that results in widgets being inflated in the wrong orientation.
257 private int mOrientation;
258
Winson Chung4a2afa32012-07-19 14:53:05 -0700259 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
260 // scroll issues (because the workspace may not have been measured yet) and extra work.
261 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
262 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
264 private SpannableStringBuilder mDefaultKeySsb = null;
265
Adam Cohen091440a2015-03-18 14:16:05 -0700266 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800268 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700269 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700271 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
272 private final ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700273 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700274
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800276 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800277 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700278 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700279 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700280 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281
Jon Miranda2d89ea82017-05-04 11:47:53 -0700282 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700283 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700284
Tony Wickham010d2552017-01-20 08:15:28 -0800285 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700286
Winson Chung400438b2011-01-16 17:53:48 -0800287 // Determines how long to wait after a rotation before restoring the screen orientation to
288 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700289 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800290
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700291 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700292
Winson Chung46353de2012-02-16 14:05:10 -0800293 // We only want to get the SharedPreferences once since it does an FS stat each time we get
294 // it from the context.
295 private SharedPreferences mSharedPrefs;
296
Winson Chung13eb5272015-05-11 16:30:13 -0700297 // This is set to the view that launched the activity that navigated the user away from
298 // launcher. Since there is no callback for when the activity has finished launching, enable
299 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800300 private BubbleTextView mWaitingForResume;
301
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700302 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
303 // state transition. If another state transition happened during this delay,
304 // simply unregister this runnable.
305 private Runnable mExitSpringLoadedModeRunnable;
306
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700307 @Thunk final Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700308 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700309 if (mWorkspace != null) {
310 mWorkspace.buildPageHardwareLayers();
311 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700312 }
313 };
314
Sunny Goyal2100c782016-08-22 16:00:03 -0700315 // Activity result which needs to be processed after workspace has loaded.
316 private ActivityResultInfo mPendingActivityResult;
317 /**
318 * Holds extra information required to handle a result from an external call, like
319 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
320 */
321 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800322
Jon Miranda379198e2016-09-23 08:54:40 -0700323 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700324
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700325 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700326 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400327
328 @Thunk void setOrientation() {
329 if (mRotationEnabled) {
330 unlockScreenOrientation(true);
331 } else {
332 setRequestedOrientation(
333 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700334 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400335 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700336
Sunny Goyal745bad92016-05-02 10:54:12 -0700337 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 @Override
340 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700341 if (DEBUG_STRICT_MODE) {
342 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
343 .detectDiskReads()
344 .detectDiskWrites()
345 .detectNetwork() // or .detectAll() for all detectable problems
346 .penaltyLog()
347 .build());
348 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
349 .detectLeakedSqlLiteObjects()
350 .detectLeakedClosableObjects()
351 .penaltyLog()
352 .penaltyDeath()
353 .build());
354 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700355 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700356
Adam Cohen9211d422014-10-07 18:14:53 -0700357 if (mLauncherCallbacks != null) {
358 mLauncherCallbacks.preOnCreate();
359 }
360
Mario Bertschler27288382017-05-24 15:35:09 -0700361 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
362 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700363 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700366 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400367
Sunny Goyal87f784c2017-01-11 10:48:34 -0800368 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700369
Adam Cohen2e6da152015-05-06 11:42:25 -0700370 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800371 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700372 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700373 Display display = getWindowManager().getDefaultDisplay();
374 Point mwSize = new Point();
375 display.getSize(mwSize);
376 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
377 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700378
Jon Miranda6bed3502017-09-19 14:43:17 -0700379 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700380 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700381 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800382 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800383 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800384 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700385 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700386
Joe Onorato41a12d22009-10-31 18:30:00 -0400387 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700388 mAllAppsController = new AllAppsTransitionController(this);
389 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700390
Sunny Goyalffe83f12014-08-14 17:39:34 -0700391 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700392
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700393 mAppWidgetHost = new LauncherAppWidgetHost(this);
394 if (Utilities.ATLEAST_MARSHMALLOW) {
395 mAppWidgetHost.addProviderChangeListener(this);
396 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700397 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700398
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700399 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
400 // this also ensures that any synchronous binding below doesn't re-trigger another
401 // LauncherModel load.
402 mPaused = false;
403
Sunny Goyal60820d72017-05-09 12:40:11 -0700404 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 setupViews();
Winson1f064272016-07-18 17:18:02 -0700407 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700408 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409
Tony Wickham010d2552017-01-20 08:15:28 -0800410 mPopupDataProvider = new PopupDataProvider(this);
411
Tony Wickhame2217252016-03-22 16:34:23 -0700412 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
413 .addAccessibilityStateChangeListener(this);
414
Sunny Goyalfe770c92016-09-27 14:38:58 -0700415 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416
Sunny Goyal2100c782016-08-22 16:00:03 -0700417 // We only load the page synchronously if the user rotates (or triggers a
418 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700419 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
420 if (savedInstanceState != null) {
421 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
422 }
423 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700424 // If we are not binding synchronously, show a fade in animation when
425 // the first page bind completes.
426 mDragLayer.setAlpha(0);
427 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700428 // Pages bound synchronously.
429 mWorkspace.setCurrentPage(currentScreen);
430
Sunny Goyal2100c782016-08-22 16:00:03 -0700431 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 }
433
434 // For handling default keys
435 mDefaultKeySsb = new SpannableStringBuilder();
436 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800437
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800438 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700439 // In case we are on a device with locked rotation, we should look at preferences to check
440 // if the user has specifically allowed rotation.
441 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700442 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700443 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
444 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700445 }
446
Sunny Goyal5a81c382017-03-20 15:08:06 -0700447 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
448 // Temporarily enable the rotation
449 mRotationEnabled = true;
450 }
451
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700452 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
453 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400454 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700455
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800456 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700457
Jon Miranda7fda2852017-07-19 16:07:01 -0700458 // Listen for broadcasts
459 IntentFilter filter = new IntentFilter();
460 filter.addAction(Intent.ACTION_SCREEN_OFF);
461 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
462 registerReceiver(mReceiver, filter);
463 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700464
Sunny Goyal8392c822017-06-20 10:03:56 -0700465 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
466 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700467
468 if (mLauncherCallbacks != null) {
469 mLauncherCallbacks.onCreate(savedInstanceState);
470 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700471
472 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700473 }
474
Sunny Goyal7779d622015-06-11 16:18:39 -0700475 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700476 public void onThemeChanged() {
477 recreate();
478 }
479
Mario Bertschlera6936942017-05-31 14:48:19 -0700480 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700481 if (isDark) {
482 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700483 } else if (supportsDarkText) {
484 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700485 }
486 }
487
488 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700489 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800490 return mLauncherView.findViewById(id);
491 }
492
493 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700494 public void onExtractedColorsChanged() {
495 loadExtractedColorsAndColorItems();
Sunny Goyal60820d72017-05-09 12:40:11 -0700496 mExtractedColors.notifyChange();
Sunny Goyal5e7b2382017-04-25 23:09:06 -0700497 }
498
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700499 @Override
500 public void onAppWidgetHostReset() {
501 if (mAppWidgetHost != null) {
502 mAppWidgetHost.startListening();
503 }
504 }
505
Tony Wickham827cef22016-03-17 15:39:39 -0700506 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700507 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700508 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700509 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700510 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700511 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700512 }
513 }
514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 private LauncherCallbacks mLauncherCallbacks;
516
517 public void onPostCreate(Bundle savedInstanceState) {
518 super.onPostCreate(savedInstanceState);
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onPostCreate(savedInstanceState);
521 }
522 }
523
Winson1f064272016-07-18 17:18:02 -0700524 public void onInsetsChanged(Rect insets) {
525 mDeviceProfile.updateInsets(insets);
526 mDeviceProfile.layout(this, true /* notifyListeners */);
527 }
528
Sunny Goyal32554d12015-12-03 15:31:25 -0800529 /**
530 * Call this after onCreate to set or clear overlay.
531 */
532 public void setLauncherOverlay(LauncherOverlay overlay) {
533 if (overlay != null) {
534 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
535 }
536 mWorkspace.setLauncherOverlay(overlay);
537 }
538
Adam Cohen9211d422014-10-07 18:14:53 -0700539 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
540 mLauncherCallbacks = callbacks;
541 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700542 }
543
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700544 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700545 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700546 if (mLauncherCallbacks != null) {
547 mLauncherCallbacks.onLauncherProviderChange();
548 }
549 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700550
Hyunyoung Song3f471442015-04-08 19:01:34 -0700551 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700552 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
553 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700554 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700555 }
556
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000557 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700558 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
559 // This cast is safe as long as the id < 0x00FFFFFF
560 // Since we jail all the dynamically generated views, there should be no clashes
561 // with any other views.
562 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000563 }
564
Tony Wickham010d2552017-01-20 08:15:28 -0800565 public PopupDataProvider getPopupDataProvider() {
566 return mPopupDataProvider;
567 }
568
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000569 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700570 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
571 * a configuration step, this allows the proper animations to run after other transitions.
572 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700573 private long completeAdd(
574 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
575 long screenId = info.screenId;
576 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700577 // When the screen id represents an actual screen (as opposed to a rank) we make sure
578 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700580 }
Adam Cohendb364c32014-05-20 14:23:40 -0700581
Sunny Goyal2100c782016-08-22 16:00:03 -0700582 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800583 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700584 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700585 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800586 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700587 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700588 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700589 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700590 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700591 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700592 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700593 int widgetId = appWidgetId;
594 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700595 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700596 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700597 // Since the view was just bound, also launch the configure activity if needed
598 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
599 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800600 if (provider != null) {
601 new WidgetAddFlowHandler(provider)
602 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700603 }
604 }
605 break;
606 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700608
Adam Cohendb364c32014-05-20 14:23:40 -0700609 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 }
611
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800612 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700613 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700614 if (isWorkspaceLoading()) {
615 // process the result once the workspace has loaded.
616 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
617 return;
618 }
619 mPendingActivityResult = null;
620
Winson Chunge341d302013-08-16 14:31:00 -0700621 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700622 final PendingRequestArgs requestArgs = mPendingRequestArgs;
623 setWaitingForResult(null);
624 if (requestArgs == null) {
625 return;
626 }
627
628 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700629
Adam Cohenad4e15c2013-10-17 16:21:35 -0700630 Runnable exitSpringLoaded = new Runnable() {
631 @Override
632 public void run() {
633 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
634 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
635 }
636 };
637
Michael Jurka8b805b12012-04-18 14:23:14 -0700638 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700639 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700640 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700641 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
642 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700644 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700645 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700647 addAppWidgetImpl(
648 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800649 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700651 }
652 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200653 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
654 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700655 // User could have free-scrolled between pages before picking a wallpaper; make sure
656 // we move to the closest one now.
657 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700658 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200659 }
660 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700661 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200662
Adam Cohened66b2b2012-01-23 17:28:51 -0800663 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700664 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700665
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 // We have special handling for widgets
667 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800668 final int appWidgetId;
669 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
670 : -1;
671 if (widgetId < 0) {
672 appWidgetId = pendingAddWidgetId;
673 } else {
674 appWidgetId = widgetId;
675 }
676
Adam Cohenad4e15c2013-10-17 16:21:35 -0700677 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800678 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700679 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
680 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700681 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700683 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700684 @Override
685 public void run() {
686 exitSpringLoadedDragModeDelayed(false, 0, null);
687 }
688 };
Adam Cohendb364c32014-05-20 14:23:40 -0700689
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
691 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
692 } else {
693 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
694 // When the screen id represents an actual screen (as opposed to a rank)
695 // we make sure that the drop page actually exists.
696 requestArgs.screenId =
697 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700698 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700699 final CellLayout dropLayout =
700 mWorkspace.getScreenWithId(requestArgs.screenId);
701
702 dropLayout.setDropPending(true);
703 final Runnable onComplete = new Runnable() {
704 @Override
705 public void run() {
706 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
707 dropLayout.setDropPending(false);
708 }
709 };
710 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
711 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700712 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800713 return;
714 }
715
Sunny Goyald478c832016-04-01 12:04:16 -0700716 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
717 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700718 if (resultCode == RESULT_OK) {
719 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700720 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700721 }
722 // Leave the widget in the pending state if the user canceled the configure.
723 return;
724 }
725
Sunny Goyalaad90582015-07-09 14:22:50 -0700726 if (requestCode == REQUEST_CREATE_SHORTCUT) {
727 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700728 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
729 completeAdd(requestCode, data, -1, requestArgs);
730 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
731 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
732
Sunny Goyalaad90582015-07-09 14:22:50 -0700733 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700734 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
735 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800739 }
740
741 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700742 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800743 final int requestCode, final int resultCode, final Intent data) {
744 handleActivityResult(requestCode, resultCode, data);
745 if (mLauncherCallbacks != null) {
746 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
747 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 }
749
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700750 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700751 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600752 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700753 PendingRequestArgs pendingArgs = mPendingRequestArgs;
754 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
755 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
756 setWaitingForResult(null);
757
Sunny Goyal28c6b962015-10-12 11:42:05 -0700758 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700759 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700760 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700761 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700762 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700763 Intent intent = pendingArgs.getPendingIntent();
764
Sunny Goyal28c6b962015-10-12 11:42:05 -0700765 if (grantResults.length > 0
766 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700767 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700768 } else {
769 // TODO: Show a snack bar with link to settings
770 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700771 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700772 }
773 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600774 if (mLauncherCallbacks != null) {
775 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
776 grantResults);
777 }
778 }
779
Adam Cohendb364c32014-05-20 14:23:40 -0700780 /**
781 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
782 *
783 * @param screenId the screen id to check
784 * @return the new screen, or screenId if it exists
785 */
786 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800787 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700788 if (dropLayout == null) {
789 // it's possible that the add screen was removed because it was
790 // empty and a re-bind occurred
791 mWorkspace.addExtraEmptyScreen();
792 return mWorkspace.commitExtraEmptyScreen();
793 } else {
794 return screenId;
795 }
796 }
797
Sunny Goyal2100c782016-08-22 16:00:03 -0700798 @Thunk void completeTwoStageWidgetDrop(
799 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
800 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800801 Runnable onCompleteRunnable = null;
802 int animationType = 0;
803
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700804 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 if (resultCode == RESULT_OK) {
806 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
807 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800808 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700809 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 onCompleteRunnable = new Runnable() {
811 @Override
812 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700813 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700814 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
815 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800816 }
817 };
818 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800819 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800821 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700822 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700823 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700824 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
825 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700826 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700827 // The animated view may be null in the case of a rotation during widget configuration
828 onCompleteRunnable.run();
829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831
832 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700833 protected void onStop() {
834 super.onStop();
835 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700836
837 if (mLauncherCallbacks != null) {
838 mLauncherCallbacks.onStop();
839 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700840
Sunny Goyalf5e37442016-11-02 10:31:24 -0700841 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700842 mAppWidgetHost.stopListening();
843 }
Tony Wickham010d2552017-01-20 08:15:28 -0800844
845 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700846 }
847
848 @Override
849 protected void onStart() {
850 super.onStart();
851 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700852
853 if (mLauncherCallbacks != null) {
854 mLauncherCallbacks.onStart();
855 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700856
Sunny Goyalf5e37442016-11-02 10:31:24 -0700857 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700858 mAppWidgetHost.startListening();
859 }
Tony Wickham010d2552017-01-20 08:15:28 -0800860
861 if (!isWorkspaceLoading()) {
862 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
863 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700864
Jon Miranda7fda2852017-07-19 16:07:01 -0700865 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700866 if (mScrimAnimator != null) {
867 mScrimAnimator.cancel();
868 }
869 mDragLayer.getBackground().setAlpha(0);
870 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
871 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
872 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
873 @Override
874 public void onAnimationEnd(Animator animation) {
875 mScrimAnimator = null;
876 }
877 });
878 mScrimAnimator.setDuration(600);
879 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
880 mScrimAnimator.start();
881 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700882 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700883 }
884
885 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700887 TraceHelper.beginSection("ON_RESUME");
Adam Cohen9211d422014-10-07 18:14:53 -0700888 if (mLauncherCallbacks != null) {
889 mLauncherCallbacks.preOnResume();
890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700892 TraceHelper.partitionSection("ON_RESUME", "superCall");
893
Hyunyoung Songaa953652016-04-19 18:30:24 -0700894 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700895
Winson Chung4a2afa32012-07-19 14:53:05 -0700896 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700897 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700898 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800899 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700900 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700901 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700902 // view after launching an app, as they may be depending on the UI to be static to
903 // switch to another app, otherwise, if it was
Sunny Goyal5606e072017-06-15 14:24:21 -0700904 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800905 } else if (mOnResumeState == State.WIDGETS) {
906 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700907 }
Jon Miranda7653a602017-07-28 10:09:34 -0700908 if (mOnResumeState != State.APPS) {
909 tryAndUpdatePredictedApps();
910 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700911 mOnResumeState = State.NONE;
912
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800913 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700914 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700915 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700916 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700917 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700919 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200920 // We might have postponed some bind calls until onResume (see waitUntilResume) --
921 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700922 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
923 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200924 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700925 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200926 }
Michael Jurka54554252013-08-01 12:52:23 +0200927 if (mOnResumeCallbacks.size() > 0) {
928 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
929 mOnResumeCallbacks.get(i).run();
930 }
931 mOnResumeCallbacks.clear();
932 }
Winson Chunge4e50662012-01-23 14:45:13 -0800933
934 // Reset the pressed state of icons that were locked in the press state while activities
935 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800936 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800937 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800938 mWaitingForResume.setStayPressed(false);
939 }
Winson Chung82963d52013-10-09 11:20:57 -0700940
Adam Cohen06dff352012-06-01 17:17:08 -0700941 // It is possible that widgets can receive updates while launcher is not in the foreground.
942 // Consequently, the widgets will be inflated in the orientation of the foreground activity
943 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
944 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -0700945 if (!isWorkspaceLoading()) {
946 getWorkspace().reinflateWidgetsIfNecessary();
947 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700948
Winson Chungcd99cd32015-04-29 11:03:24 -0700949 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -0700950 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -0700951
Sunny Goyala474a9b2017-05-04 16:47:11 -0700952 // Process any items that were added while Launcher was away.
953 InstallShortcutReceiver.disableAndFlushInstallQueue(
954 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700955
Sunny Goyala474a9b2017-05-04 16:47:11 -0700956 // Refresh shortcuts if the permission changed.
957 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700958
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700959 if (shouldShowDiscoveryBounce()) {
960 mAllAppsController.showDiscoveryBounce();
961 }
Adam Cohen9211d422014-10-07 18:14:53 -0700962 if (mLauncherCallbacks != null) {
963 mLauncherCallbacks.onResume();
964 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800965
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700966 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700967 }
968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700970 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700971 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700972 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700973
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700974 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700975 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800976 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700977 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700978
Adam Cohen9211d422014-10-07 18:14:53 -0700979 if (mLauncherCallbacks != null) {
980 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700981 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700982 }
983
Adam Cohenc2d6e892014-10-16 09:49:24 -0700984 public interface LauncherOverlay {
985
986 /**
987 * Touch interaction leading to overscroll has begun
988 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700989 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700990
991 /**
992 * Touch interaction related to overscroll has ended
993 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700994 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700995
996 /**
997 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
998 * screen (or in the case of RTL, the rightmost screen).
999 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001000 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001001
1002 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001003 * Called when the launcher is ready to use the overlay
1004 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001005 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001006 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001007 }
1008
1009 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001010 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001011 }
1012
1013 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1014
Sunny Goyalc86df472016-02-25 09:19:38 -08001015 public void onScrollChanged(float progress) {
1016 if (mWorkspace != null) {
1017 mWorkspace.onOverlayScrollChanged(progress);
1018 }
1019 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001020 }
1021
Jorim Jaggid017f882014-01-14 17:08:48 -08001022 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001023 if (mLauncherCallbacks != null) {
1024 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001025 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -08001026 // On O and above we there is always some setting present settings (add icon to
1027 // home screen or icon badging). On earlier APIs we will have the allow rotation
1028 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001029 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001030 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001031 }
1032
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001033 @Override
1034 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001036 if (mModel.isCurrentCallbacks(this)) {
1037 mModel.stopLoader();
1038 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001039 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1040
Romain Guy13c2e7b2010-03-10 19:45:00 -08001041 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001042 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001043
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001044 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001045 @Override
1046 public void onWindowFocusChanged(boolean hasFocus) {
1047 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001048 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001049
1050 if (mLauncherCallbacks != null) {
1051 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1052 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001053 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 private boolean acceptFilter() {
1056 final InputMethodManager inputManager = (InputMethodManager)
1057 getSystemService(Context.INPUT_METHOD_SERVICE);
1058 return !inputManager.isFullscreenMode();
1059 }
1060
1061 @Override
1062 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001063 final int uniChar = event.getUnicodeChar();
1064 final boolean handled = super.onKeyDown(keyCode, event);
1065 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1066 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1068 keyCode, event);
1069 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001071 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001072 // showSearchDialog()
1073 // If there are multiple keystrokes before the search dialog takes focus,
1074 // onSearchRequested() will be called for every keystroke,
1075 // but it is idempotent, so it's fine.
1076 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 }
1078 }
1079
Joe Onorato8a9625e2010-01-28 15:55:35 -08001080 // Eat the long press event so the keyboard doesn't come up.
1081 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1082 return true;
1083 }
1084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 return handled;
1086 }
1087
Winson46163472015-09-22 17:31:56 -07001088 @Override
1089 public boolean onKeyUp(int keyCode, KeyEvent event) {
1090 if (keyCode == KeyEvent.KEYCODE_MENU) {
1091 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001092 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001093 // Close any open floating view
1094 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001095
Winson46163472015-09-22 17:31:56 -07001096 // Stop resizing any widgets
1097 mWorkspace.exitWidgetResizeMode();
1098
1099 // Show the overview mode if we are on the workspace
1100 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1101 !mWorkspace.isSwitchingState()) {
1102 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001103 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001104 }
1105 }
1106 return true;
1107 }
1108 return super.onKeyUp(keyCode, event);
1109 }
1110
Karl Rosaen138a0412009-04-23 19:00:21 -07001111 private String getTypedText() {
1112 return mDefaultKeySsb.toString();
1113 }
1114
Sunny Goyal6f28e712016-09-13 14:19:29 -07001115 @Override
1116 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001117 mDefaultKeySsb.clear();
1118 mDefaultKeySsb.clearSpans();
1119 Selection.setSelection(mDefaultKeySsb, 0);
1120 }
1121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 /**
1123 * Restores the previous state, if it exists.
1124 *
1125 * @param savedState The previous state.
1126 */
1127 private void restoreState(Bundle savedState) {
1128 if (savedState == null) {
1129 return;
1130 }
1131
Sunny Goyalfe770c92016-09-27 14:38:58 -07001132 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1133 State[] stateValues = State.values();
1134 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1135 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001136 if (state == State.APPS || state == State.WIDGETS) {
1137 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001138 }
1139
Sunny Goyal2100c782016-08-22 16:00:03 -07001140 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1141 if (requestArgs != null) {
1142 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001144
1145 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 }
1147
1148 /**
1149 * Finds all the views we need and configure them properly.
1150 */
1151 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001152 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001153 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001154 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001155 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001156
Sunny Goyal784f9c32016-03-23 16:56:54 -07001157 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1158 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1159 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
Winson Chung4c98d922011-05-31 16:50:48 -07001161 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001162 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001163
Winson Chung3d503fb2011-07-13 17:25:49 -07001164 // Setup the hotseat
1165 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1166 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001167 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001168 }
1169
Winsone9f27272015-10-13 10:47:51 -07001170 // Setup the overview panel
1171 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001172
Winson Chung4c98d922011-05-31 16:50:48 -07001173 // Setup the workspace
1174 mWorkspace.setHapticFeedbackEnabled(false);
1175 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001176 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001177 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1178 // default state, otherwise we will update to the wrong offsets in RTL
1179 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001180 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001181 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001182
Tony Wickham34d2c912015-09-11 09:27:58 -07001183 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001184 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001185
Winson Chungef7f8742015-06-04 17:18:17 -07001186 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001187 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001188 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001189
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001191 mDragController.setMoveTarget(mWorkspace);
1192 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001193 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001194
Hyunyoung Songd725f642017-08-17 22:26:35 -07001195 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001196
Sunny Goyal322d5562015-06-25 19:35:49 -07001197 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1198 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001199 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
1201
Winsone9f27272015-10-13 10:47:51 -07001202 private void setupOverviewPanel() {
1203 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1204
Winsone9f27272015-10-13 10:47:51 -07001205 // Bind wallpaper button actions
1206 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301207 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001208 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001209 public void handleViewClick(View view) {
1210 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001211 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001212 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001213
1214 // Bind widget button actions
1215 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301216 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001217 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001218 public void handleViewClick(View view) {
1219 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001220 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001221 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001222
1223 // Bind settings actions
1224 View settingsButton = findViewById(R.id.settings_button);
1225 boolean hasSettings = hasSettings();
1226 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301227 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001228 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001229 public void handleViewClick(View view) {
1230 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001231 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001232 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001233 } else {
1234 settingsButton.setVisibility(View.GONE);
1235 }
1236
1237 mOverviewPanel.setAlpha(0f);
1238 }
1239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001241 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001242 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001243 */
1244 public void setAllAppsButton(View allAppsButton) {
1245 mAllAppsButton = allAppsButton;
1246 }
1247
Sunny Goyalbb011da2016-06-15 15:42:29 -07001248 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001249 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001250 }
1251
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001252 public View getWidgetsButton() {
1253 return mWidgetsButton;
1254 }
1255
Anjali Koppal5ad44842014-03-10 20:34:39 -07001256 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 * Creates a view representing a shortcut.
1258 *
1259 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001261 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001262 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
1264
1265 /**
1266 * Creates a view representing a shortcut inflated from the specified resource.
1267 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 * @param parent The group the shortcut belongs to.
1269 * @param info The data structure describing the shortcut.
1270 *
1271 * @return A View inflated from layoutResId.
1272 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001273 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001274 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1275 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001276 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001278 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 return favorite;
1280 }
1281
1282 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001283 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 *
1285 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001287 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001288 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001289 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1290 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001291 return;
1292 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001293
Jon Mirandac476d6e2017-05-04 16:30:01 -07001294 int[] cellXY = mTmpAddItemCellCoordinates;
1295 CellLayout layout = getCellLayout(container, screenId);
1296
Sunny Goyal782f0c92017-01-19 10:27:54 -08001297 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001298 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001299 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1300 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001301 }
1302
1303 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001304 // Legacy shortcuts are only supported for primary profile.
1305 info = Process.myUserHandle().equals(args.user)
1306 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001307
Sunny Goyalb57645f2017-03-20 13:57:28 -07001308 if (info == null) {
1309 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1310 return;
1311 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001312 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001313 // The app is trying to add a shortcut without sufficient permissions
1314 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1315 return;
1316 }
1317 }
1318
Jon Mirandac476d6e2017-05-04 16:30:01 -07001319 if (container < 0) {
1320 // Adding a shortcut to the Workspace.
1321 final View view = createShortcut(info);
1322 boolean foundCellSpan = false;
1323 // First we check if we already know the exact location where we want to add this item.
1324 if (cellX >= 0 && cellY >= 0) {
1325 cellXY[0] = cellX;
1326 cellXY[1] = cellY;
1327 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001328
Jon Mirandac476d6e2017-05-04 16:30:01 -07001329 // If appropriate, either create a folder or add to an existing folder
1330 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1331 true, null, null)) {
1332 return;
1333 }
1334 DragObject dragObject = new DragObject();
1335 dragObject.dragInfo = info;
1336 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1337 true)) {
1338 return;
1339 }
1340 } else {
1341 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1342 }
1343
1344 if (!foundCellSpan) {
1345 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001346 return;
1347 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001348
1349 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1350 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001351 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001352 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001353 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001354 if (folderIcon != null) {
1355 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1356 folderInfo.add(info, args.rank, false);
1357 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001358 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001359 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362
Sunny Goyale29897f52017-07-20 10:09:42 -07001363 public FolderIcon findFolderIcon(final long folderIconId) {
1364 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1365 @Override
1366 public boolean evaluate(ItemInfo info, View view) {
1367 return info != null && info.id == folderIconId;
1368 }
1369 });
1370 }
1371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001373 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001375 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001377 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001378 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1379
Adam Cohened66b2b2012-01-23 17:28:51 -08001380 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001381 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001382 }
Romain Guycbb89e42009-06-08 15:52:54 -07001383
Adam Cohen59400422014-03-05 18:07:04 -08001384 LauncherAppWidgetInfo launcherInfo;
1385 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001386 launcherInfo.spanX = itemInfo.spanX;
1387 launcherInfo.spanY = itemInfo.spanY;
1388 launcherInfo.minSpanX = itemInfo.minSpanX;
1389 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001390 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001391
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001392 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001393 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394
Sunny Goyal2100c782016-08-22 16:00:03 -07001395 if (hostView == null) {
1396 // Perform actual inflation because we're live
1397 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001399 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301400 prepareAppWidget(hostView, launcherInfo);
1401 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Sunny Goyald5462aa2016-11-22 16:52:39 +05301404 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001405 hostView.setTag(item);
1406 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001407 hostView.setFocusable(true);
1408 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001409 }
1410
Adam Cohended9f8d2010-11-03 13:25:16 -07001411 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1412 @Override
1413 public void onReceive(Context context, Intent intent) {
1414 final String action = intent.getAction();
1415 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001416 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001417
Winson Chungc100e8e2011-08-09 16:02:43 -07001418 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001419 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001420 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001421 if (!showWorkspace(false)) {
1422 // If we are already on the workspace, then manually reset all apps
1423 mAppsView.reset();
1424 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001425 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001426 mShouldFadeInScrim = true;
1427 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1428 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1429 // the user unlocked and the Launcher is not in the foreground.
1430 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001431 }
1432 }
1433 };
1434
Tony Wickham010d2552017-01-20 08:15:28 -08001435 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1436 Runnable r = new Runnable() {
1437 @Override
1438 public void run() {
1439 mWorkspace.updateIconBadges(updatedBadges);
1440 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001441
1442 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1443 if (popup != null) {
1444 popup.updateNotificationHeader(updatedBadges);
1445 }
Tony Wickham010d2552017-01-20 08:15:28 -08001446 }
1447 };
1448 if (!waitUntilResume(r)) {
1449 r.run();
1450 }
1451 }
1452
Adam Cohended9f8d2010-11-03 13:25:16 -07001453 @Override
1454 public void onAttachedToWindow() {
1455 super.onAttachedToWindow();
1456
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001457 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001458 if (mLauncherCallbacks != null) {
1459 mLauncherCallbacks.onAttachedToWindow();
1460 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001461 }
1462
1463 @Override
1464 public void onDetachedFromWindow() {
1465 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001466
1467 if (mLauncherCallbacks != null) {
1468 mLauncherCallbacks.onDetachedFromWindow();
1469 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001470 }
1471
1472 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001473 // The following code used to be in onResume, but it turns out onResume is called when
1474 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1475 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001476 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001477 if (!mWorkspaceLoading) {
1478 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001479 // We want to let Launcher draw itself at least once before we force it to build
1480 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001481 // apps is nice and speedy.
1482 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001483 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001484 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001485 if (mStarted) return;
1486 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001487 // We delay the layer building a bit in order to give
1488 // other message processing a time to run. In particular
1489 // this avoids a delay in hiding the IME if it was
1490 // currently shown, because doing that may involve
1491 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001492 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001493 final ViewTreeObserver.OnDrawListener listener = this;
1494 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001495 public void run() {
1496 if (mWorkspace != null &&
1497 mWorkspace.getViewTreeObserver() != null) {
1498 mWorkspace.getViewTreeObserver().
1499 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001500 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001501 }
1502 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001503 }
1504 });
1505 }
1506 clearTypedText();
1507 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001508 }
1509
Winson Chunga6945242014-01-08 14:04:34 -08001510 public DragLayer getDragLayer() {
1511 return mDragLayer;
1512 }
1513
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001514 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001515 return mAppsView;
1516 }
1517
Hyunyoung Song3f471442015-04-08 19:01:34 -07001518 public WidgetsContainerView getWidgetsView() {
1519 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001520 }
1521
Winson Chunga6945242014-01-08 14:04:34 -08001522 public Workspace getWorkspace() {
1523 return mWorkspace;
1524 }
1525
1526 public Hotseat getHotseat() {
1527 return mHotseat;
1528 }
1529
Jorim Jaggid017f882014-01-14 17:08:48 -08001530 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001531 return mOverviewPanel;
1532 }
1533
Sunny Goyal47328fd2016-05-25 18:56:41 -07001534 public DropTargetBar getDropTargetBar() {
1535 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001536 }
1537
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001538 public LauncherAppWidgetHost getAppWidgetHost() {
1539 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Winson Chunga9abd0e2010-10-27 17:18:37 -07001542 public LauncherModel getModel() {
1543 return mModel;
1544 }
1545
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001546 public ModelWriter getModelWriter() {
1547 return mModelWriter;
1548 }
1549
Adam Cohen79d90c52016-04-22 13:29:20 -07001550 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001551 return mSharedPrefs;
1552 }
1553
Jon Miranda6bed3502017-09-19 14:43:17 -07001554 public int getOrientation() { return mOrientation; }
1555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 @Override
1557 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001558 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 super.onNewIntent(intent);
1560
Winson15f8b172015-08-19 11:02:39 -07001561 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1562 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1563 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001564
1565 // Check this condition before handling isActionMain, as this will get reset.
1566 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001567 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001568
Winson15f8b172015-08-19 11:02:39 -07001569 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1570 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001571 if (mWorkspace == null) {
1572 // Can be cases where mWorkspace is null, this prevents a NPE
1573 return;
1574 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001575
1576 // Note: There should be at most one log per method call. This is enforced implicitly
1577 // by using if-else statements.
1578 UserEventDispatcher ued = getUserEventDispatcher();
1579
1580 // TODO: Log this case.
Adam Cohen6fecd412013-10-02 17:41:50 -07001581 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001582
Jon Mirandafeba90f2016-10-06 10:53:29 -07001583 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham540913e2017-01-23 11:47:51 -08001584 if (topOpenView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301585 ued.logActionCommand(Action.Command.HOME_INTENT,
1586 topOpenView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001587 } else if (topOpenView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301588 ued.logActionCommand(Action.Command.HOME_INTENT,
1589 ((Folder) topOpenView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001590 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301591 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001592 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1593 }
1594
1595 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001596 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001597 exitSpringLoadedDragMode();
1598
1599 // If we are already on home, then just animate back to the workspace,
1600 // otherwise, just wait until onResume to set the state back to Workspace
1601 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001602 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001603 } else {
1604 mOnResumeState = State.WORKSPACE;
1605 }
1606
1607 final View v = getWindow().peekDecorView();
1608 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001609 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001610 INPUT_METHOD_SERVICE);
1611 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1612 }
1613
Winson Chungb745afb2015-03-02 11:51:23 -08001614 // Reset the apps view
1615 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001616 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001617 }
1618
Hyunyoung Song3f471442015-04-08 19:01:34 -07001619 // Reset the widgets view
1620 if (!alreadyOnHome && mWidgetsView != null) {
1621 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001622 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001623
Adam Cohen9211d422014-10-07 18:14:53 -07001624 if (mLauncherCallbacks != null) {
1625 mLauncherCallbacks.onHomeIntent();
1626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001628 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001629
Adam Cohen9211d422014-10-07 18:14:53 -07001630 if (mLauncherCallbacks != null) {
1631 mLauncherCallbacks.onNewIntent(intent);
1632 }
Winson15f8b172015-08-19 11:02:39 -07001633
1634 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1635 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1636 // animation.
1637 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001638 boolean callbackAllowsMoveToDefaultScreen =
1639 mLauncherCallbacks == null || mLauncherCallbacks
1640 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001641 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1642 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001643
Winson15f8b172015-08-19 11:02:39 -07001644 mWorkspace.post(new Runnable() {
1645 @Override
1646 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001647 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001648 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001649 }
Winson15f8b172015-08-19 11:02:39 -07001650 }
1651 });
1652 }
1653 }
1654
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001655 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001656 }
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001659 public void onRestoreInstanceState(Bundle state) {
1660 super.onRestoreInstanceState(state);
1661 for (int page: mSynchronouslyBoundPages) {
1662 mWorkspace.restoreInstanceStateForChild(page);
1663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
1665
1666 @Override
1667 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001668 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001669 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001670
Adam Cohen21cd0022013-10-09 18:57:02 -07001671 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001672 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673
Michael Jurka883f55b2010-10-21 15:47:14 -07001674 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001675 // We close any open folders and shortcut containers since they will not be re-opened,
1676 // and we need to make sure this state is reflected.
1677 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001678
Sunny Goyal2100c782016-08-22 16:00:03 -07001679 if (mPendingRequestArgs != null) {
1680 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1681 }
1682 if (mPendingActivityResult != null) {
1683 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
1685
Adam Cohen9211d422014-10-07 18:14:53 -07001686 if (mLauncherCallbacks != null) {
1687 mLauncherCallbacks.onSaveInstanceState(outState);
1688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 }
1690
1691 @Override
1692 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001694
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001695 unregisterReceiver(mReceiver);
Michael Jurka9d906c72011-10-14 06:25:36 -07001696 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001697 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001698
Winson Chungcd2b0142011-06-08 16:02:26 -07001699 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001700 // It's possible to receive onDestroy after a new Launcher activity has
1701 // been created. In this case, don't interfere with the new Launcher.
1702 if (mModel.isCurrentCallbacks(this)) {
1703 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001704 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001705 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001706
Sunny Goyal745bad92016-05-02 10:54:12 -07001707 if (mRotationPrefChangeHandler != null) {
1708 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1709 }
1710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001712 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001714 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001716 mAppWidgetHost = null;
1717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 TextKeyListener.getInstance().release();
1719
Tony Wickhame2217252016-03-22 16:34:23 -07001720 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1721 .removeAccessibilityStateChangeListener(this);
1722
Mario Bertschler27288382017-05-24 15:35:09 -07001723 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1724
Michael Jurka2ecf9952012-06-18 12:52:28 -07001725 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001726
Tony2917a8b2017-07-31 23:29:42 -07001727 clearPendingBinds();
1728
Adam Cohen9211d422014-10-07 18:14:53 -07001729 if (mLauncherCallbacks != null) {
1730 mLauncherCallbacks.onDestroy();
1731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 }
1733
Sunny Goyalae502842016-06-17 08:43:56 -07001734 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1735 return mAccessibilityDelegate;
1736 }
1737
Adam Cohena9cf38f2011-05-02 15:36:58 -07001738 public DragController getDragController() {
1739 return mDragController;
1740 }
1741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001743 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001744 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001745 }
1746
1747 @Override
1748 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1749 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001750 try {
1751 super.startIntentSenderForResult(intent, requestCode,
1752 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1753 } catch (IntentSender.SendIntentException e) {
1754 throw new ActivityNotFoundException();
1755 }
1756 }
1757
Winson Chung70d72102011-08-12 11:24:35 -07001758 /**
1759 * Indicates that we want global search for this activity by setting the globalSearch
1760 * argument for {@link #startSearch} to true.
1761 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001763 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001765
Karl Rosaen138a0412009-04-23 19:00:21 -07001766 if (initialQuery == null) {
1767 // Use any text typed in the launcher as the initial query
1768 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 if (appSearchData == null) {
1771 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001772 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001774
Sunny Goyal6f28e712016-09-13 14:19:29 -07001775 if (mLauncherCallbacks == null ||
1776 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1777 // Starting search from the callbacks failed. Start the default global search.
1778 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001779 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001780
1781 // We need to show the workspace after starting the search
1782 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001783 }
1784
Ian Parkinson047036e2014-09-01 15:40:53 +01001785 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001786 * Starts the global search activity. This code is a copied from SearchManager
1787 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001788 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001789 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001790 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001791 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1792 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1793 if (globalSearchActivity == null) {
1794 Log.w(TAG, "No global search activity found.");
1795 return;
1796 }
1797 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1798 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1799 intent.setComponent(globalSearchActivity);
1800 // Make sure that we have a Bundle to put source in
1801 if (appSearchData == null) {
1802 appSearchData = new Bundle();
1803 } else {
1804 appSearchData = new Bundle(appSearchData);
1805 }
Adam Cohen9211d422014-10-07 18:14:53 -07001806 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001807 if (!appSearchData.containsKey("source")) {
1808 appSearchData.putString("source", getPackageName());
1809 }
1810 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1811 if (!TextUtils.isEmpty(initialQuery)) {
1812 intent.putExtra(SearchManager.QUERY, initialQuery);
1813 }
1814 if (selectInitialQuery) {
1815 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1816 }
1817 intent.setSourceBounds(sourceBounds);
1818 try {
1819 startActivity(intent);
1820 } catch (ActivityNotFoundException ex) {
1821 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
1824
Winson Chung70d72102011-08-12 11:24:35 -07001825 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001826 public boolean onPrepareOptionsMenu(Menu menu) {
1827 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001828 if (mLauncherCallbacks != null) {
1829 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1830 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001831 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001834 @Override
1835 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001836 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001837 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001838 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001839 }
1840
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001842 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 }
1844
Adam Cohen517a7f52014-03-01 12:12:59 -08001845 public boolean isWorkspaceLoading() {
1846 return mWorkspaceLoading;
1847 }
1848
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001849 private void setWorkspaceLoading(boolean value) {
1850 boolean isLocked = isWorkspaceLocked();
1851 mWorkspaceLoading = value;
1852 if (isLocked != isWorkspaceLocked()) {
1853 onWorkspaceLockedChanged();
1854 }
1855 }
1856
Sunny Goyal04a324a2017-01-20 21:08:59 -08001857 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001858 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001859 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001860 if (isLocked != isWorkspaceLocked()) {
1861 onWorkspaceLockedChanged();
1862 }
1863 }
1864
Adam Cohen9211d422014-10-07 18:14:53 -07001865 protected void onWorkspaceLockedChanged() {
1866 if (mLauncherCallbacks != null) {
1867 mLauncherCallbacks.onWorkspaceLockedChanged();
1868 }
1869 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001870
Sunny Goyal2100c782016-08-22 16:00:03 -07001871 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001872 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001873 if (LOGD) {
1874 Log.d(TAG, "Adding widget from drop");
1875 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001876 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001877 }
1878
Sunny Goyal2100c782016-08-22 16:00:03 -07001879 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001880 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1881 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1882 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001883
Adam Cohenad4e15c2013-10-17 16:21:35 -07001884 Runnable onComplete = new Runnable() {
1885 @Override
1886 public void run() {
1887 // Exit spring loaded mode if necessary after adding the widget
1888 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1889 null);
1890 }
1891 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001892 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001893 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 }
1895 }
Romain Guycbb89e42009-06-08 15:52:54 -07001896
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001897 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1898 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001899 info.container = container;
1900 info.screenId = screenId;
1901 if (cell != null) {
1902 info.cellX = cell[0];
1903 info.cellY = cell[1];
1904 }
1905 info.spanX = spanX;
1906 info.spanY = spanY;
1907
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001908 switch (info.itemType) {
1909 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1910 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001911 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001912 break;
1913 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001914 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001915 break;
1916 default:
1917 throw new IllegalStateException("Unknown item type: " + info.itemType);
1918 }
1919 }
1920
Adam Cohenfbba09b2011-07-18 21:43:05 -07001921 /**
1922 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001923 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001924 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001925 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1926 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001927 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1928 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1929 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001930 }
1931
Adam Cohenfbba09b2011-07-18 21:43:05 -07001932 /**
1933 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001934 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001935 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001936 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001937 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001938 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001939 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001940 // In the case where we've prebound the widget, we remove it from the DragLayer
1941 if (LOGD) {
1942 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1943 }
1944 getDragLayer().removeView(hostView);
1945
Adam Cohened66b2b2012-01-23 17:28:51 -08001946 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001947 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001948
1949 // Clear the boundWidget so that it doesn't get destroyed.
1950 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001951 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001952 // In this case, we either need to start an activity to get permission to bind
1953 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001954 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1955 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1956 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1957 this, info.componentName);
1958 } else {
1959 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1960 }
Adam Cohendd70d662012-10-04 16:53:44 -07001961 Bundle options = info.bindOptions;
1962
Sunny Goyalffe83f12014-08-14 17:39:34 -07001963 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1964 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001965 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001966 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001967 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001968 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001969 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001970 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001971 }
1972
Adam Cohendcd297f2013-06-18 13:13:40 -07001973 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001974 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001975 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 folderInfo.title = getText(R.string.folder_name);
1977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001979 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980
1981 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001982 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301983 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001984 // Force measure the new folder icon
1985 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1986 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001987 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
Romain Guycbb89e42009-06-08 15:52:54 -07001989
Winsonc0b52fe2015-09-09 16:38:15 -07001990 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001991 * Unbinds the view for the specified item, and removes the item and all its children.
1992 *
1993 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001994 * @param itemInfo the {@link ItemInfo} for this view.
1995 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001996 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001997 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001998 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001999 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002000 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2001 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002002 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002003 } else {
2004 mWorkspace.removeWorkspaceItem(v);
2005 }
Winsonc0b52fe2015-09-09 16:38:15 -07002006 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002007 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002008 }
2009 } else if (itemInfo instanceof FolderInfo) {
2010 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002011 if (v instanceof FolderIcon) {
2012 ((FolderIcon) v).removeListeners();
2013 }
Winsonc0b52fe2015-09-09 16:38:15 -07002014 mWorkspace.removeWorkspaceItem(v);
2015 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002016 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002017 }
2018 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2019 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002020 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002021 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002022 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002023 }
2024 } else {
2025 return false;
2026 }
2027 return true;
2028 }
2029
2030 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002031 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002032 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002033 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002034 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002035 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002036 // Deleting an app widget ID is a void call but writes to disk before returning
2037 // to the caller...
2038 new AsyncTask<Void, Void, Void>() {
2039 public Void doInBackground(Void ... args) {
2040 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2041 return null;
2042 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002043 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002044 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002045 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002046 }
2047
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 @Override
2049 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002050 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
2052
Joe Onorato88ec0992009-11-19 13:16:06 -08002053 @Override
2054 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002055 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2056 return;
2057 }
2058
Sunny Goyal45478022015-06-08 16:52:41 -07002059 if (mDragController.isDragging()) {
2060 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002061 return;
2062 }
2063
Jon Mirandafeba90f2016-10-06 10:53:29 -07002064 // Note: There should be at most one log per method call. This is enforced implicitly
2065 // by using if-else statements.
2066 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002067 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2068 if (topView != null) {
2069 if (topView.getActiveTextView() != null) {
2070 topView.getActiveTextView().dispatchBackKey();
2071 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002072 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302073 ued.logActionCommand(Action.Command.BACK,
2074 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002075 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302076 ued.logActionCommand(Action.Command.BACK,
2077 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002078 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002079 topView.close(true);
2080 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002081 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302082 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002083 showWorkspace(true);
2084 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302085 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002086 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002087 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302088 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002089 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002090 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002091 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002092 mWorkspace.exitWidgetResizeMode();
2093
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002094 // Back button is a no-op here, but give at least some feedback for the button press
2095 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002096 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002097 }
2098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 * Launches the intent referred by the clicked shortcut.
2101 *
2102 * @param v The view representing the clicked shortcut.
2103 */
2104 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002105 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2106 // view has detached (it's possible for this to happen if the view is removed mid touch).
2107 if (v.getWindowToken() == null) {
2108 return;
2109 }
2110
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002111 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002112 return;
2113 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002114
Adam Cohen1697b792013-09-17 19:08:21 -07002115 if (v instanceof Workspace) {
2116 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08002117 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2118 LauncherLogProto.Action.Direction.NONE,
2119 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002120 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002121 }
2122 return;
2123 }
2124
2125 if (v instanceof CellLayout) {
2126 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002127 int page = mWorkspace.indexOfChild(v);
2128 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2129 LauncherLogProto.Action.Direction.NONE,
2130 LauncherLogProto.ContainerType.OVERVIEW, page);
2131 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002132 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002133 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002134 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002135 }
2136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002138 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002139 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002141 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002142 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002143 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07002144 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002145 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002146 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002147 } else if (tag instanceof AppInfo) {
2148 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002149 } else if (tag instanceof LauncherAppWidgetInfo) {
2150 if (v instanceof PendingAppWidgetHostView) {
2151 onClickPendingWidget((PendingAppWidgetHostView) v);
2152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 }
2154 }
2155
Sunny Goyal70660032015-05-14 00:07:08 -07002156 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002157 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002158 return false;
2159 }
2160
Michael Jurkaaf442092010-06-10 17:01:57 -07002161 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002162 * Event handler for the app widget view which has not fully restored.
2163 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002164 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002165 if (mIsSafeModeEnabled) {
2166 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2167 return;
2168 }
2169
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002170 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002171 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002172 LauncherAppWidgetProviderInfo appWidgetInfo =
2173 mAppWidgetManager.findProvider(info.providerName, info.user);
2174 if (appWidgetInfo == null) {
2175 return;
2176 }
2177 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2178
Sunny Goyald478c832016-04-01 12:04:16 -07002179 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2180 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2181 // This should not happen, as we make sure that an Id is allocated during bind.
2182 return;
2183 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002184 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2185 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002186 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002187 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002188 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002189 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002190 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002191 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002192 }
2193 }
2194
2195 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002196 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2197 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2198 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002199 *
2200 * @param v The view that was clicked.
2201 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002202 protected void onClickAllAppsButton(View v) {
2203 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002204 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302205 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2206 ControlType.ALL_APPS_BUTTON);
Sunny Goyal5606e072017-06-15 14:24:21 -07002207 showAppsView(true /* animated */, true /* updatePredictedApps */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002208 } else {
2209 showWorkspace(true);
2210 }
2211 }
2212
Sunny Goyale6e72002017-01-12 16:55:36 -08002213 private void onClickPendingAppItem(final View v, final String packageName,
2214 boolean downloadStarted) {
2215 if (downloadStarted) {
2216 // If the download has started, simply direct to the market app.
2217 startMarketIntentForPackage(v, packageName);
2218 return;
2219 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002220 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002221 .setTitle(R.string.abandoned_promises_title)
2222 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002223 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2224 @Override
2225 public void onClick(DialogInterface dialogInterface, int i) {
2226 startMarketIntentForPackage(v, packageName);
2227 }
2228 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002229 .setNeutralButton(R.string.abandoned_clean_this,
2230 new DialogInterface.OnClickListener() {
2231 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002232 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002233 mWorkspace.removeAbandonedPromise(packageName, user);
2234 }
2235 })
2236 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002237 }
2238
2239 private void startMarketIntentForPackage(View v, String packageName) {
2240 ItemInfo item = (ItemInfo) v.getTag();
2241 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2242 boolean success = startActivitySafely(v, intent, item);
2243 if (success && v instanceof BubbleTextView) {
2244 mWaitingForResume = (BubbleTextView) v;
2245 mWaitingForResume.setStayPressed(true);
2246 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002247 }
2248
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002249 /**
2250 * Event handler for an app shortcut click.
2251 *
2252 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2253 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002254 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002255 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2256 Object tag = v.getTag();
2257 if (!(tag instanceof ShortcutInfo)) {
2258 throw new IllegalArgumentException("Input must be a Shortcut");
2259 }
2260
2261 // Open shortcut
2262 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002263
2264 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002265 if ((shortcut.isDisabled &
2266 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2267 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2268 // If the app is only disabled because of the above flags, launch activity anyway.
2269 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002270 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002271 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2272 // Use a message specific to this shortcut, if it has one.
2273 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2274 return;
2275 }
2276 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002277 int error = R.string.activity_not_available;
2278 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2279 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002280 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2281 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002282 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002283 }
2284 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2285 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002286 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002287 }
2288
Chris Wren40c5ed32014-06-24 18:24:23 -04002289 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002290 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002291 String packageName = shortcut.intent.getComponent() != null ?
2292 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2293 if (!TextUtils.isEmpty(packageName)) {
2294 onClickPendingAppItem(v, packageName,
2295 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2296 return;
2297 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002298 }
2299
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002300 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002301 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002302 }
2303
Sunny Goyala7ce1662016-05-31 15:01:35 -07002304 private void startAppShortcutOrInfoActivity(View v) {
2305 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002306 Intent intent;
2307 if (item instanceof PromiseAppInfo) {
2308 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2309 intent = promiseAppInfo.getMarketIntent();
2310 } else {
2311 intent = item.getIntent();
2312 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002313 if (intent == null) {
2314 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002315 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002316 boolean success = startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002317 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002318
2319 if (success && v instanceof BubbleTextView) {
2320 mWaitingForResume = (BubbleTextView) v;
2321 mWaitingForResume.setStayPressed(true);
2322 }
2323 }
2324
2325 /**
2326 * Event handler for a folder icon click.
2327 *
2328 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2329 */
2330 protected void onClickFolderIcon(View v) {
2331 if (LOGD) Log.d(TAG, "onClickFolder");
2332 if (!(v instanceof FolderIcon)){
2333 throw new IllegalArgumentException("Input must be a FolderIcon");
2334 }
2335
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002336 Folder folder = ((FolderIcon) v).getFolder();
2337 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002338 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002339 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002340 }
Michael Jurka5130e402011-10-13 04:55:35 -07002341 }
2342
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002343 /**
2344 * Event handler for the (Add) Widgets button that appears after a long press
2345 * on the home screen.
2346 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002347 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002348 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002349 if (mIsSafeModeEnabled) {
2350 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2351 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002352 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002353 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002354 }
2355
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002356 /**
2357 * Event handler for the wallpaper picker button that appears after a long press
2358 * on the home screen.
2359 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002360 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002361 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002362 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2363 return;
2364 }
2365
Tony Wickham785f7a52015-08-31 17:28:32 -07002366 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2367 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002368 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002369 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002370 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002371
2372 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002373 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2374 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002375 intent.setPackage(pickerPackage);
2376 }
2377
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002378 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002379 try {
2380 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2381 // If there is no target package, use the default intent chooser animation
2382 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2383 } catch (ActivityNotFoundException e) {
2384 setWaitingForResult(null);
2385 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2386 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002387 }
2388
2389 /**
2390 * Event handler for a click on the settings button that appears after a long press
2391 * on the home screen.
2392 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002393 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002394 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002395 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2396 .setPackage(getPackageName());
2397 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002398 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002399 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002400 }
2401
Tony Wickhame2217252016-03-22 16:34:23 -07002402 @Override
2403 public void onAccessibilityStateChanged(boolean enabled) {
2404 mDragLayer.onAccessibilityStateChanged(enabled);
2405 }
2406
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002407 /**
2408 * Called when the user stops interacting with the launcher.
2409 * This implies that the user is now on the homescreen and is not doing housekeeping.
2410 */
Adam Cohen9211d422014-10-07 18:14:53 -07002411 protected void onInteractionEnd() {
2412 if (mLauncherCallbacks != null) {
2413 mLauncherCallbacks.onInteractionEnd();
2414 }
2415 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002416
2417 /**
2418 * Called when the user starts interacting with the launcher.
2419 * The possible interactions are:
2420 * - open all apps
2421 * - reorder an app shortcut, or a widget
2422 * - open the overview mode.
2423 * This is a good time to stop doing things that only make sense
2424 * when the user is on the homescreen and not doing housekeeping.
2425 */
Adam Cohen9211d422014-10-07 18:14:53 -07002426 protected void onInteractionBegin() {
2427 if (mLauncherCallbacks != null) {
2428 mLauncherCallbacks.onInteractionBegin();
2429 }
2430 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002431
Winson Chungcd99cd32015-04-29 11:03:24 -07002432 /** Updates the interaction state. */
2433 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002434 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002435 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002436 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2437 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002438 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002439 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002440 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002441 onInteractionEnd();
2442 }
2443 }
2444
Sunny Goyala7ce1662016-05-31 15:01:35 -07002445 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002447 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2448 try {
2449 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2450 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2451 // is enabled by default on NYC.
2452 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2453 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002454
2455 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2456 String id = ((ShortcutInfo) info).getDeepShortcutId();
2457 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302458 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002459 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002460 } else {
2461 // Could be launching some bookkeeping activity
2462 startActivity(intent, optsBundle);
2463 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002464 } finally {
2465 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002468 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2469 // corresponding permission. Show the appropriate permission prompt if that
2470 // is the case.
2471 if (intent.getComponent() == null
2472 && Intent.ACTION_CALL.equals(intent.getAction())
2473 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2474 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002475
2476 setWaitingForResult(PendingRequestArgs
2477 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002478 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2479 REQUEST_PERMISSION_CALL_PHONE);
2480 } else {
2481 // No idea why this was thrown.
2482 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 }
2485 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002486
Sunny Goyalfe770c92016-09-27 14:38:58 -07002487 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002488 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002489 if (Utilities.ATLEAST_MARSHMALLOW) {
2490 int left = 0, top = 0;
2491 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002492 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002493 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002494 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002495 if (icon != null) {
2496 Rect bounds = icon.getBounds();
2497 left = (width - bounds.width()) / 2;
2498 top = v.getPaddingTop();
2499 width = bounds.width();
2500 height = bounds.height();
2501 }
2502 }
2503 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2504 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2505 // On L devices, we use the device default slide-up transition.
2506 // On L MR1 devices, we use a custom version of the slide-up transition which
2507 // doesn't have the delay present in the device default.
2508 return ActivityOptions.makeCustomAnimation(
2509 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2510 }
2511 return null;
2512 }
2513
Tonye3c59252017-05-02 21:32:27 -07002514 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002515 int[] pos = new int[2];
2516 v.getLocationOnScreen(pos);
2517 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2518 }
2519
Sunny Goyala7ce1662016-05-31 15:01:35 -07002520 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002521 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2522 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2523 return false;
2524 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002525 // Only launch using the new animation if the shortcut has not opted out (this is a
2526 // private contract between launcher and may be ignored in the future).
2527 boolean useLaunchAnimation = (v != null) &&
2528 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2529 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2530
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002531 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002532
2533 // Prepare intent
2534 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2535 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002536 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002537 }
2538 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002539 if (Utilities.ATLEAST_MARSHMALLOW
2540 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002541 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002542 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002543 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002544 // Shortcuts need some special checks due to legacy reasons.
2545 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002546 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002547 // Could be launching some bookkeeping activity
2548 startActivity(intent, optsBundle);
2549 } else {
2550 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2551 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2552 }
2553 return true;
2554 } catch (ActivityNotFoundException|SecurityException e) {
2555 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2556 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2557 }
2558 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002559 }
2560
Tony Wickhamdadb3042016-02-24 11:07:00 -08002561 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002562 public boolean dispatchTouchEvent(MotionEvent ev) {
2563 mLastDispatchTouchEventX = ev.getX();
2564 return super.dispatchTouchEvent(ev);
2565 }
2566
2567 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002569 if (!isDraggingEnabled()) return false;
2570 if (isWorkspaceLocked()) return false;
2571 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572
Jon Miranda51f037d2016-11-07 08:37:20 -08002573 boolean ignoreLongPressToOverview =
2574 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002575
Adam Cohen1697b792013-09-17 19:08:21 -07002576 if (v instanceof Workspace) {
2577 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002578 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302579 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2580 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002581 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002582 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002583 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2584 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2585 return true;
2586 } else {
2587 return false;
2588 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002589 } else {
2590 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002591 }
Adam Cohen1697b792013-09-17 19:08:21 -07002592 }
2593
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002594 CellLayout.CellInfo longClickCellInfo = null;
2595 View itemUnderLongClick = null;
2596 if (v.getTag() instanceof ItemInfo) {
2597 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002598 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002599 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002600 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 }
2602
Winson Chung3d503fb2011-07-13 17:25:49 -07002603 // The hotseat touch handling does not go through Workspace, and we always allow long press
2604 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002605 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002607 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002608 if (mWorkspace.isInOverviewMode()) {
2609 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302610 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2611 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002612 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002613 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002614 return false;
2615 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302616 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2617 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002618 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002619 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002620 }
Jon Miranda379198e2016-09-23 08:54:40 -07002621 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2622 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002623 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002624 final boolean isAllAppsButton =
2625 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2626 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2627 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002628 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002630 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 }
2632 }
2633 }
2634 return true;
2635 }
2636
Winson Chung3d503fb2011-07-13 17:25:49 -07002637 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002638 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002639 return mHotseat != null && layout != null &&
2640 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2641 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002642
Winson Chung3d503fb2011-07-13 17:25:49 -07002643 /**
2644 * Returns the CellLayout of the specified container at the specified screen.
2645 */
Sunny Goyal92820592015-03-02 11:31:35 -08002646 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002647 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2648 if (mHotseat != null) {
2649 return mHotseat.getLayout();
2650 } else {
2651 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002652 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002653 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002654 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002655 }
2656 }
2657
Winson Chungb745afb2015-03-02 11:51:23 -08002658 /**
2659 * For overridden classes.
2660 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002661 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002662 return isAppsViewVisible();
2663 }
2664
2665 public boolean isAppsViewVisible() {
2666 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2667 }
2668
2669 public boolean isWidgetsViewVisible() {
2670 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002671 }
2672
Michael Jurkae326f182011-11-21 14:05:46 -08002673 @Override
2674 public void onTrimMemory(int level) {
2675 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002676 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2677 // The widget preview db can result in holding onto over
2678 // 3MB of memory for caching which isn't necessary.
2679 SQLiteDatabase.releaseMemory();
2680
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002681 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002682 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002683 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002684 if (mLauncherCallbacks != null) {
2685 mLauncherCallbacks.onTrimMemory(level);
2686 }
Michael Jurkae326f182011-11-21 14:05:46 -08002687 }
2688
Winson5c6bdbb2015-09-03 11:36:19 -07002689 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002690 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002691 }
2692
Winson5c6bdbb2015-09-03 11:36:19 -07002693 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002694 boolean changed = mState != State.WORKSPACE ||
2695 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002696 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002697 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002698 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002699 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002700
Michael Jurkab3e22d92011-10-31 15:58:33 -07002701 // Set focus to the AppsCustomize button
2702 if (mAllAppsButton != null) {
2703 mAllAppsButton.requestFocus();
2704 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002705 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002706
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002707 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002708 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002709
Winson Chung0f785722015-04-08 10:27:49 -07002710 if (changed) {
2711 // Send an accessibility event to announce the context change
2712 getWindow().getDecorView()
2713 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002714 }
Winson5c6bdbb2015-09-03 11:36:19 -07002715 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002716 }
2717
Winsone9f27272015-10-13 10:47:51 -07002718 /**
2719 * Shows the overview button.
2720 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002721 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002722 showOverviewMode(animated, false);
2723 }
2724
2725 /**
2726 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2727 * onto one of the overview panel buttons.
2728 */
2729 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2730 Runnable postAnimRunnable = null;
2731 if (requestButtonFocus) {
2732 postAnimRunnable = new Runnable() {
2733 @Override
2734 public void run() {
2735 // Hitting the menu button when in touch mode does not trigger touch mode to
2736 // be disabled, so if requested, force focus on one of the overview panel
2737 // buttons.
2738 mOverviewPanel.requestFocusFromTouch();
2739 }
2740 };
2741 }
Adam Cohened307df2013-10-02 09:37:31 -07002742 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002743 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002744 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002745 setState(State.WORKSPACE);
2746
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002747 // If animated from long press, then don't allow any of the controller in the drag
2748 // layer to intercept any remaining touch.
2749 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002750 }
2751
Mario Bertschleracbf5702017-03-03 10:58:06 -08002752 private void setState(State state) {
2753 this.mState = state;
2754 updateSoftInputMode();
2755 }
2756
2757 private void updateSoftInputMode() {
2758 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2759 final int mode;
2760 if (isAppsViewVisible()) {
2761 mode = SOFT_INPUT_MODE_ALL_APPS;
2762 } else {
2763 mode = SOFT_INPUT_MODE_DEFAULT;
2764 }
2765 getWindow().setSoftInputMode(mode);
2766 }
2767 }
2768
Winson Chungb745afb2015-03-02 11:51:23 -08002769 /**
2770 * Shows the apps view.
2771 */
Sunny Goyal5606e072017-06-15 14:24:21 -07002772 public void showAppsView(boolean animated, boolean updatePredictedApps) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002773 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002774 if (updatePredictedApps) {
2775 tryAndUpdatePredictedApps();
2776 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002777 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002778 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002779
Winson Chungb745afb2015-03-02 11:51:23 -08002780 /**
2781 * Shows the widgets view.
2782 */
2783 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002784 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002785 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002786 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002787 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002788 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002789
2790 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002791 @Override
2792 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002793 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002794 }
2795 });
Winson Chungb745afb2015-03-02 11:51:23 -08002796 }
2797
2798 /**
2799 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002800 *
2801 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002802 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002803 // TODO: calling method should use the return value so that when {@code false} is returned
2804 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002805 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002806 if (!(mState == State.WORKSPACE ||
2807 mState == State.APPS_SPRING_LOADED ||
2808 mState == State.WIDGETS_SPRING_LOADED ||
2809 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002810 return false;
2811 }
2812 if (toState != State.APPS && toState != State.WIDGETS) {
2813 return false;
2814 }
Winson Chungb745afb2015-03-02 11:51:23 -08002815
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002816 // This is a safe and supported transition to bypass spring_loaded mode.
2817 if (mExitSpringLoadedModeRunnable != null) {
2818 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2819 mExitSpringLoadedModeRunnable = null;
2820 }
2821
Winson Chungb745afb2015-03-02 11:51:23 -08002822 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002823 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002824 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002825 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002826 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002827
Michael Jurkab3e22d92011-10-31 15:58:33 -07002828 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002829 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002830 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002831
2832 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002833 getWindow().getDecorView()
2834 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002835 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002836 }
2837
Winson Chungcd99cd32015-04-29 11:03:24 -07002838 /**
2839 * Updates the workspace and interaction state on state change, and return the animation to this
2840 * new state.
2841 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002842 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002843 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002844 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002845 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002846 updateInteraction(fromState, toState);
2847 return anim;
2848 }
2849
Hyunyoung Song3f471442015-04-08 19:01:34 -07002850 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002851 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002852 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002853 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002854 }
Winson Chungb745afb2015-03-02 11:51:23 -08002855
Winson Chung006ee262015-08-03 14:40:11 -07002856 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002857 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002858 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002859 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002860 }
Adam Cohen7777d962011-08-18 18:58:38 -07002861
Hyunyoung Song3f471442015-04-08 19:01:34 -07002862 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002863 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002864 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002865
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002866 if (mExitSpringLoadedModeRunnable != null) {
2867 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2868 }
2869 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002870 @Override
2871 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002872 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002873 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2874 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002875 // Before we show workspace, hide all apps again because
2876 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2877 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002878 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002879 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002880 } else {
2881 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002882 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002883 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002884 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002885 };
2886 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002887 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002888
Tony Wickhame0c33232016-02-08 11:37:04 -08002889 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002890 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2891 || mState == State.WIDGETS_SPRING_LOADED;
2892 }
2893
Sunny Goyal0f76b562016-12-13 19:37:10 -08002894 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002895 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002896 showAppsView(true /* animated */, false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08002897 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002898 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002899 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2900 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002901 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 }
2903
Winson Chung4ac30062015-05-08 17:34:17 -07002904 /**
2905 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
2906 * resumed.
2907 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07002908 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07002909 if (mLauncherCallbacks != null) {
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002910 List<ComponentKeyMapper<AppInfo>> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07002911 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07002912 mAppsView.setPredictedApps(apps);
2913 }
2914 }
2915 }
2916
Michael Jurkad7c28052012-04-27 15:43:36 -07002917 @Override
2918 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002919 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002920 final List<CharSequence> text = event.getText();
2921 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002922 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002923 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002924 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002925 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002926 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002927 } else if (mWorkspace != null) {
2928 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002929 } else {
2930 text.add(getString(R.string.all_apps_home_button_label));
2931 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002932 return result;
2933 }
2934
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002935 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002936 * If the activity is currently paused, signal that we need to run the passed Runnable
2937 * in onResume.
2938 *
2939 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002940 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2941 * wrong when we're not running, and if the activity comes back to what the configuration was
2942 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002943 *
2944 * Implementation of the method from LauncherModel.Callbacks.
2945 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002946 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002947 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002948 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002949 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002950 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002951 if (run instanceof RunnableWithId) {
2952 // Remove any runnables which have the same id
2953 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002954 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002955 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002956 return true;
2957 } else {
2958 return false;
2959 }
2960 }
2961
Michael Jurka1e2f4652013-07-08 18:03:46 -07002962 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02002963 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002964 }
2965
Michael Jurka7607c2f2013-04-03 14:33:19 -07002966 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002967 * If the activity is currently paused, signal that we need to re-run the loader
2968 * in onResume.
2969 *
2970 * This needs to be called from incoming places where resources might have been loaded
2971 * while we are paused. That is becaues the Configuration might be wrong
2972 * when we're not running, and if it comes back to what it was when we
2973 * were paused, we are not restarted.
2974 *
2975 * Implementation of the method from LauncherModel.Callbacks.
2976 *
2977 * @return true if we are currently paused. The caller might be able to
2978 * skip some work in that case since we will come back again.
2979 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002980 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002981 public boolean setLoadOnResume() {
2982 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002983 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002984 mOnResumeNeedsLoad = true;
2985 return true;
2986 } else {
2987 return false;
2988 }
2989 }
2990
2991 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002994 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002996 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002997 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002998 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002999 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003002
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003004 * Clear any pending bind callbacks. This is called when is loader is planning to
3005 * perform a full rebind from scratch.
3006 */
3007 @Override
3008 public void clearPendingBinds() {
3009 mBindOnResumeCallbacks.clear();
3010 if (mPendingExecutor != null) {
3011 mPendingExecutor.markCompleted();
3012 mPendingExecutor = null;
3013 }
3014 }
3015
3016 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003017 * Refreshes the shortcuts shown on the workspace.
3018 *
3019 * Implementation of the method from LauncherModel.Callbacks.
3020 */
3021 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003022 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08003023 AbstractFloatingView.closeAllOpenViews(this);
3024
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003025 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003026
Winson Chungd64d1762013-08-20 14:37:16 -07003027 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003028 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003029 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003030
Winson Chung3d503fb2011-07-13 17:25:49 -07003031 if (mHotseat != null) {
3032 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003034 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 }
3036
Adam Cohendcd297f2013-06-18 13:13:40 -07003037 @Override
3038 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003039 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003040 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3041 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003042 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3043 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003044 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003045 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3046 // If there are no screens, we need to have an empty screen
3047 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003048 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003049 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003050
Winsonc7d2e832016-07-28 12:24:55 -07003051 // After we have added all the screens, if the wallpaper was locked to the default state,
3052 // then notify to indicate that it can be released and a proper wallpaper offset can be
3053 // computed before the next layout
3054 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003055 }
3056
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003057 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003058 int count = orderedScreenIds.size();
3059 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003060 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003061 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003062 // No need to bind the first screen, as its always bound.
3063 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3064 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003065 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003066 }
3067
Sunny Goyalb23980c2017-08-17 07:45:25 -07003068 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07003069 public void bindAppsAdded(final ArrayList<Long> newScreens,
3070 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07003071 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07003072 Runnable r = new Runnable() {
3073 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003074 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07003075 }
3076 };
3077 if (waitUntilResume(r)) {
3078 return;
3079 }
3080
Winson Chungd64d1762013-08-20 14:37:16 -07003081 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003082 if (newScreens != null) {
3083 bindAddScreens(newScreens);
3084 }
Winson Chungd64d1762013-08-20 14:37:16 -07003085
3086 // We add the items without animation on non-visible pages, and with
3087 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003088 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003089 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07003090 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003091 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003092 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07003093 }
Winson Chungc58497e2013-09-03 17:48:37 -07003094
Winson Chung87412982013-10-03 18:34:14 -07003095 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003096 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07003097 }
3098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 * Bind the items start-end from the list.
3101 *
3102 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003104 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07003105 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003106 Runnable r = new Runnable() {
3107 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003108 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003109 }
3110 };
3111 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003112 return;
3113 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003114
Sunny Goyal3be633b2016-12-08 09:59:25 -08003115 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003116 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003117 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07003118 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003119 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003120 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07003121 int end = items.size();
3122 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003123 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003124
3125 // Short circuit if we are loading dock items for a configuration which has no dock
3126 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3127 mHotseat == null) {
3128 continue;
3129 }
3130
Sunny Goyal639e9062015-08-19 19:17:06 -07003131 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 switch (item.itemType) {
3133 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3134 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003135 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003136 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003137 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003139 }
3140 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003141 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003142 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003143 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003144 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003145 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07003146 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
3147 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07003148 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003149 if (view == null) {
3150 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003151 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08003152 break;
3153 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003154 default:
3155 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003157
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003158 /*
3159 * Remove colliding items.
3160 */
3161 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3162 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3163 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3164 View v = cl.getChildAt(item.cellX, item.cellY);
3165 Object tag = v.getTag();
3166 String desc = "Collision while binding workspace item: " + item
3167 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003168 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003169 throw (new RuntimeException(desc));
3170 } else {
3171 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003172 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003173 continue;
3174 }
3175 }
3176 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303177 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003178 if (animateIcons) {
3179 // Animate all the applications up now
3180 view.setAlpha(0f);
3181 view.setScaleX(0f);
3182 view.setScaleY(0f);
3183 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003184 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003187
Winson Chung997a9232013-07-24 15:33:46 -07003188 if (animateIcons) {
3189 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003190 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003191 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003192 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003193 final Runnable startBounceAnimRunnable = new Runnable() {
3194 public void run() {
3195 anim.playTogether(bounceAnims);
3196 anim.start();
3197 }
3198 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003199 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003200 // We post the animation slightly delayed to prevent slowdowns
3201 // when we are loading right after we return to launcher.
3202 mWorkspace.postDelayed(new Runnable() {
3203 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003204 if (mWorkspace != null) {
3205 mWorkspace.snapToPage(newScreenIndex);
3206 mWorkspace.postDelayed(startBounceAnimRunnable,
3207 NEW_APPS_ANIMATION_DELAY);
3208 }
Winson Chung94d67682013-09-25 16:29:40 -07003209 }
3210 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003211 } else {
3212 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003213 }
3214 }
Winson Chung64359a52013-07-08 17:17:08 -07003215 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 }
3218
Joe Onorato9c1289c2009-08-17 11:03:03 -04003219 /**
3220 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003221 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003222 public void bindAppWidget(LauncherAppWidgetInfo item) {
3223 View view = inflateAppWidget(item);
3224 if (view != null) {
3225 mWorkspace.addInScreen(view, item);
3226 mWorkspace.requestLayout();
3227 }
3228 }
3229
3230 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003231 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303232 PendingAppWidgetHostView view =
3233 new PendingAppWidgetHostView(this, item, mIconCache, true);
3234 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003235 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003236 }
3237
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003238 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003239
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003240 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003241
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003242 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3243 // If the provider is not ready, bind as a pending widget.
3244 appWidgetInfo = null;
3245 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3246 // The widget id is not valid. Try to find the widget based on the provider info.
3247 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3248 } else {
3249 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3250 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003251
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003252 // If the provider is ready, but the width is not yet restored, try to restore it.
3253 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3254 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003255 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003256 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3257 + " belongs to component " + item.providerName
3258 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003259 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003260 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003261 }
Sunny Goyalff572272014-07-23 13:58:07 -07003262
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003263 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003264 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003265 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3266 // Id has not been allocated yet. Allocate a new id.
3267 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3268 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003269
Sunny Goyald478c832016-04-01 12:04:16 -07003270 // Also try to bind the widget. If the bind fails, the user will be shown
3271 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003272 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003273 pendingInfo.spanX = item.spanX;
3274 pendingInfo.spanY = item.spanY;
3275 pendingInfo.minSpanX = item.minSpanX;
3276 pendingInfo.minSpanY = item.minSpanY;
3277 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003278
3279 boolean isDirectConfig =
3280 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3281 if (isDirectConfig && item.bindOptions != null) {
3282 Bundle newOptions = item.bindOptions.getExtras();
3283 if (options != null) {
3284 newOptions.putAll(options);
3285 }
3286 options = newOptions;
3287 }
Sunny Goyald478c832016-04-01 12:04:16 -07003288 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3289 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003290
Sunny Goyal86df1382016-08-10 15:03:22 -07003291 // We tried to bind once. If we were not able to bind, we would need to
3292 // go through the permission dialog, which means we cannot skip the config
3293 // activity.
3294 item.bindOptions = null;
3295 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3296
Sunny Goyald478c832016-04-01 12:04:16 -07003297 // Bind succeeded
3298 if (success) {
3299 // If the widget has a configure activity, it is still needs to set it up,
3300 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003301 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003302 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3303 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003304 }
Sunny Goyald478c832016-04-01 12:04:16 -07003305
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003306 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003307 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003308 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003309 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003310 // The widget was marked as UI not ready, but there is no configure activity to
3311 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003312 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003313 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003314 }
Sunny Goyalff572272014-07-23 13:58:07 -07003315 }
3316
Sunny Goyald5462aa2016-11-22 16:52:39 +05303317 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003318 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003319 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003320 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003321 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003322 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003323 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003324 }
3325
Sunny Goyal233ee962015-08-03 13:05:01 -07003326 item.minSpanX = appWidgetInfo.minSpanX;
3327 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303328 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003329 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303330 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003331 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303332 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003334 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003335 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 }
3337
Sunny Goyalff572272014-07-23 13:58:07 -07003338 /**
3339 * Restores a pending widget.
3340 *
3341 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003342 */
Sunny Goyald478c832016-04-01 12:04:16 -07003343 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003344 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3345 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3346 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003347 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003348 }
3349
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003350 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003351 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003352 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3353 info.pendingItemInfo = null;
3354 }
Sunny Goyalff572272014-07-23 13:58:07 -07003355
3356 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003357 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003358 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003359 }
3360
Adam Cohen1462de32012-07-24 22:34:36 -07003361 public void onPageBoundSynchronously(int page) {
3362 mSynchronouslyBoundPages.add(page);
3363 }
3364
Sunny Goyal527c7d32015-08-28 15:19:36 -07003365 @Override
3366 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3367 if (mPendingExecutor != null) {
3368 mPendingExecutor.markCompleted();
3369 }
3370 mPendingExecutor = executor;
3371 executor.attachTo(this);
3372 }
3373
3374 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3375 if (mPendingExecutor == executor) {
3376 mPendingExecutor = null;
3377 }
3378 }
3379
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003380 @Override
3381 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3382 Runnable r = new Runnable() {
3383 public void run() {
3384 finishFirstPageBind(executor);
3385 }
3386 };
3387 if (waitUntilResume(r)) {
3388 return;
3389 }
3390
3391 Runnable onComplete = new Runnable() {
3392 @Override
3393 public void run() {
3394 if (executor != null) {
3395 executor.onLoadAnimationCompleted();
3396 }
3397 }
3398 };
3399 if (mDragLayer.getAlpha() < 1) {
3400 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3401 } else {
3402 onComplete.run();
3403 }
3404 }
3405
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 /**
3407 * Callback saying that there aren't any more items to bind.
3408 *
3409 * Implementation of the method from LauncherModel.Callbacks.
3410 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003411 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003412 Runnable r = new Runnable() {
3413 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003414 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003415 }
3416 };
3417 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003418 return;
3419 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003420 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003421 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003423 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003424
Sunny Goyal2100c782016-08-22 16:00:03 -07003425 if (mPendingActivityResult != null) {
3426 handleActivityResult(mPendingActivityResult.requestCode,
3427 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3428 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003429 }
3430
Sunny Goyala474a9b2017-05-04 16:47:11 -07003431 InstallShortcutReceiver.disableAndFlushInstallQueue(
3432 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003433
Tony Wickham010d2552017-01-20 08:15:28 -08003434 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3435
Adam Cohen9211d422014-10-07 18:14:53 -07003436 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003437 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003438 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003439 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003440 }
3441
Winson Chunga2413752012-04-03 14:22:34 -07003442 private boolean canRunNewAppsAnimation() {
3443 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003444 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003445 }
3446
Winson Chungc9168342013-06-26 14:54:55 -07003447 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003448 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003449 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3450 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003451 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003452 return bounceAnim;
3453 }
3454
Adam Cohen27772732013-11-11 14:00:56 +00003455 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003456 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003457 }
3458
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003459 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003460 * Add the icons for all apps.
3461 *
3462 * Implementation of the method from LauncherModel.Callbacks.
3463 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003464 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003465 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3466 public void run() {
3467 bindAllApplications(apps);
3468 }
3469 };
3470 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003471 return;
3472 }
3473
Winson Chungb745afb2015-03-02 11:51:23 -08003474 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003475 Executor pendingExecutor = getPendingExecutor();
3476 if (pendingExecutor != null && mState != State.APPS) {
3477 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003478 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003479 return;
3480 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003481
Winson Chungb745afb2015-03-02 11:51:23 -08003482 mAppsView.setApps(apps);
3483 }
Adam Cohen9211d422014-10-07 18:14:53 -07003484 if (mLauncherCallbacks != null) {
3485 mLauncherCallbacks.bindAllApplications(apps);
3486 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 }
3488
3489 /**
Tony2917a8b2017-07-31 23:29:42 -07003490 * Returns an Executor that will run after the launcher is first drawn (including after the
3491 * initial fade in animation). Returns null if the first draw has already occurred.
3492 */
3493 public @Nullable Executor getPendingExecutor() {
3494 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3495 }
3496
3497 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003498 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3499 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3500 */
3501 @Override
3502 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003503 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003504 }
3505
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003506 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 * A package was updated.
3508 *
3509 * Implementation of the method from LauncherModel.Callbacks.
3510 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003511 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003512 Runnable r = new Runnable() {
3513 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003514 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003515 }
3516 };
3517 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 return;
3519 }
3520
Winson Chungb745afb2015-03-02 11:51:23 -08003521 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003522 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003523 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 }
3525
Sunny Goyal4390ace2014-10-13 11:33:11 -07003526 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003527 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3528 Runnable r = new Runnable() {
3529 public void run() {
3530 bindPromiseAppProgressUpdated(app);
3531 }
3532 };
3533 if (waitUntilResume(r)) {
3534 return;
3535 }
3536
3537 if (mAppsView != null) {
3538 mAppsView.updatePromiseAppProgress(app);
3539 }
3540 }
3541
3542 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003543 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3544 Runnable r = new Runnable() {
3545 public void run() {
3546 bindWidgetsRestored(widgets);
3547 }
3548 };
3549 if (waitUntilResume(r)) {
3550 return;
3551 }
3552 mWorkspace.widgetsRestored(widgets);
3553 }
3554
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003556 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003557 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003558 *
3559 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003560 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003561 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003562 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003563 Runnable r = new Runnable() {
3564 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003565 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003566 }
3567 };
3568 if (waitUntilResume(r)) {
3569 return;
3570 }
3571
Sunny Goyal4390ace2014-10-13 11:33:11 -07003572 if (!updated.isEmpty()) {
3573 mWorkspace.updateShortcuts(updated);
3574 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003575 }
3576
3577 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003578 * Update the state of a package, typically related to install state.
3579 *
3580 * Implementation of the method from LauncherModel.Callbacks.
3581 */
Sunny Goyale755d462014-07-22 13:48:29 -07003582 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003583 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3584 Runnable r = new Runnable() {
3585 public void run() {
3586 bindRestoreItemsChange(updates);
3587 }
3588 };
3589 if (waitUntilResume(r)) {
3590 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003591 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003592
Sunny Goyal756adbc2015-04-16 15:20:51 -07003593 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003594 }
3595
3596 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003597 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003598 * in addition to specific applications to remove, the reason being that
3599 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003600 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003601 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003603 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003604 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003605 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003606 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003607 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003608 }
Winson Chungd64d1762013-08-20 14:37:16 -07003609 };
3610 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003611 return;
3612 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003613 mWorkspace.removeItemsByMatcher(matcher);
3614 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003615 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003616
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003617 @Override
3618 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3619 Runnable r = new Runnable() {
3620 public void run() {
3621 bindAppInfosRemoved(appInfos);
3622 }
3623 };
3624 if (waitUntilResume(r)) {
3625 return;
Joe Onorato36115782010-06-17 13:28:48 -04003626 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003627
Winson Chungdf95eb12013-10-16 14:57:07 -07003628 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003629 if (mAppsView != null) {
3630 mAppsView.removeApps(appInfos);
Hyunyoung Song51114f22017-05-02 16:06:28 -07003631 tryAndUpdatePredictedApps();
Winson Chungc58497e2013-09-03 17:48:37 -07003632 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 }
Joe Onoratobe386092009-11-17 17:32:16 -08003634
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003635 @Override
3636 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3637 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3638 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003639 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003640 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003641 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003642 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003643 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003644 return;
3645 }
3646
Sunny Goyald164b7f2016-10-12 20:49:31 -07003647 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003648 Executor pendingExecutor = getPendingExecutor();
3649 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003650 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003651 return;
3652 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003653 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003654 }
Tony Wickham26b17462017-03-20 17:12:24 -07003655
3656 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3657 if (topView != null) {
3658 topView.onWidgetsBound();
3659 }
3660 }
3661
3662 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3663 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003664 }
3665
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003666 @Override
3667 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003668 if (mWorkspace.getState().shouldUpdateWidget) {
3669 refreshAndBindWidgetsForPackageUser(null);
3670 }
Tony Wickham26b17462017-03-20 17:12:24 -07003671 }
3672
Tony Wickham86222d22017-03-29 15:30:43 -07003673 /**
3674 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3675 * refreshes the widgets and shortcuts associated with the given package/user
3676 */
3677 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003678 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003679 }
3680
Winson Chung4b919f82012-05-01 10:44:08 -07003681 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003682 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003683 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003684 }
3685 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003686
Winson Chung4b919f82012-05-01 10:44:08 -07003687 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003688 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003689 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003690 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003691 } else {
3692 mHandler.postDelayed(new Runnable() {
3693 public void run() {
3694 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3695 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003696 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003697 }
Winson Chung4b919f82012-05-01 10:44:08 -07003698 }
Winson Chung400438b2011-01-16 17:53:48 -08003699 }
3700
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003701 private void markAppsViewShown() {
3702 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3703 return;
3704 }
3705 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3706 }
3707
3708 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003709 UserManagerCompat um = UserManagerCompat.getInstance(this);
3710 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003711 }
3712
Winson Chung80baf5a2010-08-09 16:03:15 -07003713 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003714 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003715 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003716 @Override
3717 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3718 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003719
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003720 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3721 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003722 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003723 writer.println(prefix + " Homescreen " + i);
3724
3725 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3726 for (int j = 0; j < layout.getChildCount(); j++) {
3727 Object tag = layout.getChildAt(j).getTag();
3728 if (tag != null) {
3729 writer.println(prefix + " " + tag.toString());
3730 }
3731 }
3732 }
3733
3734 writer.println(prefix + " Hotseat");
3735 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003736 for (int j = 0; j < layout.getChildCount(); j++) {
3737 Object tag = layout.getChildAt(j).getTag();
3738 if (tag != null) {
3739 writer.println(prefix + " " + tag.toString());
3740 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003741 }
Sunny Goyala1365452015-10-01 15:46:24 -07003742
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003743 try {
3744 FileLog.flushAll(writer);
3745 } catch (Exception e) {
3746 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003747 }
3748 }
3749
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003750 writer.println(prefix + "Misc:");
3751 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3752 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3753 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3754
3755 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003756
Adam Cohen9211d422014-10-07 18:14:53 -07003757 if (mLauncherCallbacks != null) {
3758 mLauncherCallbacks.dump(prefix, fd, writer, args);
3759 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003760 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003761
Sunny Goyal66b24572016-09-21 15:57:55 -07003762 @Override
3763 @TargetApi(Build.VERSION_CODES.N)
3764 public void onProvideKeyboardShortcuts(
3765 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3766
3767 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3768 if (mState == State.WORKSPACE) {
3769 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3770 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3771 }
3772 View currentFocus = getCurrentFocus();
3773 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3774 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3775 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3776 }
Tony18c4aa42017-05-10 21:23:57 -05003777 if (currentFocus.getTag() instanceof ItemInfo
3778 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003779 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3780 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3781 }
3782 if (!shortcutInfos.isEmpty()) {
3783 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3784 }
3785
3786 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3787 }
3788
3789 @Override
3790 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3791 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3792 switch (keyCode) {
3793 case KeyEvent.KEYCODE_A:
3794 if (mState == State.WORKSPACE) {
Sunny Goyal5606e072017-06-15 14:24:21 -07003795 showAppsView(true, true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003796 return true;
3797 }
3798 break;
3799 case KeyEvent.KEYCODE_S: {
3800 View focusedView = getCurrentFocus();
3801 if (focusedView instanceof BubbleTextView
3802 && focusedView.getTag() instanceof ItemInfo
3803 && mAccessibilityDelegate.performAction(focusedView,
3804 (ItemInfo) focusedView.getTag(),
3805 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003806 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003807 return true;
3808 }
3809 break;
3810 }
3811 case KeyEvent.KEYCODE_O:
3812 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3813 return true;
3814 }
3815 break;
3816 }
3817 }
3818 return super.onKeyShortcut(keyCode, event);
3819 }
3820
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003821 public static Launcher getLauncher(Context context) {
3822 if (context instanceof Launcher) {
3823 return (Launcher) context;
3824 }
3825 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3826 }
3827
Sunny Goyal5a81c382017-03-20 15:08:06 -07003828 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003829
3830 @Override
3831 public void onSharedPreferenceChanged(
3832 SharedPreferences sharedPreferences, String key) {
3833 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003834 // Recreate the activity so that it initializes the rotation preference again.
3835 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003836 }
3837 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003839}