blob: d8cb06ce22f862a75f1a4e3a9e75b68a24866c4d [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
477 public void onPostCreate(Bundle savedInstanceState) {
478 super.onPostCreate(savedInstanceState);
479 if (mLauncherCallbacks != null) {
480 mLauncherCallbacks.onPostCreate(savedInstanceState);
481 }
482 }
483
Winson1f064272016-07-18 17:18:02 -0700484 public void onInsetsChanged(Rect insets) {
485 mDeviceProfile.updateInsets(insets);
486 mDeviceProfile.layout(this, true /* notifyListeners */);
487 }
488
Sunny Goyal32554d12015-12-03 15:31:25 -0800489 /**
490 * Call this after onCreate to set or clear overlay.
491 */
492 public void setLauncherOverlay(LauncherOverlay overlay) {
493 if (overlay != null) {
494 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
495 }
496 mWorkspace.setLauncherOverlay(overlay);
497 }
498
Adam Cohen9211d422014-10-07 18:14:53 -0700499 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
500 mLauncherCallbacks = callbacks;
501 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700502 }
503
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700504 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700505 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700506 if (mLauncherCallbacks != null) {
507 mLauncherCallbacks.onLauncherProviderChange();
508 }
509 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700510
Hyunyoung Song3f471442015-04-08 19:01:34 -0700511 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700512 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
513 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700514 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700515 }
516
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000517 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700518 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
519 // This cast is safe as long as the id < 0x00FFFFFF
520 // Since we jail all the dynamically generated views, there should be no clashes
521 // with any other views.
522 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000523 }
524
Tony Wickham010d2552017-01-20 08:15:28 -0800525 public PopupDataProvider getPopupDataProvider() {
526 return mPopupDataProvider;
527 }
528
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000529 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700530 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
531 * a configuration step, this allows the proper animations to run after other transitions.
532 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 private long completeAdd(
534 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
535 long screenId = info.screenId;
536 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700537 // When the screen id represents an actual screen (as opposed to a rank) we make sure
538 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700539 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700540 }
Adam Cohendb364c32014-05-20 14:23:40 -0700541
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800543 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700544 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700545 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800546 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700547 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700548 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700549 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700550 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700551 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700552 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700553 int widgetId = appWidgetId;
554 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700555 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700556 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700557 // Since the view was just bound, also launch the configure activity if needed
558 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
559 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800560 if (provider != null) {
561 new WidgetAddFlowHandler(provider)
562 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700563 }
564 }
565 break;
566 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800567 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700568
Adam Cohendb364c32014-05-20 14:23:40 -0700569 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800570 }
571
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800572 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700574 if (isWorkspaceLoading()) {
575 // process the result once the workspace has loaded.
576 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
577 return;
578 }
579 mPendingActivityResult = null;
580
Winson Chunge341d302013-08-16 14:31:00 -0700581 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700582 final PendingRequestArgs requestArgs = mPendingRequestArgs;
583 setWaitingForResult(null);
584 if (requestArgs == null) {
585 return;
586 }
587
588 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700589
Adam Cohenad4e15c2013-10-17 16:21:35 -0700590 Runnable exitSpringLoaded = new Runnable() {
591 @Override
592 public void run() {
593 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
594 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
595 }
596 };
597
Michael Jurka8b805b12012-04-18 14:23:14 -0700598 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700599 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700601 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
602 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700603 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700604 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700605 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700606 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700607 addAppWidgetImpl(
608 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800609 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700610 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700611 }
612 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200613 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
614 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700615 // User could have free-scrolled between pages before picking a wallpaper; make sure
616 // we move to the closest one now.
617 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700618 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200619 }
620 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200622
Adam Cohened66b2b2012-01-23 17:28:51 -0800623 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700624 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700625
Adam Cohened66b2b2012-01-23 17:28:51 -0800626 // We have special handling for widgets
627 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800628 final int appWidgetId;
629 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
630 : -1;
631 if (widgetId < 0) {
632 appWidgetId = pendingAddWidgetId;
633 } else {
634 appWidgetId = widgetId;
635 }
636
Adam Cohenad4e15c2013-10-17 16:21:35 -0700637 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800638 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700639 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
640 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700641 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700643 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700644 @Override
645 public void run() {
646 exitSpringLoadedDragModeDelayed(false, 0, null);
647 }
648 };
Adam Cohendb364c32014-05-20 14:23:40 -0700649
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
651 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
652 } else {
653 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
654 // When the screen id represents an actual screen (as opposed to a rank)
655 // we make sure that the drop page actually exists.
656 requestArgs.screenId =
657 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700658 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 final CellLayout dropLayout =
660 mWorkspace.getScreenWithId(requestArgs.screenId);
661
662 dropLayout.setDropPending(true);
663 final Runnable onComplete = new Runnable() {
664 @Override
665 public void run() {
666 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
667 dropLayout.setDropPending(false);
668 }
669 };
670 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
671 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 return;
674 }
675
Sunny Goyald478c832016-04-01 12:04:16 -0700676 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
677 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700678 if (resultCode == RESULT_OK) {
679 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700680 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700681 }
682 // Leave the widget in the pending state if the user canceled the configure.
683 return;
684 }
685
Sunny Goyalaad90582015-07-09 14:22:50 -0700686 if (requestCode == REQUEST_CREATE_SHORTCUT) {
687 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
689 completeAdd(requestCode, data, -1, requestArgs);
690 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
691 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
692
Sunny Goyalaad90582015-07-09 14:22:50 -0700693 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700694 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
695 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800699 }
700
701 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700702 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800703 final int requestCode, final int resultCode, final Intent data) {
704 handleActivityResult(requestCode, resultCode, data);
705 if (mLauncherCallbacks != null) {
706 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
707 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 }
709
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700710 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700711 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600712 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700713 PendingRequestArgs pendingArgs = mPendingRequestArgs;
714 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
715 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
716 setWaitingForResult(null);
717
Sunny Goyal28c6b962015-10-12 11:42:05 -0700718 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700720 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700722 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 Intent intent = pendingArgs.getPendingIntent();
724
Sunny Goyal28c6b962015-10-12 11:42:05 -0700725 if (grantResults.length > 0
726 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700727 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700728 } else {
729 // TODO: Show a snack bar with link to settings
730 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700731 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700732 }
733 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600734 if (mLauncherCallbacks != null) {
735 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
736 grantResults);
737 }
738 }
739
Adam Cohendb364c32014-05-20 14:23:40 -0700740 /**
741 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
742 *
743 * @param screenId the screen id to check
744 * @return the new screen, or screenId if it exists
745 */
746 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800747 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700748 if (dropLayout == null) {
749 // it's possible that the add screen was removed because it was
750 // empty and a re-bind occurred
751 mWorkspace.addExtraEmptyScreen();
752 return mWorkspace.commitExtraEmptyScreen();
753 } else {
754 return screenId;
755 }
756 }
757
Sunny Goyal2100c782016-08-22 16:00:03 -0700758 @Thunk void completeTwoStageWidgetDrop(
759 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
760 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800761 Runnable onCompleteRunnable = null;
762 int animationType = 0;
763
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700764 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 if (resultCode == RESULT_OK) {
766 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
767 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800768 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700769 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 onCompleteRunnable = new Runnable() {
771 @Override
772 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700773 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
775 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800776 }
777 };
778 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800779 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800781 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700782 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700784 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
785 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700787 // The animated view may be null in the case of a rotation during widget configuration
788 onCompleteRunnable.run();
789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 }
791
792 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700793 protected void onStop() {
794 super.onStop();
795 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700796
797 if (mLauncherCallbacks != null) {
798 mLauncherCallbacks.onStop();
799 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700800
Sunny Goyalf5e37442016-11-02 10:31:24 -0700801 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700802 mAppWidgetHost.stopListening();
803 }
Tony Wickham010d2552017-01-20 08:15:28 -0800804
805 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700806 }
807
808 @Override
809 protected void onStart() {
810 super.onStart();
811 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700812
813 if (mLauncherCallbacks != null) {
814 mLauncherCallbacks.onStart();
815 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700816
Sunny Goyalf5e37442016-11-02 10:31:24 -0700817 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700818 mAppWidgetHost.startListening();
819 }
Tony Wickham010d2552017-01-20 08:15:28 -0800820
821 if (!isWorkspaceLoading()) {
822 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
823 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700824
Jon Miranda7fda2852017-07-19 16:07:01 -0700825 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700826 if (mScrimAnimator != null) {
827 mScrimAnimator.cancel();
828 }
829 mDragLayer.getBackground().setAlpha(0);
830 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
831 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
832 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
833 @Override
834 public void onAnimationEnd(Animator animation) {
835 mScrimAnimator = null;
836 }
837 });
838 mScrimAnimator.setDuration(600);
839 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
840 mScrimAnimator.start();
841 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700842 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700843 }
844
845 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700847 TraceHelper.beginSection("ON_RESUME");
Adam Cohen9211d422014-10-07 18:14:53 -0700848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.preOnResume();
850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700852 TraceHelper.partitionSection("ON_RESUME", "superCall");
853
Hyunyoung Songaa953652016-04-19 18:30:24 -0700854 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800855 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700856 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700857 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700858 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700859 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700861 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200862 // We might have postponed some bind calls until onResume (see waitUntilResume) --
863 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700864 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
865 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200866 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700867 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200868 }
Winson Chunge4e50662012-01-23 14:45:13 -0800869
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700870 setOnResumeCallback(null);
Winson Chung82963d52013-10-09 11:20:57 -0700871
Winson Chungcd99cd32015-04-29 11:03:24 -0700872 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Sunny Goyale755d462014-07-22 13:48:29 -0700873
Sunny Goyala474a9b2017-05-04 16:47:11 -0700874 // Process any items that were added while Launcher was away.
875 InstallShortcutReceiver.disableAndFlushInstallQueue(
876 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700877
Sunny Goyala474a9b2017-05-04 16:47:11 -0700878 // Refresh shortcuts if the permission changed.
879 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700880
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700881 if (shouldShowDiscoveryBounce()) {
882 mAllAppsController.showDiscoveryBounce();
883 }
Adam Cohen9211d422014-10-07 18:14:53 -0700884 if (mLauncherCallbacks != null) {
885 mLauncherCallbacks.onResume();
886 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800887
Sunny Goyala502aa32017-10-02 16:04:06 -0700888 clearTypedText();
889
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700890 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700894 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700895 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700896 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700897
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700898 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700899 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800900 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700901 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700902
Adam Cohen9211d422014-10-07 18:14:53 -0700903 if (mLauncherCallbacks != null) {
904 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700905 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700906 }
907
Adam Cohenc2d6e892014-10-16 09:49:24 -0700908 public interface LauncherOverlay {
909
910 /**
911 * Touch interaction leading to overscroll has begun
912 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700913 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700914
915 /**
916 * Touch interaction related to overscroll has ended
917 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700918 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700919
920 /**
921 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
922 * screen (or in the case of RTL, the rightmost screen).
923 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700924 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700925
926 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800927 * Called when the launcher is ready to use the overlay
928 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700929 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700930 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700931 }
932
933 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700934 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700935 }
936
937 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
938
Sunny Goyalc86df472016-02-25 09:19:38 -0800939 public void onScrollChanged(float progress) {
940 if (mWorkspace != null) {
941 mWorkspace.onOverlayScrollChanged(progress);
942 }
943 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700944 }
945
Jorim Jaggid017f882014-01-14 17:08:48 -0800946 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700947 if (mLauncherCallbacks != null) {
948 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700949 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800950 // On O and above we there is always some setting present settings (add icon to
951 // home screen or icon badging). On earlier APIs we will have the allow rotation
952 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700953 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700954 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800955 }
956
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700957 @Override
958 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400959 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700960 if (mModel.isCurrentCallbacks(this)) {
961 mModel.stopLoader();
962 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700963 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
964
Romain Guy13c2e7b2010-03-10 19:45:00 -0800965 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700966 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700967
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800968 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800969 @Override
970 public void onWindowFocusChanged(boolean hasFocus) {
971 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700972 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700973
974 if (mLauncherCallbacks != null) {
975 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
976 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800977 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800978
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 private boolean acceptFilter() {
980 final InputMethodManager inputManager = (InputMethodManager)
981 getSystemService(Context.INPUT_METHOD_SERVICE);
982 return !inputManager.isFullscreenMode();
983 }
984
985 @Override
986 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700987 final int uniChar = event.getUnicodeChar();
988 final boolean handled = super.onKeyDown(keyCode, event);
989 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
990 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
992 keyCode, event);
993 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700994 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700995 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700996 // showSearchDialog()
997 // If there are multiple keystrokes before the search dialog takes focus,
998 // onSearchRequested() will be called for every keystroke,
999 // but it is idempotent, so it's fine.
1000 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002 }
1003
Joe Onorato8a9625e2010-01-28 15:55:35 -08001004 // Eat the long press event so the keyboard doesn't come up.
1005 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1006 return true;
1007 }
1008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 return handled;
1010 }
1011
Winson46163472015-09-22 17:31:56 -07001012 @Override
1013 public boolean onKeyUp(int keyCode, KeyEvent event) {
1014 if (keyCode == KeyEvent.KEYCODE_MENU) {
1015 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001016 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001017 // Close any open floating view
1018 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001019
Winson46163472015-09-22 17:31:56 -07001020 // Show the overview mode if we are on the workspace
1021 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1022 !mWorkspace.isSwitchingState()) {
1023 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001024 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001025 }
1026 }
1027 return true;
1028 }
1029 return super.onKeyUp(keyCode, event);
1030 }
1031
Karl Rosaen138a0412009-04-23 19:00:21 -07001032 private String getTypedText() {
1033 return mDefaultKeySsb.toString();
1034 }
1035
Sunny Goyal6f28e712016-09-13 14:19:29 -07001036 @Override
1037 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001038 mDefaultKeySsb.clear();
1039 mDefaultKeySsb.clearSpans();
1040 Selection.setSelection(mDefaultKeySsb, 0);
1041 }
1042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 /**
1044 * Restores the previous state, if it exists.
1045 *
1046 * @param savedState The previous state.
1047 */
1048 private void restoreState(Bundle savedState) {
1049 if (savedState == null) {
1050 return;
1051 }
1052
Sunny Goyalfe770c92016-09-27 14:38:58 -07001053 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1054 State[] stateValues = State.values();
1055 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1056 ? stateValues[stateOrdinal] : State.WORKSPACE;
Sunny Goyal53b99362017-10-05 14:58:56 -07001057 if (state == State.APPS) {
1058 showAppsView(false /* animated */);
1059 } else if (state == State.WIDGETS) {
1060 showWidgetsView(false, false);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001061 }
1062
Sunny Goyal2100c782016-08-22 16:00:03 -07001063 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1064 if (requestArgs != null) {
1065 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001067
1068 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 }
1070
1071 /**
1072 * Finds all the views we need and configure them properly.
1073 */
1074 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001075 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001076 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001077 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001078 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001079
Sunny Goyal784f9c32016-03-23 16:56:54 -07001080 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1081 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1082 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
Winson Chung4c98d922011-05-31 16:50:48 -07001084 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001085 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001086
Winson Chung3d503fb2011-07-13 17:25:49 -07001087 // Setup the hotseat
1088 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1089 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001090 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 }
1092
Winsone9f27272015-10-13 10:47:51 -07001093 // Setup the overview panel
1094 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001095
Winson Chung4c98d922011-05-31 16:50:48 -07001096 // Setup the workspace
1097 mWorkspace.setHapticFeedbackEnabled(false);
1098 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001099 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001100 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1101 // default state, otherwise we will update to the wrong offsets in RTL
1102 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001103 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001104 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001105
Tony Wickham34d2c912015-09-11 09:27:58 -07001106 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001107 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001108
Winson Chungef7f8742015-06-04 17:18:17 -07001109 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001110 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001111 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001112
Winson Chung3d503fb2011-07-13 17:25:49 -07001113 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001114 mDragController.setMoveTarget(mWorkspace);
1115 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001116 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001117
Hyunyoung Songd725f642017-08-17 22:26:35 -07001118 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001119
Sunny Goyal322d5562015-06-25 19:35:49 -07001120 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1121 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124
Winsone9f27272015-10-13 10:47:51 -07001125 private void setupOverviewPanel() {
1126 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1127
Winsone9f27272015-10-13 10:47:51 -07001128 // Bind wallpaper button actions
1129 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301130 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001131 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001132 public void handleViewClick(View view) {
1133 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001134 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001135 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001136
1137 // Bind widget button actions
1138 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301139 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001140 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001141 public void handleViewClick(View view) {
1142 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001143 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001144 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001145
1146 // Bind settings actions
1147 View settingsButton = findViewById(R.id.settings_button);
1148 boolean hasSettings = hasSettings();
1149 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301150 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001151 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001152 public void handleViewClick(View view) {
1153 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001154 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001155 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001156 } else {
1157 settingsButton.setVisibility(View.GONE);
1158 }
1159
1160 mOverviewPanel.setAlpha(0f);
1161 }
1162
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001164 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001165 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001166 */
1167 public void setAllAppsButton(View allAppsButton) {
1168 mAllAppsButton = allAppsButton;
1169 }
1170
Sunny Goyalbb011da2016-06-15 15:42:29 -07001171 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001172 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001173 }
1174
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001175 public View getWidgetsButton() {
1176 return mWidgetsButton;
1177 }
1178
Anjali Koppal5ad44842014-03-10 20:34:39 -07001179 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 * Creates a view representing a shortcut.
1181 *
1182 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001184 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001185 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
1187
1188 /**
1189 * Creates a view representing a shortcut inflated from the specified resource.
1190 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 * @param parent The group the shortcut belongs to.
1192 * @param info The data structure describing the shortcut.
1193 *
1194 * @return A View inflated from layoutResId.
1195 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001196 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001197 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1198 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001199 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001201 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 return favorite;
1203 }
1204
1205 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001206 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 *
1208 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001210 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001211 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001212 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1213 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001214 return;
1215 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001216
Jon Mirandac476d6e2017-05-04 16:30:01 -07001217 int[] cellXY = mTmpAddItemCellCoordinates;
1218 CellLayout layout = getCellLayout(container, screenId);
1219
Sunny Goyal782f0c92017-01-19 10:27:54 -08001220 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001221 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001222 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1223 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001224 }
1225
1226 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001227 // Legacy shortcuts are only supported for primary profile.
1228 info = Process.myUserHandle().equals(args.user)
1229 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001230
Sunny Goyalb57645f2017-03-20 13:57:28 -07001231 if (info == null) {
1232 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1233 return;
1234 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001235 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001236 // The app is trying to add a shortcut without sufficient permissions
1237 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1238 return;
1239 }
1240 }
1241
Jon Mirandac476d6e2017-05-04 16:30:01 -07001242 if (container < 0) {
1243 // Adding a shortcut to the Workspace.
1244 final View view = createShortcut(info);
1245 boolean foundCellSpan = false;
1246 // First we check if we already know the exact location where we want to add this item.
1247 if (cellX >= 0 && cellY >= 0) {
1248 cellXY[0] = cellX;
1249 cellXY[1] = cellY;
1250 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001251
Jon Mirandac476d6e2017-05-04 16:30:01 -07001252 // If appropriate, either create a folder or add to an existing folder
1253 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1254 true, null, null)) {
1255 return;
1256 }
1257 DragObject dragObject = new DragObject();
1258 dragObject.dragInfo = info;
1259 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1260 true)) {
1261 return;
1262 }
1263 } else {
1264 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1265 }
1266
1267 if (!foundCellSpan) {
1268 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001269 return;
1270 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001271
1272 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1273 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001274 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001275 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001276 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001277 if (folderIcon != null) {
1278 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1279 folderInfo.add(info, args.rank, false);
1280 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001281 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001282 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001283 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285
Sunny Goyale29897f2017-07-20 10:09:42 -07001286 public FolderIcon findFolderIcon(final long folderIconId) {
1287 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1288 @Override
1289 public boolean evaluate(ItemInfo info, View view) {
1290 return info != null && info.id == folderIconId;
1291 }
1292 });
1293 }
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001296 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001298 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001300 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001301 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1302
Adam Cohened66b2b2012-01-23 17:28:51 -08001303 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001304 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001305 }
Romain Guycbb89e42009-06-08 15:52:54 -07001306
Adam Cohen59400422014-03-05 18:07:04 -08001307 LauncherAppWidgetInfo launcherInfo;
1308 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001309 launcherInfo.spanX = itemInfo.spanX;
1310 launcherInfo.spanY = itemInfo.spanY;
1311 launcherInfo.minSpanX = itemInfo.minSpanX;
1312 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001313 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001315 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001316 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317
Sunny Goyal2100c782016-08-22 16:00:03 -07001318 if (hostView == null) {
1319 // Perform actual inflation because we're live
1320 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001322 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301323 prepareAppWidget(hostView, launcherInfo);
1324 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
Romain Guycbb89e42009-06-08 15:52:54 -07001326
Sunny Goyald5462aa2016-11-22 16:52:39 +05301327 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001328 hostView.setTag(item);
1329 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001330 hostView.setFocusable(true);
1331 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001332 }
1333
Adam Cohended9f8d2010-11-03 13:25:16 -07001334 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1335 @Override
1336 public void onReceive(Context context, Intent intent) {
1337 final String action = intent.getAction();
1338 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001339 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001340 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001341 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001342 if (!showWorkspace(false)) {
1343 // If we are already on the workspace, then manually reset all apps
1344 mAppsView.reset();
1345 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001346 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001347 mShouldFadeInScrim = true;
1348 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1349 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1350 // the user unlocked and the Launcher is not in the foreground.
1351 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001352 }
1353 }
1354 };
1355
Tony Wickham010d2552017-01-20 08:15:28 -08001356 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1357 Runnable r = new Runnable() {
1358 @Override
1359 public void run() {
1360 mWorkspace.updateIconBadges(updatedBadges);
1361 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001362
1363 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1364 if (popup != null) {
1365 popup.updateNotificationHeader(updatedBadges);
1366 }
Tony Wickham010d2552017-01-20 08:15:28 -08001367 }
1368 };
1369 if (!waitUntilResume(r)) {
1370 r.run();
1371 }
1372 }
1373
Adam Cohended9f8d2010-11-03 13:25:16 -07001374 @Override
1375 public void onAttachedToWindow() {
1376 super.onAttachedToWindow();
1377
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001378 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001379 if (mLauncherCallbacks != null) {
1380 mLauncherCallbacks.onAttachedToWindow();
1381 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001382 }
1383
1384 @Override
1385 public void onDetachedFromWindow() {
1386 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001387
1388 if (mLauncherCallbacks != null) {
1389 mLauncherCallbacks.onDetachedFromWindow();
1390 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001391 }
1392
Winson Chunga6945242014-01-08 14:04:34 -08001393 public DragLayer getDragLayer() {
1394 return mDragLayer;
1395 }
1396
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001397 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001398 return mAppsView;
1399 }
1400
Hyunyoung Song3f471442015-04-08 19:01:34 -07001401 public WidgetsContainerView getWidgetsView() {
1402 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001403 }
1404
Winson Chunga6945242014-01-08 14:04:34 -08001405 public Workspace getWorkspace() {
1406 return mWorkspace;
1407 }
1408
1409 public Hotseat getHotseat() {
1410 return mHotseat;
1411 }
1412
Jorim Jaggid017f882014-01-14 17:08:48 -08001413 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001414 return mOverviewPanel;
1415 }
1416
Sunny Goyal47328fd2016-05-25 18:56:41 -07001417 public DropTargetBar getDropTargetBar() {
1418 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001419 }
1420
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001421 public LauncherAppWidgetHost getAppWidgetHost() {
1422 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
Romain Guycbb89e42009-06-08 15:52:54 -07001424
Winson Chunga9abd0e2010-10-27 17:18:37 -07001425 public LauncherModel getModel() {
1426 return mModel;
1427 }
1428
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001429 public ModelWriter getModelWriter() {
1430 return mModelWriter;
1431 }
1432
Adam Cohen79d90c52016-04-22 13:29:20 -07001433 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001434 return mSharedPrefs;
1435 }
1436
Jon Miranda6bed3502017-09-19 14:43:17 -07001437 public int getOrientation() { return mOrientation; }
1438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 @Override
1440 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001441 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 super.onNewIntent(intent);
1443
Winson15f8b172015-08-19 11:02:39 -07001444 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1445 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1446 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001447
1448 // Check this condition before handling isActionMain, as this will get reset.
1449 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001450 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001451
Winson15f8b172015-08-19 11:02:39 -07001452 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1453 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001454 if (mWorkspace == null) {
1455 // Can be cases where mWorkspace is null, this prevents a NPE
1456 return;
1457 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001458
1459 // Note: There should be at most one log per method call. This is enforced implicitly
1460 // by using if-else statements.
1461 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001462 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001463 if (topOpenView != null) {
1464 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001465 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301466 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001467 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1468 }
1469
1470 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001471 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal53b99362017-10-05 14:58:56 -07001472 showWorkspace(alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001473
1474 final View v = getWindow().peekDecorView();
1475 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001476 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001477 }
1478
Winson Chungb745afb2015-03-02 11:51:23 -08001479 // Reset the apps view
1480 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001481 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001482 }
1483
Hyunyoung Song3f471442015-04-08 19:01:34 -07001484 // Reset the widgets view
1485 if (!alreadyOnHome && mWidgetsView != null) {
1486 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001487 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001488
Adam Cohen9211d422014-10-07 18:14:53 -07001489 if (mLauncherCallbacks != null) {
1490 mLauncherCallbacks.onHomeIntent();
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001493 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001494
Adam Cohen9211d422014-10-07 18:14:53 -07001495 if (mLauncherCallbacks != null) {
1496 mLauncherCallbacks.onNewIntent(intent);
1497 }
Winson15f8b172015-08-19 11:02:39 -07001498
1499 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1500 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1501 // animation.
1502 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001503 boolean callbackAllowsMoveToDefaultScreen =
1504 mLauncherCallbacks == null || mLauncherCallbacks
1505 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001506 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1507 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001508
Winson15f8b172015-08-19 11:02:39 -07001509 mWorkspace.post(new Runnable() {
1510 @Override
1511 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001512 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001513 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001514 }
Winson15f8b172015-08-19 11:02:39 -07001515 }
1516 });
1517 }
1518 }
1519
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001520 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001521 }
1522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001524 public void onRestoreInstanceState(Bundle state) {
1525 super.onRestoreInstanceState(state);
1526 for (int page: mSynchronouslyBoundPages) {
1527 mWorkspace.restoreInstanceStateForChild(page);
1528 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530
1531 @Override
1532 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001533 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001534 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001535
Adam Cohen21cd0022013-10-09 18:57:02 -07001536 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001537 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
Michael Jurka883f55b2010-10-21 15:47:14 -07001539 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001540 // We close any open folders and shortcut containers since they will not be re-opened,
1541 // and we need to make sure this state is reflected.
1542 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001543
Sunny Goyal2100c782016-08-22 16:00:03 -07001544 if (mPendingRequestArgs != null) {
1545 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1546 }
1547 if (mPendingActivityResult != null) {
1548 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550
Adam Cohen9211d422014-10-07 18:14:53 -07001551 if (mLauncherCallbacks != null) {
1552 mLauncherCallbacks.onSaveInstanceState(outState);
1553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555
1556 @Override
1557 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001559
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001560 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001561 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001562
Winson Chungcd2b0142011-06-08 16:02:26 -07001563 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001564 // It's possible to receive onDestroy after a new Launcher activity has
1565 // been created. In this case, don't interfere with the new Launcher.
1566 if (mModel.isCurrentCallbacks(this)) {
1567 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001568 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001569 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001570
Sunny Goyal745bad92016-05-02 10:54:12 -07001571 if (mRotationPrefChangeHandler != null) {
1572 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1573 }
1574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001576 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001578 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001580 mAppWidgetHost = null;
1581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 TextKeyListener.getInstance().release();
1583
Tony Wickhame2217252016-03-22 16:34:23 -07001584 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1585 .removeAccessibilityStateChangeListener(this);
1586
Mario Bertschler27288382017-05-24 15:35:09 -07001587 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1588
Michael Jurka2ecf9952012-06-18 12:52:28 -07001589 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001590
Tony2917a8b2017-07-31 23:29:42 -07001591 clearPendingBinds();
1592
Adam Cohen9211d422014-10-07 18:14:53 -07001593 if (mLauncherCallbacks != null) {
1594 mLauncherCallbacks.onDestroy();
1595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
1597
Sunny Goyalae502842016-06-17 08:43:56 -07001598 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1599 return mAccessibilityDelegate;
1600 }
1601
Adam Cohena9cf38f2011-05-02 15:36:58 -07001602 public DragController getDragController() {
1603 return mDragController;
1604 }
1605
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001607 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001608 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001609 }
1610
1611 @Override
1612 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1613 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001614 try {
1615 super.startIntentSenderForResult(intent, requestCode,
1616 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1617 } catch (IntentSender.SendIntentException e) {
1618 throw new ActivityNotFoundException();
1619 }
1620 }
1621
Winson Chung70d72102011-08-12 11:24:35 -07001622 /**
1623 * Indicates that we want global search for this activity by setting the globalSearch
1624 * argument for {@link #startSearch} to true.
1625 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001627 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001629
Karl Rosaen138a0412009-04-23 19:00:21 -07001630 if (initialQuery == null) {
1631 // Use any text typed in the launcher as the initial query
1632 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 if (appSearchData == null) {
1635 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001636 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001638
Sunny Goyal6f28e712016-09-13 14:19:29 -07001639 if (mLauncherCallbacks == null ||
1640 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1641 // Starting search from the callbacks failed. Start the default global search.
1642 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001643 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001644
1645 // We need to show the workspace after starting the search
1646 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001647 }
1648
Ian Parkinson047036e2014-09-01 15:40:53 +01001649 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001650 * Starts the global search activity. This code is a copied from SearchManager
1651 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001652 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001653 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001654 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001655 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1656 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1657 if (globalSearchActivity == null) {
1658 Log.w(TAG, "No global search activity found.");
1659 return;
1660 }
1661 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1662 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1663 intent.setComponent(globalSearchActivity);
1664 // Make sure that we have a Bundle to put source in
1665 if (appSearchData == null) {
1666 appSearchData = new Bundle();
1667 } else {
1668 appSearchData = new Bundle(appSearchData);
1669 }
Adam Cohen9211d422014-10-07 18:14:53 -07001670 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001671 if (!appSearchData.containsKey("source")) {
1672 appSearchData.putString("source", getPackageName());
1673 }
1674 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1675 if (!TextUtils.isEmpty(initialQuery)) {
1676 intent.putExtra(SearchManager.QUERY, initialQuery);
1677 }
1678 if (selectInitialQuery) {
1679 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1680 }
1681 intent.setSourceBounds(sourceBounds);
1682 try {
1683 startActivity(intent);
1684 } catch (ActivityNotFoundException ex) {
1685 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 }
1688
Winson Chung70d72102011-08-12 11:24:35 -07001689 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001690 public boolean onPrepareOptionsMenu(Menu menu) {
1691 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001692 if (mLauncherCallbacks != null) {
1693 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1694 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001695 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001698 @Override
1699 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001700 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001701 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001702 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001703 }
1704
Joe Onorato9c1289c2009-08-17 11:03:03 -04001705 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001706 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707 }
1708
Adam Cohen517a7f52014-03-01 12:12:59 -08001709 public boolean isWorkspaceLoading() {
1710 return mWorkspaceLoading;
1711 }
1712
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001713 private void setWorkspaceLoading(boolean value) {
1714 boolean isLocked = isWorkspaceLocked();
1715 mWorkspaceLoading = value;
1716 if (isLocked != isWorkspaceLocked()) {
1717 onWorkspaceLockedChanged();
1718 }
1719 }
1720
Sunny Goyal04a324a2017-01-20 21:08:59 -08001721 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001722 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001723 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001724 if (isLocked != isWorkspaceLocked()) {
1725 onWorkspaceLockedChanged();
1726 }
1727 }
1728
Adam Cohen9211d422014-10-07 18:14:53 -07001729 protected void onWorkspaceLockedChanged() {
1730 if (mLauncherCallbacks != null) {
1731 mLauncherCallbacks.onWorkspaceLockedChanged();
1732 }
1733 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001734
Sunny Goyal2100c782016-08-22 16:00:03 -07001735 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001736 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001737 if (LOGD) {
1738 Log.d(TAG, "Adding widget from drop");
1739 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001740 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001741 }
1742
Sunny Goyal2100c782016-08-22 16:00:03 -07001743 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001744 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1745 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1746 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001747
Adam Cohenad4e15c2013-10-17 16:21:35 -07001748 Runnable onComplete = new Runnable() {
1749 @Override
1750 public void run() {
1751 // Exit spring loaded mode if necessary after adding the widget
1752 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1753 null);
1754 }
1755 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001756 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001757 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
1759 }
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001761 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1762 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001763 info.container = container;
1764 info.screenId = screenId;
1765 if (cell != null) {
1766 info.cellX = cell[0];
1767 info.cellY = cell[1];
1768 }
1769 info.spanX = spanX;
1770 info.spanY = spanY;
1771
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001772 switch (info.itemType) {
1773 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1774 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001775 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001776 break;
1777 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001778 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001779 break;
1780 default:
1781 throw new IllegalStateException("Unknown item type: " + info.itemType);
1782 }
1783 }
1784
Adam Cohenfbba09b2011-07-18 21:43:05 -07001785 /**
1786 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001787 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001788 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001789 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1790 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001791 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1792 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1793 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001794 }
1795
Adam Cohenfbba09b2011-07-18 21:43:05 -07001796 /**
1797 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001798 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001799 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001800 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001801 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001802 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001803 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001804 // In the case where we've prebound the widget, we remove it from the DragLayer
1805 if (LOGD) {
1806 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1807 }
1808 getDragLayer().removeView(hostView);
1809
Adam Cohened66b2b2012-01-23 17:28:51 -08001810 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001811 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001812
1813 // Clear the boundWidget so that it doesn't get destroyed.
1814 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001815 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001816 // In this case, we either need to start an activity to get permission to bind
1817 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001818 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1819 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1820 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1821 this, info.componentName);
1822 } else {
1823 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1824 }
Adam Cohendd70d662012-10-04 16:53:44 -07001825 Bundle options = info.bindOptions;
1826
Sunny Goyalffe83f12014-08-14 17:39:34 -07001827 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1828 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001829 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001830 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001831 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001832 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001833 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001834 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001835 }
1836
Adam Cohendcd297f2013-06-18 13:13:40 -07001837 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001838 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001839 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 folderInfo.title = getText(R.string.folder_name);
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001843 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844
1845 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001846 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301847 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001848 // Force measure the new folder icon
1849 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1850 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001851 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
Romain Guycbb89e42009-06-08 15:52:54 -07001853
Winsonc0b52fe2015-09-09 16:38:15 -07001854 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001855 * Unbinds the view for the specified item, and removes the item and all its children.
1856 *
1857 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001858 * @param itemInfo the {@link ItemInfo} for this view.
1859 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001860 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001861 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001862 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001863 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001864 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1865 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001866 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001867 } else {
1868 mWorkspace.removeWorkspaceItem(v);
1869 }
Winsonc0b52fe2015-09-09 16:38:15 -07001870 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001871 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001872 }
1873 } else if (itemInfo instanceof FolderInfo) {
1874 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001875 if (v instanceof FolderIcon) {
1876 ((FolderIcon) v).removeListeners();
1877 }
Winsonc0b52fe2015-09-09 16:38:15 -07001878 mWorkspace.removeWorkspaceItem(v);
1879 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001880 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001881 }
1882 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1883 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001884 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001885 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001886 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001887 }
1888 } else {
1889 return false;
1890 }
1891 return true;
1892 }
1893
1894 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001895 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001896 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001897 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001898 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001899 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001900 // Deleting an app widget ID is a void call but writes to disk before returning
1901 // to the caller...
1902 new AsyncTask<Void, Void, Void>() {
1903 public Void doInBackground(Void ... args) {
1904 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1905 return null;
1906 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001907 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001908 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001909 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 @Override
1913 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001914 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916
Joe Onorato88ec0992009-11-19 13:16:06 -08001917 @Override
1918 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001919 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1920 return;
1921 }
1922
Sunny Goyal45478022015-06-08 16:52:41 -07001923 if (mDragController.isDragging()) {
1924 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001925 return;
1926 }
1927
Jon Mirandafeba90f2016-10-06 10:53:29 -07001928 // Note: There should be at most one log per method call. This is enforced implicitly
1929 // by using if-else statements.
1930 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001931 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1932 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001933 topView.onBackPressed();
Tony Wickham49c8d292016-07-01 11:44:34 -07001934 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301935 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001936 showWorkspace(true);
1937 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301938 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08001939 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001940 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301941 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001942 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001943 } else {
1944 // Back button is a no-op here, but give at least some feedback for the button press
1945 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001946 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001947 }
1948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 * Launches the intent referred by the clicked shortcut.
1951 *
1952 * @param v The view representing the clicked shortcut.
1953 */
1954 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001955 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1956 // view has detached (it's possible for this to happen if the view is removed mid touch).
1957 if (v.getWindowToken() == null) {
1958 return;
1959 }
1960
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001961 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001962 return;
1963 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001964
Adam Cohen1697b792013-09-17 19:08:21 -07001965 if (v instanceof Workspace) {
1966 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001967 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1968 LauncherLogProto.Action.Direction.NONE,
1969 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07001970 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001971 }
1972 return;
1973 }
1974
1975 if (v instanceof CellLayout) {
1976 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001977 int page = mWorkspace.indexOfChild(v);
1978 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1979 LauncherLogProto.Action.Direction.NONE,
1980 LauncherLogProto.ContainerType.OVERVIEW, page);
1981 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001982 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001983 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001984 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001988 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001989 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001992 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001993 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001994 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001995 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001996 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001997 } else if (tag instanceof AppInfo) {
1998 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001999 } else if (tag instanceof LauncherAppWidgetInfo) {
2000 if (v instanceof PendingAppWidgetHostView) {
2001 onClickPendingWidget((PendingAppWidgetHostView) v);
2002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
2004 }
2005
Sunny Goyal70660032015-05-14 00:07:08 -07002006 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002007 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002008 return false;
2009 }
2010
Michael Jurkaaf442092010-06-10 17:01:57 -07002011 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002012 * Event handler for the app widget view which has not fully restored.
2013 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002014 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002015 if (mIsSafeModeEnabled) {
2016 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2017 return;
2018 }
2019
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002020 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002021 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002022 LauncherAppWidgetProviderInfo appWidgetInfo =
2023 mAppWidgetManager.findProvider(info.providerName, info.user);
2024 if (appWidgetInfo == null) {
2025 return;
2026 }
2027 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2028
Sunny Goyald478c832016-04-01 12:04:16 -07002029 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2030 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2031 // This should not happen, as we make sure that an Id is allocated during bind.
2032 return;
2033 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002034 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2035 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002036 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002037 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002038 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002039 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002040 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002041 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002042 }
2043 }
2044
2045 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002046 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2047 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2048 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002049 *
2050 * @param v The view that was clicked.
2051 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002052 protected void onClickAllAppsButton(View v) {
2053 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002054 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302055 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2056 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002057 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002058 } else {
2059 showWorkspace(true);
2060 }
2061 }
2062
Sunny Goyale6e72002017-01-12 16:55:36 -08002063 private void onClickPendingAppItem(final View v, final String packageName,
2064 boolean downloadStarted) {
2065 if (downloadStarted) {
2066 // If the download has started, simply direct to the market app.
2067 startMarketIntentForPackage(v, packageName);
2068 return;
2069 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002070 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002071 .setTitle(R.string.abandoned_promises_title)
2072 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002073 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2074 @Override
2075 public void onClick(DialogInterface dialogInterface, int i) {
2076 startMarketIntentForPackage(v, packageName);
2077 }
2078 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002079 .setNeutralButton(R.string.abandoned_clean_this,
2080 new DialogInterface.OnClickListener() {
2081 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002082 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002083 mWorkspace.removeAbandonedPromise(packageName, user);
2084 }
2085 })
2086 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002087 }
2088
2089 private void startMarketIntentForPackage(View v, String packageName) {
2090 ItemInfo item = (ItemInfo) v.getTag();
2091 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002092 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002093 }
2094
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002095 /**
2096 * Event handler for an app shortcut click.
2097 *
2098 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2099 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002100 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002101 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2102 Object tag = v.getTag();
2103 if (!(tag instanceof ShortcutInfo)) {
2104 throw new IllegalArgumentException("Input must be a Shortcut");
2105 }
2106
2107 // Open shortcut
2108 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002109
2110 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002111 if ((shortcut.isDisabled &
2112 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2113 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2114 // If the app is only disabled because of the above flags, launch activity anyway.
2115 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002116 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002117 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2118 // Use a message specific to this shortcut, if it has one.
2119 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2120 return;
2121 }
2122 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002123 int error = R.string.activity_not_available;
2124 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2125 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002126 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2127 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002128 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002129 }
2130 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2131 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002132 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002133 }
2134
Chris Wren40c5ed32014-06-24 18:24:23 -04002135 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002136 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002137 String packageName = shortcut.intent.getComponent() != null ?
2138 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2139 if (!TextUtils.isEmpty(packageName)) {
2140 onClickPendingAppItem(v, packageName,
2141 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2142 return;
2143 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002144 }
2145
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002146 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002147 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002148 }
2149
Sunny Goyala7ce1662016-05-31 15:01:35 -07002150 private void startAppShortcutOrInfoActivity(View v) {
2151 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002152 Intent intent;
2153 if (item instanceof PromiseAppInfo) {
2154 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2155 intent = promiseAppInfo.getMarketIntent();
2156 } else {
2157 intent = item.getIntent();
2158 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002159 if (intent == null) {
2160 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002161 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002162 startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002163 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002164 }
2165
2166 /**
2167 * Event handler for a folder icon click.
2168 *
2169 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2170 */
2171 protected void onClickFolderIcon(View v) {
2172 if (LOGD) Log.d(TAG, "onClickFolder");
2173 if (!(v instanceof FolderIcon)){
2174 throw new IllegalArgumentException("Input must be a FolderIcon");
2175 }
2176
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002177 Folder folder = ((FolderIcon) v).getFolder();
2178 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002179 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002180 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002181 }
Michael Jurka5130e402011-10-13 04:55:35 -07002182 }
2183
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002184 /**
2185 * Event handler for the (Add) Widgets button that appears after a long press
2186 * on the home screen.
2187 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002188 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002189 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002190 if (mIsSafeModeEnabled) {
2191 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2192 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002193 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002194 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002195 }
2196
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002197 /**
2198 * Event handler for the wallpaper picker button that appears after a long press
2199 * on the home screen.
2200 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002201 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002202 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002203 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2204 return;
2205 }
2206
Tony Wickham785f7a52015-08-31 17:28:32 -07002207 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2208 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002209 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002210 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002211 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002212
2213 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002214 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2215 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002216 intent.setPackage(pickerPackage);
2217 }
2218
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002219 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002220 try {
2221 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2222 // If there is no target package, use the default intent chooser animation
2223 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2224 } catch (ActivityNotFoundException e) {
2225 setWaitingForResult(null);
2226 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2227 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002228 }
2229
2230 /**
2231 * Event handler for a click on the settings button that appears after a long press
2232 * on the home screen.
2233 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002234 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002235 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002236 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2237 .setPackage(getPackageName());
2238 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002239 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002240 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002241 }
2242
Tony Wickhame2217252016-03-22 16:34:23 -07002243 @Override
2244 public void onAccessibilityStateChanged(boolean enabled) {
2245 mDragLayer.onAccessibilityStateChanged(enabled);
2246 }
2247
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002248 /**
2249 * Called when the user stops interacting with the launcher.
2250 * This implies that the user is now on the homescreen and is not doing housekeeping.
2251 */
Adam Cohen9211d422014-10-07 18:14:53 -07002252 protected void onInteractionEnd() {
2253 if (mLauncherCallbacks != null) {
2254 mLauncherCallbacks.onInteractionEnd();
2255 }
2256 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002257
2258 /**
2259 * Called when the user starts interacting with the launcher.
2260 * The possible interactions are:
2261 * - open all apps
2262 * - reorder an app shortcut, or a widget
2263 * - open the overview mode.
2264 * This is a good time to stop doing things that only make sense
2265 * when the user is on the homescreen and not doing housekeeping.
2266 */
Adam Cohen9211d422014-10-07 18:14:53 -07002267 protected void onInteractionBegin() {
2268 if (mLauncherCallbacks != null) {
2269 mLauncherCallbacks.onInteractionBegin();
2270 }
2271 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002272
Winson Chungcd99cd32015-04-29 11:03:24 -07002273 /** Updates the interaction state. */
2274 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002275 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002276 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002277 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2278 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002279 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002280 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002281 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002282 onInteractionEnd();
2283 }
2284 }
2285
Sunny Goyala7ce1662016-05-31 15:01:35 -07002286 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002288 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2289 try {
2290 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2291 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2292 // is enabled by default on NYC.
2293 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2294 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002295
2296 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2297 String id = ((ShortcutInfo) info).getDeepShortcutId();
2298 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302299 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002300 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002301 } else {
2302 // Could be launching some bookkeeping activity
2303 startActivity(intent, optsBundle);
2304 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002305 } finally {
2306 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002309 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2310 // corresponding permission. Show the appropriate permission prompt if that
2311 // is the case.
2312 if (intent.getComponent() == null
2313 && Intent.ACTION_CALL.equals(intent.getAction())
2314 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2315 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002316
2317 setWaitingForResult(PendingRequestArgs
2318 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002319 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2320 REQUEST_PERMISSION_CALL_PHONE);
2321 } else {
2322 // No idea why this was thrown.
2323 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 }
2326 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002327
Sunny Goyalfe770c92016-09-27 14:38:58 -07002328 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002329 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002330 if (Utilities.ATLEAST_MARSHMALLOW) {
2331 int left = 0, top = 0;
2332 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002333 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002334 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002335 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002336 if (icon != null) {
2337 Rect bounds = icon.getBounds();
2338 left = (width - bounds.width()) / 2;
2339 top = v.getPaddingTop();
2340 width = bounds.width();
2341 height = bounds.height();
2342 }
2343 }
2344 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2345 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2346 // On L devices, we use the device default slide-up transition.
2347 // On L MR1 devices, we use a custom version of the slide-up transition which
2348 // doesn't have the delay present in the device default.
2349 return ActivityOptions.makeCustomAnimation(
2350 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2351 }
2352 return null;
2353 }
2354
Tonye3c59252017-05-02 21:32:27 -07002355 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002356 int[] pos = new int[2];
2357 v.getLocationOnScreen(pos);
2358 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2359 }
2360
Sunny Goyala7ce1662016-05-31 15:01:35 -07002361 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002362 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2363 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2364 return false;
2365 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002366 // Only launch using the new animation if the shortcut has not opted out (this is a
2367 // private contract between launcher and may be ignored in the future).
2368 boolean useLaunchAnimation = (v != null) &&
2369 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2370 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2371
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002372 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002373
2374 // Prepare intent
2375 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2376 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002377 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002378 }
2379 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002380 if (Utilities.ATLEAST_MARSHMALLOW
2381 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002382 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002383 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002384 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002385 // Shortcuts need some special checks due to legacy reasons.
2386 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002387 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002388 // Could be launching some bookkeeping activity
2389 startActivity(intent, optsBundle);
2390 } else {
2391 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2392 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2393 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002394
2395 if (v instanceof BubbleTextView) {
2396 // This is set to the view that launched the activity that navigated the user away
2397 // from launcher. Since there is no callback for when the activity has finished
2398 // launching, enable the press state and keep this reference to reset the press
2399 // state when we return to launcher.
2400 BubbleTextView btv = (BubbleTextView) v;
2401 btv.setStayPressed(true);
2402 setOnResumeCallback(btv);
2403 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002404 return true;
2405 } catch (ActivityNotFoundException|SecurityException e) {
2406 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2407 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2408 }
2409 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002410 }
2411
Tony Wickhamdadb3042016-02-24 11:07:00 -08002412 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002413 public boolean dispatchTouchEvent(MotionEvent ev) {
2414 mLastDispatchTouchEventX = ev.getX();
2415 return super.dispatchTouchEvent(ev);
2416 }
2417
2418 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002420 if (!isDraggingEnabled()) return false;
2421 if (isWorkspaceLocked()) return false;
2422 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423
Jon Miranda51f037d2016-11-07 08:37:20 -08002424 boolean ignoreLongPressToOverview =
2425 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002426
Adam Cohen1697b792013-09-17 19:08:21 -07002427 if (v instanceof Workspace) {
2428 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002429 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302430 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2431 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002432 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002433 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002434 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2435 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2436 return true;
2437 } else {
2438 return false;
2439 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002440 } else {
2441 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002442 }
Adam Cohen1697b792013-09-17 19:08:21 -07002443 }
2444
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002445 CellLayout.CellInfo longClickCellInfo = null;
2446 View itemUnderLongClick = null;
2447 if (v.getTag() instanceof ItemInfo) {
2448 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002449 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002450 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002451 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 }
2453
Winson Chung3d503fb2011-07-13 17:25:49 -07002454 // The hotseat touch handling does not go through Workspace, and we always allow long press
2455 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002456 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002457 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002458 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002459 if (mWorkspace.isInOverviewMode()) {
2460 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302461 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2462 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002463 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002464 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002465 return false;
2466 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302467 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2468 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002469 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002470 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002471 }
Jon Miranda379198e2016-09-23 08:54:40 -07002472 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2473 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002475 final boolean isAllAppsButton =
2476 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2477 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2478 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002479 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002481 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 }
2483 }
2484 }
2485 return true;
2486 }
2487
Winson Chung3d503fb2011-07-13 17:25:49 -07002488 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002489 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002490 return mHotseat != null && layout != null &&
2491 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2492 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002493
Winson Chung3d503fb2011-07-13 17:25:49 -07002494 /**
2495 * Returns the CellLayout of the specified container at the specified screen.
2496 */
Sunny Goyal92820592015-03-02 11:31:35 -08002497 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002498 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2499 if (mHotseat != null) {
2500 return mHotseat.getLayout();
2501 } else {
2502 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002503 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002504 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002505 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002506 }
2507 }
2508
Winson Chungb745afb2015-03-02 11:51:23 -08002509 /**
2510 * For overridden classes.
2511 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002512 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002513 return isAppsViewVisible();
2514 }
2515
2516 public boolean isAppsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002517 return mState == State.APPS;
Winson Chungb745afb2015-03-02 11:51:23 -08002518 }
2519
2520 public boolean isWidgetsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002521 return mState == State.WIDGETS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002522 }
2523
Michael Jurkae326f182011-11-21 14:05:46 -08002524 @Override
2525 public void onTrimMemory(int level) {
2526 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002527 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2528 // The widget preview db can result in holding onto over
2529 // 3MB of memory for caching which isn't necessary.
2530 SQLiteDatabase.releaseMemory();
2531
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002532 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002533 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002534 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002535 if (mLauncherCallbacks != null) {
2536 mLauncherCallbacks.onTrimMemory(level);
2537 }
Michael Jurkae326f182011-11-21 14:05:46 -08002538 }
2539
Winson5c6bdbb2015-09-03 11:36:19 -07002540 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002541 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002542 }
2543
Winson5c6bdbb2015-09-03 11:36:19 -07002544 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002545 boolean changed = mState != State.WORKSPACE ||
2546 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002547 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002548 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002549 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002550 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002551
Michael Jurkab3e22d92011-10-31 15:58:33 -07002552 // Set focus to the AppsCustomize button
2553 if (mAllAppsButton != null) {
2554 mAllAppsButton.requestFocus();
2555 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002556 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002557
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002558 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002559 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002560
Winson Chung0f785722015-04-08 10:27:49 -07002561 if (changed) {
2562 // Send an accessibility event to announce the context change
2563 getWindow().getDecorView()
2564 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002565 }
Winson5c6bdbb2015-09-03 11:36:19 -07002566 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002567 }
2568
Winsone9f27272015-10-13 10:47:51 -07002569 /**
2570 * Shows the overview button.
2571 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002572 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002573 showOverviewMode(animated, false);
2574 }
2575
2576 /**
2577 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2578 * onto one of the overview panel buttons.
2579 */
2580 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2581 Runnable postAnimRunnable = null;
2582 if (requestButtonFocus) {
2583 postAnimRunnable = new Runnable() {
2584 @Override
2585 public void run() {
2586 // Hitting the menu button when in touch mode does not trigger touch mode to
2587 // be disabled, so if requested, force focus on one of the overview panel
2588 // buttons.
2589 mOverviewPanel.requestFocusFromTouch();
2590 }
2591 };
2592 }
Adam Cohened307df2013-10-02 09:37:31 -07002593 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002594 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002595 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002596 setState(State.WORKSPACE);
2597
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002598 // If animated from long press, then don't allow any of the controller in the drag
2599 // layer to intercept any remaining touch.
2600 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002601 }
2602
Mario Bertschleracbf5702017-03-03 10:58:06 -08002603 private void setState(State state) {
2604 this.mState = state;
2605 updateSoftInputMode();
2606 }
2607
2608 private void updateSoftInputMode() {
2609 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2610 final int mode;
2611 if (isAppsViewVisible()) {
2612 mode = SOFT_INPUT_MODE_ALL_APPS;
2613 } else {
2614 mode = SOFT_INPUT_MODE_DEFAULT;
2615 }
2616 getWindow().setSoftInputMode(mode);
2617 }
2618 }
2619
Winson Chungb745afb2015-03-02 11:51:23 -08002620 /**
2621 * Shows the apps view.
2622 */
Sunny Goyald0d07032017-10-04 10:40:28 -07002623 public void showAppsView(boolean animated) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002624 markAppsViewShown();
Sunny Goyal5606e072017-06-15 14:24:21 -07002625 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002626 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002627
Winson Chungb745afb2015-03-02 11:51:23 -08002628 /**
2629 * Shows the widgets view.
2630 */
2631 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002632 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002633 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002634 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002635 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002636 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002637
2638 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002639 @Override
2640 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002641 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002642 }
2643 });
Winson Chungb745afb2015-03-02 11:51:23 -08002644 }
2645
2646 /**
2647 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002648 *
2649 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002650 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002651 // TODO: calling method should use the return value so that when {@code false} is returned
2652 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002653 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002654 if (!(mState == State.WORKSPACE ||
2655 mState == State.APPS_SPRING_LOADED ||
2656 mState == State.WIDGETS_SPRING_LOADED ||
2657 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002658 return false;
2659 }
2660 if (toState != State.APPS && toState != State.WIDGETS) {
2661 return false;
2662 }
Winson Chungb745afb2015-03-02 11:51:23 -08002663
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002664 // This is a safe and supported transition to bypass spring_loaded mode.
2665 if (mExitSpringLoadedModeRunnable != null) {
2666 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2667 mExitSpringLoadedModeRunnable = null;
2668 }
2669
Winson Chungb745afb2015-03-02 11:51:23 -08002670 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002671 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002672 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002673 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002674 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002675
Michael Jurkab3e22d92011-10-31 15:58:33 -07002676 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002677 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002678 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002679
2680 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002681 getWindow().getDecorView()
2682 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002683 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002684 }
2685
Winson Chungcd99cd32015-04-29 11:03:24 -07002686 /**
2687 * Updates the workspace and interaction state on state change, and return the animation to this
2688 * new state.
2689 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002690 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002691 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002692 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002693 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002694 updateInteraction(fromState, toState);
2695 return anim;
2696 }
2697
Hyunyoung Song3f471442015-04-08 19:01:34 -07002698 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002699 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002700 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002701 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002702 }
Winson Chungb745afb2015-03-02 11:51:23 -08002703
Winson Chung006ee262015-08-03 14:40:11 -07002704 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002705 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002706 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002707 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002708 }
Adam Cohen7777d962011-08-18 18:58:38 -07002709
Hyunyoung Song3f471442015-04-08 19:01:34 -07002710 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002711 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002712 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002713
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002714 if (mExitSpringLoadedModeRunnable != null) {
2715 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2716 }
2717 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002718 @Override
2719 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002720 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002721 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2722 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002723 // Before we show workspace, hide all apps again because
2724 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2725 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002726 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002727 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002728 } else {
2729 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002730 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002731 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002732 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002733 };
2734 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002735 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002736
Tony Wickhame0c33232016-02-08 11:37:04 -08002737 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002738 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2739 || mState == State.WIDGETS_SPRING_LOADED;
2740 }
2741
Sunny Goyal0f76b562016-12-13 19:37:10 -08002742 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002743 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyald0d07032017-10-04 10:40:28 -07002744 showAppsView(true /* animated */);
Winson Chungb745afb2015-03-02 11:51:23 -08002745 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002746 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002747 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2748 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002749 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002750 }
2751
Michael Jurkad7c28052012-04-27 15:43:36 -07002752 @Override
2753 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002754 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002755 final List<CharSequence> text = event.getText();
2756 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002757 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002758 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002759 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002760 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002761 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002762 } else if (mWorkspace != null) {
2763 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002764 } else {
2765 text.add(getString(R.string.all_apps_home_button_label));
2766 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002767 return result;
2768 }
2769
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002770 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002771 * If the activity is currently paused, signal that we need to run the passed Runnable
2772 * in onResume.
2773 *
2774 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002775 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2776 * wrong when we're not running, and if the activity comes back to what the configuration was
2777 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002778 *
2779 * Implementation of the method from LauncherModel.Callbacks.
2780 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002781 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002782 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002783 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002784 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002785 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002786 if (run instanceof RunnableWithId) {
2787 // Remove any runnables which have the same id
2788 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002789 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002790 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002791 return true;
2792 } else {
2793 return false;
2794 }
2795 }
2796
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002797 public void setOnResumeCallback(OnResumeCallback callback) {
2798 if (mOnResumeCallback != null) {
2799 mOnResumeCallback.onLauncherResume();
2800 }
2801 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002802 }
2803
Michael Jurka7607c2f2013-04-03 14:33:19 -07002804 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002805 * If the activity is currently paused, signal that we need to re-run the loader
2806 * in onResume.
2807 *
2808 * This needs to be called from incoming places where resources might have been loaded
2809 * while we are paused. That is becaues the Configuration might be wrong
2810 * when we're not running, and if it comes back to what it was when we
2811 * were paused, we are not restarted.
2812 *
2813 * Implementation of the method from LauncherModel.Callbacks.
2814 *
2815 * @return true if we are currently paused. The caller might be able to
2816 * skip some work in that case since we will come back again.
2817 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002818 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002819 public boolean setLoadOnResume() {
2820 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002821 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002822 mOnResumeNeedsLoad = true;
2823 return true;
2824 } else {
2825 return false;
2826 }
2827 }
2828
2829 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002832 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002834 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002835 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002836 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002837 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002840
Joe Onorato9c1289c2009-08-17 11:03:03 -04002841 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002842 * Clear any pending bind callbacks. This is called when is loader is planning to
2843 * perform a full rebind from scratch.
2844 */
2845 @Override
2846 public void clearPendingBinds() {
2847 mBindOnResumeCallbacks.clear();
2848 if (mPendingExecutor != null) {
2849 mPendingExecutor.markCompleted();
2850 mPendingExecutor = null;
2851 }
2852 }
2853
2854 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002855 * Refreshes the shortcuts shown on the workspace.
2856 *
2857 * Implementation of the method from LauncherModel.Callbacks.
2858 */
2859 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002860 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002861 AbstractFloatingView.closeAllOpenViews(this);
2862
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002863 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002864
Winson Chungd64d1762013-08-20 14:37:16 -07002865 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002866 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002867 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002868
Winson Chung3d503fb2011-07-13 17:25:49 -07002869 if (mHotseat != null) {
2870 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002872 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 }
2874
Adam Cohendcd297f2013-06-18 13:13:40 -07002875 @Override
2876 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002877 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002878 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2879 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002880 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2881 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002882 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002883 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2884 // If there are no screens, we need to have an empty screen
2885 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002886 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002887 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002888
Winsonc7d2e832016-07-28 12:24:55 -07002889 // After we have added all the screens, if the wallpaper was locked to the default state,
2890 // then notify to indicate that it can be released and a proper wallpaper offset can be
2891 // computed before the next layout
2892 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002893 }
2894
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002895 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002896 int count = orderedScreenIds.size();
2897 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002898 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002899 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002900 // No need to bind the first screen, as its always bound.
2901 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2902 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002903 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002904 }
2905
Sunny Goyalb23980c2017-08-17 07:45:25 -07002906 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002907 public void bindAppsAdded(final ArrayList<Long> newScreens,
2908 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002909 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002910 Runnable r = new Runnable() {
2911 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002912 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002913 }
2914 };
2915 if (waitUntilResume(r)) {
2916 return;
2917 }
2918
Winson Chungd64d1762013-08-20 14:37:16 -07002919 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002920 if (newScreens != null) {
2921 bindAddScreens(newScreens);
2922 }
Winson Chungd64d1762013-08-20 14:37:16 -07002923
2924 // We add the items without animation on non-visible pages, and with
2925 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002926 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002927 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002928 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002929 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002930 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002931 }
Winson Chungc58497e2013-09-03 17:48:37 -07002932
Winson Chung87412982013-10-03 18:34:14 -07002933 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002934 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002935 }
2936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 * Bind the items start-end from the list.
2939 *
2940 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002942 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002943 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002944 Runnable r = new Runnable() {
2945 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002946 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002947 }
2948 };
2949 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002950 return;
2951 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002952
Sunny Goyal3be633b2016-12-08 09:59:25 -08002953 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002954 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002955 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002956 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002957 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002958 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002959 int end = items.size();
2960 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002961 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002962
2963 // Short circuit if we are loading dock items for a configuration which has no dock
2964 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2965 mHotseat == null) {
2966 continue;
2967 }
2968
Sunny Goyal639e9062015-08-19 19:17:06 -07002969 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002970 switch (item.itemType) {
2971 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2972 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002973 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002974 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002975 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002977 }
2978 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002979 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002980 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002981 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002982 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002983 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002984 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2985 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002986 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002987 if (view == null) {
2988 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002989 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002990 break;
2991 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002992 default:
2993 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002994 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002995
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002996 /*
2997 * Remove colliding items.
2998 */
2999 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3000 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3001 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3002 View v = cl.getChildAt(item.cellX, item.cellY);
3003 Object tag = v.getTag();
3004 String desc = "Collision while binding workspace item: " + item
3005 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003006 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003007 throw (new RuntimeException(desc));
3008 } else {
3009 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003010 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003011 continue;
3012 }
3013 }
3014 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303015 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003016 if (animateIcons) {
3017 // Animate all the applications up now
3018 view.setAlpha(0f);
3019 view.setScaleX(0f);
3020 view.setScaleY(0f);
3021 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003022 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003025
Winson Chung997a9232013-07-24 15:33:46 -07003026 if (animateIcons) {
3027 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003028 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003029 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003030 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003031 final Runnable startBounceAnimRunnable = new Runnable() {
3032 public void run() {
3033 anim.playTogether(bounceAnims);
3034 anim.start();
3035 }
3036 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003037 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003038 // We post the animation slightly delayed to prevent slowdowns
3039 // when we are loading right after we return to launcher.
3040 mWorkspace.postDelayed(new Runnable() {
3041 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003042 if (mWorkspace != null) {
3043 mWorkspace.snapToPage(newScreenIndex);
3044 mWorkspace.postDelayed(startBounceAnimRunnable,
3045 NEW_APPS_ANIMATION_DELAY);
3046 }
Winson Chung94d67682013-09-25 16:29:40 -07003047 }
3048 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003049 } else {
3050 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003051 }
3052 }
Winson Chung64359a52013-07-08 17:17:08 -07003053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003054 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055 }
3056
Joe Onorato9c1289c2009-08-17 11:03:03 -04003057 /**
3058 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003060 public void bindAppWidget(LauncherAppWidgetInfo item) {
3061 View view = inflateAppWidget(item);
3062 if (view != null) {
3063 mWorkspace.addInScreen(view, item);
3064 mWorkspace.requestLayout();
3065 }
3066 }
3067
3068 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003069 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303070 PendingAppWidgetHostView view =
3071 new PendingAppWidgetHostView(this, item, mIconCache, true);
3072 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003073 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003074 }
3075
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003076 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003077
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003078 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003079
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003080 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3081 // If the provider is not ready, bind as a pending widget.
3082 appWidgetInfo = null;
3083 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3084 // The widget id is not valid. Try to find the widget based on the provider info.
3085 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3086 } else {
3087 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3088 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003089
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003090 // If the provider is ready, but the width is not yet restored, try to restore it.
3091 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3092 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003093 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003094 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3095 + " belongs to component " + item.providerName
3096 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003097 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003098 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003099 }
Sunny Goyalff572272014-07-23 13:58:07 -07003100
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003101 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003102 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003103 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3104 // Id has not been allocated yet. Allocate a new id.
3105 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3106 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003107
Sunny Goyald478c832016-04-01 12:04:16 -07003108 // Also try to bind the widget. If the bind fails, the user will be shown
3109 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003110 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003111 pendingInfo.spanX = item.spanX;
3112 pendingInfo.spanY = item.spanY;
3113 pendingInfo.minSpanX = item.minSpanX;
3114 pendingInfo.minSpanY = item.minSpanY;
3115 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003116
3117 boolean isDirectConfig =
3118 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3119 if (isDirectConfig && item.bindOptions != null) {
3120 Bundle newOptions = item.bindOptions.getExtras();
3121 if (options != null) {
3122 newOptions.putAll(options);
3123 }
3124 options = newOptions;
3125 }
Sunny Goyald478c832016-04-01 12:04:16 -07003126 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3127 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003128
Sunny Goyal86df1382016-08-10 15:03:22 -07003129 // We tried to bind once. If we were not able to bind, we would need to
3130 // go through the permission dialog, which means we cannot skip the config
3131 // activity.
3132 item.bindOptions = null;
3133 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3134
Sunny Goyald478c832016-04-01 12:04:16 -07003135 // Bind succeeded
3136 if (success) {
3137 // If the widget has a configure activity, it is still needs to set it up,
3138 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003139 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003140 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3141 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003142 }
Sunny Goyald478c832016-04-01 12:04:16 -07003143
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003144 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003145 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003146 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003147 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003148 // The widget was marked as UI not ready, but there is no configure activity to
3149 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003150 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003151 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003152 }
Sunny Goyalff572272014-07-23 13:58:07 -07003153 }
3154
Sunny Goyald5462aa2016-11-22 16:52:39 +05303155 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003156 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003157 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003158 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003159 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003160 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003161 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003162 }
3163
Sunny Goyal233ee962015-08-03 13:05:01 -07003164 item.minSpanX = appWidgetInfo.minSpanX;
3165 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303166 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003167 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303168 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003169 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303170 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003172 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003173 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 }
3175
Sunny Goyalff572272014-07-23 13:58:07 -07003176 /**
3177 * Restores a pending widget.
3178 *
3179 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003180 */
Sunny Goyald478c832016-04-01 12:04:16 -07003181 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003182 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3183 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3184 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003185 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003186 }
3187
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003188 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003189 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003190 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3191 info.pendingItemInfo = null;
3192 }
Sunny Goyalff572272014-07-23 13:58:07 -07003193
Sunny Goyalba47faa2017-10-04 16:50:57 -07003194 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3195 view.reinflate();
3196 }
3197
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003198 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003199 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003200 }
3201
Adam Cohen1462de32012-07-24 22:34:36 -07003202 public void onPageBoundSynchronously(int page) {
3203 mSynchronouslyBoundPages.add(page);
3204 }
3205
Sunny Goyal527c7d32015-08-28 15:19:36 -07003206 @Override
3207 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3208 if (mPendingExecutor != null) {
3209 mPendingExecutor.markCompleted();
3210 }
3211 mPendingExecutor = executor;
3212 executor.attachTo(this);
3213 }
3214
3215 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3216 if (mPendingExecutor == executor) {
3217 mPendingExecutor = null;
3218 }
3219 }
3220
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003221 @Override
3222 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3223 Runnable r = new Runnable() {
3224 public void run() {
3225 finishFirstPageBind(executor);
3226 }
3227 };
3228 if (waitUntilResume(r)) {
3229 return;
3230 }
3231
3232 Runnable onComplete = new Runnable() {
3233 @Override
3234 public void run() {
3235 if (executor != null) {
3236 executor.onLoadAnimationCompleted();
3237 }
3238 }
3239 };
3240 if (mDragLayer.getAlpha() < 1) {
3241 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3242 } else {
3243 onComplete.run();
3244 }
3245 }
3246
Joe Onorato9c1289c2009-08-17 11:03:03 -04003247 /**
3248 * Callback saying that there aren't any more items to bind.
3249 *
3250 * Implementation of the method from LauncherModel.Callbacks.
3251 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003252 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003253 Runnable r = new Runnable() {
3254 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003255 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003256 }
3257 };
3258 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003259 return;
3260 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003261 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003262 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003263
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003264 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003265
Sunny Goyal2100c782016-08-22 16:00:03 -07003266 if (mPendingActivityResult != null) {
3267 handleActivityResult(mPendingActivityResult.requestCode,
3268 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3269 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003270 }
3271
Sunny Goyala474a9b2017-05-04 16:47:11 -07003272 InstallShortcutReceiver.disableAndFlushInstallQueue(
3273 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003274
Tony Wickham010d2552017-01-20 08:15:28 -08003275 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3276
Adam Cohen9211d422014-10-07 18:14:53 -07003277 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003278 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003279 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003280 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003281 }
3282
Winson Chunga2413752012-04-03 14:22:34 -07003283 private boolean canRunNewAppsAnimation() {
3284 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003285 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003286 }
3287
Winson Chungc9168342013-06-26 14:54:55 -07003288 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003289 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003290 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3291 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003292 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003293 return bounceAnim;
3294 }
3295
Adam Cohen27772732013-11-11 14:00:56 +00003296 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003297 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003298 }
3299
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003300 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 * Add the icons for all apps.
3302 *
3303 * Implementation of the method from LauncherModel.Callbacks.
3304 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003305 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003306 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3307 public void run() {
3308 bindAllApplications(apps);
3309 }
3310 };
3311 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003312 return;
3313 }
3314
Winson Chungb745afb2015-03-02 11:51:23 -08003315 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003316 Executor pendingExecutor = getPendingExecutor();
3317 if (pendingExecutor != null && mState != State.APPS) {
3318 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003319 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003320 return;
3321 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003322
Winson Chungb745afb2015-03-02 11:51:23 -08003323 mAppsView.setApps(apps);
3324 }
Adam Cohen9211d422014-10-07 18:14:53 -07003325 if (mLauncherCallbacks != null) {
3326 mLauncherCallbacks.bindAllApplications(apps);
3327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 }
3329
3330 /**
Tony2917a8b2017-07-31 23:29:42 -07003331 * Returns an Executor that will run after the launcher is first drawn (including after the
3332 * initial fade in animation). Returns null if the first draw has already occurred.
3333 */
3334 public @Nullable Executor getPendingExecutor() {
3335 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3336 }
3337
3338 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003339 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3340 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3341 */
3342 @Override
3343 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003344 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003345 }
3346
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003347 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 * A package was updated.
3349 *
3350 * Implementation of the method from LauncherModel.Callbacks.
3351 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003352 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003353 Runnable r = new Runnable() {
3354 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003355 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003356 }
3357 };
3358 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003359 return;
3360 }
3361
Winson Chungb745afb2015-03-02 11:51:23 -08003362 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003363 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003364 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 }
3366
Sunny Goyal4390ace2014-10-13 11:33:11 -07003367 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003368 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3369 Runnable r = new Runnable() {
3370 public void run() {
3371 bindPromiseAppProgressUpdated(app);
3372 }
3373 };
3374 if (waitUntilResume(r)) {
3375 return;
3376 }
3377
3378 if (mAppsView != null) {
3379 mAppsView.updatePromiseAppProgress(app);
3380 }
3381 }
3382
3383 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003384 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3385 Runnable r = new Runnable() {
3386 public void run() {
3387 bindWidgetsRestored(widgets);
3388 }
3389 };
3390 if (waitUntilResume(r)) {
3391 return;
3392 }
3393 mWorkspace.widgetsRestored(widgets);
3394 }
3395
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003397 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003398 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003399 *
3400 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003401 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003402 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003403 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003404 Runnable r = new Runnable() {
3405 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003406 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003407 }
3408 };
3409 if (waitUntilResume(r)) {
3410 return;
3411 }
3412
Sunny Goyal4390ace2014-10-13 11:33:11 -07003413 if (!updated.isEmpty()) {
3414 mWorkspace.updateShortcuts(updated);
3415 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003416 }
3417
3418 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003419 * Update the state of a package, typically related to install state.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 */
Sunny Goyale755d462014-07-22 13:48:29 -07003423 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003424 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3425 Runnable r = new Runnable() {
3426 public void run() {
3427 bindRestoreItemsChange(updates);
3428 }
3429 };
3430 if (waitUntilResume(r)) {
3431 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003432 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003433
Sunny Goyal756adbc2015-04-16 15:20:51 -07003434 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003435 }
3436
3437 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003438 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003439 * in addition to specific applications to remove, the reason being that
3440 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003441 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003442 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003444 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003445 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003446 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003447 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003448 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003449 }
Winson Chungd64d1762013-08-20 14:37:16 -07003450 };
3451 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003452 return;
3453 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003454 mWorkspace.removeItemsByMatcher(matcher);
3455 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003456 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003457
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003458 @Override
3459 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3460 Runnable r = new Runnable() {
3461 public void run() {
3462 bindAppInfosRemoved(appInfos);
3463 }
3464 };
3465 if (waitUntilResume(r)) {
3466 return;
Joe Onorato36115782010-06-17 13:28:48 -04003467 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003468
Winson Chungdf95eb12013-10-16 14:57:07 -07003469 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003470 if (mAppsView != null) {
3471 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003472 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 }
Joe Onoratobe386092009-11-17 17:32:16 -08003474
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003475 @Override
3476 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3477 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3478 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003479 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003480 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003481 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003482 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003483 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003484 return;
3485 }
3486
Sunny Goyald164b7f2016-10-12 20:49:31 -07003487 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003488 Executor pendingExecutor = getPendingExecutor();
3489 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003490 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003491 return;
3492 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003493 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003494 }
Tony Wickham26b17462017-03-20 17:12:24 -07003495
3496 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3497 if (topView != null) {
3498 topView.onWidgetsBound();
3499 }
3500 }
3501
3502 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3503 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003504 }
3505
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003506 @Override
3507 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003508 if (mWorkspace.getState().shouldUpdateWidget) {
3509 refreshAndBindWidgetsForPackageUser(null);
3510 }
Tony Wickham26b17462017-03-20 17:12:24 -07003511 }
3512
Tony Wickham86222d22017-03-29 15:30:43 -07003513 /**
3514 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3515 * refreshes the widgets and shortcuts associated with the given package/user
3516 */
3517 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003518 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003519 }
3520
Winson Chung4b919f82012-05-01 10:44:08 -07003521 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003522 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003523 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003524 }
3525 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003526
Winson Chung4b919f82012-05-01 10:44:08 -07003527 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003528 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003529 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003530 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003531 } else {
3532 mHandler.postDelayed(new Runnable() {
3533 public void run() {
3534 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3535 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003536 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003537 }
Winson Chung4b919f82012-05-01 10:44:08 -07003538 }
Winson Chung400438b2011-01-16 17:53:48 -08003539 }
3540
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003541 private void markAppsViewShown() {
3542 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3543 return;
3544 }
3545 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3546 }
3547
3548 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003549 UserManagerCompat um = UserManagerCompat.getInstance(this);
3550 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003551 }
3552
Winson Chung80baf5a2010-08-09 16:03:15 -07003553 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003554 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003555 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003556 @Override
3557 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3558 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003559
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003560 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3561 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003562 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003563 writer.println(prefix + " Homescreen " + i);
3564
3565 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3566 for (int j = 0; j < layout.getChildCount(); j++) {
3567 Object tag = layout.getChildAt(j).getTag();
3568 if (tag != null) {
3569 writer.println(prefix + " " + tag.toString());
3570 }
3571 }
3572 }
3573
3574 writer.println(prefix + " Hotseat");
3575 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003576 for (int j = 0; j < layout.getChildCount(); j++) {
3577 Object tag = layout.getChildAt(j).getTag();
3578 if (tag != null) {
3579 writer.println(prefix + " " + tag.toString());
3580 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003581 }
Sunny Goyala1365452015-10-01 15:46:24 -07003582
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003583 try {
3584 FileLog.flushAll(writer);
3585 } catch (Exception e) {
3586 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003587 }
3588 }
3589
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003590 writer.println(prefix + "Misc:");
3591 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3592 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3593 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3594
3595 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003596
Adam Cohen9211d422014-10-07 18:14:53 -07003597 if (mLauncherCallbacks != null) {
3598 mLauncherCallbacks.dump(prefix, fd, writer, args);
3599 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003600 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003601
Sunny Goyal66b24572016-09-21 15:57:55 -07003602 @Override
3603 @TargetApi(Build.VERSION_CODES.N)
3604 public void onProvideKeyboardShortcuts(
3605 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3606
3607 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3608 if (mState == State.WORKSPACE) {
3609 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3610 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3611 }
3612 View currentFocus = getCurrentFocus();
3613 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3614 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3615 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3616 }
Tony18c4aa42017-05-10 21:23:57 -05003617 if (currentFocus.getTag() instanceof ItemInfo
3618 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003619 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3620 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3621 }
3622 if (!shortcutInfos.isEmpty()) {
3623 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3624 }
3625
3626 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3627 }
3628
3629 @Override
3630 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3631 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3632 switch (keyCode) {
3633 case KeyEvent.KEYCODE_A:
3634 if (mState == State.WORKSPACE) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003635 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003636 return true;
3637 }
3638 break;
3639 case KeyEvent.KEYCODE_S: {
3640 View focusedView = getCurrentFocus();
3641 if (focusedView instanceof BubbleTextView
3642 && focusedView.getTag() instanceof ItemInfo
3643 && mAccessibilityDelegate.performAction(focusedView,
3644 (ItemInfo) focusedView.getTag(),
3645 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003646 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003647 return true;
3648 }
3649 break;
3650 }
3651 case KeyEvent.KEYCODE_O:
3652 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3653 return true;
3654 }
3655 break;
3656 }
3657 }
3658 return super.onKeyShortcut(keyCode, event);
3659 }
3660
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003661 public static Launcher getLauncher(Context context) {
3662 if (context instanceof Launcher) {
3663 return (Launcher) context;
3664 }
3665 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3666 }
3667
Sunny Goyal5a81c382017-03-20 15:08:06 -07003668 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003669
3670 @Override
3671 public void onSharedPreferenceChanged(
3672 SharedPreferences sharedPreferences, String key) {
3673 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003674 // Recreate the activity so that it initializes the rotation preference again.
3675 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003676 }
3677 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003678 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003679
3680 /**
3681 * Callback for listening for onResume
3682 */
3683 public interface OnResumeCallback {
3684
3685 void onLauncherResume();
3686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003687}