blob: 2945b22943ea976baf0da2a7f24d5bf3c9010c0e [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 Goyal326403e2017-10-02 12:45:10 -070019import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
20import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
21
Sunny Goyal28c6b962015-10-12 11:42:05 -070022import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070024import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070026import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070028import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000029import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070040import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070050import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070053import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020054import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.os.Handler;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070058import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070059import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070060import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.Selection;
62import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070063import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.HapticFeedbackConstants;
68import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070069import android.view.KeyboardShortcutGroup;
70import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080071import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080075import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.ViewGroup;
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;
Mario Bertschler27288382017-05-24 15:35:09 -0700101import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000102import com.android.launcher3.folder.Folder;
103import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700104import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700105import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700106import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700107import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800108import com.android.launcher3.model.ModelWriter;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700109import com.android.launcher3.model.PackageItemInfo;
110import com.android.launcher3.model.WidgetItem;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700112import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800116import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530117import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
118import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
119import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700120import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700122import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700123import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700124import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800125import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700126import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700127import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700128import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700129import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700132import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700134import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800135import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800137import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700138import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700139import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700140import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700141
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700142import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700143import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700144import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700145import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700146import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700147import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800148import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700149import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151/**
152 * Default launcher application.
153 */
Sunny Goyal27835952017-01-13 12:15:53 -0800154public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700155 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
156 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700157 AccessibilityManager.AccessibilityStateChangeListener,
158 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700159 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700160 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chunga2413752012-04-03 14:22:34 -0700162 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700165 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700166
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700168 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Michael Jurka8b805b12012-04-18 14:23:14 -0700170 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700171 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
172 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700173
Jon Mirandac476d6e2017-05-04 16:30:01 -0700174 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700175
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700176 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
177
Mathew Inwood876a8462013-06-14 14:12:41 +0100178 /**
179 * IntentStarter uses request codes starting with this. This must be greater than all activity
180 * request codes used internally.
181 */
182 protected static final int REQUEST_LAST = 100;
183
Mario Bertschleracbf5702017-03-03 10:58:06 -0800184 private static final int SOFT_INPUT_MODE_DEFAULT =
Jon Mirandafd516322017-03-09 13:14:50 -0800185 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800186 private static final int SOFT_INPUT_MODE_ALL_APPS =
Jon Mirandafd516322017-03-09 13:14:50 -0800187 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800188
Winson Chung2672ff92012-05-04 16:22:30 -0700189 // The Intent extra that defines whether to ignore the launch animation
190 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400191 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 // Type: int
194 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700195 // Type: int
196 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700197 // Type: PendingRequestArgs
198 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
199 // Type: ActivityResultInfo
200 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700202 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700204 /** The different states that Launcher can be in. */
Sunny Goyal53b99362017-10-05 14:58:56 -0700205 enum State { WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700207
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700210
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700211 private boolean mIsSafeModeEnabled;
212
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700217 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
219 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700225
226 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Sunny Goyalffe83f12014-08-14 17:39:34 -0700228 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700229 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700231 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700232
Sunny Goyal316490e2015-06-02 09:38:28 -0700233 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800234 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700235
Michael Jurka838a4ca2011-02-07 13:33:06 -0800236 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700237 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700238
Sunny Goyal47328fd2016-05-25 18:56:41 -0700239 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700240
241 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700242 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700243 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700244
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700245 // Main container view and the model for the widget tray screen.
246 @Thunk WidgetsContainerView mWidgetsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700247
Jon Miranda6bed3502017-09-19 14:43:17 -0700248 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
249 // that results in widgets being inflated in the wrong orientation.
250 private int mOrientation;
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700257 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700259 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700260 private OnResumeCallback mOnResumeCallback;
261
Sunny Goyal527c7d32015-08-28 15:19:36 -0700262 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700263
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800265 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800266 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700267 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700268 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700269 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270
Jon Miranda2d89ea82017-05-04 11:47:53 -0700271 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700272 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700273
Tony Wickham010d2552017-01-20 08:15:28 -0800274 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700275
Winson Chung400438b2011-01-16 17:53:48 -0800276 // Determines how long to wait after a rotation before restoring the screen orientation to
277 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700278 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800279
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700280 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700281
Winson Chung46353de2012-02-16 14:05:10 -0800282 // We only want to get the SharedPreferences once since it does an FS stat each time we get
283 // it from the context.
284 private SharedPreferences mSharedPrefs;
285
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700286 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
287 // state transition. If another state transition happened during this delay,
288 // simply unregister this runnable.
289 private Runnable mExitSpringLoadedModeRunnable;
290
Sunny Goyal2100c782016-08-22 16:00:03 -0700291 // Activity result which needs to be processed after workspace has loaded.
292 private ActivityResultInfo mPendingActivityResult;
293 /**
294 * Holds extra information required to handle a result from an external call, like
295 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
296 */
297 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800298
Jon Miranda379198e2016-09-23 08:54:40 -0700299 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700300
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700301 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700302 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400303
304 @Thunk void setOrientation() {
305 if (mRotationEnabled) {
306 unlockScreenOrientation(true);
307 } else {
308 setRequestedOrientation(
309 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700310 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400311 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700312
Sunny Goyal745bad92016-05-02 10:54:12 -0700313 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 @Override
316 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700317 if (DEBUG_STRICT_MODE) {
318 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
319 .detectDiskReads()
320 .detectDiskWrites()
321 .detectNetwork() // or .detectAll() for all detectable problems
322 .penaltyLog()
323 .build());
324 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
325 .detectLeakedSqlLiteObjects()
326 .detectLeakedClosableObjects()
327 .penaltyLog()
328 .penaltyDeath()
329 .build());
330 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700331 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700332
Adam Cohen9211d422014-10-07 18:14:53 -0700333 if (mLauncherCallbacks != null) {
334 mLauncherCallbacks.preOnCreate();
335 }
336
Mario Bertschler27288382017-05-24 15:35:09 -0700337 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
338 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700339 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700342 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400343
Sunny Goyal87f784c2017-01-11 10:48:34 -0800344 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700345
Adam Cohen2e6da152015-05-06 11:42:25 -0700346 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800347 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700348 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700349 Display display = getWindowManager().getDefaultDisplay();
350 Point mwSize = new Point();
351 display.getSize(mwSize);
352 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
353 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700354
Jon Miranda6bed3502017-09-19 14:43:17 -0700355 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700356 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700357 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800359 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800360 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700361 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700362
Joe Onorato41a12d22009-10-31 18:30:00 -0400363 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700364 mAllAppsController = new AllAppsTransitionController(this);
365 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700366
Sunny Goyalffe83f12014-08-14 17:39:34 -0700367 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700368
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700369 mAppWidgetHost = new LauncherAppWidgetHost(this);
370 if (Utilities.ATLEAST_MARSHMALLOW) {
371 mAppWidgetHost.addProviderChangeListener(this);
372 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700373 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700374
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700375 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
376 // this also ensures that any synchronous binding below doesn't re-trigger another
377 // LauncherModel load.
378 mPaused = false;
379
Sunny Goyal60820d72017-05-09 12:40:11 -0700380 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 setupViews();
Winson1f064272016-07-18 17:18:02 -0700383 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384
Tony Wickham010d2552017-01-20 08:15:28 -0800385 mPopupDataProvider = new PopupDataProvider(this);
386
Tony Wickhame2217252016-03-22 16:34:23 -0700387 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
388 .addAccessibilityStateChangeListener(this);
389
Sunny Goyalfe770c92016-09-27 14:38:58 -0700390 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391
Sunny Goyal2100c782016-08-22 16:00:03 -0700392 // We only load the page synchronously if the user rotates (or triggers a
393 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700394 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
395 if (savedInstanceState != null) {
396 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
397 }
398 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700399 // If we are not binding synchronously, show a fade in animation when
400 // the first page bind completes.
401 mDragLayer.setAlpha(0);
402 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700403 // Pages bound synchronously.
404 mWorkspace.setCurrentPage(currentScreen);
405
Sunny Goyal2100c782016-08-22 16:00:03 -0700406 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
409 // For handling default keys
410 mDefaultKeySsb = new SpannableStringBuilder();
411 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800412
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800413 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700414 // In case we are on a device with locked rotation, we should look at preferences to check
415 // if the user has specifically allowed rotation.
416 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700417 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700418 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
419 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700420 }
421
Sunny Goyal5a81c382017-03-20 15:08:06 -0700422 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
423 // Temporarily enable the rotation
424 mRotationEnabled = true;
425 }
426
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700427 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
428 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400429 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700430
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800431 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700432
Jon Miranda7fda2852017-07-19 16:07:01 -0700433 // Listen for broadcasts
434 IntentFilter filter = new IntentFilter();
435 filter.addAction(Intent.ACTION_SCREEN_OFF);
436 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
437 registerReceiver(mReceiver, filter);
438 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700439
Sunny Goyal8392c822017-06-20 10:03:56 -0700440 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
441 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700442
443 if (mLauncherCallbacks != null) {
444 mLauncherCallbacks.onCreate(savedInstanceState);
445 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700446
447 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700448 }
449
Sunny Goyal7779d622015-06-11 16:18:39 -0700450 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700451 public void onThemeChanged() {
452 recreate();
453 }
454
Mario Bertschlera6936942017-05-31 14:48:19 -0700455 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700456 if (isDark) {
457 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700458 } else if (supportsDarkText) {
459 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700460 }
461 }
462
463 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700464 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800465 return mLauncherView.findViewById(id);
466 }
467
468 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700469 public void onAppWidgetHostReset() {
470 if (mAppWidgetHost != null) {
471 mAppWidgetHost.startListening();
472 }
473 }
474
Adam Cohen9211d422014-10-07 18:14:53 -0700475 private LauncherCallbacks mLauncherCallbacks;
476
Winson1f064272016-07-18 17:18:02 -0700477 public void onInsetsChanged(Rect insets) {
478 mDeviceProfile.updateInsets(insets);
479 mDeviceProfile.layout(this, true /* notifyListeners */);
480 }
481
Sunny Goyal32554d12015-12-03 15:31:25 -0800482 /**
483 * Call this after onCreate to set or clear overlay.
484 */
485 public void setLauncherOverlay(LauncherOverlay overlay) {
486 if (overlay != null) {
487 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
488 }
489 mWorkspace.setLauncherOverlay(overlay);
490 }
491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
493 mLauncherCallbacks = callbacks;
494 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 }
496
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700497 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700498 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700499 if (mLauncherCallbacks != null) {
500 mLauncherCallbacks.onLauncherProviderChange();
501 }
502 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700503
Hyunyoung Song3f471442015-04-08 19:01:34 -0700504 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700505 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
506 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700507 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700508 }
509
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000510 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700511 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
512 // This cast is safe as long as the id < 0x00FFFFFF
513 // Since we jail all the dynamically generated views, there should be no clashes
514 // with any other views.
515 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000516 }
517
Tony Wickham010d2552017-01-20 08:15:28 -0800518 public PopupDataProvider getPopupDataProvider() {
519 return mPopupDataProvider;
520 }
521
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000522 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700523 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
524 * a configuration step, this allows the proper animations to run after other transitions.
525 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700526 private long completeAdd(
527 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
528 long screenId = info.screenId;
529 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700530 // When the screen id represents an actual screen (as opposed to a rank) we make sure
531 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700532 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700533 }
Adam Cohendb364c32014-05-20 14:23:40 -0700534
Sunny Goyal2100c782016-08-22 16:00:03 -0700535 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700537 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700538 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700540 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700541 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700542 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700544 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700545 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 int widgetId = appWidgetId;
547 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700548 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700549 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700550 // Since the view was just bound, also launch the configure activity if needed
551 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
552 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800553 if (provider != null) {
554 new WidgetAddFlowHandler(provider)
555 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700556 }
557 }
558 break;
559 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800560 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700561
Adam Cohendb364c32014-05-20 14:23:40 -0700562 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800563 }
564
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800565 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700567 if (isWorkspaceLoading()) {
568 // process the result once the workspace has loaded.
569 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
570 return;
571 }
572 mPendingActivityResult = null;
573
Winson Chunge341d302013-08-16 14:31:00 -0700574 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700575 final PendingRequestArgs requestArgs = mPendingRequestArgs;
576 setWaitingForResult(null);
577 if (requestArgs == null) {
578 return;
579 }
580
581 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700582
Adam Cohenad4e15c2013-10-17 16:21:35 -0700583 Runnable exitSpringLoaded = new Runnable() {
584 @Override
585 public void run() {
586 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
587 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
588 }
589 };
590
Michael Jurka8b805b12012-04-18 14:23:14 -0700591 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700592 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700593 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
595 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700596 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700597 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700598 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700599 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700600 addAppWidgetImpl(
601 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800602 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700603 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700604 }
605 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200606 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
607 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700608 // User could have free-scrolled between pages before picking a wallpaper; make sure
609 // we move to the closest one now.
610 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700611 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200612 }
613 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700614 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200615
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700617 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700618
Adam Cohened66b2b2012-01-23 17:28:51 -0800619 // We have special handling for widgets
620 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800621 final int appWidgetId;
622 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
623 : -1;
624 if (widgetId < 0) {
625 appWidgetId = pendingAddWidgetId;
626 } else {
627 appWidgetId = widgetId;
628 }
629
Adam Cohenad4e15c2013-10-17 16:21:35 -0700630 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800631 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700632 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
633 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700634 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700635 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700636 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700637 @Override
638 public void run() {
639 exitSpringLoadedDragModeDelayed(false, 0, null);
640 }
641 };
Adam Cohendb364c32014-05-20 14:23:40 -0700642
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
644 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
645 } else {
646 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
647 // When the screen id represents an actual screen (as opposed to a rank)
648 // we make sure that the drop page actually exists.
649 requestArgs.screenId =
650 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700651 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700652 final CellLayout dropLayout =
653 mWorkspace.getScreenWithId(requestArgs.screenId);
654
655 dropLayout.setDropPending(true);
656 final Runnable onComplete = new Runnable() {
657 @Override
658 public void run() {
659 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
660 dropLayout.setDropPending(false);
661 }
662 };
663 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
664 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 return;
667 }
668
Sunny Goyald478c832016-04-01 12:04:16 -0700669 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
670 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700671 if (resultCode == RESULT_OK) {
672 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700674 }
675 // Leave the widget in the pending state if the user canceled the configure.
676 return;
677 }
678
Sunny Goyalaad90582015-07-09 14:22:50 -0700679 if (requestCode == REQUEST_CREATE_SHORTCUT) {
680 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
682 completeAdd(requestCode, data, -1, requestArgs);
683 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
684 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
685
Sunny Goyalaad90582015-07-09 14:22:50 -0700686 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700687 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
688 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800692 }
693
694 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700695 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800696 final int requestCode, final int resultCode, final Intent data) {
697 handleActivityResult(requestCode, resultCode, data);
698 if (mLauncherCallbacks != null) {
699 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
700 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800701 }
702
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700703 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700704 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600705 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700706 PendingRequestArgs pendingArgs = mPendingRequestArgs;
707 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
708 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
709 setWaitingForResult(null);
710
Sunny Goyal28c6b962015-10-12 11:42:05 -0700711 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700712 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700713 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700714 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700715 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700716 Intent intent = pendingArgs.getPendingIntent();
717
Sunny Goyal28c6b962015-10-12 11:42:05 -0700718 if (grantResults.length > 0
719 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700720 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700721 } else {
722 // TODO: Show a snack bar with link to settings
723 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700724 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700725 }
726 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600727 if (mLauncherCallbacks != null) {
728 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
729 grantResults);
730 }
731 }
732
Adam Cohendb364c32014-05-20 14:23:40 -0700733 /**
734 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
735 *
736 * @param screenId the screen id to check
737 * @return the new screen, or screenId if it exists
738 */
739 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800740 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700741 if (dropLayout == null) {
742 // it's possible that the add screen was removed because it was
743 // empty and a re-bind occurred
744 mWorkspace.addExtraEmptyScreen();
745 return mWorkspace.commitExtraEmptyScreen();
746 } else {
747 return screenId;
748 }
749 }
750
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 @Thunk void completeTwoStageWidgetDrop(
752 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
753 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 Runnable onCompleteRunnable = null;
755 int animationType = 0;
756
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700757 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 if (resultCode == RESULT_OK) {
759 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
760 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800761 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700762 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 onCompleteRunnable = new Runnable() {
764 @Override
765 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700766 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
768 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800769 }
770 };
771 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800772 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700775 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700776 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700777 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
778 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700780 // The animated view may be null in the case of a rotation during widget configuration
781 onCompleteRunnable.run();
782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784
785 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700786 protected void onStop() {
787 super.onStop();
788 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700789
790 if (mLauncherCallbacks != null) {
791 mLauncherCallbacks.onStop();
792 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700793
Sunny Goyalf5e37442016-11-02 10:31:24 -0700794 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700795 mAppWidgetHost.stopListening();
796 }
Tony Wickham010d2552017-01-20 08:15:28 -0800797
798 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700799 }
800
801 @Override
802 protected void onStart() {
803 super.onStart();
804 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700805
806 if (mLauncherCallbacks != null) {
807 mLauncherCallbacks.onStart();
808 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700809
Sunny Goyalf5e37442016-11-02 10:31:24 -0700810 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700811 mAppWidgetHost.startListening();
812 }
Tony Wickham010d2552017-01-20 08:15:28 -0800813
814 if (!isWorkspaceLoading()) {
815 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
816 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700817
Jon Miranda7fda2852017-07-19 16:07:01 -0700818 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700819 if (mScrimAnimator != null) {
820 mScrimAnimator.cancel();
821 }
822 mDragLayer.getBackground().setAlpha(0);
823 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
824 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
825 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
826 @Override
827 public void onAnimationEnd(Animator animation) {
828 mScrimAnimator = null;
829 }
830 });
831 mScrimAnimator.setDuration(600);
832 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
833 mScrimAnimator.start();
834 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700835 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700836 }
837
838 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700840 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700842 TraceHelper.partitionSection("ON_RESUME", "superCall");
843
Hyunyoung Songaa953652016-04-19 18:30:24 -0700844 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800845 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700846 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700847 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700848 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700849 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700851 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200852 // We might have postponed some bind calls until onResume (see waitUntilResume) --
853 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700854 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
855 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200856 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700857 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200858 }
Winson Chunge4e50662012-01-23 14:45:13 -0800859
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700860 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700861 // Process any items that were added while Launcher was away.
862 InstallShortcutReceiver.disableAndFlushInstallQueue(
863 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700864
Sunny Goyala474a9b2017-05-04 16:47:11 -0700865 // Refresh shortcuts if the permission changed.
866 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700867
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700868 if (shouldShowDiscoveryBounce()) {
869 mAllAppsController.showDiscoveryBounce();
870 }
Adam Cohen9211d422014-10-07 18:14:53 -0700871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onResume();
873 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800874
Sunny Goyala502aa32017-10-02 16:04:06 -0700875 clearTypedText();
876
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700877 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700878 }
879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700881 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700882 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700883 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700884
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700885 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700886 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800887 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700888 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700889
Adam Cohen9211d422014-10-07 18:14:53 -0700890 if (mLauncherCallbacks != null) {
891 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700892 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700893 }
894
Adam Cohenc2d6e892014-10-16 09:49:24 -0700895 public interface LauncherOverlay {
896
897 /**
898 * Touch interaction leading to overscroll has begun
899 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700900 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700901
902 /**
903 * Touch interaction related to overscroll has ended
904 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700905 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700906
907 /**
908 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
909 * screen (or in the case of RTL, the rightmost screen).
910 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700911 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700912
913 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800914 * Called when the launcher is ready to use the overlay
915 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700916 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700917 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700918 }
919
920 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700921 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700922 }
923
924 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
925
Sunny Goyalc86df472016-02-25 09:19:38 -0800926 public void onScrollChanged(float progress) {
927 if (mWorkspace != null) {
928 mWorkspace.onOverlayScrollChanged(progress);
929 }
930 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700931 }
932
Jorim Jaggid017f882014-01-14 17:08:48 -0800933 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700934 if (mLauncherCallbacks != null) {
935 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700936 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800937 // On O and above we there is always some setting present settings (add icon to
938 // home screen or icon badging). On earlier APIs we will have the allow rotation
939 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700940 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700941 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800942 }
943
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700944 @Override
945 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700947 if (mModel.isCurrentCallbacks(this)) {
948 mModel.stopLoader();
949 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700950 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
951
Romain Guy13c2e7b2010-03-10 19:45:00 -0800952 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700953 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700954
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800955 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800956 @Override
957 public void onWindowFocusChanged(boolean hasFocus) {
958 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700959 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700960
961 if (mLauncherCallbacks != null) {
962 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
963 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800964 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 private boolean acceptFilter() {
967 final InputMethodManager inputManager = (InputMethodManager)
968 getSystemService(Context.INPUT_METHOD_SERVICE);
969 return !inputManager.isFullscreenMode();
970 }
971
972 @Override
973 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700974 final int uniChar = event.getUnicodeChar();
975 final boolean handled = super.onKeyDown(keyCode, event);
976 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
977 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
979 keyCode, event);
980 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700981 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700982 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700983 // showSearchDialog()
984 // If there are multiple keystrokes before the search dialog takes focus,
985 // onSearchRequested() will be called for every keystroke,
986 // but it is idempotent, so it's fine.
987 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 }
989 }
990
Joe Onorato8a9625e2010-01-28 15:55:35 -0800991 // Eat the long press event so the keyboard doesn't come up.
992 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
993 return true;
994 }
995
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 return handled;
997 }
998
Winson46163472015-09-22 17:31:56 -0700999 @Override
1000 public boolean onKeyUp(int keyCode, KeyEvent event) {
1001 if (keyCode == KeyEvent.KEYCODE_MENU) {
1002 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001003 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001004 // Close any open floating view
1005 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001006
Winson46163472015-09-22 17:31:56 -07001007 // Show the overview mode if we are on the workspace
1008 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1009 !mWorkspace.isSwitchingState()) {
1010 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001011 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001012 }
1013 }
1014 return true;
1015 }
1016 return super.onKeyUp(keyCode, event);
1017 }
1018
Karl Rosaen138a0412009-04-23 19:00:21 -07001019 private String getTypedText() {
1020 return mDefaultKeySsb.toString();
1021 }
1022
Sunny Goyal6f28e712016-09-13 14:19:29 -07001023 @Override
1024 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001025 mDefaultKeySsb.clear();
1026 mDefaultKeySsb.clearSpans();
1027 Selection.setSelection(mDefaultKeySsb, 0);
1028 }
1029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 /**
1031 * Restores the previous state, if it exists.
1032 *
1033 * @param savedState The previous state.
1034 */
1035 private void restoreState(Bundle savedState) {
1036 if (savedState == null) {
1037 return;
1038 }
1039
Sunny Goyalfe770c92016-09-27 14:38:58 -07001040 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1041 State[] stateValues = State.values();
1042 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1043 ? stateValues[stateOrdinal] : State.WORKSPACE;
Sunny Goyal53b99362017-10-05 14:58:56 -07001044 if (state == State.APPS) {
1045 showAppsView(false /* animated */);
1046 } else if (state == State.WIDGETS) {
1047 showWidgetsView(false, false);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001048 }
1049
Sunny Goyal2100c782016-08-22 16:00:03 -07001050 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1051 if (requestArgs != null) {
1052 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001054
1055 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057
1058 /**
1059 * Finds all the views we need and configure them properly.
1060 */
1061 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001062 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001063 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001064 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001065 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001066
Sunny Goyal784f9c32016-03-23 16:56:54 -07001067 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1068 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1069 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070
Winson Chung4c98d922011-05-31 16:50:48 -07001071 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001072 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001073
Winson Chung3d503fb2011-07-13 17:25:49 -07001074 // Setup the hotseat
1075 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1076 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001077 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001078 }
1079
Winsone9f27272015-10-13 10:47:51 -07001080 // Setup the overview panel
1081 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001082
Winson Chung4c98d922011-05-31 16:50:48 -07001083 // Setup the workspace
1084 mWorkspace.setHapticFeedbackEnabled(false);
1085 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001086 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001087 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1088 // default state, otherwise we will update to the wrong offsets in RTL
1089 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001090 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001091 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001092
Tony Wickham34d2c912015-09-11 09:27:58 -07001093 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001094 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001095
Winson Chungef7f8742015-06-04 17:18:17 -07001096 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001097 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001098 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001099
Winson Chung3d503fb2011-07-13 17:25:49 -07001100 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001101 mDragController.setMoveTarget(mWorkspace);
1102 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001103 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001104
Hyunyoung Songd725f642017-08-17 22:26:35 -07001105 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001106
Sunny Goyal322d5562015-06-25 19:35:49 -07001107 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1108 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
1111
Winsone9f27272015-10-13 10:47:51 -07001112 private void setupOverviewPanel() {
1113 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1114
Winsone9f27272015-10-13 10:47:51 -07001115 // Bind wallpaper button actions
1116 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301117 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001118 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001119 public void handleViewClick(View view) {
1120 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001121 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001122 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001123
1124 // Bind widget button actions
1125 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301126 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001127 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001128 public void handleViewClick(View view) {
1129 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001130 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001131 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001132
1133 // Bind settings actions
1134 View settingsButton = findViewById(R.id.settings_button);
1135 boolean hasSettings = hasSettings();
1136 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301137 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001138 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001139 public void handleViewClick(View view) {
1140 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001141 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001142 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001143 } else {
1144 settingsButton.setVisibility(View.GONE);
1145 }
1146
1147 mOverviewPanel.setAlpha(0f);
1148 }
1149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001151 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001152 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001153 */
1154 public void setAllAppsButton(View allAppsButton) {
1155 mAllAppsButton = allAppsButton;
1156 }
1157
Sunny Goyalbb011da2016-06-15 15:42:29 -07001158 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001159 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001160 }
1161
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001162 public View getWidgetsButton() {
1163 return mWidgetsButton;
1164 }
1165
Anjali Koppal5ad44842014-03-10 20:34:39 -07001166 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 * Creates a view representing a shortcut.
1168 *
1169 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001171 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001172 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 }
1174
1175 /**
1176 * Creates a view representing a shortcut inflated from the specified resource.
1177 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 * @param parent The group the shortcut belongs to.
1179 * @param info The data structure describing the shortcut.
1180 *
1181 * @return A View inflated from layoutResId.
1182 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001183 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001184 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1185 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001186 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001188 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 return favorite;
1190 }
1191
1192 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001193 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 *
1195 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001197 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001198 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001199 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1200 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001201 return;
1202 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001203
Jon Mirandac476d6e2017-05-04 16:30:01 -07001204 int[] cellXY = mTmpAddItemCellCoordinates;
1205 CellLayout layout = getCellLayout(container, screenId);
1206
Sunny Goyal782f0c92017-01-19 10:27:54 -08001207 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001208 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001209 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1210 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001211 }
1212
1213 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001214 // Legacy shortcuts are only supported for primary profile.
1215 info = Process.myUserHandle().equals(args.user)
1216 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001217
Sunny Goyalb57645f2017-03-20 13:57:28 -07001218 if (info == null) {
1219 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1220 return;
1221 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001222 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001223 // The app is trying to add a shortcut without sufficient permissions
1224 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1225 return;
1226 }
1227 }
1228
Jon Mirandac476d6e2017-05-04 16:30:01 -07001229 if (container < 0) {
1230 // Adding a shortcut to the Workspace.
1231 final View view = createShortcut(info);
1232 boolean foundCellSpan = false;
1233 // First we check if we already know the exact location where we want to add this item.
1234 if (cellX >= 0 && cellY >= 0) {
1235 cellXY[0] = cellX;
1236 cellXY[1] = cellY;
1237 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001238
Jon Mirandac476d6e2017-05-04 16:30:01 -07001239 // If appropriate, either create a folder or add to an existing folder
1240 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1241 true, null, null)) {
1242 return;
1243 }
1244 DragObject dragObject = new DragObject();
1245 dragObject.dragInfo = info;
1246 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1247 true)) {
1248 return;
1249 }
1250 } else {
1251 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1252 }
1253
1254 if (!foundCellSpan) {
1255 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001256 return;
1257 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001258
1259 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1260 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001261 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001262 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001263 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001264 if (folderIcon != null) {
1265 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1266 folderInfo.add(info, args.rank, false);
1267 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001268 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001269 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
Sunny Goyale29897f2017-07-20 10:09:42 -07001273 public FolderIcon findFolderIcon(final long folderIconId) {
1274 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1275 @Override
1276 public boolean evaluate(ItemInfo info, View view) {
1277 return info != null && info.id == folderIconId;
1278 }
1279 });
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001283 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001285 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001287 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001288 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1289
Adam Cohened66b2b2012-01-23 17:28:51 -08001290 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001291 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001292 }
Romain Guycbb89e42009-06-08 15:52:54 -07001293
Adam Cohen59400422014-03-05 18:07:04 -08001294 LauncherAppWidgetInfo launcherInfo;
1295 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001296 launcherInfo.spanX = itemInfo.spanX;
1297 launcherInfo.spanY = itemInfo.spanY;
1298 launcherInfo.minSpanX = itemInfo.minSpanX;
1299 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001300 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001301
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001302 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001303 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304
Sunny Goyal2100c782016-08-22 16:00:03 -07001305 if (hostView == null) {
1306 // Perform actual inflation because we're live
1307 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001309 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301310 prepareAppWidget(hostView, launcherInfo);
1311 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
Romain Guycbb89e42009-06-08 15:52:54 -07001313
Sunny Goyald5462aa2016-11-22 16:52:39 +05301314 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001315 hostView.setTag(item);
1316 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001317 hostView.setFocusable(true);
1318 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001319 }
1320
Adam Cohended9f8d2010-11-03 13:25:16 -07001321 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1322 @Override
1323 public void onReceive(Context context, Intent intent) {
1324 final String action = intent.getAction();
1325 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001326 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001327 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001328 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001329 if (!showWorkspace(false)) {
1330 // If we are already on the workspace, then manually reset all apps
1331 mAppsView.reset();
1332 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001333 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001334 mShouldFadeInScrim = true;
1335 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1336 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1337 // the user unlocked and the Launcher is not in the foreground.
1338 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001339 }
1340 }
1341 };
1342
Tony Wickham010d2552017-01-20 08:15:28 -08001343 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1344 Runnable r = new Runnable() {
1345 @Override
1346 public void run() {
1347 mWorkspace.updateIconBadges(updatedBadges);
1348 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001349
1350 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1351 if (popup != null) {
1352 popup.updateNotificationHeader(updatedBadges);
1353 }
Tony Wickham010d2552017-01-20 08:15:28 -08001354 }
1355 };
1356 if (!waitUntilResume(r)) {
1357 r.run();
1358 }
1359 }
1360
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 @Override
1362 public void onAttachedToWindow() {
1363 super.onAttachedToWindow();
1364
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001365 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001366 if (mLauncherCallbacks != null) {
1367 mLauncherCallbacks.onAttachedToWindow();
1368 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001369 }
1370
1371 @Override
1372 public void onDetachedFromWindow() {
1373 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001374
1375 if (mLauncherCallbacks != null) {
1376 mLauncherCallbacks.onDetachedFromWindow();
1377 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001378 }
1379
Winson Chunga6945242014-01-08 14:04:34 -08001380 public DragLayer getDragLayer() {
1381 return mDragLayer;
1382 }
1383
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001384 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001385 return mAppsView;
1386 }
1387
Hyunyoung Song3f471442015-04-08 19:01:34 -07001388 public WidgetsContainerView getWidgetsView() {
1389 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001390 }
1391
Winson Chunga6945242014-01-08 14:04:34 -08001392 public Workspace getWorkspace() {
1393 return mWorkspace;
1394 }
1395
1396 public Hotseat getHotseat() {
1397 return mHotseat;
1398 }
1399
Jorim Jaggid017f882014-01-14 17:08:48 -08001400 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001401 return mOverviewPanel;
1402 }
1403
Sunny Goyal47328fd2016-05-25 18:56:41 -07001404 public DropTargetBar getDropTargetBar() {
1405 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001406 }
1407
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001408 public LauncherAppWidgetHost getAppWidgetHost() {
1409 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Winson Chunga9abd0e2010-10-27 17:18:37 -07001412 public LauncherModel getModel() {
1413 return mModel;
1414 }
1415
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001416 public ModelWriter getModelWriter() {
1417 return mModelWriter;
1418 }
1419
Adam Cohen79d90c52016-04-22 13:29:20 -07001420 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001421 return mSharedPrefs;
1422 }
1423
Jon Miranda6bed3502017-09-19 14:43:17 -07001424 public int getOrientation() { return mOrientation; }
1425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 @Override
1427 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001428 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 super.onNewIntent(intent);
1430
Winson15f8b172015-08-19 11:02:39 -07001431 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1432 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1433 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001434
1435 // Check this condition before handling isActionMain, as this will get reset.
1436 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001437 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001438
Winson15f8b172015-08-19 11:02:39 -07001439 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1440 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001441 if (mWorkspace == null) {
1442 // Can be cases where mWorkspace is null, this prevents a NPE
1443 return;
1444 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001445
1446 // Note: There should be at most one log per method call. This is enforced implicitly
1447 // by using if-else statements.
1448 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001449 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001450 if (topOpenView != null) {
1451 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001452 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301453 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001454 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1455 }
1456
1457 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001458 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal53b99362017-10-05 14:58:56 -07001459 showWorkspace(alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001460
1461 final View v = getWindow().peekDecorView();
1462 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001463 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001464 }
1465
Winson Chungb745afb2015-03-02 11:51:23 -08001466 // Reset the apps view
1467 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001468 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001469 }
1470
Hyunyoung Song3f471442015-04-08 19:01:34 -07001471 // Reset the widgets view
1472 if (!alreadyOnHome && mWidgetsView != null) {
1473 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001474 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001475
Adam Cohen9211d422014-10-07 18:14:53 -07001476 if (mLauncherCallbacks != null) {
1477 mLauncherCallbacks.onHomeIntent();
1478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001480 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001481
Adam Cohen9211d422014-10-07 18:14:53 -07001482 if (mLauncherCallbacks != null) {
1483 mLauncherCallbacks.onNewIntent(intent);
1484 }
Winson15f8b172015-08-19 11:02:39 -07001485
1486 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1487 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1488 // animation.
1489 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001490 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001491
Winson15f8b172015-08-19 11:02:39 -07001492 mWorkspace.post(new Runnable() {
1493 @Override
1494 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001495 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001496 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001497 }
Winson15f8b172015-08-19 11:02:39 -07001498 }
1499 });
1500 }
1501 }
1502
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001503 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001504 }
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001507 public void onRestoreInstanceState(Bundle state) {
1508 super.onRestoreInstanceState(state);
1509 for (int page: mSynchronouslyBoundPages) {
1510 mWorkspace.restoreInstanceStateForChild(page);
1511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
1513
1514 @Override
1515 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001516 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001517 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001518
Adam Cohen21cd0022013-10-09 18:57:02 -07001519 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001520 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521
Michael Jurka883f55b2010-10-21 15:47:14 -07001522 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001523 // We close any open folders and shortcut containers since they will not be re-opened,
1524 // and we need to make sure this state is reflected.
1525 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001526
Sunny Goyal2100c782016-08-22 16:00:03 -07001527 if (mPendingRequestArgs != null) {
1528 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1529 }
1530 if (mPendingActivityResult != null) {
1531 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533
Adam Cohen9211d422014-10-07 18:14:53 -07001534 if (mLauncherCallbacks != null) {
1535 mLauncherCallbacks.onSaveInstanceState(outState);
1536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538
1539 @Override
1540 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001542
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001543 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001544 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001545
Winson Chungcd2b0142011-06-08 16:02:26 -07001546 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001547 // It's possible to receive onDestroy after a new Launcher activity has
1548 // been created. In this case, don't interfere with the new Launcher.
1549 if (mModel.isCurrentCallbacks(this)) {
1550 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001551 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001552 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001553
Sunny Goyal745bad92016-05-02 10:54:12 -07001554 if (mRotationPrefChangeHandler != null) {
1555 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1556 }
1557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001559 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001561 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001563 mAppWidgetHost = null;
1564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 TextKeyListener.getInstance().release();
1566
Tony Wickhame2217252016-03-22 16:34:23 -07001567 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1568 .removeAccessibilityStateChangeListener(this);
1569
Mario Bertschler27288382017-05-24 15:35:09 -07001570 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1571
Michael Jurka2ecf9952012-06-18 12:52:28 -07001572 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001573
Tony2917a8b2017-07-31 23:29:42 -07001574 clearPendingBinds();
1575
Adam Cohen9211d422014-10-07 18:14:53 -07001576 if (mLauncherCallbacks != null) {
1577 mLauncherCallbacks.onDestroy();
1578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
1580
Sunny Goyalae502842016-06-17 08:43:56 -07001581 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1582 return mAccessibilityDelegate;
1583 }
1584
Adam Cohena9cf38f2011-05-02 15:36:58 -07001585 public DragController getDragController() {
1586 return mDragController;
1587 }
1588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001590 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001591 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001592 }
1593
1594 @Override
1595 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1596 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001597 try {
1598 super.startIntentSenderForResult(intent, requestCode,
1599 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1600 } catch (IntentSender.SendIntentException e) {
1601 throw new ActivityNotFoundException();
1602 }
1603 }
1604
Winson Chung70d72102011-08-12 11:24:35 -07001605 /**
1606 * Indicates that we want global search for this activity by setting the globalSearch
1607 * argument for {@link #startSearch} to true.
1608 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001610 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001612
Karl Rosaen138a0412009-04-23 19:00:21 -07001613 if (initialQuery == null) {
1614 // Use any text typed in the launcher as the initial query
1615 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 if (appSearchData == null) {
1618 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001619 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001621
Sunny Goyal6f28e712016-09-13 14:19:29 -07001622 if (mLauncherCallbacks == null ||
1623 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1624 // Starting search from the callbacks failed. Start the default global search.
1625 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001626 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001627
1628 // We need to show the workspace after starting the search
1629 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001630 }
1631
Ian Parkinson047036e2014-09-01 15:40:53 +01001632 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001633 * Starts the global search activity. This code is a copied from SearchManager
1634 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001635 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001636 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001637 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001638 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1639 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1640 if (globalSearchActivity == null) {
1641 Log.w(TAG, "No global search activity found.");
1642 return;
1643 }
1644 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1645 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1646 intent.setComponent(globalSearchActivity);
1647 // Make sure that we have a Bundle to put source in
1648 if (appSearchData == null) {
1649 appSearchData = new Bundle();
1650 } else {
1651 appSearchData = new Bundle(appSearchData);
1652 }
Adam Cohen9211d422014-10-07 18:14:53 -07001653 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001654 if (!appSearchData.containsKey("source")) {
1655 appSearchData.putString("source", getPackageName());
1656 }
1657 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1658 if (!TextUtils.isEmpty(initialQuery)) {
1659 intent.putExtra(SearchManager.QUERY, initialQuery);
1660 }
1661 if (selectInitialQuery) {
1662 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1663 }
1664 intent.setSourceBounds(sourceBounds);
1665 try {
1666 startActivity(intent);
1667 } catch (ActivityNotFoundException ex) {
1668 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
1671
Winson Chung70d72102011-08-12 11:24:35 -07001672 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001673 public boolean onPrepareOptionsMenu(Menu menu) {
1674 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001675 if (mLauncherCallbacks != null) {
1676 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1677 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001678 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001681 @Override
1682 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001683 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001684 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001685 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001686 }
1687
Joe Onorato9c1289c2009-08-17 11:03:03 -04001688 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001689 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 }
1691
Adam Cohen517a7f52014-03-01 12:12:59 -08001692 public boolean isWorkspaceLoading() {
1693 return mWorkspaceLoading;
1694 }
1695
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001696 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001697 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001698 }
1699
Sunny Goyal04a324a2017-01-20 21:08:59 -08001700 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001701 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001702 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001703
Sunny Goyal2100c782016-08-22 16:00:03 -07001704 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001705 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001706 if (LOGD) {
1707 Log.d(TAG, "Adding widget from drop");
1708 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001709 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001710 }
1711
Sunny Goyal2100c782016-08-22 16:00:03 -07001712 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001713 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1714 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1715 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001716
Adam Cohenad4e15c2013-10-17 16:21:35 -07001717 Runnable onComplete = new Runnable() {
1718 @Override
1719 public void run() {
1720 // Exit spring loaded mode if necessary after adding the widget
1721 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1722 null);
1723 }
1724 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001725 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001726 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 }
1728 }
Romain Guycbb89e42009-06-08 15:52:54 -07001729
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001730 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1731 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001732 info.container = container;
1733 info.screenId = screenId;
1734 if (cell != null) {
1735 info.cellX = cell[0];
1736 info.cellY = cell[1];
1737 }
1738 info.spanX = spanX;
1739 info.spanY = spanY;
1740
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001741 switch (info.itemType) {
1742 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1743 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001744 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001745 break;
1746 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001747 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001748 break;
1749 default:
1750 throw new IllegalStateException("Unknown item type: " + info.itemType);
1751 }
1752 }
1753
Adam Cohenfbba09b2011-07-18 21:43:05 -07001754 /**
1755 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001756 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001757 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001758 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1759 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001760 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1761 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1762 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763 }
1764
Adam Cohenfbba09b2011-07-18 21:43:05 -07001765 /**
1766 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001767 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001768 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001769 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001770 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001771 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001772 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001773 // In the case where we've prebound the widget, we remove it from the DragLayer
1774 if (LOGD) {
1775 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1776 }
1777 getDragLayer().removeView(hostView);
1778
Adam Cohened66b2b2012-01-23 17:28:51 -08001779 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001780 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001781
1782 // Clear the boundWidget so that it doesn't get destroyed.
1783 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001784 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001785 // In this case, we either need to start an activity to get permission to bind
1786 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001787 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1788 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1789 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1790 this, info.componentName);
1791 } else {
1792 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1793 }
Adam Cohendd70d662012-10-04 16:53:44 -07001794 Bundle options = info.bindOptions;
1795
Sunny Goyalffe83f12014-08-14 17:39:34 -07001796 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1797 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001798 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001799 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001800 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001801 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001802 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001803 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001804 }
1805
Adam Cohendcd297f2013-06-18 13:13:40 -07001806 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001807 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001808 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 folderInfo.title = getText(R.string.folder_name);
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001812 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813
1814 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001815 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301816 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001817 // Force measure the new folder icon
1818 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1819 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001820 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
Romain Guycbb89e42009-06-08 15:52:54 -07001822
Winsonc0b52fe2015-09-09 16:38:15 -07001823 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001824 * Unbinds the view for the specified item, and removes the item and all its children.
1825 *
1826 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001827 * @param itemInfo the {@link ItemInfo} for this view.
1828 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001829 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001830 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001831 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001832 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001833 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1834 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001835 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001836 } else {
1837 mWorkspace.removeWorkspaceItem(v);
1838 }
Winsonc0b52fe2015-09-09 16:38:15 -07001839 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001840 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001841 }
1842 } else if (itemInfo instanceof FolderInfo) {
1843 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001844 if (v instanceof FolderIcon) {
1845 ((FolderIcon) v).removeListeners();
1846 }
Winsonc0b52fe2015-09-09 16:38:15 -07001847 mWorkspace.removeWorkspaceItem(v);
1848 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001849 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001850 }
1851 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1852 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001853 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001854 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001855 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001856 }
1857 } else {
1858 return false;
1859 }
1860 return true;
1861 }
1862
1863 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001864 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001865 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001866 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001867 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001868 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001869 // Deleting an app widget ID is a void call but writes to disk before returning
1870 // to the caller...
1871 new AsyncTask<Void, Void, Void>() {
1872 public Void doInBackground(Void ... args) {
1873 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1874 return null;
1875 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001876 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001877 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001878 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001879 }
1880
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 @Override
1882 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001883 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 }
1885
Joe Onorato88ec0992009-11-19 13:16:06 -08001886 @Override
1887 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001888 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1889 return;
1890 }
1891
Sunny Goyal45478022015-06-08 16:52:41 -07001892 if (mDragController.isDragging()) {
1893 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001894 return;
1895 }
1896
Jon Mirandafeba90f2016-10-06 10:53:29 -07001897 // Note: There should be at most one log per method call. This is enforced implicitly
1898 // by using if-else statements.
1899 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001900 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1901 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001902 topView.onBackPressed();
Tony Wickham49c8d292016-07-01 11:44:34 -07001903 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301904 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001905 showWorkspace(true);
1906 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301907 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08001908 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001909 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301910 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001911 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001912 } else {
1913 // Back button is a no-op here, but give at least some feedback for the button press
1914 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001915 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001916 }
1917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 * Launches the intent referred by the clicked shortcut.
1920 *
1921 * @param v The view representing the clicked shortcut.
1922 */
1923 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001924 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1925 // view has detached (it's possible for this to happen if the view is removed mid touch).
1926 if (v.getWindowToken() == null) {
1927 return;
1928 }
1929
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001930 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001931 return;
1932 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001933
Adam Cohen1697b792013-09-17 19:08:21 -07001934 if (v instanceof Workspace) {
1935 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001936 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1937 LauncherLogProto.Action.Direction.NONE,
1938 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07001939 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001940 }
1941 return;
1942 }
1943
1944 if (v instanceof CellLayout) {
1945 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001946 int page = mWorkspace.indexOfChild(v);
1947 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1948 LauncherLogProto.Action.Direction.NONE,
1949 LauncherLogProto.ContainerType.OVERVIEW, page);
1950 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001951 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001952 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001953 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001954 }
1955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001957 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001958 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001960 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001961 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001962 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001963 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001964 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001965 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001966 } else if (tag instanceof AppInfo) {
1967 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001968 } else if (tag instanceof LauncherAppWidgetInfo) {
1969 if (v instanceof PendingAppWidgetHostView) {
1970 onClickPendingWidget((PendingAppWidgetHostView) v);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973 }
1974
Sunny Goyal70660032015-05-14 00:07:08 -07001975 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001976 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001977 return false;
1978 }
1979
Michael Jurkaaf442092010-06-10 17:01:57 -07001980 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001981 * Event handler for the app widget view which has not fully restored.
1982 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001983 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001984 if (mIsSafeModeEnabled) {
1985 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1986 return;
1987 }
1988
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001989 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001990 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001991 LauncherAppWidgetProviderInfo appWidgetInfo =
1992 mAppWidgetManager.findProvider(info.providerName, info.user);
1993 if (appWidgetInfo == null) {
1994 return;
1995 }
1996 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1997
Sunny Goyald478c832016-04-01 12:04:16 -07001998 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1999 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2000 // This should not happen, as we make sure that an Id is allocated during bind.
2001 return;
2002 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002003 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2004 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002005 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002006 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002007 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002008 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002009 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002010 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002011 }
2012 }
2013
2014 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002015 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2016 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2017 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002018 *
2019 * @param v The view that was clicked.
2020 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002021 protected void onClickAllAppsButton(View v) {
2022 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002023 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302024 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2025 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002026 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002027 } else {
2028 showWorkspace(true);
2029 }
2030 }
2031
Sunny Goyale6e72002017-01-12 16:55:36 -08002032 private void onClickPendingAppItem(final View v, final String packageName,
2033 boolean downloadStarted) {
2034 if (downloadStarted) {
2035 // If the download has started, simply direct to the market app.
2036 startMarketIntentForPackage(v, packageName);
2037 return;
2038 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002039 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002040 .setTitle(R.string.abandoned_promises_title)
2041 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002042 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2043 @Override
2044 public void onClick(DialogInterface dialogInterface, int i) {
2045 startMarketIntentForPackage(v, packageName);
2046 }
2047 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002048 .setNeutralButton(R.string.abandoned_clean_this,
2049 new DialogInterface.OnClickListener() {
2050 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002051 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002052 mWorkspace.removeAbandonedPromise(packageName, user);
2053 }
2054 })
2055 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002056 }
2057
2058 private void startMarketIntentForPackage(View v, String packageName) {
2059 ItemInfo item = (ItemInfo) v.getTag();
2060 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002061 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002062 }
2063
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002064 /**
2065 * Event handler for an app shortcut click.
2066 *
2067 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2068 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002069 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002070 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2071 Object tag = v.getTag();
2072 if (!(tag instanceof ShortcutInfo)) {
2073 throw new IllegalArgumentException("Input must be a Shortcut");
2074 }
2075
2076 // Open shortcut
2077 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002078
2079 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002080 if ((shortcut.isDisabled &
2081 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2082 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2083 // If the app is only disabled because of the above flags, launch activity anyway.
2084 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002085 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002086 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2087 // Use a message specific to this shortcut, if it has one.
2088 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2089 return;
2090 }
2091 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002092 int error = R.string.activity_not_available;
2093 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2094 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002095 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2096 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002097 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002098 }
2099 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2100 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002101 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002102 }
2103
Chris Wren40c5ed32014-06-24 18:24:23 -04002104 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002105 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002106 String packageName = shortcut.intent.getComponent() != null ?
2107 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2108 if (!TextUtils.isEmpty(packageName)) {
2109 onClickPendingAppItem(v, packageName,
2110 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2111 return;
2112 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002113 }
2114
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002115 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002116 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002117 }
2118
Sunny Goyala7ce1662016-05-31 15:01:35 -07002119 private void startAppShortcutOrInfoActivity(View v) {
2120 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002121 Intent intent;
2122 if (item instanceof PromiseAppInfo) {
2123 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2124 intent = promiseAppInfo.getMarketIntent();
2125 } else {
2126 intent = item.getIntent();
2127 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002128 if (intent == null) {
2129 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002130 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002131 startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002132 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002133 }
2134
2135 /**
2136 * Event handler for a folder icon click.
2137 *
2138 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2139 */
2140 protected void onClickFolderIcon(View v) {
2141 if (LOGD) Log.d(TAG, "onClickFolder");
2142 if (!(v instanceof FolderIcon)){
2143 throw new IllegalArgumentException("Input must be a FolderIcon");
2144 }
2145
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002146 Folder folder = ((FolderIcon) v).getFolder();
2147 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002148 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002149 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002150 }
Michael Jurka5130e402011-10-13 04:55:35 -07002151 }
2152
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002153 /**
2154 * Event handler for the (Add) Widgets button that appears after a long press
2155 * on the home screen.
2156 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002157 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002158 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002159 if (mIsSafeModeEnabled) {
2160 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2161 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002162 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002163 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002164 }
2165
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002166 /**
2167 * Event handler for the wallpaper picker button that appears after a long press
2168 * on the home screen.
2169 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002170 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002171 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002172 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2173 return;
2174 }
2175
Tony Wickham785f7a52015-08-31 17:28:32 -07002176 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2177 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002178 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002179 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002180 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002181
2182 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002183 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2184 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002185 intent.setPackage(pickerPackage);
2186 }
2187
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002188 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002189 try {
2190 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2191 // If there is no target package, use the default intent chooser animation
2192 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2193 } catch (ActivityNotFoundException e) {
2194 setWaitingForResult(null);
2195 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2196 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002197 }
2198
2199 /**
2200 * Event handler for a click on the settings button that appears after a long press
2201 * on the home screen.
2202 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002203 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002204 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002205 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2206 .setPackage(getPackageName());
2207 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002208 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002209 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002210 }
2211
Tony Wickhame2217252016-03-22 16:34:23 -07002212 @Override
2213 public void onAccessibilityStateChanged(boolean enabled) {
2214 mDragLayer.onAccessibilityStateChanged(enabled);
2215 }
2216
Sunny Goyala7ce1662016-05-31 15:01:35 -07002217 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002219 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2220 try {
2221 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2222 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2223 // is enabled by default on NYC.
2224 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2225 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002226
2227 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2228 String id = ((ShortcutInfo) info).getDeepShortcutId();
2229 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302230 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002231 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002232 } else {
2233 // Could be launching some bookkeeping activity
2234 startActivity(intent, optsBundle);
2235 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002236 } finally {
2237 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002240 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2241 // corresponding permission. Show the appropriate permission prompt if that
2242 // is the case.
2243 if (intent.getComponent() == null
2244 && Intent.ACTION_CALL.equals(intent.getAction())
2245 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2246 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002247
2248 setWaitingForResult(PendingRequestArgs
2249 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002250 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2251 REQUEST_PERMISSION_CALL_PHONE);
2252 } else {
2253 // No idea why this was thrown.
2254 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 }
2257 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002258
Sunny Goyalfe770c92016-09-27 14:38:58 -07002259 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002260 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002261 if (Utilities.ATLEAST_MARSHMALLOW) {
2262 int left = 0, top = 0;
2263 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002264 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002265 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002266 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002267 if (icon != null) {
2268 Rect bounds = icon.getBounds();
2269 left = (width - bounds.width()) / 2;
2270 top = v.getPaddingTop();
2271 width = bounds.width();
2272 height = bounds.height();
2273 }
2274 }
2275 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2276 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2277 // On L devices, we use the device default slide-up transition.
2278 // On L MR1 devices, we use a custom version of the slide-up transition which
2279 // doesn't have the delay present in the device default.
2280 return ActivityOptions.makeCustomAnimation(
2281 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2282 }
2283 return null;
2284 }
2285
Tonye3c59252017-05-02 21:32:27 -07002286 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002287 int[] pos = new int[2];
2288 v.getLocationOnScreen(pos);
2289 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2290 }
2291
Sunny Goyala7ce1662016-05-31 15:01:35 -07002292 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002293 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2294 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2295 return false;
2296 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002297 // Only launch using the new animation if the shortcut has not opted out (this is a
2298 // private contract between launcher and may be ignored in the future).
2299 boolean useLaunchAnimation = (v != null) &&
2300 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2301 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2302
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002303 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002304
2305 // Prepare intent
2306 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2307 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002308 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002309 }
2310 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002311 if (Utilities.ATLEAST_MARSHMALLOW
2312 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002313 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002314 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002315 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002316 // Shortcuts need some special checks due to legacy reasons.
2317 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002318 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002319 // Could be launching some bookkeeping activity
2320 startActivity(intent, optsBundle);
2321 } else {
2322 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2323 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2324 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002325
2326 if (v instanceof BubbleTextView) {
2327 // This is set to the view that launched the activity that navigated the user away
2328 // from launcher. Since there is no callback for when the activity has finished
2329 // launching, enable the press state and keep this reference to reset the press
2330 // state when we return to launcher.
2331 BubbleTextView btv = (BubbleTextView) v;
2332 btv.setStayPressed(true);
2333 setOnResumeCallback(btv);
2334 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002335 return true;
2336 } catch (ActivityNotFoundException|SecurityException e) {
2337 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2338 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2339 }
2340 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002341 }
2342
Tony Wickhamdadb3042016-02-24 11:07:00 -08002343 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002344 public boolean dispatchTouchEvent(MotionEvent ev) {
2345 mLastDispatchTouchEventX = ev.getX();
2346 return super.dispatchTouchEvent(ev);
2347 }
2348
2349 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002351 if (!isDraggingEnabled()) return false;
2352 if (isWorkspaceLocked()) return false;
2353 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354
Jon Miranda51f037d2016-11-07 08:37:20 -08002355 boolean ignoreLongPressToOverview =
2356 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002357
Adam Cohen1697b792013-09-17 19:08:21 -07002358 if (v instanceof Workspace) {
2359 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002360 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302361 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2362 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002363 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002364 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002365 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2366 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2367 return true;
2368 } else {
2369 return false;
2370 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002371 } else {
2372 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002373 }
Adam Cohen1697b792013-09-17 19:08:21 -07002374 }
2375
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002376 CellLayout.CellInfo longClickCellInfo = null;
2377 View itemUnderLongClick = null;
2378 if (v.getTag() instanceof ItemInfo) {
2379 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002380 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002381 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002382 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
2384
Winson Chung3d503fb2011-07-13 17:25:49 -07002385 // The hotseat touch handling does not go through Workspace, and we always allow long press
2386 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002387 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002388 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002389 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002390 if (mWorkspace.isInOverviewMode()) {
2391 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302392 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2393 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002394 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002395 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002396 return false;
2397 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302398 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2399 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002400 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002401 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002402 }
Jon Miranda379198e2016-09-23 08:54:40 -07002403 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2404 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002406 final boolean isAllAppsButton =
2407 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2408 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2409 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002410 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002412 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 }
2414 }
2415 }
2416 return true;
2417 }
2418
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002420 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002421 return mHotseat != null && layout != null &&
2422 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2423 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002424
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 /**
2426 * Returns the CellLayout of the specified container at the specified screen.
2427 */
Sunny Goyal92820592015-03-02 11:31:35 -08002428 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2430 if (mHotseat != null) {
2431 return mHotseat.getLayout();
2432 } else {
2433 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002434 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002435 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002436 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002437 }
2438 }
2439
Winson Chungb745afb2015-03-02 11:51:23 -08002440 /**
2441 * For overridden classes.
2442 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002443 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002444 return isAppsViewVisible();
2445 }
2446
2447 public boolean isAppsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002448 return mState == State.APPS;
Winson Chungb745afb2015-03-02 11:51:23 -08002449 }
2450
2451 public boolean isWidgetsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002452 return mState == State.WIDGETS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002453 }
2454
Michael Jurkae326f182011-11-21 14:05:46 -08002455 @Override
2456 public void onTrimMemory(int level) {
2457 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002458 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2459 // The widget preview db can result in holding onto over
2460 // 3MB of memory for caching which isn't necessary.
2461 SQLiteDatabase.releaseMemory();
2462
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002463 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002464 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002465 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002466 if (mLauncherCallbacks != null) {
2467 mLauncherCallbacks.onTrimMemory(level);
2468 }
Michael Jurkae326f182011-11-21 14:05:46 -08002469 }
2470
Winson5c6bdbb2015-09-03 11:36:19 -07002471 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002472 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002473 }
2474
Winson5c6bdbb2015-09-03 11:36:19 -07002475 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002476 boolean changed = mState != State.WORKSPACE ||
2477 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002478 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002479 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002480 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002481 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002482
Michael Jurkab3e22d92011-10-31 15:58:33 -07002483 // Set focus to the AppsCustomize button
2484 if (mAllAppsButton != null) {
2485 mAllAppsButton.requestFocus();
2486 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002487 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002488
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002489 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002490 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002491
Winson Chung0f785722015-04-08 10:27:49 -07002492 if (changed) {
2493 // Send an accessibility event to announce the context change
2494 getWindow().getDecorView()
2495 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002496 }
Winson5c6bdbb2015-09-03 11:36:19 -07002497 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002498 }
2499
Winsone9f27272015-10-13 10:47:51 -07002500 /**
2501 * Shows the overview button.
2502 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002503 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002504 showOverviewMode(animated, false);
2505 }
2506
2507 /**
2508 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2509 * onto one of the overview panel buttons.
2510 */
2511 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2512 Runnable postAnimRunnable = null;
2513 if (requestButtonFocus) {
2514 postAnimRunnable = new Runnable() {
2515 @Override
2516 public void run() {
2517 // Hitting the menu button when in touch mode does not trigger touch mode to
2518 // be disabled, so if requested, force focus on one of the overview panel
2519 // buttons.
2520 mOverviewPanel.requestFocusFromTouch();
2521 }
2522 };
2523 }
Adam Cohened307df2013-10-02 09:37:31 -07002524 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002525 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002526 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002527 setState(State.WORKSPACE);
2528
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002529 // If animated from long press, then don't allow any of the controller in the drag
2530 // layer to intercept any remaining touch.
2531 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002532 }
2533
Mario Bertschleracbf5702017-03-03 10:58:06 -08002534 private void setState(State state) {
2535 this.mState = state;
2536 updateSoftInputMode();
2537 }
2538
2539 private void updateSoftInputMode() {
2540 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2541 final int mode;
2542 if (isAppsViewVisible()) {
2543 mode = SOFT_INPUT_MODE_ALL_APPS;
2544 } else {
2545 mode = SOFT_INPUT_MODE_DEFAULT;
2546 }
2547 getWindow().setSoftInputMode(mode);
2548 }
2549 }
2550
Winson Chungb745afb2015-03-02 11:51:23 -08002551 /**
2552 * Shows the apps view.
2553 */
Sunny Goyald0d07032017-10-04 10:40:28 -07002554 public void showAppsView(boolean animated) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002555 markAppsViewShown();
Sunny Goyal5606e072017-06-15 14:24:21 -07002556 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002557 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002558
Winson Chungb745afb2015-03-02 11:51:23 -08002559 /**
2560 * Shows the widgets view.
2561 */
2562 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002563 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002564 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002565 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002566 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002567 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002568
2569 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002570 @Override
2571 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002572 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002573 }
2574 });
Winson Chungb745afb2015-03-02 11:51:23 -08002575 }
2576
2577 /**
2578 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002579 *
2580 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002581 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002582 // TODO: calling method should use the return value so that when {@code false} is returned
2583 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002584 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002585 if (!(mState == State.WORKSPACE ||
2586 mState == State.APPS_SPRING_LOADED ||
2587 mState == State.WIDGETS_SPRING_LOADED ||
2588 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002589 return false;
2590 }
2591 if (toState != State.APPS && toState != State.WIDGETS) {
2592 return false;
2593 }
Winson Chungb745afb2015-03-02 11:51:23 -08002594
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002595 // This is a safe and supported transition to bypass spring_loaded mode.
2596 if (mExitSpringLoadedModeRunnable != null) {
2597 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2598 mExitSpringLoadedModeRunnable = null;
2599 }
2600
Winson Chungb745afb2015-03-02 11:51:23 -08002601 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002602 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002603 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002604 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002605 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002606
Michael Jurkab3e22d92011-10-31 15:58:33 -07002607 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002608 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002609 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002610
2611 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002612 getWindow().getDecorView()
2613 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002614 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002615 }
2616
Hyunyoung Song3f471442015-04-08 19:01:34 -07002617 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002618 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002619 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002620 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002621 }
Winson Chungb745afb2015-03-02 11:51:23 -08002622
Winson Chung006ee262015-08-03 14:40:11 -07002623 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002624 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002625 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002626 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002627 }
Adam Cohen7777d962011-08-18 18:58:38 -07002628
Hyunyoung Song3f471442015-04-08 19:01:34 -07002629 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002630 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002631 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002632
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002633 if (mExitSpringLoadedModeRunnable != null) {
2634 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2635 }
2636 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002637 @Override
2638 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002639 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002640 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2641 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002642 // Before we show workspace, hide all apps again because
2643 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2644 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002645 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002646 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002647 } else {
2648 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002649 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002650 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002651 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002652 };
2653 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002654 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002655
Tony Wickhame0c33232016-02-08 11:37:04 -08002656 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002657 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2658 || mState == State.WIDGETS_SPRING_LOADED;
2659 }
2660
Sunny Goyal0f76b562016-12-13 19:37:10 -08002661 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002662 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyald0d07032017-10-04 10:40:28 -07002663 showAppsView(true /* animated */);
Winson Chungb745afb2015-03-02 11:51:23 -08002664 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002665 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002666 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2667 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002668 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002669 }
2670
Michael Jurkad7c28052012-04-27 15:43:36 -07002671 @Override
2672 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002673 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002674 final List<CharSequence> text = event.getText();
2675 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002676 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002677 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002678 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002679 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002680 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002681 } else if (mWorkspace != null) {
2682 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002683 } else {
2684 text.add(getString(R.string.all_apps_home_button_label));
2685 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002686 return result;
2687 }
2688
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002689 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002690 * If the activity is currently paused, signal that we need to run the passed Runnable
2691 * in onResume.
2692 *
2693 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002694 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2695 * wrong when we're not running, and if the activity comes back to what the configuration was
2696 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002697 *
2698 * Implementation of the method from LauncherModel.Callbacks.
2699 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002700 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002701 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002702 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002703 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002704 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002705 if (run instanceof RunnableWithId) {
2706 // Remove any runnables which have the same id
2707 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002708 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002709 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002710 return true;
2711 } else {
2712 return false;
2713 }
2714 }
2715
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002716 public void setOnResumeCallback(OnResumeCallback callback) {
2717 if (mOnResumeCallback != null) {
2718 mOnResumeCallback.onLauncherResume();
2719 }
2720 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002721 }
2722
Michael Jurka7607c2f2013-04-03 14:33:19 -07002723 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002724 * If the activity is currently paused, signal that we need to re-run the loader
2725 * in onResume.
2726 *
2727 * This needs to be called from incoming places where resources might have been loaded
2728 * while we are paused. That is becaues the Configuration might be wrong
2729 * when we're not running, and if it comes back to what it was when we
2730 * were paused, we are not restarted.
2731 *
2732 * Implementation of the method from LauncherModel.Callbacks.
2733 *
2734 * @return true if we are currently paused. The caller might be able to
2735 * skip some work in that case since we will come back again.
2736 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002737 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002738 public boolean setLoadOnResume() {
2739 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002740 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002741 mOnResumeNeedsLoad = true;
2742 return true;
2743 } else {
2744 return false;
2745 }
2746 }
2747
2748 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002749 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002751 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002752 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002753 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002754 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002755 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002756 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002758 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002759
Joe Onorato9c1289c2009-08-17 11:03:03 -04002760 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002761 * Clear any pending bind callbacks. This is called when is loader is planning to
2762 * perform a full rebind from scratch.
2763 */
2764 @Override
2765 public void clearPendingBinds() {
2766 mBindOnResumeCallbacks.clear();
2767 if (mPendingExecutor != null) {
2768 mPendingExecutor.markCompleted();
2769 mPendingExecutor = null;
2770 }
2771 }
2772
2773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002774 * Refreshes the shortcuts shown on the workspace.
2775 *
2776 * Implementation of the method from LauncherModel.Callbacks.
2777 */
2778 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002779 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002780 AbstractFloatingView.closeAllOpenViews(this);
2781
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002782 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002783
Winson Chungd64d1762013-08-20 14:37:16 -07002784 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002785 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002786 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002787
Winson Chung3d503fb2011-07-13 17:25:49 -07002788 if (mHotseat != null) {
2789 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002791 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 }
2793
Adam Cohendcd297f2013-06-18 13:13:40 -07002794 @Override
2795 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002796 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002797 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2798 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002799 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2800 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002801 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002802 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2803 // If there are no screens, we need to have an empty screen
2804 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002805 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002806 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002807
Winsonc7d2e832016-07-28 12:24:55 -07002808 // After we have added all the screens, if the wallpaper was locked to the default state,
2809 // then notify to indicate that it can be released and a proper wallpaper offset can be
2810 // computed before the next layout
2811 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002812 }
2813
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002814 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002815 int count = orderedScreenIds.size();
2816 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002817 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002818 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002819 // No need to bind the first screen, as its always bound.
2820 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2821 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002822 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002823 }
2824
Sunny Goyalb23980c2017-08-17 07:45:25 -07002825 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002826 public void bindAppsAdded(final ArrayList<Long> newScreens,
2827 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002828 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002829 Runnable r = new Runnable() {
2830 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002831 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002832 }
2833 };
2834 if (waitUntilResume(r)) {
2835 return;
2836 }
2837
Winson Chungd64d1762013-08-20 14:37:16 -07002838 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002839 if (newScreens != null) {
2840 bindAddScreens(newScreens);
2841 }
Winson Chungd64d1762013-08-20 14:37:16 -07002842
2843 // We add the items without animation on non-visible pages, and with
2844 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002845 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002846 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002847 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002848 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002849 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002850 }
Winson Chungc58497e2013-09-03 17:48:37 -07002851
Winson Chung87412982013-10-03 18:34:14 -07002852 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002853 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002854 }
2855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 * Bind the items start-end from the list.
2858 *
2859 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002861 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002862 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002863 Runnable r = new Runnable() {
2864 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002865 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002866 }
2867 };
2868 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002869 return;
2870 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002871
Sunny Goyal3be633b2016-12-08 09:59:25 -08002872 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002873 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002874 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002875 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002876 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002877 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002878 int end = items.size();
2879 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002880 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002881
2882 // Short circuit if we are loading dock items for a configuration which has no dock
2883 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2884 mHotseat == null) {
2885 continue;
2886 }
2887
Sunny Goyal639e9062015-08-19 19:17:06 -07002888 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002889 switch (item.itemType) {
2890 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2891 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002892 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002893 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002894 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002895 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002896 }
2897 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002898 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002899 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002900 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002901 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002902 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002903 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2904 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002905 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002906 if (view == null) {
2907 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002908 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002909 break;
2910 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002911 default:
2912 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002914
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002915 /*
2916 * Remove colliding items.
2917 */
2918 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2919 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2920 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2921 View v = cl.getChildAt(item.cellX, item.cellY);
2922 Object tag = v.getTag();
2923 String desc = "Collision while binding workspace item: " + item
2924 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002925 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002926 throw (new RuntimeException(desc));
2927 } else {
2928 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002929 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002930 continue;
2931 }
2932 }
2933 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302934 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002935 if (animateIcons) {
2936 // Animate all the applications up now
2937 view.setAlpha(0f);
2938 view.setScaleX(0f);
2939 view.setScaleY(0f);
2940 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002941 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002944
Winson Chung997a9232013-07-24 15:33:46 -07002945 if (animateIcons) {
2946 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002947 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002948 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002949 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002950 final Runnable startBounceAnimRunnable = new Runnable() {
2951 public void run() {
2952 anim.playTogether(bounceAnims);
2953 anim.start();
2954 }
2955 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002956 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002957 // We post the animation slightly delayed to prevent slowdowns
2958 // when we are loading right after we return to launcher.
2959 mWorkspace.postDelayed(new Runnable() {
2960 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002961 if (mWorkspace != null) {
2962 mWorkspace.snapToPage(newScreenIndex);
2963 mWorkspace.postDelayed(startBounceAnimRunnable,
2964 NEW_APPS_ANIMATION_DELAY);
2965 }
Winson Chung94d67682013-09-25 16:29:40 -07002966 }
2967 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002968 } else {
2969 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002970 }
2971 }
Winson Chung64359a52013-07-08 17:17:08 -07002972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
2975
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 /**
2977 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002979 public void bindAppWidget(LauncherAppWidgetInfo item) {
2980 View view = inflateAppWidget(item);
2981 if (view != null) {
2982 mWorkspace.addInScreen(view, item);
2983 mWorkspace.requestLayout();
2984 }
2985 }
2986
2987 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002988 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302989 PendingAppWidgetHostView view =
2990 new PendingAppWidgetHostView(this, item, mIconCache, true);
2991 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002992 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002993 }
2994
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002995 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002996
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002997 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002998
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002999 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3000 // If the provider is not ready, bind as a pending widget.
3001 appWidgetInfo = null;
3002 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3003 // The widget id is not valid. Try to find the widget based on the provider info.
3004 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3005 } else {
3006 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3007 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003008
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003009 // If the provider is ready, but the width is not yet restored, try to restore it.
3010 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3011 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003012 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003013 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3014 + " belongs to component " + item.providerName
3015 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003016 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003017 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003018 }
Sunny Goyalff572272014-07-23 13:58:07 -07003019
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003020 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003021 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003022 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3023 // Id has not been allocated yet. Allocate a new id.
3024 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3025 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003026
Sunny Goyald478c832016-04-01 12:04:16 -07003027 // Also try to bind the widget. If the bind fails, the user will be shown
3028 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003029 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003030 pendingInfo.spanX = item.spanX;
3031 pendingInfo.spanY = item.spanY;
3032 pendingInfo.minSpanX = item.minSpanX;
3033 pendingInfo.minSpanY = item.minSpanY;
3034 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003035
3036 boolean isDirectConfig =
3037 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3038 if (isDirectConfig && item.bindOptions != null) {
3039 Bundle newOptions = item.bindOptions.getExtras();
3040 if (options != null) {
3041 newOptions.putAll(options);
3042 }
3043 options = newOptions;
3044 }
Sunny Goyald478c832016-04-01 12:04:16 -07003045 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3046 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003047
Sunny Goyal86df1382016-08-10 15:03:22 -07003048 // We tried to bind once. If we were not able to bind, we would need to
3049 // go through the permission dialog, which means we cannot skip the config
3050 // activity.
3051 item.bindOptions = null;
3052 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3053
Sunny Goyald478c832016-04-01 12:04:16 -07003054 // Bind succeeded
3055 if (success) {
3056 // If the widget has a configure activity, it is still needs to set it up,
3057 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003058 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003059 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3060 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003061 }
Sunny Goyald478c832016-04-01 12:04:16 -07003062
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003063 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003064 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003065 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003066 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003067 // The widget was marked as UI not ready, but there is no configure activity to
3068 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003069 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003070 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003071 }
Sunny Goyalff572272014-07-23 13:58:07 -07003072 }
3073
Sunny Goyald5462aa2016-11-22 16:52:39 +05303074 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003075 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003076 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003077 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003078 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003079 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003080 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003081 }
3082
Sunny Goyal233ee962015-08-03 13:05:01 -07003083 item.minSpanX = appWidgetInfo.minSpanX;
3084 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303085 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003086 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303087 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003088 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303089 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003090
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003091 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003092 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 }
3094
Sunny Goyalff572272014-07-23 13:58:07 -07003095 /**
3096 * Restores a pending widget.
3097 *
3098 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003099 */
Sunny Goyald478c832016-04-01 12:04:16 -07003100 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003101 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3102 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3103 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003104 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003105 }
3106
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003107 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003108 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003109 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3110 info.pendingItemInfo = null;
3111 }
Sunny Goyalff572272014-07-23 13:58:07 -07003112
Sunny Goyalba47faa2017-10-04 16:50:57 -07003113 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3114 view.reinflate();
3115 }
3116
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003117 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003118 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003119 }
3120
Adam Cohen1462de32012-07-24 22:34:36 -07003121 public void onPageBoundSynchronously(int page) {
3122 mSynchronouslyBoundPages.add(page);
3123 }
3124
Sunny Goyal527c7d32015-08-28 15:19:36 -07003125 @Override
3126 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3127 if (mPendingExecutor != null) {
3128 mPendingExecutor.markCompleted();
3129 }
3130 mPendingExecutor = executor;
3131 executor.attachTo(this);
3132 }
3133
3134 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3135 if (mPendingExecutor == executor) {
3136 mPendingExecutor = null;
3137 }
3138 }
3139
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003140 @Override
3141 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3142 Runnable r = new Runnable() {
3143 public void run() {
3144 finishFirstPageBind(executor);
3145 }
3146 };
3147 if (waitUntilResume(r)) {
3148 return;
3149 }
3150
3151 Runnable onComplete = new Runnable() {
3152 @Override
3153 public void run() {
3154 if (executor != null) {
3155 executor.onLoadAnimationCompleted();
3156 }
3157 }
3158 };
3159 if (mDragLayer.getAlpha() < 1) {
3160 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3161 } else {
3162 onComplete.run();
3163 }
3164 }
3165
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 /**
3167 * Callback saying that there aren't any more items to bind.
3168 *
3169 * Implementation of the method from LauncherModel.Callbacks.
3170 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003171 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003172 Runnable r = new Runnable() {
3173 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003174 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003175 }
3176 };
3177 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003178 return;
3179 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003180 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003181 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003183 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003184
Sunny Goyal2100c782016-08-22 16:00:03 -07003185 if (mPendingActivityResult != null) {
3186 handleActivityResult(mPendingActivityResult.requestCode,
3187 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3188 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003189 }
3190
Sunny Goyala474a9b2017-05-04 16:47:11 -07003191 InstallShortcutReceiver.disableAndFlushInstallQueue(
3192 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003193
Tony Wickham010d2552017-01-20 08:15:28 -08003194 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003195 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003196 }
3197
Winson Chunga2413752012-04-03 14:22:34 -07003198 private boolean canRunNewAppsAnimation() {
3199 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003200 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003201 }
3202
Winson Chungc9168342013-06-26 14:54:55 -07003203 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003204 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003205 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3206 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003207 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003208 return bounceAnim;
3209 }
3210
Adam Cohen27772732013-11-11 14:00:56 +00003211 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003212 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003213 }
3214
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003215 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 * Add the icons for all apps.
3217 *
3218 * Implementation of the method from LauncherModel.Callbacks.
3219 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003220 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003221 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3222 public void run() {
3223 bindAllApplications(apps);
3224 }
3225 };
3226 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003227 return;
3228 }
3229
Winson Chungb745afb2015-03-02 11:51:23 -08003230 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003231 Executor pendingExecutor = getPendingExecutor();
3232 if (pendingExecutor != null && mState != State.APPS) {
3233 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003234 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003235 return;
3236 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003237
Winson Chungb745afb2015-03-02 11:51:23 -08003238 mAppsView.setApps(apps);
3239 }
Adam Cohen9211d422014-10-07 18:14:53 -07003240 if (mLauncherCallbacks != null) {
3241 mLauncherCallbacks.bindAllApplications(apps);
3242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 }
3244
3245 /**
Tony2917a8b2017-07-31 23:29:42 -07003246 * Returns an Executor that will run after the launcher is first drawn (including after the
3247 * initial fade in animation). Returns null if the first draw has already occurred.
3248 */
3249 public @Nullable Executor getPendingExecutor() {
3250 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3251 }
3252
3253 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003254 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3255 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3256 */
3257 @Override
3258 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003259 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003260 }
3261
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003262 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003263 * A package was updated.
3264 *
3265 * Implementation of the method from LauncherModel.Callbacks.
3266 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003267 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003268 Runnable r = new Runnable() {
3269 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003270 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003271 }
3272 };
3273 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003274 return;
3275 }
3276
Winson Chungb745afb2015-03-02 11:51:23 -08003277 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003278 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003279 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 }
3281
Sunny Goyal4390ace2014-10-13 11:33:11 -07003282 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003283 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3284 Runnable r = new Runnable() {
3285 public void run() {
3286 bindPromiseAppProgressUpdated(app);
3287 }
3288 };
3289 if (waitUntilResume(r)) {
3290 return;
3291 }
3292
3293 if (mAppsView != null) {
3294 mAppsView.updatePromiseAppProgress(app);
3295 }
3296 }
3297
3298 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003299 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3300 Runnable r = new Runnable() {
3301 public void run() {
3302 bindWidgetsRestored(widgets);
3303 }
3304 };
3305 if (waitUntilResume(r)) {
3306 return;
3307 }
3308 mWorkspace.widgetsRestored(widgets);
3309 }
3310
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003312 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003313 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003314 *
3315 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003316 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003317 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003318 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003319 Runnable r = new Runnable() {
3320 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003321 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003322 }
3323 };
3324 if (waitUntilResume(r)) {
3325 return;
3326 }
3327
Sunny Goyal4390ace2014-10-13 11:33:11 -07003328 if (!updated.isEmpty()) {
3329 mWorkspace.updateShortcuts(updated);
3330 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003331 }
3332
3333 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003334 * Update the state of a package, typically related to install state.
3335 *
3336 * Implementation of the method from LauncherModel.Callbacks.
3337 */
Sunny Goyale755d462014-07-22 13:48:29 -07003338 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003339 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3340 Runnable r = new Runnable() {
3341 public void run() {
3342 bindRestoreItemsChange(updates);
3343 }
3344 };
3345 if (waitUntilResume(r)) {
3346 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003347 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003348
Sunny Goyal756adbc2015-04-16 15:20:51 -07003349 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003350 }
3351
3352 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003353 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003354 * in addition to specific applications to remove, the reason being that
3355 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003356 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003357 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003359 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003360 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003361 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003362 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003363 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003364 }
Winson Chungd64d1762013-08-20 14:37:16 -07003365 };
3366 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003367 return;
3368 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003369 mWorkspace.removeItemsByMatcher(matcher);
3370 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003371 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003372
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003373 @Override
3374 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3375 Runnable r = new Runnable() {
3376 public void run() {
3377 bindAppInfosRemoved(appInfos);
3378 }
3379 };
3380 if (waitUntilResume(r)) {
3381 return;
Joe Onorato36115782010-06-17 13:28:48 -04003382 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003383
Winson Chungdf95eb12013-10-16 14:57:07 -07003384 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003385 if (mAppsView != null) {
3386 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003387 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 }
Joe Onoratobe386092009-11-17 17:32:16 -08003389
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003390 @Override
3391 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3392 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3393 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003394 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003395 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003396 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003397 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003398 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003399 return;
3400 }
3401
Sunny Goyald164b7f2016-10-12 20:49:31 -07003402 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003403 Executor pendingExecutor = getPendingExecutor();
3404 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003405 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003406 return;
3407 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003408 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003409 }
Tony Wickham26b17462017-03-20 17:12:24 -07003410
3411 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3412 if (topView != null) {
3413 topView.onWidgetsBound();
3414 }
3415 }
3416
3417 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3418 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003419 }
3420
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003421 @Override
3422 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003423 if (mWorkspace.getState().shouldUpdateWidget) {
3424 refreshAndBindWidgetsForPackageUser(null);
3425 }
Tony Wickham26b17462017-03-20 17:12:24 -07003426 }
3427
Tony Wickham86222d22017-03-29 15:30:43 -07003428 /**
3429 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3430 * refreshes the widgets and shortcuts associated with the given package/user
3431 */
3432 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003433 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003434 }
3435
Winson Chung4b919f82012-05-01 10:44:08 -07003436 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003437 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003438 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003439 }
3440 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003441
Winson Chung4b919f82012-05-01 10:44:08 -07003442 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003443 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003444 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003445 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003446 } else {
3447 mHandler.postDelayed(new Runnable() {
3448 public void run() {
3449 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3450 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003451 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003452 }
Winson Chung4b919f82012-05-01 10:44:08 -07003453 }
Winson Chung400438b2011-01-16 17:53:48 -08003454 }
3455
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003456 private void markAppsViewShown() {
3457 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3458 return;
3459 }
3460 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3461 }
3462
3463 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003464 UserManagerCompat um = UserManagerCompat.getInstance(this);
3465 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003466 }
3467
Winson Chung80baf5a2010-08-09 16:03:15 -07003468 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003469 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003470 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003471 @Override
3472 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3473 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003474
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003475 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3476 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003477 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003478 writer.println(prefix + " Homescreen " + i);
3479
3480 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3481 for (int j = 0; j < layout.getChildCount(); j++) {
3482 Object tag = layout.getChildAt(j).getTag();
3483 if (tag != null) {
3484 writer.println(prefix + " " + tag.toString());
3485 }
3486 }
3487 }
3488
3489 writer.println(prefix + " Hotseat");
3490 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003491 for (int j = 0; j < layout.getChildCount(); j++) {
3492 Object tag = layout.getChildAt(j).getTag();
3493 if (tag != null) {
3494 writer.println(prefix + " " + tag.toString());
3495 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003496 }
Sunny Goyala1365452015-10-01 15:46:24 -07003497
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003498 try {
3499 FileLog.flushAll(writer);
3500 } catch (Exception e) {
3501 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003502 }
3503 }
3504
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003505 writer.println(prefix + "Misc:");
3506 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3507 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3508 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3509
3510 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003511
Adam Cohen9211d422014-10-07 18:14:53 -07003512 if (mLauncherCallbacks != null) {
3513 mLauncherCallbacks.dump(prefix, fd, writer, args);
3514 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003515 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003516
Sunny Goyal66b24572016-09-21 15:57:55 -07003517 @Override
3518 @TargetApi(Build.VERSION_CODES.N)
3519 public void onProvideKeyboardShortcuts(
3520 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3521
3522 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3523 if (mState == State.WORKSPACE) {
3524 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3525 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3526 }
3527 View currentFocus = getCurrentFocus();
3528 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3529 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3530 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3531 }
Tony18c4aa42017-05-10 21:23:57 -05003532 if (currentFocus.getTag() instanceof ItemInfo
3533 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003534 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3535 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3536 }
3537 if (!shortcutInfos.isEmpty()) {
3538 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3539 }
3540
3541 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3542 }
3543
3544 @Override
3545 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3546 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3547 switch (keyCode) {
3548 case KeyEvent.KEYCODE_A:
3549 if (mState == State.WORKSPACE) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003550 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003551 return true;
3552 }
3553 break;
3554 case KeyEvent.KEYCODE_S: {
3555 View focusedView = getCurrentFocus();
3556 if (focusedView instanceof BubbleTextView
3557 && focusedView.getTag() instanceof ItemInfo
3558 && mAccessibilityDelegate.performAction(focusedView,
3559 (ItemInfo) focusedView.getTag(),
3560 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003561 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003562 return true;
3563 }
3564 break;
3565 }
3566 case KeyEvent.KEYCODE_O:
3567 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3568 return true;
3569 }
3570 break;
3571 }
3572 }
3573 return super.onKeyShortcut(keyCode, event);
3574 }
3575
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003576 public static Launcher getLauncher(Context context) {
3577 if (context instanceof Launcher) {
3578 return (Launcher) context;
3579 }
3580 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3581 }
3582
Sunny Goyal5a81c382017-03-20 15:08:06 -07003583 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003584
3585 @Override
3586 public void onSharedPreferenceChanged(
3587 SharedPreferences sharedPreferences, String key) {
3588 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003589 // Recreate the activity so that it initializes the rotation preference again.
3590 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003591 }
3592 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003593 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003594
3595 /**
3596 * Callback for listening for onResume
3597 */
3598 public interface OnResumeCallback {
3599
3600 void onLauncherResume();
3601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003602}