blob: 2e893654c6d9555fa2987b506a2f5b8bcd3b00d8 [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<>();
260 private final ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700261 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800264 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800265 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700266 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700267 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700268 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Jon Miranda2d89ea82017-05-04 11:47:53 -0700270 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700271 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700272
Tony Wickham010d2552017-01-20 08:15:28 -0800273 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700274
Winson Chung400438b2011-01-16 17:53:48 -0800275 // Determines how long to wait after a rotation before restoring the screen orientation to
276 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700277 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800278
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700279 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700280
Winson Chung46353de2012-02-16 14:05:10 -0800281 // We only want to get the SharedPreferences once since it does an FS stat each time we get
282 // it from the context.
283 private SharedPreferences mSharedPrefs;
284
Winson Chung13eb5272015-05-11 16:30:13 -0700285 // This is set to the view that launched the activity that navigated the user away from
286 // launcher. Since there is no callback for when the activity has finished launching, enable
287 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800288 private BubbleTextView mWaitingForResume;
289
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700290 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
291 // state transition. If another state transition happened during this delay,
292 // simply unregister this runnable.
293 private Runnable mExitSpringLoadedModeRunnable;
294
Sunny Goyal2100c782016-08-22 16:00:03 -0700295 // Activity result which needs to be processed after workspace has loaded.
296 private ActivityResultInfo mPendingActivityResult;
297 /**
298 * Holds extra information required to handle a result from an external call, like
299 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
300 */
301 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800302
Jon Miranda379198e2016-09-23 08:54:40 -0700303 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700304
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700305 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700306 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400307
308 @Thunk void setOrientation() {
309 if (mRotationEnabled) {
310 unlockScreenOrientation(true);
311 } else {
312 setRequestedOrientation(
313 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700314 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400315 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700316
Sunny Goyal745bad92016-05-02 10:54:12 -0700317 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 @Override
320 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700321 if (DEBUG_STRICT_MODE) {
322 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
323 .detectDiskReads()
324 .detectDiskWrites()
325 .detectNetwork() // or .detectAll() for all detectable problems
326 .penaltyLog()
327 .build());
328 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
329 .detectLeakedSqlLiteObjects()
330 .detectLeakedClosableObjects()
331 .penaltyLog()
332 .penaltyDeath()
333 .build());
334 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700335 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700336
Adam Cohen9211d422014-10-07 18:14:53 -0700337 if (mLauncherCallbacks != null) {
338 mLauncherCallbacks.preOnCreate();
339 }
340
Mario Bertschler27288382017-05-24 15:35:09 -0700341 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
342 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700343 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700346 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400347
Sunny Goyal87f784c2017-01-11 10:48:34 -0800348 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700349
Adam Cohen2e6da152015-05-06 11:42:25 -0700350 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800351 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700352 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700353 Display display = getWindowManager().getDefaultDisplay();
354 Point mwSize = new Point();
355 display.getSize(mwSize);
356 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
357 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700358
Jon Miranda6bed3502017-09-19 14:43:17 -0700359 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700360 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700361 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800362 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800363 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800364 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700365 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700366
Joe Onorato41a12d22009-10-31 18:30:00 -0400367 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700368 mAllAppsController = new AllAppsTransitionController(this);
369 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700370
Sunny Goyalffe83f12014-08-14 17:39:34 -0700371 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700372
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700373 mAppWidgetHost = new LauncherAppWidgetHost(this);
374 if (Utilities.ATLEAST_MARSHMALLOW) {
375 mAppWidgetHost.addProviderChangeListener(this);
376 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700378
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700379 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
380 // this also ensures that any synchronous binding below doesn't re-trigger another
381 // LauncherModel load.
382 mPaused = false;
383
Sunny Goyal60820d72017-05-09 12:40:11 -0700384 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 setupViews();
Winson1f064272016-07-18 17:18:02 -0700387 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388
Tony Wickham010d2552017-01-20 08:15:28 -0800389 mPopupDataProvider = new PopupDataProvider(this);
390
Tony Wickhame2217252016-03-22 16:34:23 -0700391 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
392 .addAccessibilityStateChangeListener(this);
393
Sunny Goyalfe770c92016-09-27 14:38:58 -0700394 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395
Sunny Goyal2100c782016-08-22 16:00:03 -0700396 // We only load the page synchronously if the user rotates (or triggers a
397 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700398 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
399 if (savedInstanceState != null) {
400 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
401 }
402 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700403 // If we are not binding synchronously, show a fade in animation when
404 // the first page bind completes.
405 mDragLayer.setAlpha(0);
406 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700407 // Pages bound synchronously.
408 mWorkspace.setCurrentPage(currentScreen);
409
Sunny Goyal2100c782016-08-22 16:00:03 -0700410 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
413 // For handling default keys
414 mDefaultKeySsb = new SpannableStringBuilder();
415 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800416
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800417 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700418 // In case we are on a device with locked rotation, we should look at preferences to check
419 // if the user has specifically allowed rotation.
420 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700421 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700422 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
423 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700424 }
425
Sunny Goyal5a81c382017-03-20 15:08:06 -0700426 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
427 // Temporarily enable the rotation
428 mRotationEnabled = true;
429 }
430
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700431 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
432 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400433 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700434
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800435 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700436
Jon Miranda7fda2852017-07-19 16:07:01 -0700437 // Listen for broadcasts
438 IntentFilter filter = new IntentFilter();
439 filter.addAction(Intent.ACTION_SCREEN_OFF);
440 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
441 registerReceiver(mReceiver, filter);
442 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700443
Sunny Goyal8392c822017-06-20 10:03:56 -0700444 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
445 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700446
447 if (mLauncherCallbacks != null) {
448 mLauncherCallbacks.onCreate(savedInstanceState);
449 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700450
451 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700452 }
453
Sunny Goyal7779d622015-06-11 16:18:39 -0700454 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700455 public void onThemeChanged() {
456 recreate();
457 }
458
Mario Bertschlera6936942017-05-31 14:48:19 -0700459 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700460 if (isDark) {
461 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700462 } else if (supportsDarkText) {
463 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700464 }
465 }
466
467 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700468 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800469 return mLauncherView.findViewById(id);
470 }
471
472 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700473 public void onAppWidgetHostReset() {
474 if (mAppWidgetHost != null) {
475 mAppWidgetHost.startListening();
476 }
477 }
478
Adam Cohen9211d422014-10-07 18:14:53 -0700479 private LauncherCallbacks mLauncherCallbacks;
480
481 public void onPostCreate(Bundle savedInstanceState) {
482 super.onPostCreate(savedInstanceState);
483 if (mLauncherCallbacks != null) {
484 mLauncherCallbacks.onPostCreate(savedInstanceState);
485 }
486 }
487
Winson1f064272016-07-18 17:18:02 -0700488 public void onInsetsChanged(Rect insets) {
489 mDeviceProfile.updateInsets(insets);
490 mDeviceProfile.layout(this, true /* notifyListeners */);
491 }
492
Sunny Goyal32554d12015-12-03 15:31:25 -0800493 /**
494 * Call this after onCreate to set or clear overlay.
495 */
496 public void setLauncherOverlay(LauncherOverlay overlay) {
497 if (overlay != null) {
498 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
499 }
500 mWorkspace.setLauncherOverlay(overlay);
501 }
502
Adam Cohen9211d422014-10-07 18:14:53 -0700503 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
504 mLauncherCallbacks = callbacks;
505 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700506 }
507
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700508 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700509 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700510 if (mLauncherCallbacks != null) {
511 mLauncherCallbacks.onLauncherProviderChange();
512 }
513 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700514
Hyunyoung Song3f471442015-04-08 19:01:34 -0700515 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700516 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
517 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700518 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700519 }
520
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000521 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700522 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
523 // This cast is safe as long as the id < 0x00FFFFFF
524 // Since we jail all the dynamically generated views, there should be no clashes
525 // with any other views.
526 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000527 }
528
Tony Wickham010d2552017-01-20 08:15:28 -0800529 public PopupDataProvider getPopupDataProvider() {
530 return mPopupDataProvider;
531 }
532
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000533 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700534 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
535 * a configuration step, this allows the proper animations to run after other transitions.
536 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700537 private long completeAdd(
538 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
539 long screenId = info.screenId;
540 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700541 // When the screen id represents an actual screen (as opposed to a rank) we make sure
542 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700544 }
Adam Cohendb364c32014-05-20 14:23:40 -0700545
Sunny Goyal2100c782016-08-22 16:00:03 -0700546 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800547 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700548 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700549 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800550 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700551 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700552 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700553 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700555 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700556 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700557 int widgetId = appWidgetId;
558 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700559 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700561 // Since the view was just bound, also launch the configure activity if needed
562 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
563 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800564 if (provider != null) {
565 new WidgetAddFlowHandler(provider)
566 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700567 }
568 }
569 break;
570 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800571 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700572
Adam Cohendb364c32014-05-20 14:23:40 -0700573 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800574 }
575
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800576 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700578 if (isWorkspaceLoading()) {
579 // process the result once the workspace has loaded.
580 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
581 return;
582 }
583 mPendingActivityResult = null;
584
Winson Chunge341d302013-08-16 14:31:00 -0700585 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700586 final PendingRequestArgs requestArgs = mPendingRequestArgs;
587 setWaitingForResult(null);
588 if (requestArgs == null) {
589 return;
590 }
591
592 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700593
Adam Cohenad4e15c2013-10-17 16:21:35 -0700594 Runnable exitSpringLoaded = new Runnable() {
595 @Override
596 public void run() {
597 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
598 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
599 }
600 };
601
Michael Jurka8b805b12012-04-18 14:23:14 -0700602 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700603 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700605 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
606 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700607 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700608 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700609 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700610 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 addAppWidgetImpl(
612 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800613 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700614 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700615 }
616 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200617 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
618 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700619 // User could have free-scrolled between pages before picking a wallpaper; make sure
620 // we move to the closest one now.
621 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700622 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200623 }
624 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700625 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200626
Adam Cohened66b2b2012-01-23 17:28:51 -0800627 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700628 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700629
Adam Cohened66b2b2012-01-23 17:28:51 -0800630 // We have special handling for widgets
631 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800632 final int appWidgetId;
633 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
634 : -1;
635 if (widgetId < 0) {
636 appWidgetId = pendingAddWidgetId;
637 } else {
638 appWidgetId = widgetId;
639 }
640
Adam Cohenad4e15c2013-10-17 16:21:35 -0700641 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800642 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700643 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
644 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700645 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700646 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700647 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700648 @Override
649 public void run() {
650 exitSpringLoadedDragModeDelayed(false, 0, null);
651 }
652 };
Adam Cohendb364c32014-05-20 14:23:40 -0700653
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
655 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
656 } else {
657 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
658 // When the screen id represents an actual screen (as opposed to a rank)
659 // we make sure that the drop page actually exists.
660 requestArgs.screenId =
661 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700662 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 final CellLayout dropLayout =
664 mWorkspace.getScreenWithId(requestArgs.screenId);
665
666 dropLayout.setDropPending(true);
667 final Runnable onComplete = new Runnable() {
668 @Override
669 public void run() {
670 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
671 dropLayout.setDropPending(false);
672 }
673 };
674 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
675 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 return;
678 }
679
Sunny Goyald478c832016-04-01 12:04:16 -0700680 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
681 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700682 if (resultCode == RESULT_OK) {
683 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700685 }
686 // Leave the widget in the pending state if the user canceled the configure.
687 return;
688 }
689
Sunny Goyalaad90582015-07-09 14:22:50 -0700690 if (requestCode == REQUEST_CREATE_SHORTCUT) {
691 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
693 completeAdd(requestCode, data, -1, requestArgs);
694 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
695 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
696
Sunny Goyalaad90582015-07-09 14:22:50 -0700697 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700698 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
699 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800703 }
704
705 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700706 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800707 final int requestCode, final int resultCode, final Intent data) {
708 handleActivityResult(requestCode, resultCode, data);
709 if (mLauncherCallbacks != null) {
710 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
711 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 }
713
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700714 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700715 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600716 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700717 PendingRequestArgs pendingArgs = mPendingRequestArgs;
718 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
719 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
720 setWaitingForResult(null);
721
Sunny Goyal28c6b962015-10-12 11:42:05 -0700722 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700724 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700726 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700727 Intent intent = pendingArgs.getPendingIntent();
728
Sunny Goyal28c6b962015-10-12 11:42:05 -0700729 if (grantResults.length > 0
730 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700731 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700732 } else {
733 // TODO: Show a snack bar with link to settings
734 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700735 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700736 }
737 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600738 if (mLauncherCallbacks != null) {
739 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
740 grantResults);
741 }
742 }
743
Adam Cohendb364c32014-05-20 14:23:40 -0700744 /**
745 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
746 *
747 * @param screenId the screen id to check
748 * @return the new screen, or screenId if it exists
749 */
750 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800751 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700752 if (dropLayout == null) {
753 // it's possible that the add screen was removed because it was
754 // empty and a re-bind occurred
755 mWorkspace.addExtraEmptyScreen();
756 return mWorkspace.commitExtraEmptyScreen();
757 } else {
758 return screenId;
759 }
760 }
761
Sunny Goyal2100c782016-08-22 16:00:03 -0700762 @Thunk void completeTwoStageWidgetDrop(
763 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
764 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 Runnable onCompleteRunnable = null;
766 int animationType = 0;
767
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700768 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800769 if (resultCode == RESULT_OK) {
770 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
771 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800772 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700773 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 onCompleteRunnable = new Runnable() {
775 @Override
776 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700777 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
779 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 }
781 };
782 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800783 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800784 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700786 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700787 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700788 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
789 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700791 // The animated view may be null in the case of a rotation during widget configuration
792 onCompleteRunnable.run();
793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
795
796 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700797 protected void onStop() {
798 super.onStop();
799 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700800
801 if (mLauncherCallbacks != null) {
802 mLauncherCallbacks.onStop();
803 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700804
Sunny Goyalf5e37442016-11-02 10:31:24 -0700805 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700806 mAppWidgetHost.stopListening();
807 }
Tony Wickham010d2552017-01-20 08:15:28 -0800808
809 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700810 }
811
812 @Override
813 protected void onStart() {
814 super.onStart();
815 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700816
817 if (mLauncherCallbacks != null) {
818 mLauncherCallbacks.onStart();
819 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700820
Sunny Goyalf5e37442016-11-02 10:31:24 -0700821 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700822 mAppWidgetHost.startListening();
823 }
Tony Wickham010d2552017-01-20 08:15:28 -0800824
825 if (!isWorkspaceLoading()) {
826 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
827 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700828
Jon Miranda7fda2852017-07-19 16:07:01 -0700829 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700830 if (mScrimAnimator != null) {
831 mScrimAnimator.cancel();
832 }
833 mDragLayer.getBackground().setAlpha(0);
834 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
835 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
836 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
837 @Override
838 public void onAnimationEnd(Animator animation) {
839 mScrimAnimator = null;
840 }
841 });
842 mScrimAnimator.setDuration(600);
843 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
844 mScrimAnimator.start();
845 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700846 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700847 }
848
849 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700851 TraceHelper.beginSection("ON_RESUME");
Adam Cohen9211d422014-10-07 18:14:53 -0700852 if (mLauncherCallbacks != null) {
853 mLauncherCallbacks.preOnResume();
854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700856 TraceHelper.partitionSection("ON_RESUME", "superCall");
857
Hyunyoung Songaa953652016-04-19 18:30:24 -0700858 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800859 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700860 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700861 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700862 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700863 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700865 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200866 // We might have postponed some bind calls until onResume (see waitUntilResume) --
867 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700868 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
869 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200870 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700871 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200872 }
Michael Jurka54554252013-08-01 12:52:23 +0200873 if (mOnResumeCallbacks.size() > 0) {
874 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
875 mOnResumeCallbacks.get(i).run();
876 }
877 mOnResumeCallbacks.clear();
878 }
Winson Chunge4e50662012-01-23 14:45:13 -0800879
880 // Reset the pressed state of icons that were locked in the press state while activities
881 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800882 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800883 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800884 mWaitingForResume.setStayPressed(false);
885 }
Winson Chung82963d52013-10-09 11:20:57 -0700886
Winson Chungcd99cd32015-04-29 11:03:24 -0700887 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Sunny Goyale755d462014-07-22 13:48:29 -0700888
Sunny Goyala474a9b2017-05-04 16:47:11 -0700889 // Process any items that were added while Launcher was away.
890 InstallShortcutReceiver.disableAndFlushInstallQueue(
891 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700892
Sunny Goyala474a9b2017-05-04 16:47:11 -0700893 // Refresh shortcuts if the permission changed.
894 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700895
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700896 if (shouldShowDiscoveryBounce()) {
897 mAllAppsController.showDiscoveryBounce();
898 }
Adam Cohen9211d422014-10-07 18:14:53 -0700899 if (mLauncherCallbacks != null) {
900 mLauncherCallbacks.onResume();
901 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800902
Sunny Goyala502aa32017-10-02 16:04:06 -0700903 clearTypedText();
904
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700905 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700906 }
907
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700909 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700910 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700911 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700912
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700913 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700914 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800915 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700916 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700917
Adam Cohen9211d422014-10-07 18:14:53 -0700918 if (mLauncherCallbacks != null) {
919 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700920 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700921 }
922
Adam Cohenc2d6e892014-10-16 09:49:24 -0700923 public interface LauncherOverlay {
924
925 /**
926 * Touch interaction leading to overscroll has begun
927 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700928 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700929
930 /**
931 * Touch interaction related to overscroll has ended
932 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700933 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700934
935 /**
936 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
937 * screen (or in the case of RTL, the rightmost screen).
938 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700939 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700940
941 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800942 * Called when the launcher is ready to use the overlay
943 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700944 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700945 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700946 }
947
948 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700949 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700950 }
951
952 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
953
Sunny Goyalc86df472016-02-25 09:19:38 -0800954 public void onScrollChanged(float progress) {
955 if (mWorkspace != null) {
956 mWorkspace.onOverlayScrollChanged(progress);
957 }
958 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700959 }
960
Jorim Jaggid017f882014-01-14 17:08:48 -0800961 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700962 if (mLauncherCallbacks != null) {
963 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700964 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800965 // On O and above we there is always some setting present settings (add icon to
966 // home screen or icon badging). On earlier APIs we will have the allow rotation
967 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700968 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700969 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800970 }
971
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700972 @Override
973 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700975 if (mModel.isCurrentCallbacks(this)) {
976 mModel.stopLoader();
977 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700978 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
979
Romain Guy13c2e7b2010-03-10 19:45:00 -0800980 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700981 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700982
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800983 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800984 @Override
985 public void onWindowFocusChanged(boolean hasFocus) {
986 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700987 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
991 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800992 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800993
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 private boolean acceptFilter() {
995 final InputMethodManager inputManager = (InputMethodManager)
996 getSystemService(Context.INPUT_METHOD_SERVICE);
997 return !inputManager.isFullscreenMode();
998 }
999
1000 @Override
1001 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001002 final int uniChar = event.getUnicodeChar();
1003 final boolean handled = super.onKeyDown(keyCode, event);
1004 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1005 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1007 keyCode, event);
1008 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001009 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001010 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 // showSearchDialog()
1012 // If there are multiple keystrokes before the search dialog takes focus,
1013 // onSearchRequested() will be called for every keystroke,
1014 // but it is idempotent, so it's fine.
1015 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 }
1017 }
1018
Joe Onorato8a9625e2010-01-28 15:55:35 -08001019 // Eat the long press event so the keyboard doesn't come up.
1020 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1021 return true;
1022 }
1023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 return handled;
1025 }
1026
Winson46163472015-09-22 17:31:56 -07001027 @Override
1028 public boolean onKeyUp(int keyCode, KeyEvent event) {
1029 if (keyCode == KeyEvent.KEYCODE_MENU) {
1030 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001031 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001032 // Close any open floating view
1033 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001034
Winson46163472015-09-22 17:31:56 -07001035 // Show the overview mode if we are on the workspace
1036 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1037 !mWorkspace.isSwitchingState()) {
1038 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001039 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001040 }
1041 }
1042 return true;
1043 }
1044 return super.onKeyUp(keyCode, event);
1045 }
1046
Karl Rosaen138a0412009-04-23 19:00:21 -07001047 private String getTypedText() {
1048 return mDefaultKeySsb.toString();
1049 }
1050
Sunny Goyal6f28e712016-09-13 14:19:29 -07001051 @Override
1052 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001053 mDefaultKeySsb.clear();
1054 mDefaultKeySsb.clearSpans();
1055 Selection.setSelection(mDefaultKeySsb, 0);
1056 }
1057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 /**
1059 * Restores the previous state, if it exists.
1060 *
1061 * @param savedState The previous state.
1062 */
1063 private void restoreState(Bundle savedState) {
1064 if (savedState == null) {
1065 return;
1066 }
1067
Sunny Goyalfe770c92016-09-27 14:38:58 -07001068 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1069 State[] stateValues = State.values();
1070 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1071 ? stateValues[stateOrdinal] : State.WORKSPACE;
Sunny Goyal53b99362017-10-05 14:58:56 -07001072 if (state == State.APPS) {
1073 showAppsView(false /* animated */);
1074 } else if (state == State.WIDGETS) {
1075 showWidgetsView(false, false);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001076 }
1077
Sunny Goyal2100c782016-08-22 16:00:03 -07001078 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1079 if (requestArgs != null) {
1080 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001082
1083 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085
1086 /**
1087 * Finds all the views we need and configure them properly.
1088 */
1089 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001090 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001091 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001092 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001093 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001094
Sunny Goyal784f9c32016-03-23 16:56:54 -07001095 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1096 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1097 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098
Winson Chung4c98d922011-05-31 16:50:48 -07001099 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001100 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001101
Winson Chung3d503fb2011-07-13 17:25:49 -07001102 // Setup the hotseat
1103 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1104 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001105 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001106 }
1107
Winsone9f27272015-10-13 10:47:51 -07001108 // Setup the overview panel
1109 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001110
Winson Chung4c98d922011-05-31 16:50:48 -07001111 // Setup the workspace
1112 mWorkspace.setHapticFeedbackEnabled(false);
1113 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001114 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001115 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1116 // default state, otherwise we will update to the wrong offsets in RTL
1117 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001118 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001119 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001120
Tony Wickham34d2c912015-09-11 09:27:58 -07001121 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001122 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001123
Winson Chungef7f8742015-06-04 17:18:17 -07001124 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001125 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001126 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001127
Winson Chung3d503fb2011-07-13 17:25:49 -07001128 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001129 mDragController.setMoveTarget(mWorkspace);
1130 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001131 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001132
Hyunyoung Songd725f642017-08-17 22:26:35 -07001133 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001134
Sunny Goyal322d5562015-06-25 19:35:49 -07001135 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1136 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139
Winsone9f27272015-10-13 10:47:51 -07001140 private void setupOverviewPanel() {
1141 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1142
Winsone9f27272015-10-13 10:47:51 -07001143 // Bind wallpaper button actions
1144 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301145 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001146 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001147 public void handleViewClick(View view) {
1148 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001149 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001150 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001151
1152 // Bind widget button actions
1153 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301154 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001155 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001156 public void handleViewClick(View view) {
1157 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001158 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001159 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001160
1161 // Bind settings actions
1162 View settingsButton = findViewById(R.id.settings_button);
1163 boolean hasSettings = hasSettings();
1164 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301165 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001166 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001167 public void handleViewClick(View view) {
1168 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001169 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001170 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001171 } else {
1172 settingsButton.setVisibility(View.GONE);
1173 }
1174
1175 mOverviewPanel.setAlpha(0f);
1176 }
1177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001179 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001180 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001181 */
1182 public void setAllAppsButton(View allAppsButton) {
1183 mAllAppsButton = allAppsButton;
1184 }
1185
Sunny Goyalbb011da2016-06-15 15:42:29 -07001186 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001187 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001188 }
1189
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001190 public View getWidgetsButton() {
1191 return mWidgetsButton;
1192 }
1193
Anjali Koppal5ad44842014-03-10 20:34:39 -07001194 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 * Creates a view representing a shortcut.
1196 *
1197 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001199 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001200 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
1202
1203 /**
1204 * Creates a view representing a shortcut inflated from the specified resource.
1205 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 * @param parent The group the shortcut belongs to.
1207 * @param info The data structure describing the shortcut.
1208 *
1209 * @return A View inflated from layoutResId.
1210 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001211 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001212 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1213 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001214 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001216 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 return favorite;
1218 }
1219
1220 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001221 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 *
1223 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001225 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001226 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001227 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1228 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001229 return;
1230 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001231
Jon Mirandac476d6e2017-05-04 16:30:01 -07001232 int[] cellXY = mTmpAddItemCellCoordinates;
1233 CellLayout layout = getCellLayout(container, screenId);
1234
Sunny Goyal782f0c92017-01-19 10:27:54 -08001235 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001236 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001237 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1238 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001239 }
1240
1241 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001242 // Legacy shortcuts are only supported for primary profile.
1243 info = Process.myUserHandle().equals(args.user)
1244 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001245
Sunny Goyalb57645f2017-03-20 13:57:28 -07001246 if (info == null) {
1247 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1248 return;
1249 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001250 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001251 // The app is trying to add a shortcut without sufficient permissions
1252 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1253 return;
1254 }
1255 }
1256
Jon Mirandac476d6e2017-05-04 16:30:01 -07001257 if (container < 0) {
1258 // Adding a shortcut to the Workspace.
1259 final View view = createShortcut(info);
1260 boolean foundCellSpan = false;
1261 // First we check if we already know the exact location where we want to add this item.
1262 if (cellX >= 0 && cellY >= 0) {
1263 cellXY[0] = cellX;
1264 cellXY[1] = cellY;
1265 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001266
Jon Mirandac476d6e2017-05-04 16:30:01 -07001267 // If appropriate, either create a folder or add to an existing folder
1268 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1269 true, null, null)) {
1270 return;
1271 }
1272 DragObject dragObject = new DragObject();
1273 dragObject.dragInfo = info;
1274 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1275 true)) {
1276 return;
1277 }
1278 } else {
1279 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1280 }
1281
1282 if (!foundCellSpan) {
1283 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001284 return;
1285 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001286
1287 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1288 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001289 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001290 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001291 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001292 if (folderIcon != null) {
1293 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1294 folderInfo.add(info, args.rank, false);
1295 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001296 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001297 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300
Sunny Goyale29897f52017-07-20 10:09:42 -07001301 public FolderIcon findFolderIcon(final long folderIconId) {
1302 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1303 @Override
1304 public boolean evaluate(ItemInfo info, View view) {
1305 return info != null && info.id == folderIconId;
1306 }
1307 });
1308 }
1309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001311 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001313 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001315 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001316 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1317
Adam Cohened66b2b2012-01-23 17:28:51 -08001318 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001319 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001320 }
Romain Guycbb89e42009-06-08 15:52:54 -07001321
Adam Cohen59400422014-03-05 18:07:04 -08001322 LauncherAppWidgetInfo launcherInfo;
1323 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001324 launcherInfo.spanX = itemInfo.spanX;
1325 launcherInfo.spanY = itemInfo.spanY;
1326 launcherInfo.minSpanX = itemInfo.minSpanX;
1327 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001328 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001329
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001330 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001331 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332
Sunny Goyal2100c782016-08-22 16:00:03 -07001333 if (hostView == null) {
1334 // Perform actual inflation because we're live
1335 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001337 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301338 prepareAppWidget(hostView, launcherInfo);
1339 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
Romain Guycbb89e42009-06-08 15:52:54 -07001341
Sunny Goyald5462aa2016-11-22 16:52:39 +05301342 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001343 hostView.setTag(item);
1344 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001345 hostView.setFocusable(true);
1346 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001347 }
1348
Adam Cohended9f8d2010-11-03 13:25:16 -07001349 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1350 @Override
1351 public void onReceive(Context context, Intent intent) {
1352 final String action = intent.getAction();
1353 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001354 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001355 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001356 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001357 if (!showWorkspace(false)) {
1358 // If we are already on the workspace, then manually reset all apps
1359 mAppsView.reset();
1360 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001361 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001362 mShouldFadeInScrim = true;
1363 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1364 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1365 // the user unlocked and the Launcher is not in the foreground.
1366 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001367 }
1368 }
1369 };
1370
Tony Wickham010d2552017-01-20 08:15:28 -08001371 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1372 Runnable r = new Runnable() {
1373 @Override
1374 public void run() {
1375 mWorkspace.updateIconBadges(updatedBadges);
1376 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001377
1378 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1379 if (popup != null) {
1380 popup.updateNotificationHeader(updatedBadges);
1381 }
Tony Wickham010d2552017-01-20 08:15:28 -08001382 }
1383 };
1384 if (!waitUntilResume(r)) {
1385 r.run();
1386 }
1387 }
1388
Adam Cohended9f8d2010-11-03 13:25:16 -07001389 @Override
1390 public void onAttachedToWindow() {
1391 super.onAttachedToWindow();
1392
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001393 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001394 if (mLauncherCallbacks != null) {
1395 mLauncherCallbacks.onAttachedToWindow();
1396 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001397 }
1398
1399 @Override
1400 public void onDetachedFromWindow() {
1401 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001402
1403 if (mLauncherCallbacks != null) {
1404 mLauncherCallbacks.onDetachedFromWindow();
1405 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001406 }
1407
Winson Chunga6945242014-01-08 14:04:34 -08001408 public DragLayer getDragLayer() {
1409 return mDragLayer;
1410 }
1411
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001412 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001413 return mAppsView;
1414 }
1415
Hyunyoung Song3f471442015-04-08 19:01:34 -07001416 public WidgetsContainerView getWidgetsView() {
1417 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001418 }
1419
Winson Chunga6945242014-01-08 14:04:34 -08001420 public Workspace getWorkspace() {
1421 return mWorkspace;
1422 }
1423
1424 public Hotseat getHotseat() {
1425 return mHotseat;
1426 }
1427
Jorim Jaggid017f882014-01-14 17:08:48 -08001428 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001429 return mOverviewPanel;
1430 }
1431
Sunny Goyal47328fd2016-05-25 18:56:41 -07001432 public DropTargetBar getDropTargetBar() {
1433 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001434 }
1435
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001436 public LauncherAppWidgetHost getAppWidgetHost() {
1437 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
Romain Guycbb89e42009-06-08 15:52:54 -07001439
Winson Chunga9abd0e2010-10-27 17:18:37 -07001440 public LauncherModel getModel() {
1441 return mModel;
1442 }
1443
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001444 public ModelWriter getModelWriter() {
1445 return mModelWriter;
1446 }
1447
Adam Cohen79d90c52016-04-22 13:29:20 -07001448 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001449 return mSharedPrefs;
1450 }
1451
Jon Miranda6bed3502017-09-19 14:43:17 -07001452 public int getOrientation() { return mOrientation; }
1453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 @Override
1455 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001456 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 super.onNewIntent(intent);
1458
Winson15f8b172015-08-19 11:02:39 -07001459 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1460 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1461 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001462
1463 // Check this condition before handling isActionMain, as this will get reset.
1464 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001465 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001466
Winson15f8b172015-08-19 11:02:39 -07001467 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1468 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001469 if (mWorkspace == null) {
1470 // Can be cases where mWorkspace is null, this prevents a NPE
1471 return;
1472 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001473
1474 // Note: There should be at most one log per method call. This is enforced implicitly
1475 // by using if-else statements.
1476 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001477 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001478 if (topOpenView != null) {
1479 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001480 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301481 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001482 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1483 }
1484
1485 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001486 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal53b99362017-10-05 14:58:56 -07001487 showWorkspace(alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001488
1489 final View v = getWindow().peekDecorView();
1490 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001491 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001492 }
1493
Winson Chungb745afb2015-03-02 11:51:23 -08001494 // Reset the apps view
1495 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001496 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001497 }
1498
Hyunyoung Song3f471442015-04-08 19:01:34 -07001499 // Reset the widgets view
1500 if (!alreadyOnHome && mWidgetsView != null) {
1501 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001502 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001503
Adam Cohen9211d422014-10-07 18:14:53 -07001504 if (mLauncherCallbacks != null) {
1505 mLauncherCallbacks.onHomeIntent();
1506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001508 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001509
Adam Cohen9211d422014-10-07 18:14:53 -07001510 if (mLauncherCallbacks != null) {
1511 mLauncherCallbacks.onNewIntent(intent);
1512 }
Winson15f8b172015-08-19 11:02:39 -07001513
1514 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1515 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1516 // animation.
1517 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001518 boolean callbackAllowsMoveToDefaultScreen =
1519 mLauncherCallbacks == null || mLauncherCallbacks
1520 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001521 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1522 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001523
Winson15f8b172015-08-19 11:02:39 -07001524 mWorkspace.post(new Runnable() {
1525 @Override
1526 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001527 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001528 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001529 }
Winson15f8b172015-08-19 11:02:39 -07001530 }
1531 });
1532 }
1533 }
1534
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001535 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001536 }
1537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001539 public void onRestoreInstanceState(Bundle state) {
1540 super.onRestoreInstanceState(state);
1541 for (int page: mSynchronouslyBoundPages) {
1542 mWorkspace.restoreInstanceStateForChild(page);
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545
1546 @Override
1547 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001548 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001549 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001550
Adam Cohen21cd0022013-10-09 18:57:02 -07001551 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001552 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
Michael Jurka883f55b2010-10-21 15:47:14 -07001554 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001555 // We close any open folders and shortcut containers since they will not be re-opened,
1556 // and we need to make sure this state is reflected.
1557 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001558
Sunny Goyal2100c782016-08-22 16:00:03 -07001559 if (mPendingRequestArgs != null) {
1560 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1561 }
1562 if (mPendingActivityResult != null) {
1563 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
1565
Adam Cohen9211d422014-10-07 18:14:53 -07001566 if (mLauncherCallbacks != null) {
1567 mLauncherCallbacks.onSaveInstanceState(outState);
1568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570
1571 @Override
1572 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001575 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001576 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001577
Winson Chungcd2b0142011-06-08 16:02:26 -07001578 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001579 // It's possible to receive onDestroy after a new Launcher activity has
1580 // been created. In this case, don't interfere with the new Launcher.
1581 if (mModel.isCurrentCallbacks(this)) {
1582 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001583 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001584 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001585
Sunny Goyal745bad92016-05-02 10:54:12 -07001586 if (mRotationPrefChangeHandler != null) {
1587 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1588 }
1589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001591 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001593 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001595 mAppWidgetHost = null;
1596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 TextKeyListener.getInstance().release();
1598
Tony Wickhame2217252016-03-22 16:34:23 -07001599 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1600 .removeAccessibilityStateChangeListener(this);
1601
Mario Bertschler27288382017-05-24 15:35:09 -07001602 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1603
Michael Jurka2ecf9952012-06-18 12:52:28 -07001604 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001605
Tony2917a8b2017-07-31 23:29:42 -07001606 clearPendingBinds();
1607
Adam Cohen9211d422014-10-07 18:14:53 -07001608 if (mLauncherCallbacks != null) {
1609 mLauncherCallbacks.onDestroy();
1610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
1612
Sunny Goyalae502842016-06-17 08:43:56 -07001613 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1614 return mAccessibilityDelegate;
1615 }
1616
Adam Cohena9cf38f2011-05-02 15:36:58 -07001617 public DragController getDragController() {
1618 return mDragController;
1619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001622 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001623 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001624 }
1625
1626 @Override
1627 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1628 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001629 try {
1630 super.startIntentSenderForResult(intent, requestCode,
1631 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1632 } catch (IntentSender.SendIntentException e) {
1633 throw new ActivityNotFoundException();
1634 }
1635 }
1636
Winson Chung70d72102011-08-12 11:24:35 -07001637 /**
1638 * Indicates that we want global search for this activity by setting the globalSearch
1639 * argument for {@link #startSearch} to true.
1640 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001642 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001644
Karl Rosaen138a0412009-04-23 19:00:21 -07001645 if (initialQuery == null) {
1646 // Use any text typed in the launcher as the initial query
1647 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 if (appSearchData == null) {
1650 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001651 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001653
Sunny Goyal6f28e712016-09-13 14:19:29 -07001654 if (mLauncherCallbacks == null ||
1655 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1656 // Starting search from the callbacks failed. Start the default global search.
1657 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001658 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001659
1660 // We need to show the workspace after starting the search
1661 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001662 }
1663
Ian Parkinson047036e2014-09-01 15:40:53 +01001664 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001665 * Starts the global search activity. This code is a copied from SearchManager
1666 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001667 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001668 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001669 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001670 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1671 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1672 if (globalSearchActivity == null) {
1673 Log.w(TAG, "No global search activity found.");
1674 return;
1675 }
1676 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1677 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1678 intent.setComponent(globalSearchActivity);
1679 // Make sure that we have a Bundle to put source in
1680 if (appSearchData == null) {
1681 appSearchData = new Bundle();
1682 } else {
1683 appSearchData = new Bundle(appSearchData);
1684 }
Adam Cohen9211d422014-10-07 18:14:53 -07001685 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001686 if (!appSearchData.containsKey("source")) {
1687 appSearchData.putString("source", getPackageName());
1688 }
1689 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1690 if (!TextUtils.isEmpty(initialQuery)) {
1691 intent.putExtra(SearchManager.QUERY, initialQuery);
1692 }
1693 if (selectInitialQuery) {
1694 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1695 }
1696 intent.setSourceBounds(sourceBounds);
1697 try {
1698 startActivity(intent);
1699 } catch (ActivityNotFoundException ex) {
1700 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703
Winson Chung70d72102011-08-12 11:24:35 -07001704 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001705 public boolean onPrepareOptionsMenu(Menu menu) {
1706 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001707 if (mLauncherCallbacks != null) {
1708 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1709 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001710 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001713 @Override
1714 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001715 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001716 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001717 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001718 }
1719
Joe Onorato9c1289c2009-08-17 11:03:03 -04001720 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001721 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722 }
1723
Adam Cohen517a7f52014-03-01 12:12:59 -08001724 public boolean isWorkspaceLoading() {
1725 return mWorkspaceLoading;
1726 }
1727
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001728 private void setWorkspaceLoading(boolean value) {
1729 boolean isLocked = isWorkspaceLocked();
1730 mWorkspaceLoading = value;
1731 if (isLocked != isWorkspaceLocked()) {
1732 onWorkspaceLockedChanged();
1733 }
1734 }
1735
Sunny Goyal04a324a2017-01-20 21:08:59 -08001736 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001737 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001738 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001739 if (isLocked != isWorkspaceLocked()) {
1740 onWorkspaceLockedChanged();
1741 }
1742 }
1743
Adam Cohen9211d422014-10-07 18:14:53 -07001744 protected void onWorkspaceLockedChanged() {
1745 if (mLauncherCallbacks != null) {
1746 mLauncherCallbacks.onWorkspaceLockedChanged();
1747 }
1748 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001749
Sunny Goyal2100c782016-08-22 16:00:03 -07001750 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001751 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001752 if (LOGD) {
1753 Log.d(TAG, "Adding widget from drop");
1754 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001755 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001756 }
1757
Sunny Goyal2100c782016-08-22 16:00:03 -07001758 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001759 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1760 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1761 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001762
Adam Cohenad4e15c2013-10-17 16:21:35 -07001763 Runnable onComplete = new Runnable() {
1764 @Override
1765 public void run() {
1766 // Exit spring loaded mode if necessary after adding the widget
1767 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1768 null);
1769 }
1770 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001771 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001772 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
1774 }
Romain Guycbb89e42009-06-08 15:52:54 -07001775
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001776 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1777 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001778 info.container = container;
1779 info.screenId = screenId;
1780 if (cell != null) {
1781 info.cellX = cell[0];
1782 info.cellY = cell[1];
1783 }
1784 info.spanX = spanX;
1785 info.spanY = spanY;
1786
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001787 switch (info.itemType) {
1788 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1789 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001790 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001791 break;
1792 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001793 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001794 break;
1795 default:
1796 throw new IllegalStateException("Unknown item type: " + info.itemType);
1797 }
1798 }
1799
Adam Cohenfbba09b2011-07-18 21:43:05 -07001800 /**
1801 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001802 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001803 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001804 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1805 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001806 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1807 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1808 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001809 }
1810
Adam Cohenfbba09b2011-07-18 21:43:05 -07001811 /**
1812 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001813 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001814 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001815 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001816 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001817 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001818 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001819 // In the case where we've prebound the widget, we remove it from the DragLayer
1820 if (LOGD) {
1821 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1822 }
1823 getDragLayer().removeView(hostView);
1824
Adam Cohened66b2b2012-01-23 17:28:51 -08001825 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001826 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001827
1828 // Clear the boundWidget so that it doesn't get destroyed.
1829 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001830 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001831 // In this case, we either need to start an activity to get permission to bind
1832 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001833 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1834 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1835 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1836 this, info.componentName);
1837 } else {
1838 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1839 }
Adam Cohendd70d662012-10-04 16:53:44 -07001840 Bundle options = info.bindOptions;
1841
Sunny Goyalffe83f12014-08-14 17:39:34 -07001842 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1843 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001844 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001845 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001846 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001847 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001848 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001849 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001850 }
1851
Adam Cohendcd297f2013-06-18 13:13:40 -07001852 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001853 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001854 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 folderInfo.title = getText(R.string.folder_name);
1856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001858 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859
1860 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001861 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301862 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001863 // Force measure the new folder icon
1864 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1865 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001866 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
Romain Guycbb89e42009-06-08 15:52:54 -07001868
Winsonc0b52fe2015-09-09 16:38:15 -07001869 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001870 * Unbinds the view for the specified item, and removes the item and all its children.
1871 *
1872 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001873 * @param itemInfo the {@link ItemInfo} for this view.
1874 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001875 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001876 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001877 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001878 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001879 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1880 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001881 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001882 } else {
1883 mWorkspace.removeWorkspaceItem(v);
1884 }
Winsonc0b52fe2015-09-09 16:38:15 -07001885 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001886 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001887 }
1888 } else if (itemInfo instanceof FolderInfo) {
1889 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001890 if (v instanceof FolderIcon) {
1891 ((FolderIcon) v).removeListeners();
1892 }
Winsonc0b52fe2015-09-09 16:38:15 -07001893 mWorkspace.removeWorkspaceItem(v);
1894 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001895 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001896 }
1897 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1898 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001899 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001900 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001901 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001902 }
1903 } else {
1904 return false;
1905 }
1906 return true;
1907 }
1908
1909 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001910 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001911 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001912 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001913 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001914 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001915 // Deleting an app widget ID is a void call but writes to disk before returning
1916 // to the caller...
1917 new AsyncTask<Void, Void, Void>() {
1918 public Void doInBackground(Void ... args) {
1919 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1920 return null;
1921 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001922 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001923 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001924 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001925 }
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 @Override
1928 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001929 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931
Joe Onorato88ec0992009-11-19 13:16:06 -08001932 @Override
1933 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001934 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1935 return;
1936 }
1937
Sunny Goyal45478022015-06-08 16:52:41 -07001938 if (mDragController.isDragging()) {
1939 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001940 return;
1941 }
1942
Jon Mirandafeba90f2016-10-06 10:53:29 -07001943 // Note: There should be at most one log per method call. This is enforced implicitly
1944 // by using if-else statements.
1945 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001946 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1947 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001948 topView.onBackPressed();
Tony Wickham49c8d292016-07-01 11:44:34 -07001949 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301950 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001951 showWorkspace(true);
1952 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301953 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08001954 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001955 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301956 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001957 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001958 } else {
1959 // Back button is a no-op here, but give at least some feedback for the button press
1960 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001961 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001962 }
1963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 * Launches the intent referred by the clicked shortcut.
1966 *
1967 * @param v The view representing the clicked shortcut.
1968 */
1969 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001970 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1971 // view has detached (it's possible for this to happen if the view is removed mid touch).
1972 if (v.getWindowToken() == null) {
1973 return;
1974 }
1975
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001976 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001977 return;
1978 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001979
Adam Cohen1697b792013-09-17 19:08:21 -07001980 if (v instanceof Workspace) {
1981 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001982 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1983 LauncherLogProto.Action.Direction.NONE,
1984 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07001985 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001986 }
1987 return;
1988 }
1989
1990 if (v instanceof CellLayout) {
1991 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001992 int page = mWorkspace.indexOfChild(v);
1993 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1994 LauncherLogProto.Action.Direction.NONE,
1995 LauncherLogProto.ContainerType.OVERVIEW, page);
1996 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001997 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001998 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001999 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002000 }
2001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002003 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002004 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002006 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002007 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002008 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07002009 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002010 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002011 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002012 } else if (tag instanceof AppInfo) {
2013 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002014 } else if (tag instanceof LauncherAppWidgetInfo) {
2015 if (v instanceof PendingAppWidgetHostView) {
2016 onClickPendingWidget((PendingAppWidgetHostView) v);
2017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
2019 }
2020
Sunny Goyal70660032015-05-14 00:07:08 -07002021 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002022 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002023 return false;
2024 }
2025
Michael Jurkaaf442092010-06-10 17:01:57 -07002026 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002027 * Event handler for the app widget view which has not fully restored.
2028 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002029 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002030 if (mIsSafeModeEnabled) {
2031 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2032 return;
2033 }
2034
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002035 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002036 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002037 LauncherAppWidgetProviderInfo appWidgetInfo =
2038 mAppWidgetManager.findProvider(info.providerName, info.user);
2039 if (appWidgetInfo == null) {
2040 return;
2041 }
2042 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2043
Sunny Goyald478c832016-04-01 12:04:16 -07002044 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2045 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2046 // This should not happen, as we make sure that an Id is allocated during bind.
2047 return;
2048 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002049 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2050 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002051 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002052 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002053 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002054 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002055 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002056 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002057 }
2058 }
2059
2060 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002061 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2062 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2063 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002064 *
2065 * @param v The view that was clicked.
2066 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002067 protected void onClickAllAppsButton(View v) {
2068 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002069 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302070 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2071 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002072 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002073 } else {
2074 showWorkspace(true);
2075 }
2076 }
2077
Sunny Goyale6e72002017-01-12 16:55:36 -08002078 private void onClickPendingAppItem(final View v, final String packageName,
2079 boolean downloadStarted) {
2080 if (downloadStarted) {
2081 // If the download has started, simply direct to the market app.
2082 startMarketIntentForPackage(v, packageName);
2083 return;
2084 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002085 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002086 .setTitle(R.string.abandoned_promises_title)
2087 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002088 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2089 @Override
2090 public void onClick(DialogInterface dialogInterface, int i) {
2091 startMarketIntentForPackage(v, packageName);
2092 }
2093 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002094 .setNeutralButton(R.string.abandoned_clean_this,
2095 new DialogInterface.OnClickListener() {
2096 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002097 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002098 mWorkspace.removeAbandonedPromise(packageName, user);
2099 }
2100 })
2101 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002102 }
2103
2104 private void startMarketIntentForPackage(View v, String packageName) {
2105 ItemInfo item = (ItemInfo) v.getTag();
2106 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2107 boolean success = startActivitySafely(v, intent, item);
2108 if (success && v instanceof BubbleTextView) {
2109 mWaitingForResume = (BubbleTextView) v;
2110 mWaitingForResume.setStayPressed(true);
2111 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002112 }
2113
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002114 /**
2115 * Event handler for an app shortcut click.
2116 *
2117 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2118 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002119 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002120 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2121 Object tag = v.getTag();
2122 if (!(tag instanceof ShortcutInfo)) {
2123 throw new IllegalArgumentException("Input must be a Shortcut");
2124 }
2125
2126 // Open shortcut
2127 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002128
2129 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002130 if ((shortcut.isDisabled &
2131 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2132 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2133 // If the app is only disabled because of the above flags, launch activity anyway.
2134 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002135 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002136 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2137 // Use a message specific to this shortcut, if it has one.
2138 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2139 return;
2140 }
2141 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002142 int error = R.string.activity_not_available;
2143 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2144 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002145 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2146 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002147 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002148 }
2149 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2150 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002151 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002152 }
2153
Chris Wren40c5ed32014-06-24 18:24:23 -04002154 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002155 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002156 String packageName = shortcut.intent.getComponent() != null ?
2157 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2158 if (!TextUtils.isEmpty(packageName)) {
2159 onClickPendingAppItem(v, packageName,
2160 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2161 return;
2162 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002163 }
2164
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002165 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002166 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002167 }
2168
Sunny Goyala7ce1662016-05-31 15:01:35 -07002169 private void startAppShortcutOrInfoActivity(View v) {
2170 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002171 Intent intent;
2172 if (item instanceof PromiseAppInfo) {
2173 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2174 intent = promiseAppInfo.getMarketIntent();
2175 } else {
2176 intent = item.getIntent();
2177 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002178 if (intent == null) {
2179 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002180 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002181 boolean success = startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002182 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002183
2184 if (success && v instanceof BubbleTextView) {
2185 mWaitingForResume = (BubbleTextView) v;
2186 mWaitingForResume.setStayPressed(true);
2187 }
2188 }
2189
2190 /**
2191 * Event handler for a folder icon click.
2192 *
2193 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2194 */
2195 protected void onClickFolderIcon(View v) {
2196 if (LOGD) Log.d(TAG, "onClickFolder");
2197 if (!(v instanceof FolderIcon)){
2198 throw new IllegalArgumentException("Input must be a FolderIcon");
2199 }
2200
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002201 Folder folder = ((FolderIcon) v).getFolder();
2202 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002203 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002204 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002205 }
Michael Jurka5130e402011-10-13 04:55:35 -07002206 }
2207
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002208 /**
2209 * Event handler for the (Add) Widgets button that appears after a long press
2210 * on the home screen.
2211 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002212 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002213 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002214 if (mIsSafeModeEnabled) {
2215 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2216 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002217 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002218 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002219 }
2220
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002221 /**
2222 * Event handler for the wallpaper picker button that appears after a long press
2223 * on the home screen.
2224 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002225 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002226 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002227 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2228 return;
2229 }
2230
Tony Wickham785f7a52015-08-31 17:28:32 -07002231 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2232 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002233 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002234 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002235 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002236
2237 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002238 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2239 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002240 intent.setPackage(pickerPackage);
2241 }
2242
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002243 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002244 try {
2245 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2246 // If there is no target package, use the default intent chooser animation
2247 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2248 } catch (ActivityNotFoundException e) {
2249 setWaitingForResult(null);
2250 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2251 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002252 }
2253
2254 /**
2255 * Event handler for a click on the settings button that appears after a long press
2256 * on the home screen.
2257 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002258 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002259 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002260 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2261 .setPackage(getPackageName());
2262 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002263 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002264 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002265 }
2266
Tony Wickhame2217252016-03-22 16:34:23 -07002267 @Override
2268 public void onAccessibilityStateChanged(boolean enabled) {
2269 mDragLayer.onAccessibilityStateChanged(enabled);
2270 }
2271
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002272 /**
2273 * Called when the user stops interacting with the launcher.
2274 * This implies that the user is now on the homescreen and is not doing housekeeping.
2275 */
Adam Cohen9211d422014-10-07 18:14:53 -07002276 protected void onInteractionEnd() {
2277 if (mLauncherCallbacks != null) {
2278 mLauncherCallbacks.onInteractionEnd();
2279 }
2280 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002281
2282 /**
2283 * Called when the user starts interacting with the launcher.
2284 * The possible interactions are:
2285 * - open all apps
2286 * - reorder an app shortcut, or a widget
2287 * - open the overview mode.
2288 * This is a good time to stop doing things that only make sense
2289 * when the user is on the homescreen and not doing housekeeping.
2290 */
Adam Cohen9211d422014-10-07 18:14:53 -07002291 protected void onInteractionBegin() {
2292 if (mLauncherCallbacks != null) {
2293 mLauncherCallbacks.onInteractionBegin();
2294 }
2295 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002296
Winson Chungcd99cd32015-04-29 11:03:24 -07002297 /** Updates the interaction state. */
2298 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002299 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002300 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002301 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2302 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002303 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002304 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002305 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002306 onInteractionEnd();
2307 }
2308 }
2309
Sunny Goyala7ce1662016-05-31 15:01:35 -07002310 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002312 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2313 try {
2314 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2315 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2316 // is enabled by default on NYC.
2317 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2318 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002319
2320 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2321 String id = ((ShortcutInfo) info).getDeepShortcutId();
2322 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302323 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002324 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002325 } else {
2326 // Could be launching some bookkeeping activity
2327 startActivity(intent, optsBundle);
2328 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002329 } finally {
2330 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002333 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2334 // corresponding permission. Show the appropriate permission prompt if that
2335 // is the case.
2336 if (intent.getComponent() == null
2337 && Intent.ACTION_CALL.equals(intent.getAction())
2338 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2339 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002340
2341 setWaitingForResult(PendingRequestArgs
2342 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002343 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2344 REQUEST_PERMISSION_CALL_PHONE);
2345 } else {
2346 // No idea why this was thrown.
2347 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 }
2350 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002351
Sunny Goyalfe770c92016-09-27 14:38:58 -07002352 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002353 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002354 if (Utilities.ATLEAST_MARSHMALLOW) {
2355 int left = 0, top = 0;
2356 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002357 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002358 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002359 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002360 if (icon != null) {
2361 Rect bounds = icon.getBounds();
2362 left = (width - bounds.width()) / 2;
2363 top = v.getPaddingTop();
2364 width = bounds.width();
2365 height = bounds.height();
2366 }
2367 }
2368 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2369 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2370 // On L devices, we use the device default slide-up transition.
2371 // On L MR1 devices, we use a custom version of the slide-up transition which
2372 // doesn't have the delay present in the device default.
2373 return ActivityOptions.makeCustomAnimation(
2374 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2375 }
2376 return null;
2377 }
2378
Tonye3c59252017-05-02 21:32:27 -07002379 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002380 int[] pos = new int[2];
2381 v.getLocationOnScreen(pos);
2382 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2383 }
2384
Sunny Goyala7ce1662016-05-31 15:01:35 -07002385 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002386 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2387 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2388 return false;
2389 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002390 // Only launch using the new animation if the shortcut has not opted out (this is a
2391 // private contract between launcher and may be ignored in the future).
2392 boolean useLaunchAnimation = (v != null) &&
2393 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2394 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2395
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002396 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002397
2398 // Prepare intent
2399 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2400 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002401 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002402 }
2403 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002404 if (Utilities.ATLEAST_MARSHMALLOW
2405 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002406 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002407 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002408 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002409 // Shortcuts need some special checks due to legacy reasons.
2410 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002411 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002412 // Could be launching some bookkeeping activity
2413 startActivity(intent, optsBundle);
2414 } else {
2415 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2416 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2417 }
2418 return true;
2419 } catch (ActivityNotFoundException|SecurityException e) {
2420 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2421 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2422 }
2423 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002424 }
2425
Tony Wickhamdadb3042016-02-24 11:07:00 -08002426 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002427 public boolean dispatchTouchEvent(MotionEvent ev) {
2428 mLastDispatchTouchEventX = ev.getX();
2429 return super.dispatchTouchEvent(ev);
2430 }
2431
2432 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002434 if (!isDraggingEnabled()) return false;
2435 if (isWorkspaceLocked()) return false;
2436 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437
Jon Miranda51f037d2016-11-07 08:37:20 -08002438 boolean ignoreLongPressToOverview =
2439 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002440
Adam Cohen1697b792013-09-17 19:08:21 -07002441 if (v instanceof Workspace) {
2442 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002443 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302444 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2445 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002446 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002447 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002448 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2449 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2450 return true;
2451 } else {
2452 return false;
2453 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002454 } else {
2455 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002456 }
Adam Cohen1697b792013-09-17 19:08:21 -07002457 }
2458
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002459 CellLayout.CellInfo longClickCellInfo = null;
2460 View itemUnderLongClick = null;
2461 if (v.getTag() instanceof ItemInfo) {
2462 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002463 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002464 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002465 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 }
2467
Winson Chung3d503fb2011-07-13 17:25:49 -07002468 // The hotseat touch handling does not go through Workspace, and we always allow long press
2469 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002470 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002471 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002472 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002473 if (mWorkspace.isInOverviewMode()) {
2474 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302475 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2476 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002477 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002478 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002479 return false;
2480 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302481 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2482 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002483 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002484 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002485 }
Jon Miranda379198e2016-09-23 08:54:40 -07002486 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2487 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002489 final boolean isAllAppsButton =
2490 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2491 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2492 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002493 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002495 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 }
2497 }
2498 }
2499 return true;
2500 }
2501
Winson Chung3d503fb2011-07-13 17:25:49 -07002502 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002503 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002504 return mHotseat != null && layout != null &&
2505 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2506 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002507
Winson Chung3d503fb2011-07-13 17:25:49 -07002508 /**
2509 * Returns the CellLayout of the specified container at the specified screen.
2510 */
Sunny Goyal92820592015-03-02 11:31:35 -08002511 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002512 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2513 if (mHotseat != null) {
2514 return mHotseat.getLayout();
2515 } else {
2516 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002517 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002518 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002519 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002520 }
2521 }
2522
Winson Chungb745afb2015-03-02 11:51:23 -08002523 /**
2524 * For overridden classes.
2525 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002526 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002527 return isAppsViewVisible();
2528 }
2529
2530 public boolean isAppsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002531 return mState == State.APPS;
Winson Chungb745afb2015-03-02 11:51:23 -08002532 }
2533
2534 public boolean isWidgetsViewVisible() {
Sunny Goyal53b99362017-10-05 14:58:56 -07002535 return mState == State.WIDGETS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002536 }
2537
Michael Jurkae326f182011-11-21 14:05:46 -08002538 @Override
2539 public void onTrimMemory(int level) {
2540 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002541 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2542 // The widget preview db can result in holding onto over
2543 // 3MB of memory for caching which isn't necessary.
2544 SQLiteDatabase.releaseMemory();
2545
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002546 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002547 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002548 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002549 if (mLauncherCallbacks != null) {
2550 mLauncherCallbacks.onTrimMemory(level);
2551 }
Michael Jurkae326f182011-11-21 14:05:46 -08002552 }
2553
Winson5c6bdbb2015-09-03 11:36:19 -07002554 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002555 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002556 }
2557
Winson5c6bdbb2015-09-03 11:36:19 -07002558 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002559 boolean changed = mState != State.WORKSPACE ||
2560 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002561 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002562 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002563 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002564 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002565
Michael Jurkab3e22d92011-10-31 15:58:33 -07002566 // Set focus to the AppsCustomize button
2567 if (mAllAppsButton != null) {
2568 mAllAppsButton.requestFocus();
2569 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002570 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002571
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002572 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002573 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002574
Winson Chung0f785722015-04-08 10:27:49 -07002575 if (changed) {
2576 // Send an accessibility event to announce the context change
2577 getWindow().getDecorView()
2578 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002579 }
Winson5c6bdbb2015-09-03 11:36:19 -07002580 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002581 }
2582
Winsone9f27272015-10-13 10:47:51 -07002583 /**
2584 * Shows the overview button.
2585 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002586 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002587 showOverviewMode(animated, false);
2588 }
2589
2590 /**
2591 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2592 * onto one of the overview panel buttons.
2593 */
2594 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2595 Runnable postAnimRunnable = null;
2596 if (requestButtonFocus) {
2597 postAnimRunnable = new Runnable() {
2598 @Override
2599 public void run() {
2600 // Hitting the menu button when in touch mode does not trigger touch mode to
2601 // be disabled, so if requested, force focus on one of the overview panel
2602 // buttons.
2603 mOverviewPanel.requestFocusFromTouch();
2604 }
2605 };
2606 }
Adam Cohened307df2013-10-02 09:37:31 -07002607 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002608 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002609 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002610 setState(State.WORKSPACE);
2611
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002612 // If animated from long press, then don't allow any of the controller in the drag
2613 // layer to intercept any remaining touch.
2614 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002615 }
2616
Mario Bertschleracbf5702017-03-03 10:58:06 -08002617 private void setState(State state) {
2618 this.mState = state;
2619 updateSoftInputMode();
2620 }
2621
2622 private void updateSoftInputMode() {
2623 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2624 final int mode;
2625 if (isAppsViewVisible()) {
2626 mode = SOFT_INPUT_MODE_ALL_APPS;
2627 } else {
2628 mode = SOFT_INPUT_MODE_DEFAULT;
2629 }
2630 getWindow().setSoftInputMode(mode);
2631 }
2632 }
2633
Winson Chungb745afb2015-03-02 11:51:23 -08002634 /**
2635 * Shows the apps view.
2636 */
Sunny Goyald0d07032017-10-04 10:40:28 -07002637 public void showAppsView(boolean animated) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002638 markAppsViewShown();
Sunny Goyal5606e072017-06-15 14:24:21 -07002639 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002640 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002641
Winson Chungb745afb2015-03-02 11:51:23 -08002642 /**
2643 * Shows the widgets view.
2644 */
2645 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002646 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002647 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002648 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002649 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002650 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002651
2652 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002653 @Override
2654 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002655 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002656 }
2657 });
Winson Chungb745afb2015-03-02 11:51:23 -08002658 }
2659
2660 /**
2661 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002662 *
2663 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002664 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002665 // TODO: calling method should use the return value so that when {@code false} is returned
2666 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002667 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002668 if (!(mState == State.WORKSPACE ||
2669 mState == State.APPS_SPRING_LOADED ||
2670 mState == State.WIDGETS_SPRING_LOADED ||
2671 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002672 return false;
2673 }
2674 if (toState != State.APPS && toState != State.WIDGETS) {
2675 return false;
2676 }
Winson Chungb745afb2015-03-02 11:51:23 -08002677
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002678 // This is a safe and supported transition to bypass spring_loaded mode.
2679 if (mExitSpringLoadedModeRunnable != null) {
2680 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2681 mExitSpringLoadedModeRunnable = null;
2682 }
2683
Winson Chungb745afb2015-03-02 11:51:23 -08002684 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002685 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002686 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002687 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002688 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002689
Michael Jurkab3e22d92011-10-31 15:58:33 -07002690 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002691 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002692 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002693
2694 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002695 getWindow().getDecorView()
2696 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002697 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002698 }
2699
Winson Chungcd99cd32015-04-29 11:03:24 -07002700 /**
2701 * Updates the workspace and interaction state on state change, and return the animation to this
2702 * new state.
2703 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002704 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002705 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002706 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002707 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002708 updateInteraction(fromState, toState);
2709 return anim;
2710 }
2711
Hyunyoung Song3f471442015-04-08 19:01:34 -07002712 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002713 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002714 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002715 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002716 }
Winson Chungb745afb2015-03-02 11:51:23 -08002717
Winson Chung006ee262015-08-03 14:40:11 -07002718 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002719 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002720 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002721 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002722 }
Adam Cohen7777d962011-08-18 18:58:38 -07002723
Hyunyoung Song3f471442015-04-08 19:01:34 -07002724 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002725 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002726 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002727
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002728 if (mExitSpringLoadedModeRunnable != null) {
2729 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2730 }
2731 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002732 @Override
2733 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002734 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002735 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2736 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002737 // Before we show workspace, hide all apps again because
2738 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2739 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002740 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002741 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002742 } else {
2743 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002744 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002745 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002746 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002747 };
2748 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002749 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002750
Tony Wickhame0c33232016-02-08 11:37:04 -08002751 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002752 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2753 || mState == State.WIDGETS_SPRING_LOADED;
2754 }
2755
Sunny Goyal0f76b562016-12-13 19:37:10 -08002756 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002757 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyald0d07032017-10-04 10:40:28 -07002758 showAppsView(true /* animated */);
Winson Chungb745afb2015-03-02 11:51:23 -08002759 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002760 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002761 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2762 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002763 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002764 }
2765
Michael Jurkad7c28052012-04-27 15:43:36 -07002766 @Override
2767 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002768 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002769 final List<CharSequence> text = event.getText();
2770 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002771 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002772 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002773 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002774 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002775 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002776 } else if (mWorkspace != null) {
2777 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002778 } else {
2779 text.add(getString(R.string.all_apps_home_button_label));
2780 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002781 return result;
2782 }
2783
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002784 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002785 * If the activity is currently paused, signal that we need to run the passed Runnable
2786 * in onResume.
2787 *
2788 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002789 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2790 * wrong when we're not running, and if the activity comes back to what the configuration was
2791 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002792 *
2793 * Implementation of the method from LauncherModel.Callbacks.
2794 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002795 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002796 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002797 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002798 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002799 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002800 if (run instanceof RunnableWithId) {
2801 // Remove any runnables which have the same id
2802 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002803 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002804 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002805 return true;
2806 } else {
2807 return false;
2808 }
2809 }
2810
Michael Jurka1e2f4652013-07-08 18:03:46 -07002811 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02002812 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002813 }
2814
Michael Jurka7607c2f2013-04-03 14:33:19 -07002815 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002816 * If the activity is currently paused, signal that we need to re-run the loader
2817 * in onResume.
2818 *
2819 * This needs to be called from incoming places where resources might have been loaded
2820 * while we are paused. That is becaues the Configuration might be wrong
2821 * when we're not running, and if it comes back to what it was when we
2822 * were paused, we are not restarted.
2823 *
2824 * Implementation of the method from LauncherModel.Callbacks.
2825 *
2826 * @return true if we are currently paused. The caller might be able to
2827 * skip some work in that case since we will come back again.
2828 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002829 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002830 public boolean setLoadOnResume() {
2831 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002832 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002833 mOnResumeNeedsLoad = true;
2834 return true;
2835 } else {
2836 return false;
2837 }
2838 }
2839
2840 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002841 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002842 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002843 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002844 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002845 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002846 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002847 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002848 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002850 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002851
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002853 * Clear any pending bind callbacks. This is called when is loader is planning to
2854 * perform a full rebind from scratch.
2855 */
2856 @Override
2857 public void clearPendingBinds() {
2858 mBindOnResumeCallbacks.clear();
2859 if (mPendingExecutor != null) {
2860 mPendingExecutor.markCompleted();
2861 mPendingExecutor = null;
2862 }
2863 }
2864
2865 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 * Refreshes the shortcuts shown on the workspace.
2867 *
2868 * Implementation of the method from LauncherModel.Callbacks.
2869 */
2870 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002871 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002872 AbstractFloatingView.closeAllOpenViews(this);
2873
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002874 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002875
Winson Chungd64d1762013-08-20 14:37:16 -07002876 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002877 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002878 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002879
Winson Chung3d503fb2011-07-13 17:25:49 -07002880 if (mHotseat != null) {
2881 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002883 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 }
2885
Adam Cohendcd297f2013-06-18 13:13:40 -07002886 @Override
2887 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002888 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002889 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2890 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002891 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2892 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002893 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002894 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2895 // If there are no screens, we need to have an empty screen
2896 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002897 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002898 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002899
Winsonc7d2e832016-07-28 12:24:55 -07002900 // After we have added all the screens, if the wallpaper was locked to the default state,
2901 // then notify to indicate that it can be released and a proper wallpaper offset can be
2902 // computed before the next layout
2903 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002904 }
2905
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002906 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002907 int count = orderedScreenIds.size();
2908 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002909 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002910 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002911 // No need to bind the first screen, as its always bound.
2912 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2913 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002914 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002915 }
2916
Sunny Goyalb23980c2017-08-17 07:45:25 -07002917 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002918 public void bindAppsAdded(final ArrayList<Long> newScreens,
2919 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002920 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002921 Runnable r = new Runnable() {
2922 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002923 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002924 }
2925 };
2926 if (waitUntilResume(r)) {
2927 return;
2928 }
2929
Winson Chungd64d1762013-08-20 14:37:16 -07002930 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002931 if (newScreens != null) {
2932 bindAddScreens(newScreens);
2933 }
Winson Chungd64d1762013-08-20 14:37:16 -07002934
2935 // We add the items without animation on non-visible pages, and with
2936 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002937 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002938 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002939 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002940 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002941 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002942 }
Winson Chungc58497e2013-09-03 17:48:37 -07002943
Winson Chung87412982013-10-03 18:34:14 -07002944 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002945 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002946 }
2947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949 * Bind the items start-end from the list.
2950 *
2951 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002953 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002954 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002955 Runnable r = new Runnable() {
2956 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002957 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002958 }
2959 };
2960 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002961 return;
2962 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002963
Sunny Goyal3be633b2016-12-08 09:59:25 -08002964 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002965 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002966 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002967 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002968 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002969 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002970 int end = items.size();
2971 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002972 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002973
2974 // Short circuit if we are loading dock items for a configuration which has no dock
2975 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2976 mHotseat == null) {
2977 continue;
2978 }
2979
Sunny Goyal639e9062015-08-19 19:17:06 -07002980 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002981 switch (item.itemType) {
2982 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2983 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002984 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002985 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002986 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002987 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002988 }
2989 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002990 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002991 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002992 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002993 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002994 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002995 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2996 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002997 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002998 if (view == null) {
2999 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003000 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08003001 break;
3002 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003003 default:
3004 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003006
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003007 /*
3008 * Remove colliding items.
3009 */
3010 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3011 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3012 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3013 View v = cl.getChildAt(item.cellX, item.cellY);
3014 Object tag = v.getTag();
3015 String desc = "Collision while binding workspace item: " + item
3016 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003017 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003018 throw (new RuntimeException(desc));
3019 } else {
3020 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003021 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003022 continue;
3023 }
3024 }
3025 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303026 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003027 if (animateIcons) {
3028 // Animate all the applications up now
3029 view.setAlpha(0f);
3030 view.setScaleX(0f);
3031 view.setScaleY(0f);
3032 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003033 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003036
Winson Chung997a9232013-07-24 15:33:46 -07003037 if (animateIcons) {
3038 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003039 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003040 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003041 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003042 final Runnable startBounceAnimRunnable = new Runnable() {
3043 public void run() {
3044 anim.playTogether(bounceAnims);
3045 anim.start();
3046 }
3047 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003048 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003049 // We post the animation slightly delayed to prevent slowdowns
3050 // when we are loading right after we return to launcher.
3051 mWorkspace.postDelayed(new Runnable() {
3052 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003053 if (mWorkspace != null) {
3054 mWorkspace.snapToPage(newScreenIndex);
3055 mWorkspace.postDelayed(startBounceAnimRunnable,
3056 NEW_APPS_ANIMATION_DELAY);
3057 }
Winson Chung94d67682013-09-25 16:29:40 -07003058 }
3059 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003060 } else {
3061 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003062 }
3063 }
Winson Chung64359a52013-07-08 17:17:08 -07003064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 }
3067
Joe Onorato9c1289c2009-08-17 11:03:03 -04003068 /**
3069 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003071 public void bindAppWidget(LauncherAppWidgetInfo item) {
3072 View view = inflateAppWidget(item);
3073 if (view != null) {
3074 mWorkspace.addInScreen(view, item);
3075 mWorkspace.requestLayout();
3076 }
3077 }
3078
3079 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003080 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303081 PendingAppWidgetHostView view =
3082 new PendingAppWidgetHostView(this, item, mIconCache, true);
3083 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003084 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003085 }
3086
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003087 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003089 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003090
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003091 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3092 // If the provider is not ready, bind as a pending widget.
3093 appWidgetInfo = null;
3094 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3095 // The widget id is not valid. Try to find the widget based on the provider info.
3096 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3097 } else {
3098 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3099 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003100
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003101 // If the provider is ready, but the width is not yet restored, try to restore it.
3102 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3103 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003104 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003105 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3106 + " belongs to component " + item.providerName
3107 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003108 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003109 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003110 }
Sunny Goyalff572272014-07-23 13:58:07 -07003111
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003112 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003113 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003114 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3115 // Id has not been allocated yet. Allocate a new id.
3116 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3117 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003118
Sunny Goyald478c832016-04-01 12:04:16 -07003119 // Also try to bind the widget. If the bind fails, the user will be shown
3120 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003121 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003122 pendingInfo.spanX = item.spanX;
3123 pendingInfo.spanY = item.spanY;
3124 pendingInfo.minSpanX = item.minSpanX;
3125 pendingInfo.minSpanY = item.minSpanY;
3126 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003127
3128 boolean isDirectConfig =
3129 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3130 if (isDirectConfig && item.bindOptions != null) {
3131 Bundle newOptions = item.bindOptions.getExtras();
3132 if (options != null) {
3133 newOptions.putAll(options);
3134 }
3135 options = newOptions;
3136 }
Sunny Goyald478c832016-04-01 12:04:16 -07003137 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3138 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003139
Sunny Goyal86df1382016-08-10 15:03:22 -07003140 // We tried to bind once. If we were not able to bind, we would need to
3141 // go through the permission dialog, which means we cannot skip the config
3142 // activity.
3143 item.bindOptions = null;
3144 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3145
Sunny Goyald478c832016-04-01 12:04:16 -07003146 // Bind succeeded
3147 if (success) {
3148 // If the widget has a configure activity, it is still needs to set it up,
3149 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003150 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003151 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3152 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003153 }
Sunny Goyald478c832016-04-01 12:04:16 -07003154
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003155 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003156 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003157 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003158 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003159 // The widget was marked as UI not ready, but there is no configure activity to
3160 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003161 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003162 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003163 }
Sunny Goyalff572272014-07-23 13:58:07 -07003164 }
3165
Sunny Goyald5462aa2016-11-22 16:52:39 +05303166 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003167 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003168 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003169 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003170 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003171 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003172 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003173 }
3174
Sunny Goyal233ee962015-08-03 13:05:01 -07003175 item.minSpanX = appWidgetInfo.minSpanX;
3176 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303177 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003178 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303179 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003180 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303181 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003183 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003184 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185 }
3186
Sunny Goyalff572272014-07-23 13:58:07 -07003187 /**
3188 * Restores a pending widget.
3189 *
3190 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003191 */
Sunny Goyald478c832016-04-01 12:04:16 -07003192 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003193 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3194 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3195 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003196 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003197 }
3198
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003199 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003200 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003201 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3202 info.pendingItemInfo = null;
3203 }
Sunny Goyalff572272014-07-23 13:58:07 -07003204
Sunny Goyalba47faa2017-10-04 16:50:57 -07003205 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3206 view.reinflate();
3207 }
3208
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003209 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003210 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003211 }
3212
Adam Cohen1462de32012-07-24 22:34:36 -07003213 public void onPageBoundSynchronously(int page) {
3214 mSynchronouslyBoundPages.add(page);
3215 }
3216
Sunny Goyal527c7d32015-08-28 15:19:36 -07003217 @Override
3218 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3219 if (mPendingExecutor != null) {
3220 mPendingExecutor.markCompleted();
3221 }
3222 mPendingExecutor = executor;
3223 executor.attachTo(this);
3224 }
3225
3226 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3227 if (mPendingExecutor == executor) {
3228 mPendingExecutor = null;
3229 }
3230 }
3231
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003232 @Override
3233 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3234 Runnable r = new Runnable() {
3235 public void run() {
3236 finishFirstPageBind(executor);
3237 }
3238 };
3239 if (waitUntilResume(r)) {
3240 return;
3241 }
3242
3243 Runnable onComplete = new Runnable() {
3244 @Override
3245 public void run() {
3246 if (executor != null) {
3247 executor.onLoadAnimationCompleted();
3248 }
3249 }
3250 };
3251 if (mDragLayer.getAlpha() < 1) {
3252 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3253 } else {
3254 onComplete.run();
3255 }
3256 }
3257
Joe Onorato9c1289c2009-08-17 11:03:03 -04003258 /**
3259 * Callback saying that there aren't any more items to bind.
3260 *
3261 * Implementation of the method from LauncherModel.Callbacks.
3262 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003263 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003264 Runnable r = new Runnable() {
3265 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003266 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003267 }
3268 };
3269 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003270 return;
3271 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003272 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003273 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003274
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003275 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003276
Sunny Goyal2100c782016-08-22 16:00:03 -07003277 if (mPendingActivityResult != null) {
3278 handleActivityResult(mPendingActivityResult.requestCode,
3279 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3280 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003281 }
3282
Sunny Goyala474a9b2017-05-04 16:47:11 -07003283 InstallShortcutReceiver.disableAndFlushInstallQueue(
3284 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003285
Tony Wickham010d2552017-01-20 08:15:28 -08003286 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3287
Adam Cohen9211d422014-10-07 18:14:53 -07003288 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003289 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003290 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003291 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003292 }
3293
Winson Chunga2413752012-04-03 14:22:34 -07003294 private boolean canRunNewAppsAnimation() {
3295 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003296 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003297 }
3298
Winson Chungc9168342013-06-26 14:54:55 -07003299 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003300 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003301 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3302 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003303 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003304 return bounceAnim;
3305 }
3306
Adam Cohen27772732013-11-11 14:00:56 +00003307 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003308 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003309 }
3310
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003311 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 * Add the icons for all apps.
3313 *
3314 * Implementation of the method from LauncherModel.Callbacks.
3315 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003316 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003317 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3318 public void run() {
3319 bindAllApplications(apps);
3320 }
3321 };
3322 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003323 return;
3324 }
3325
Winson Chungb745afb2015-03-02 11:51:23 -08003326 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003327 Executor pendingExecutor = getPendingExecutor();
3328 if (pendingExecutor != null && mState != State.APPS) {
3329 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003330 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003331 return;
3332 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003333
Winson Chungb745afb2015-03-02 11:51:23 -08003334 mAppsView.setApps(apps);
3335 }
Adam Cohen9211d422014-10-07 18:14:53 -07003336 if (mLauncherCallbacks != null) {
3337 mLauncherCallbacks.bindAllApplications(apps);
3338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 }
3340
3341 /**
Tony2917a8b2017-07-31 23:29:42 -07003342 * Returns an Executor that will run after the launcher is first drawn (including after the
3343 * initial fade in animation). Returns null if the first draw has already occurred.
3344 */
3345 public @Nullable Executor getPendingExecutor() {
3346 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3347 }
3348
3349 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003350 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3351 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3352 */
3353 @Override
3354 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003355 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003356 }
3357
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003358 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 * A package was updated.
3360 *
3361 * Implementation of the method from LauncherModel.Callbacks.
3362 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003363 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003364 Runnable r = new Runnable() {
3365 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003366 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003367 }
3368 };
3369 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003370 return;
3371 }
3372
Winson Chungb745afb2015-03-02 11:51:23 -08003373 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003374 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003376 }
3377
Sunny Goyal4390ace2014-10-13 11:33:11 -07003378 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003379 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3380 Runnable r = new Runnable() {
3381 public void run() {
3382 bindPromiseAppProgressUpdated(app);
3383 }
3384 };
3385 if (waitUntilResume(r)) {
3386 return;
3387 }
3388
3389 if (mAppsView != null) {
3390 mAppsView.updatePromiseAppProgress(app);
3391 }
3392 }
3393
3394 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003395 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3396 Runnable r = new Runnable() {
3397 public void run() {
3398 bindWidgetsRestored(widgets);
3399 }
3400 };
3401 if (waitUntilResume(r)) {
3402 return;
3403 }
3404 mWorkspace.widgetsRestored(widgets);
3405 }
3406
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003408 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003409 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003410 *
3411 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003412 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003413 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003414 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003415 Runnable r = new Runnable() {
3416 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003417 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003418 }
3419 };
3420 if (waitUntilResume(r)) {
3421 return;
3422 }
3423
Sunny Goyal4390ace2014-10-13 11:33:11 -07003424 if (!updated.isEmpty()) {
3425 mWorkspace.updateShortcuts(updated);
3426 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003427 }
3428
3429 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003430 * Update the state of a package, typically related to install state.
3431 *
3432 * Implementation of the method from LauncherModel.Callbacks.
3433 */
Sunny Goyale755d462014-07-22 13:48:29 -07003434 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003435 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3436 Runnable r = new Runnable() {
3437 public void run() {
3438 bindRestoreItemsChange(updates);
3439 }
3440 };
3441 if (waitUntilResume(r)) {
3442 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003443 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003444
Sunny Goyal756adbc2015-04-16 15:20:51 -07003445 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003446 }
3447
3448 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003449 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003450 * in addition to specific applications to remove, the reason being that
3451 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003452 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003453 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003455 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003456 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003457 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003458 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003459 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003460 }
Winson Chungd64d1762013-08-20 14:37:16 -07003461 };
3462 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003463 return;
3464 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003465 mWorkspace.removeItemsByMatcher(matcher);
3466 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003467 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003468
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003469 @Override
3470 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3471 Runnable r = new Runnable() {
3472 public void run() {
3473 bindAppInfosRemoved(appInfos);
3474 }
3475 };
3476 if (waitUntilResume(r)) {
3477 return;
Joe Onorato36115782010-06-17 13:28:48 -04003478 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003479
Winson Chungdf95eb12013-10-16 14:57:07 -07003480 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003481 if (mAppsView != null) {
3482 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003483 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003484 }
Joe Onoratobe386092009-11-17 17:32:16 -08003485
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003486 @Override
3487 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3488 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3489 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003490 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003491 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003492 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003493 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003494 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003495 return;
3496 }
3497
Sunny Goyald164b7f2016-10-12 20:49:31 -07003498 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003499 Executor pendingExecutor = getPendingExecutor();
3500 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003501 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003502 return;
3503 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003504 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003505 }
Tony Wickham26b17462017-03-20 17:12:24 -07003506
3507 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3508 if (topView != null) {
3509 topView.onWidgetsBound();
3510 }
3511 }
3512
3513 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3514 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003515 }
3516
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003517 @Override
3518 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003519 if (mWorkspace.getState().shouldUpdateWidget) {
3520 refreshAndBindWidgetsForPackageUser(null);
3521 }
Tony Wickham26b17462017-03-20 17:12:24 -07003522 }
3523
Tony Wickham86222d22017-03-29 15:30:43 -07003524 /**
3525 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3526 * refreshes the widgets and shortcuts associated with the given package/user
3527 */
3528 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003529 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003530 }
3531
Winson Chung4b919f82012-05-01 10:44:08 -07003532 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003533 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003534 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003535 }
3536 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003537
Winson Chung4b919f82012-05-01 10:44:08 -07003538 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003539 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003540 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003541 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003542 } else {
3543 mHandler.postDelayed(new Runnable() {
3544 public void run() {
3545 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3546 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003547 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003548 }
Winson Chung4b919f82012-05-01 10:44:08 -07003549 }
Winson Chung400438b2011-01-16 17:53:48 -08003550 }
3551
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003552 private void markAppsViewShown() {
3553 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3554 return;
3555 }
3556 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3557 }
3558
3559 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003560 UserManagerCompat um = UserManagerCompat.getInstance(this);
3561 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003562 }
3563
Winson Chung80baf5a2010-08-09 16:03:15 -07003564 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003565 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003566 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003567 @Override
3568 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3569 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003570
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003571 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3572 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003573 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003574 writer.println(prefix + " Homescreen " + i);
3575
3576 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3577 for (int j = 0; j < layout.getChildCount(); j++) {
3578 Object tag = layout.getChildAt(j).getTag();
3579 if (tag != null) {
3580 writer.println(prefix + " " + tag.toString());
3581 }
3582 }
3583 }
3584
3585 writer.println(prefix + " Hotseat");
3586 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003587 for (int j = 0; j < layout.getChildCount(); j++) {
3588 Object tag = layout.getChildAt(j).getTag();
3589 if (tag != null) {
3590 writer.println(prefix + " " + tag.toString());
3591 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003592 }
Sunny Goyala1365452015-10-01 15:46:24 -07003593
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003594 try {
3595 FileLog.flushAll(writer);
3596 } catch (Exception e) {
3597 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003598 }
3599 }
3600
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003601 writer.println(prefix + "Misc:");
3602 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3603 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3604 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3605
3606 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003607
Adam Cohen9211d422014-10-07 18:14:53 -07003608 if (mLauncherCallbacks != null) {
3609 mLauncherCallbacks.dump(prefix, fd, writer, args);
3610 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003611 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003612
Sunny Goyal66b24572016-09-21 15:57:55 -07003613 @Override
3614 @TargetApi(Build.VERSION_CODES.N)
3615 public void onProvideKeyboardShortcuts(
3616 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3617
3618 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3619 if (mState == State.WORKSPACE) {
3620 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3621 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3622 }
3623 View currentFocus = getCurrentFocus();
3624 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3625 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3626 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3627 }
Tony18c4aa42017-05-10 21:23:57 -05003628 if (currentFocus.getTag() instanceof ItemInfo
3629 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003630 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3631 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3632 }
3633 if (!shortcutInfos.isEmpty()) {
3634 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3635 }
3636
3637 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3638 }
3639
3640 @Override
3641 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3642 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3643 switch (keyCode) {
3644 case KeyEvent.KEYCODE_A:
3645 if (mState == State.WORKSPACE) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003646 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003647 return true;
3648 }
3649 break;
3650 case KeyEvent.KEYCODE_S: {
3651 View focusedView = getCurrentFocus();
3652 if (focusedView instanceof BubbleTextView
3653 && focusedView.getTag() instanceof ItemInfo
3654 && mAccessibilityDelegate.performAction(focusedView,
3655 (ItemInfo) focusedView.getTag(),
3656 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003657 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003658 return true;
3659 }
3660 break;
3661 }
3662 case KeyEvent.KEYCODE_O:
3663 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3664 return true;
3665 }
3666 break;
3667 }
3668 }
3669 return super.onKeyShortcut(keyCode, event);
3670 }
3671
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003672 public static Launcher getLauncher(Context context) {
3673 if (context instanceof Launcher) {
3674 return (Launcher) context;
3675 }
3676 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3677 }
3678
Sunny Goyal5a81c382017-03-20 15:08:06 -07003679 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003680
3681 @Override
3682 public void onSharedPreferenceChanged(
3683 SharedPreferences sharedPreferences, String key) {
3684 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003685 // Recreate the activity so that it initializes the rotation preference again.
3686 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003687 }
3688 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003690}