blob: 9d31492dafb9a864c44028f6c4139893e9d42441 [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;
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700122import com.android.launcher3.util.ComponentKeyMapper;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700123import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700124import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700125import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800126import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700128import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700129import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700130import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700133import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700134import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700135import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800136import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800138import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700139import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700141import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700142
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700143import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700145import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700146import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700147import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700148import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800149import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700150import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152/**
153 * Default launcher application.
154 */
Sunny Goyal27835952017-01-13 12:15:53 -0800155public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700156 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
157 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700158 AccessibilityManager.AccessibilityStateChangeListener,
159 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700160 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700161 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Winson Chunga2413752012-04-03 14:22:34 -0700163 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700167
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700169 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Michael Jurka8b805b12012-04-18 14:23:14 -0700171 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700172 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
173 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700174
Jon Mirandac476d6e2017-05-04 16:30:01 -0700175 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700176
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700177 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
178
Mathew Inwood876a8462013-06-14 14:12:41 +0100179 /**
180 * IntentStarter uses request codes starting with this. This must be greater than all activity
181 * request codes used internally.
182 */
183 protected static final int REQUEST_LAST = 100;
184
Mario Bertschleracbf5702017-03-03 10:58:06 -0800185 private static final int SOFT_INPUT_MODE_DEFAULT =
Jon Mirandafd516322017-03-09 13:14:50 -0800186 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800187 private static final int SOFT_INPUT_MODE_ALL_APPS =
Jon Mirandafd516322017-03-09 13:14:50 -0800188 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800189
Winson Chung2672ff92012-05-04 16:22:30 -0700190 // The Intent extra that defines whether to ignore the launch animation
191 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400192 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 // Type: int
195 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700196 // Type: int
197 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700198 // Type: PendingRequestArgs
199 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
200 // Type: ActivityResultInfo
201 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700203 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
207 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Adam Cohen4f8071b2016-02-13 16:06:05 -0800214 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700218 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
220 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700223 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800225 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700226
227 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Sunny Goyalffe83f12014-08-14 17:39:34 -0700229 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700230 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700232 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700233
Sunny Goyal316490e2015-06-02 09:38:28 -0700234 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800235 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700236
Michael Jurka838a4ca2011-02-07 13:33:06 -0800237 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700238 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700239
Sunny Goyal47328fd2016-05-25 18:56:41 -0700240 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700241
242 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700243 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700244 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700245
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700246 // Main container view and the model for the widget tray screen.
247 @Thunk WidgetsContainerView mWidgetsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700248
Jon Miranda6bed3502017-09-19 14:43:17 -0700249 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
250 // that results in widgets being inflated in the wrong orientation.
251 private int mOrientation;
252
Winson Chung4a2afa32012-07-19 14:53:05 -0700253 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
254 // scroll issues (because the workspace may not have been measured yet) and extra work.
255 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
256 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257
258 private SpannableStringBuilder mDefaultKeySsb = null;
259
Adam Cohen091440a2015-03-18 14:16:05 -0700260 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800262 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700263 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700265 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
266 private final ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700267 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800270 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800271 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700272 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700273 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700274 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Jon Miranda2d89ea82017-05-04 11:47:53 -0700276 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700277 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700278
Tony Wickham010d2552017-01-20 08:15:28 -0800279 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700280
Winson Chung400438b2011-01-16 17:53:48 -0800281 // Determines how long to wait after a rotation before restoring the screen orientation to
282 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700283 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800284
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700285 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700286
Winson Chung46353de2012-02-16 14:05:10 -0800287 // We only want to get the SharedPreferences once since it does an FS stat each time we get
288 // it from the context.
289 private SharedPreferences mSharedPrefs;
290
Winson Chung13eb5272015-05-11 16:30:13 -0700291 // This is set to the view that launched the activity that navigated the user away from
292 // launcher. Since there is no callback for when the activity has finished launching, enable
293 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800294 private BubbleTextView mWaitingForResume;
295
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700296 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
297 // state transition. If another state transition happened during this delay,
298 // simply unregister this runnable.
299 private Runnable mExitSpringLoadedModeRunnable;
300
Sunny Goyal2100c782016-08-22 16:00:03 -0700301 // Activity result which needs to be processed after workspace has loaded.
302 private ActivityResultInfo mPendingActivityResult;
303 /**
304 * Holds extra information required to handle a result from an external call, like
305 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
306 */
307 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800308
Jon Miranda379198e2016-09-23 08:54:40 -0700309 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700310
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700311 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700312 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400313
314 @Thunk void setOrientation() {
315 if (mRotationEnabled) {
316 unlockScreenOrientation(true);
317 } else {
318 setRequestedOrientation(
319 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700320 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400321 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700322
Sunny Goyal745bad92016-05-02 10:54:12 -0700323 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700324
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 @Override
326 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700327 if (DEBUG_STRICT_MODE) {
328 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
329 .detectDiskReads()
330 .detectDiskWrites()
331 .detectNetwork() // or .detectAll() for all detectable problems
332 .penaltyLog()
333 .build());
334 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
335 .detectLeakedSqlLiteObjects()
336 .detectLeakedClosableObjects()
337 .penaltyLog()
338 .penaltyDeath()
339 .build());
340 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700341 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700342
Adam Cohen9211d422014-10-07 18:14:53 -0700343 if (mLauncherCallbacks != null) {
344 mLauncherCallbacks.preOnCreate();
345 }
346
Mario Bertschler27288382017-05-24 15:35:09 -0700347 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
348 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700349 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700352 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400353
Sunny Goyal87f784c2017-01-11 10:48:34 -0800354 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700355
Adam Cohen2e6da152015-05-06 11:42:25 -0700356 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800357 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700358 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700359 Display display = getWindowManager().getDefaultDisplay();
360 Point mwSize = new Point();
361 display.getSize(mwSize);
362 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
363 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700364
Jon Miranda6bed3502017-09-19 14:43:17 -0700365 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700366 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700367 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800368 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800369 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700371 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700372
Joe Onorato41a12d22009-10-31 18:30:00 -0400373 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700374 mAllAppsController = new AllAppsTransitionController(this);
375 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700376
Sunny Goyalffe83f12014-08-14 17:39:34 -0700377 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700378
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700379 mAppWidgetHost = new LauncherAppWidgetHost(this);
380 if (Utilities.ATLEAST_MARSHMALLOW) {
381 mAppWidgetHost.addProviderChangeListener(this);
382 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
386 // this also ensures that any synchronous binding below doesn't re-trigger another
387 // LauncherModel load.
388 mPaused = false;
389
Sunny Goyal60820d72017-05-09 12:40:11 -0700390 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 setupViews();
Winson1f064272016-07-18 17:18:02 -0700393 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394
Tony Wickham010d2552017-01-20 08:15:28 -0800395 mPopupDataProvider = new PopupDataProvider(this);
396
Tony Wickhame2217252016-03-22 16:34:23 -0700397 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
398 .addAccessibilityStateChangeListener(this);
399
Sunny Goyalfe770c92016-09-27 14:38:58 -0700400 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Sunny Goyal2100c782016-08-22 16:00:03 -0700402 // We only load the page synchronously if the user rotates (or triggers a
403 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700404 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
405 if (savedInstanceState != null) {
406 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
407 }
408 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700409 // If we are not binding synchronously, show a fade in animation when
410 // the first page bind completes.
411 mDragLayer.setAlpha(0);
412 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700413 // Pages bound synchronously.
414 mWorkspace.setCurrentPage(currentScreen);
415
Sunny Goyal2100c782016-08-22 16:00:03 -0700416 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
418
419 // For handling default keys
420 mDefaultKeySsb = new SpannableStringBuilder();
421 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800422
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800423 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700424 // In case we are on a device with locked rotation, we should look at preferences to check
425 // if the user has specifically allowed rotation.
426 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700427 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700428 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
429 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700430 }
431
Sunny Goyal5a81c382017-03-20 15:08:06 -0700432 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
433 // Temporarily enable the rotation
434 mRotationEnabled = true;
435 }
436
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700437 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
438 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400439 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700440
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800441 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700442
Jon Miranda7fda2852017-07-19 16:07:01 -0700443 // Listen for broadcasts
444 IntentFilter filter = new IntentFilter();
445 filter.addAction(Intent.ACTION_SCREEN_OFF);
446 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
447 registerReceiver(mReceiver, filter);
448 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700449
Sunny Goyal8392c822017-06-20 10:03:56 -0700450 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
451 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700452
453 if (mLauncherCallbacks != null) {
454 mLauncherCallbacks.onCreate(savedInstanceState);
455 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700456
457 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700458 }
459
Sunny Goyal7779d622015-06-11 16:18:39 -0700460 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700461 public void onThemeChanged() {
462 recreate();
463 }
464
Mario Bertschlera6936942017-05-31 14:48:19 -0700465 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700466 if (isDark) {
467 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700468 } else if (supportsDarkText) {
469 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700470 }
471 }
472
473 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700474 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800475 return mLauncherView.findViewById(id);
476 }
477
478 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700479 public void onAppWidgetHostReset() {
480 if (mAppWidgetHost != null) {
481 mAppWidgetHost.startListening();
482 }
483 }
484
Adam Cohen9211d422014-10-07 18:14:53 -0700485 private LauncherCallbacks mLauncherCallbacks;
486
487 public void onPostCreate(Bundle savedInstanceState) {
488 super.onPostCreate(savedInstanceState);
489 if (mLauncherCallbacks != null) {
490 mLauncherCallbacks.onPostCreate(savedInstanceState);
491 }
492 }
493
Winson1f064272016-07-18 17:18:02 -0700494 public void onInsetsChanged(Rect insets) {
495 mDeviceProfile.updateInsets(insets);
496 mDeviceProfile.layout(this, true /* notifyListeners */);
497 }
498
Sunny Goyal32554d12015-12-03 15:31:25 -0800499 /**
500 * Call this after onCreate to set or clear overlay.
501 */
502 public void setLauncherOverlay(LauncherOverlay overlay) {
503 if (overlay != null) {
504 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
505 }
506 mWorkspace.setLauncherOverlay(overlay);
507 }
508
Adam Cohen9211d422014-10-07 18:14:53 -0700509 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
510 mLauncherCallbacks = callbacks;
511 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700512 }
513
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700514 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700515 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onLauncherProviderChange();
518 }
519 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700520
Hyunyoung Song3f471442015-04-08 19:01:34 -0700521 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700522 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
523 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700524 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700525 }
526
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000527 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700528 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
529 // This cast is safe as long as the id < 0x00FFFFFF
530 // Since we jail all the dynamically generated views, there should be no clashes
531 // with any other views.
532 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000533 }
534
Tony Wickham010d2552017-01-20 08:15:28 -0800535 public PopupDataProvider getPopupDataProvider() {
536 return mPopupDataProvider;
537 }
538
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000539 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700540 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
541 * a configuration step, this allows the proper animations to run after other transitions.
542 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 private long completeAdd(
544 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
545 long screenId = info.screenId;
546 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700547 // When the screen id represents an actual screen (as opposed to a rank) we make sure
548 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700549 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700550 }
Adam Cohendb364c32014-05-20 14:23:40 -0700551
Sunny Goyal2100c782016-08-22 16:00:03 -0700552 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800553 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700554 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700555 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800556 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700557 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700558 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700559 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700560 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700561 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700562 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700563 int widgetId = appWidgetId;
564 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700565 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700567 // Since the view was just bound, also launch the configure activity if needed
568 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
569 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800570 if (provider != null) {
571 new WidgetAddFlowHandler(provider)
572 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700573 }
574 }
575 break;
576 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800577 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700578
Adam Cohendb364c32014-05-20 14:23:40 -0700579 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800580 }
581
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800582 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700584 if (isWorkspaceLoading()) {
585 // process the result once the workspace has loaded.
586 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
587 return;
588 }
589 mPendingActivityResult = null;
590
Winson Chunge341d302013-08-16 14:31:00 -0700591 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700592 final PendingRequestArgs requestArgs = mPendingRequestArgs;
593 setWaitingForResult(null);
594 if (requestArgs == null) {
595 return;
596 }
597
598 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700599
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 Runnable exitSpringLoaded = new Runnable() {
601 @Override
602 public void run() {
603 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
604 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
605 }
606 };
607
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700609 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700610 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700611 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
612 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700613 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700614 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700616 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700617 addAppWidgetImpl(
618 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800619 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700620 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 }
622 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200623 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
624 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700625 // User could have free-scrolled between pages before picking a wallpaper; make sure
626 // we move to the closest one now.
627 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700628 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200629 }
630 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700631 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200632
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700634 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700635
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 // We have special handling for widgets
637 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800638 final int appWidgetId;
639 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
640 : -1;
641 if (widgetId < 0) {
642 appWidgetId = pendingAddWidgetId;
643 } else {
644 appWidgetId = widgetId;
645 }
646
Adam Cohenad4e15c2013-10-17 16:21:35 -0700647 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800648 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700649 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
650 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700651 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700652 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700653 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700654 @Override
655 public void run() {
656 exitSpringLoadedDragModeDelayed(false, 0, null);
657 }
658 };
Adam Cohendb364c32014-05-20 14:23:40 -0700659
Sunny Goyal2100c782016-08-22 16:00:03 -0700660 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
661 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
662 } else {
663 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
664 // When the screen id represents an actual screen (as opposed to a rank)
665 // we make sure that the drop page actually exists.
666 requestArgs.screenId =
667 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700668 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 final CellLayout dropLayout =
670 mWorkspace.getScreenWithId(requestArgs.screenId);
671
672 dropLayout.setDropPending(true);
673 final Runnable onComplete = new Runnable() {
674 @Override
675 public void run() {
676 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
677 dropLayout.setDropPending(false);
678 }
679 };
680 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
681 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700682 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 return;
684 }
685
Sunny Goyald478c832016-04-01 12:04:16 -0700686 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
687 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700688 if (resultCode == RESULT_OK) {
689 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700691 }
692 // Leave the widget in the pending state if the user canceled the configure.
693 return;
694 }
695
Sunny Goyalaad90582015-07-09 14:22:50 -0700696 if (requestCode == REQUEST_CREATE_SHORTCUT) {
697 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
699 completeAdd(requestCode, data, -1, requestArgs);
700 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
701 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
702
Sunny Goyalaad90582015-07-09 14:22:50 -0700703 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700704 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
705 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800709 }
710
711 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700712 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800713 final int requestCode, final int resultCode, final Intent data) {
714 handleActivityResult(requestCode, resultCode, data);
715 if (mLauncherCallbacks != null) {
716 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
717 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 }
719
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700720 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700721 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600722 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 PendingRequestArgs pendingArgs = mPendingRequestArgs;
724 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
725 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
726 setWaitingForResult(null);
727
Sunny Goyal28c6b962015-10-12 11:42:05 -0700728 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700729 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700730 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700731 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700732 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700733 Intent intent = pendingArgs.getPendingIntent();
734
Sunny Goyal28c6b962015-10-12 11:42:05 -0700735 if (grantResults.length > 0
736 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700737 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700738 } else {
739 // TODO: Show a snack bar with link to settings
740 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700741 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700742 }
743 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600744 if (mLauncherCallbacks != null) {
745 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
746 grantResults);
747 }
748 }
749
Adam Cohendb364c32014-05-20 14:23:40 -0700750 /**
751 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
752 *
753 * @param screenId the screen id to check
754 * @return the new screen, or screenId if it exists
755 */
756 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800757 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700758 if (dropLayout == null) {
759 // it's possible that the add screen was removed because it was
760 // empty and a re-bind occurred
761 mWorkspace.addExtraEmptyScreen();
762 return mWorkspace.commitExtraEmptyScreen();
763 } else {
764 return screenId;
765 }
766 }
767
Sunny Goyal2100c782016-08-22 16:00:03 -0700768 @Thunk void completeTwoStageWidgetDrop(
769 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
770 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800771 Runnable onCompleteRunnable = null;
772 int animationType = 0;
773
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700774 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 if (resultCode == RESULT_OK) {
776 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
777 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800778 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700779 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 onCompleteRunnable = new Runnable() {
781 @Override
782 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
785 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800786 }
787 };
788 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800789 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800790 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700792 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700793 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700794 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
795 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700797 // The animated view may be null in the case of a rotation during widget configuration
798 onCompleteRunnable.run();
799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
801
802 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700803 protected void onStop() {
804 super.onStop();
805 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700806
807 if (mLauncherCallbacks != null) {
808 mLauncherCallbacks.onStop();
809 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700810
Sunny Goyalf5e37442016-11-02 10:31:24 -0700811 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700812 mAppWidgetHost.stopListening();
813 }
Tony Wickham010d2552017-01-20 08:15:28 -0800814
815 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700816 }
817
818 @Override
819 protected void onStart() {
820 super.onStart();
821 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700822
823 if (mLauncherCallbacks != null) {
824 mLauncherCallbacks.onStart();
825 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700826
Sunny Goyalf5e37442016-11-02 10:31:24 -0700827 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700828 mAppWidgetHost.startListening();
829 }
Tony Wickham010d2552017-01-20 08:15:28 -0800830
831 if (!isWorkspaceLoading()) {
832 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
833 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700834
Jon Miranda7fda2852017-07-19 16:07:01 -0700835 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700836 if (mScrimAnimator != null) {
837 mScrimAnimator.cancel();
838 }
839 mDragLayer.getBackground().setAlpha(0);
840 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
841 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
842 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
843 @Override
844 public void onAnimationEnd(Animator animation) {
845 mScrimAnimator = null;
846 }
847 });
848 mScrimAnimator.setDuration(600);
849 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
850 mScrimAnimator.start();
851 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700852 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700853 }
854
855 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700857 TraceHelper.beginSection("ON_RESUME");
Adam Cohen9211d422014-10-07 18:14:53 -0700858 if (mLauncherCallbacks != null) {
859 mLauncherCallbacks.preOnResume();
860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700862 TraceHelper.partitionSection("ON_RESUME", "superCall");
863
Hyunyoung Songaa953652016-04-19 18:30:24 -0700864 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700865
Winson Chung4a2afa32012-07-19 14:53:05 -0700866 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700867 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700868 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800869 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700870 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700871 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700872 // view after launching an app, as they may be depending on the UI to be static to
873 // switch to another app, otherwise, if it was
Sunny Goyal5606e072017-06-15 14:24:21 -0700874 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800875 } else if (mOnResumeState == State.WIDGETS) {
876 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700877 }
Jon Miranda7653a602017-07-28 10:09:34 -0700878 if (mOnResumeState != State.APPS) {
879 tryAndUpdatePredictedApps();
880 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700881 mOnResumeState = State.NONE;
882
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800883 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700884 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700885 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700886 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700887 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700889 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200890 // We might have postponed some bind calls until onResume (see waitUntilResume) --
891 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700892 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
893 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200894 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700895 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200896 }
Michael Jurka54554252013-08-01 12:52:23 +0200897 if (mOnResumeCallbacks.size() > 0) {
898 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
899 mOnResumeCallbacks.get(i).run();
900 }
901 mOnResumeCallbacks.clear();
902 }
Winson Chunge4e50662012-01-23 14:45:13 -0800903
904 // Reset the pressed state of icons that were locked in the press state while activities
905 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800906 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800907 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800908 mWaitingForResume.setStayPressed(false);
909 }
Winson Chung82963d52013-10-09 11:20:57 -0700910
Adam Cohen06dff352012-06-01 17:17:08 -0700911 // It is possible that widgets can receive updates while launcher is not in the foreground.
912 // Consequently, the widgets will be inflated in the orientation of the foreground activity
913 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
914 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -0700915 if (!isWorkspaceLoading()) {
916 getWorkspace().reinflateWidgetsIfNecessary();
917 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700918
Winson Chungcd99cd32015-04-29 11:03:24 -0700919 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -0700920 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -0700921
Sunny Goyala474a9b2017-05-04 16:47:11 -0700922 // Process any items that were added while Launcher was away.
923 InstallShortcutReceiver.disableAndFlushInstallQueue(
924 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700925
Sunny Goyala474a9b2017-05-04 16:47:11 -0700926 // Refresh shortcuts if the permission changed.
927 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700928
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700929 if (shouldShowDiscoveryBounce()) {
930 mAllAppsController.showDiscoveryBounce();
931 }
Adam Cohen9211d422014-10-07 18:14:53 -0700932 if (mLauncherCallbacks != null) {
933 mLauncherCallbacks.onResume();
934 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800935
Sunny Goyala502aa32017-10-02 16:04:06 -0700936 clearTypedText();
937
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700938 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700939 }
940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700942 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700943 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700944 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700945
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700946 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700947 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800948 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700949 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700950
Adam Cohen9211d422014-10-07 18:14:53 -0700951 if (mLauncherCallbacks != null) {
952 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700953 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700954 }
955
Adam Cohenc2d6e892014-10-16 09:49:24 -0700956 public interface LauncherOverlay {
957
958 /**
959 * Touch interaction leading to overscroll has begun
960 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700961 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700962
963 /**
964 * Touch interaction related to overscroll has ended
965 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700966 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700967
968 /**
969 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
970 * screen (or in the case of RTL, the rightmost screen).
971 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700972 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700973
974 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800975 * Called when the launcher is ready to use the overlay
976 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700977 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700978 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700979 }
980
981 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700982 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700983 }
984
985 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
986
Sunny Goyalc86df472016-02-25 09:19:38 -0800987 public void onScrollChanged(float progress) {
988 if (mWorkspace != null) {
989 mWorkspace.onOverlayScrollChanged(progress);
990 }
991 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700992 }
993
Jorim Jaggid017f882014-01-14 17:08:48 -0800994 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700995 if (mLauncherCallbacks != null) {
996 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700997 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800998 // On O and above we there is always some setting present settings (add icon to
999 // home screen or icon badging). On earlier APIs we will have the allow rotation
1000 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001001 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001002 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001003 }
1004
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001005 @Override
1006 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001008 if (mModel.isCurrentCallbacks(this)) {
1009 mModel.stopLoader();
1010 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001011 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1012
Romain Guy13c2e7b2010-03-10 19:45:00 -08001013 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001014 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001015
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001016 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001017 @Override
1018 public void onWindowFocusChanged(boolean hasFocus) {
1019 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001020 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001021
1022 if (mLauncherCallbacks != null) {
1023 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1024 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001025 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 private boolean acceptFilter() {
1028 final InputMethodManager inputManager = (InputMethodManager)
1029 getSystemService(Context.INPUT_METHOD_SERVICE);
1030 return !inputManager.isFullscreenMode();
1031 }
1032
1033 @Override
1034 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001035 final int uniChar = event.getUnicodeChar();
1036 final boolean handled = super.onKeyDown(keyCode, event);
1037 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1038 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1040 keyCode, event);
1041 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001042 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001043 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001044 // showSearchDialog()
1045 // If there are multiple keystrokes before the search dialog takes focus,
1046 // onSearchRequested() will be called for every keystroke,
1047 // but it is idempotent, so it's fine.
1048 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050 }
1051
Joe Onorato8a9625e2010-01-28 15:55:35 -08001052 // Eat the long press event so the keyboard doesn't come up.
1053 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1054 return true;
1055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 return handled;
1058 }
1059
Winson46163472015-09-22 17:31:56 -07001060 @Override
1061 public boolean onKeyUp(int keyCode, KeyEvent event) {
1062 if (keyCode == KeyEvent.KEYCODE_MENU) {
1063 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001064 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001065 // Close any open floating view
1066 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001067
Winson46163472015-09-22 17:31:56 -07001068 // Show the overview mode if we are on the workspace
1069 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1070 !mWorkspace.isSwitchingState()) {
1071 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001072 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001073 }
1074 }
1075 return true;
1076 }
1077 return super.onKeyUp(keyCode, event);
1078 }
1079
Karl Rosaen138a0412009-04-23 19:00:21 -07001080 private String getTypedText() {
1081 return mDefaultKeySsb.toString();
1082 }
1083
Sunny Goyal6f28e712016-09-13 14:19:29 -07001084 @Override
1085 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001086 mDefaultKeySsb.clear();
1087 mDefaultKeySsb.clearSpans();
1088 Selection.setSelection(mDefaultKeySsb, 0);
1089 }
1090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 /**
1092 * Restores the previous state, if it exists.
1093 *
1094 * @param savedState The previous state.
1095 */
1096 private void restoreState(Bundle savedState) {
1097 if (savedState == null) {
1098 return;
1099 }
1100
Sunny Goyalfe770c92016-09-27 14:38:58 -07001101 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1102 State[] stateValues = State.values();
1103 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1104 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001105 if (state == State.APPS || state == State.WIDGETS) {
1106 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001107 }
1108
Sunny Goyal2100c782016-08-22 16:00:03 -07001109 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1110 if (requestArgs != null) {
1111 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001113
1114 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
1116
1117 /**
1118 * Finds all the views we need and configure them properly.
1119 */
1120 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001121 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001122 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001123 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001124 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001125
Sunny Goyal784f9c32016-03-23 16:56:54 -07001126 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1127 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1128 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129
Winson Chung4c98d922011-05-31 16:50:48 -07001130 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001131 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001132
Winson Chung3d503fb2011-07-13 17:25:49 -07001133 // Setup the hotseat
1134 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1135 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001136 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 }
1138
Winsone9f27272015-10-13 10:47:51 -07001139 // Setup the overview panel
1140 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001141
Winson Chung4c98d922011-05-31 16:50:48 -07001142 // Setup the workspace
1143 mWorkspace.setHapticFeedbackEnabled(false);
1144 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001145 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001146 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1147 // default state, otherwise we will update to the wrong offsets in RTL
1148 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001149 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001150 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001151
Tony Wickham34d2c912015-09-11 09:27:58 -07001152 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001153 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001154
Winson Chungef7f8742015-06-04 17:18:17 -07001155 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001156 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001157 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001158
Winson Chung3d503fb2011-07-13 17:25:49 -07001159 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001160 mDragController.setMoveTarget(mWorkspace);
1161 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001162 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001163
Hyunyoung Songd725f642017-08-17 22:26:35 -07001164 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001165
Sunny Goyal322d5562015-06-25 19:35:49 -07001166 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1167 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170
Winsone9f27272015-10-13 10:47:51 -07001171 private void setupOverviewPanel() {
1172 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1173
Winsone9f27272015-10-13 10:47:51 -07001174 // Bind wallpaper button actions
1175 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301176 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001177 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001178 public void handleViewClick(View view) {
1179 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001180 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001181 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001182
1183 // Bind widget button actions
1184 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301185 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001186 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001187 public void handleViewClick(View view) {
1188 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001189 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001190 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001191
1192 // Bind settings actions
1193 View settingsButton = findViewById(R.id.settings_button);
1194 boolean hasSettings = hasSettings();
1195 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301196 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001197 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001198 public void handleViewClick(View view) {
1199 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001200 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001201 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001202 } else {
1203 settingsButton.setVisibility(View.GONE);
1204 }
1205
1206 mOverviewPanel.setAlpha(0f);
1207 }
1208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001210 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001211 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001212 */
1213 public void setAllAppsButton(View allAppsButton) {
1214 mAllAppsButton = allAppsButton;
1215 }
1216
Sunny Goyalbb011da2016-06-15 15:42:29 -07001217 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001218 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001219 }
1220
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001221 public View getWidgetsButton() {
1222 return mWidgetsButton;
1223 }
1224
Anjali Koppal5ad44842014-03-10 20:34:39 -07001225 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 * Creates a view representing a shortcut.
1227 *
1228 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001230 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001231 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 }
1233
1234 /**
1235 * Creates a view representing a shortcut inflated from the specified resource.
1236 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 * @param parent The group the shortcut belongs to.
1238 * @param info The data structure describing the shortcut.
1239 *
1240 * @return A View inflated from layoutResId.
1241 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001242 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001243 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1244 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001245 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001247 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 return favorite;
1249 }
1250
1251 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001252 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 *
1254 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001256 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001257 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001258 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1259 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001260 return;
1261 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001262
Jon Mirandac476d6e2017-05-04 16:30:01 -07001263 int[] cellXY = mTmpAddItemCellCoordinates;
1264 CellLayout layout = getCellLayout(container, screenId);
1265
Sunny Goyal782f0c92017-01-19 10:27:54 -08001266 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001267 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001268 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1269 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001270 }
1271
1272 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001273 // Legacy shortcuts are only supported for primary profile.
1274 info = Process.myUserHandle().equals(args.user)
1275 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001276
Sunny Goyalb57645f2017-03-20 13:57:28 -07001277 if (info == null) {
1278 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1279 return;
1280 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001281 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001282 // The app is trying to add a shortcut without sufficient permissions
1283 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1284 return;
1285 }
1286 }
1287
Jon Mirandac476d6e2017-05-04 16:30:01 -07001288 if (container < 0) {
1289 // Adding a shortcut to the Workspace.
1290 final View view = createShortcut(info);
1291 boolean foundCellSpan = false;
1292 // First we check if we already know the exact location where we want to add this item.
1293 if (cellX >= 0 && cellY >= 0) {
1294 cellXY[0] = cellX;
1295 cellXY[1] = cellY;
1296 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001297
Jon Mirandac476d6e2017-05-04 16:30:01 -07001298 // If appropriate, either create a folder or add to an existing folder
1299 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1300 true, null, null)) {
1301 return;
1302 }
1303 DragObject dragObject = new DragObject();
1304 dragObject.dragInfo = info;
1305 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1306 true)) {
1307 return;
1308 }
1309 } else {
1310 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1311 }
1312
1313 if (!foundCellSpan) {
1314 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001315 return;
1316 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001317
1318 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1319 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001320 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001321 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001322 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001323 if (folderIcon != null) {
1324 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1325 folderInfo.add(info, args.rank, false);
1326 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001327 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001328 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
Sunny Goyale29897f52017-07-20 10:09:42 -07001332 public FolderIcon findFolderIcon(final long folderIconId) {
1333 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1334 @Override
1335 public boolean evaluate(ItemInfo info, View view) {
1336 return info != null && info.id == folderIconId;
1337 }
1338 });
1339 }
1340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001342 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001344 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001346 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001347 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1348
Adam Cohened66b2b2012-01-23 17:28:51 -08001349 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001350 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001351 }
Romain Guycbb89e42009-06-08 15:52:54 -07001352
Adam Cohen59400422014-03-05 18:07:04 -08001353 LauncherAppWidgetInfo launcherInfo;
1354 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001355 launcherInfo.spanX = itemInfo.spanX;
1356 launcherInfo.spanY = itemInfo.spanY;
1357 launcherInfo.minSpanX = itemInfo.minSpanX;
1358 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001359 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001360
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001361 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001362 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
Sunny Goyal2100c782016-08-22 16:00:03 -07001364 if (hostView == null) {
1365 // Perform actual inflation because we're live
1366 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001368 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301369 prepareAppWidget(hostView, launcherInfo);
1370 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 }
Romain Guycbb89e42009-06-08 15:52:54 -07001372
Sunny Goyald5462aa2016-11-22 16:52:39 +05301373 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001374 hostView.setTag(item);
1375 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001376 hostView.setFocusable(true);
1377 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001378 }
1379
Adam Cohended9f8d2010-11-03 13:25:16 -07001380 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1381 @Override
1382 public void onReceive(Context context, Intent intent) {
1383 final String action = intent.getAction();
1384 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001385 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001386 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001387 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001388 if (!showWorkspace(false)) {
1389 // If we are already on the workspace, then manually reset all apps
1390 mAppsView.reset();
1391 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001392 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001393 mShouldFadeInScrim = true;
1394 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1395 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1396 // the user unlocked and the Launcher is not in the foreground.
1397 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001398 }
1399 }
1400 };
1401
Tony Wickham010d2552017-01-20 08:15:28 -08001402 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1403 Runnable r = new Runnable() {
1404 @Override
1405 public void run() {
1406 mWorkspace.updateIconBadges(updatedBadges);
1407 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001408
1409 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1410 if (popup != null) {
1411 popup.updateNotificationHeader(updatedBadges);
1412 }
Tony Wickham010d2552017-01-20 08:15:28 -08001413 }
1414 };
1415 if (!waitUntilResume(r)) {
1416 r.run();
1417 }
1418 }
1419
Adam Cohended9f8d2010-11-03 13:25:16 -07001420 @Override
1421 public void onAttachedToWindow() {
1422 super.onAttachedToWindow();
1423
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001424 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001425 if (mLauncherCallbacks != null) {
1426 mLauncherCallbacks.onAttachedToWindow();
1427 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001428 }
1429
1430 @Override
1431 public void onDetachedFromWindow() {
1432 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001433
1434 if (mLauncherCallbacks != null) {
1435 mLauncherCallbacks.onDetachedFromWindow();
1436 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001437 }
1438
Winson Chunga6945242014-01-08 14:04:34 -08001439 public DragLayer getDragLayer() {
1440 return mDragLayer;
1441 }
1442
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001443 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001444 return mAppsView;
1445 }
1446
Hyunyoung Song3f471442015-04-08 19:01:34 -07001447 public WidgetsContainerView getWidgetsView() {
1448 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001449 }
1450
Winson Chunga6945242014-01-08 14:04:34 -08001451 public Workspace getWorkspace() {
1452 return mWorkspace;
1453 }
1454
1455 public Hotseat getHotseat() {
1456 return mHotseat;
1457 }
1458
Jorim Jaggid017f882014-01-14 17:08:48 -08001459 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001460 return mOverviewPanel;
1461 }
1462
Sunny Goyal47328fd2016-05-25 18:56:41 -07001463 public DropTargetBar getDropTargetBar() {
1464 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001465 }
1466
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001467 public LauncherAppWidgetHost getAppWidgetHost() {
1468 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
Romain Guycbb89e42009-06-08 15:52:54 -07001470
Winson Chunga9abd0e2010-10-27 17:18:37 -07001471 public LauncherModel getModel() {
1472 return mModel;
1473 }
1474
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001475 public ModelWriter getModelWriter() {
1476 return mModelWriter;
1477 }
1478
Adam Cohen79d90c52016-04-22 13:29:20 -07001479 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001480 return mSharedPrefs;
1481 }
1482
Jon Miranda6bed3502017-09-19 14:43:17 -07001483 public int getOrientation() { return mOrientation; }
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 @Override
1486 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001487 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 super.onNewIntent(intent);
1489
Winson15f8b172015-08-19 11:02:39 -07001490 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1491 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1492 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001493
1494 // Check this condition before handling isActionMain, as this will get reset.
1495 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001496 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001497
Winson15f8b172015-08-19 11:02:39 -07001498 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1499 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001500 if (mWorkspace == null) {
1501 // Can be cases where mWorkspace is null, this prevents a NPE
1502 return;
1503 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001504
1505 // Note: There should be at most one log per method call. This is enforced implicitly
1506 // by using if-else statements.
1507 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001508 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001509 if (topOpenView != null) {
1510 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001511 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301512 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001513 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1514 }
1515
1516 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001517 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001518 exitSpringLoadedDragMode();
1519
1520 // If we are already on home, then just animate back to the workspace,
1521 // otherwise, just wait until onResume to set the state back to Workspace
1522 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001523 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001524 } else {
1525 mOnResumeState = State.WORKSPACE;
1526 }
1527
1528 final View v = getWindow().peekDecorView();
1529 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001530 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001531 }
1532
Winson Chungb745afb2015-03-02 11:51:23 -08001533 // Reset the apps view
1534 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001535 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001536 }
1537
Hyunyoung Song3f471442015-04-08 19:01:34 -07001538 // Reset the widgets view
1539 if (!alreadyOnHome && mWidgetsView != null) {
1540 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001541 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001542
Adam Cohen9211d422014-10-07 18:14:53 -07001543 if (mLauncherCallbacks != null) {
1544 mLauncherCallbacks.onHomeIntent();
1545 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001547 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001548
Adam Cohen9211d422014-10-07 18:14:53 -07001549 if (mLauncherCallbacks != null) {
1550 mLauncherCallbacks.onNewIntent(intent);
1551 }
Winson15f8b172015-08-19 11:02:39 -07001552
1553 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1554 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1555 // animation.
1556 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001557 boolean callbackAllowsMoveToDefaultScreen =
1558 mLauncherCallbacks == null || mLauncherCallbacks
1559 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001560 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1561 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001562
Winson15f8b172015-08-19 11:02:39 -07001563 mWorkspace.post(new Runnable() {
1564 @Override
1565 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001566 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001567 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001568 }
Winson15f8b172015-08-19 11:02:39 -07001569 }
1570 });
1571 }
1572 }
1573
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001574 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001575 }
1576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001578 public void onRestoreInstanceState(Bundle state) {
1579 super.onRestoreInstanceState(state);
1580 for (int page: mSynchronouslyBoundPages) {
1581 mWorkspace.restoreInstanceStateForChild(page);
1582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584
1585 @Override
1586 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001587 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001588 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001589
Adam Cohen21cd0022013-10-09 18:57:02 -07001590 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001591 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592
Michael Jurka883f55b2010-10-21 15:47:14 -07001593 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001594 // We close any open folders and shortcut containers since they will not be re-opened,
1595 // and we need to make sure this state is reflected.
1596 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001597
Sunny Goyal2100c782016-08-22 16:00:03 -07001598 if (mPendingRequestArgs != null) {
1599 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1600 }
1601 if (mPendingActivityResult != null) {
1602 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 }
1604
Adam Cohen9211d422014-10-07 18:14:53 -07001605 if (mLauncherCallbacks != null) {
1606 mLauncherCallbacks.onSaveInstanceState(outState);
1607 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
1609
1610 @Override
1611 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001613
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001614 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001615 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001616
Winson Chungcd2b0142011-06-08 16:02:26 -07001617 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001618 // It's possible to receive onDestroy after a new Launcher activity has
1619 // been created. In this case, don't interfere with the new Launcher.
1620 if (mModel.isCurrentCallbacks(this)) {
1621 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001622 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001623 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001624
Sunny Goyal745bad92016-05-02 10:54:12 -07001625 if (mRotationPrefChangeHandler != null) {
1626 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1627 }
1628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001630 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001632 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001634 mAppWidgetHost = null;
1635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 TextKeyListener.getInstance().release();
1637
Tony Wickhame2217252016-03-22 16:34:23 -07001638 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1639 .removeAccessibilityStateChangeListener(this);
1640
Mario Bertschler27288382017-05-24 15:35:09 -07001641 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1642
Michael Jurka2ecf9952012-06-18 12:52:28 -07001643 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001644
Tony2917a8b2017-07-31 23:29:42 -07001645 clearPendingBinds();
1646
Adam Cohen9211d422014-10-07 18:14:53 -07001647 if (mLauncherCallbacks != null) {
1648 mLauncherCallbacks.onDestroy();
1649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
1651
Sunny Goyalae502842016-06-17 08:43:56 -07001652 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1653 return mAccessibilityDelegate;
1654 }
1655
Adam Cohena9cf38f2011-05-02 15:36:58 -07001656 public DragController getDragController() {
1657 return mDragController;
1658 }
1659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001661 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001662 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001663 }
1664
1665 @Override
1666 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1667 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001668 try {
1669 super.startIntentSenderForResult(intent, requestCode,
1670 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1671 } catch (IntentSender.SendIntentException e) {
1672 throw new ActivityNotFoundException();
1673 }
1674 }
1675
Winson Chung70d72102011-08-12 11:24:35 -07001676 /**
1677 * Indicates that we want global search for this activity by setting the globalSearch
1678 * argument for {@link #startSearch} to true.
1679 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001681 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001683
Karl Rosaen138a0412009-04-23 19:00:21 -07001684 if (initialQuery == null) {
1685 // Use any text typed in the launcher as the initial query
1686 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 if (appSearchData == null) {
1689 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001690 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001692
Sunny Goyal6f28e712016-09-13 14:19:29 -07001693 if (mLauncherCallbacks == null ||
1694 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1695 // Starting search from the callbacks failed. Start the default global search.
1696 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001697 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001698
1699 // We need to show the workspace after starting the search
1700 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001701 }
1702
Ian Parkinson047036e2014-09-01 15:40:53 +01001703 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001704 * Starts the global search activity. This code is a copied from SearchManager
1705 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001706 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001707 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001708 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001709 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1710 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1711 if (globalSearchActivity == null) {
1712 Log.w(TAG, "No global search activity found.");
1713 return;
1714 }
1715 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1716 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1717 intent.setComponent(globalSearchActivity);
1718 // Make sure that we have a Bundle to put source in
1719 if (appSearchData == null) {
1720 appSearchData = new Bundle();
1721 } else {
1722 appSearchData = new Bundle(appSearchData);
1723 }
Adam Cohen9211d422014-10-07 18:14:53 -07001724 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001725 if (!appSearchData.containsKey("source")) {
1726 appSearchData.putString("source", getPackageName());
1727 }
1728 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1729 if (!TextUtils.isEmpty(initialQuery)) {
1730 intent.putExtra(SearchManager.QUERY, initialQuery);
1731 }
1732 if (selectInitialQuery) {
1733 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1734 }
1735 intent.setSourceBounds(sourceBounds);
1736 try {
1737 startActivity(intent);
1738 } catch (ActivityNotFoundException ex) {
1739 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 }
1742
Winson Chung70d72102011-08-12 11:24:35 -07001743 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001744 public boolean onPrepareOptionsMenu(Menu menu) {
1745 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001746 if (mLauncherCallbacks != null) {
1747 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1748 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001749 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001752 @Override
1753 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001754 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001755 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001756 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001757 }
1758
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001760 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761 }
1762
Adam Cohen517a7f52014-03-01 12:12:59 -08001763 public boolean isWorkspaceLoading() {
1764 return mWorkspaceLoading;
1765 }
1766
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001767 private void setWorkspaceLoading(boolean value) {
1768 boolean isLocked = isWorkspaceLocked();
1769 mWorkspaceLoading = value;
1770 if (isLocked != isWorkspaceLocked()) {
1771 onWorkspaceLockedChanged();
1772 }
1773 }
1774
Sunny Goyal04a324a2017-01-20 21:08:59 -08001775 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001776 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001777 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001778 if (isLocked != isWorkspaceLocked()) {
1779 onWorkspaceLockedChanged();
1780 }
1781 }
1782
Adam Cohen9211d422014-10-07 18:14:53 -07001783 protected void onWorkspaceLockedChanged() {
1784 if (mLauncherCallbacks != null) {
1785 mLauncherCallbacks.onWorkspaceLockedChanged();
1786 }
1787 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001788
Sunny Goyal2100c782016-08-22 16:00:03 -07001789 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001790 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001791 if (LOGD) {
1792 Log.d(TAG, "Adding widget from drop");
1793 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001794 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001795 }
1796
Sunny Goyal2100c782016-08-22 16:00:03 -07001797 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001798 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1799 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1800 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001801
Adam Cohenad4e15c2013-10-17 16:21:35 -07001802 Runnable onComplete = new Runnable() {
1803 @Override
1804 public void run() {
1805 // Exit spring loaded mode if necessary after adding the widget
1806 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1807 null);
1808 }
1809 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001810 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001811 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813 }
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001815 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1816 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001817 info.container = container;
1818 info.screenId = screenId;
1819 if (cell != null) {
1820 info.cellX = cell[0];
1821 info.cellY = cell[1];
1822 }
1823 info.spanX = spanX;
1824 info.spanY = spanY;
1825
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001826 switch (info.itemType) {
1827 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1828 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001829 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001830 break;
1831 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001832 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001833 break;
1834 default:
1835 throw new IllegalStateException("Unknown item type: " + info.itemType);
1836 }
1837 }
1838
Adam Cohenfbba09b2011-07-18 21:43:05 -07001839 /**
1840 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001841 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001842 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001843 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1844 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001845 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1846 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1847 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001848 }
1849
Adam Cohenfbba09b2011-07-18 21:43:05 -07001850 /**
1851 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001852 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001853 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001854 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001855 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001856 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001857 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001858 // In the case where we've prebound the widget, we remove it from the DragLayer
1859 if (LOGD) {
1860 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1861 }
1862 getDragLayer().removeView(hostView);
1863
Adam Cohened66b2b2012-01-23 17:28:51 -08001864 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001865 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001866
1867 // Clear the boundWidget so that it doesn't get destroyed.
1868 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001869 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001870 // In this case, we either need to start an activity to get permission to bind
1871 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001872 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1873 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1874 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1875 this, info.componentName);
1876 } else {
1877 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1878 }
Adam Cohendd70d662012-10-04 16:53:44 -07001879 Bundle options = info.bindOptions;
1880
Sunny Goyalffe83f12014-08-14 17:39:34 -07001881 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1882 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001883 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001884 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001885 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001886 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001887 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001888 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001889 }
1890
Adam Cohendcd297f2013-06-18 13:13:40 -07001891 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001892 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001893 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 folderInfo.title = getText(R.string.folder_name);
1895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001897 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898
1899 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001900 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301901 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001902 // Force measure the new folder icon
1903 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1904 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001905 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
Romain Guycbb89e42009-06-08 15:52:54 -07001907
Winsonc0b52fe2015-09-09 16:38:15 -07001908 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001909 * Unbinds the view for the specified item, and removes the item and all its children.
1910 *
1911 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001912 * @param itemInfo the {@link ItemInfo} for this view.
1913 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001914 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001915 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001916 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001917 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001918 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1919 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001920 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001921 } else {
1922 mWorkspace.removeWorkspaceItem(v);
1923 }
Winsonc0b52fe2015-09-09 16:38:15 -07001924 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001925 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001926 }
1927 } else if (itemInfo instanceof FolderInfo) {
1928 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001929 if (v instanceof FolderIcon) {
1930 ((FolderIcon) v).removeListeners();
1931 }
Winsonc0b52fe2015-09-09 16:38:15 -07001932 mWorkspace.removeWorkspaceItem(v);
1933 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001934 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001935 }
1936 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1937 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001938 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001939 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001940 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001941 }
1942 } else {
1943 return false;
1944 }
1945 return true;
1946 }
1947
1948 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001949 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001950 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001951 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001952 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001953 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001954 // Deleting an app widget ID is a void call but writes to disk before returning
1955 // to the caller...
1956 new AsyncTask<Void, Void, Void>() {
1957 public Void doInBackground(Void ... args) {
1958 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1959 return null;
1960 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001961 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001962 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001963 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
1967 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001968 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
Joe Onorato88ec0992009-11-19 13:16:06 -08001971 @Override
1972 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001973 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1974 return;
1975 }
1976
Sunny Goyal45478022015-06-08 16:52:41 -07001977 if (mDragController.isDragging()) {
1978 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001979 return;
1980 }
1981
Jon Mirandafeba90f2016-10-06 10:53:29 -07001982 // Note: There should be at most one log per method call. This is enforced implicitly
1983 // by using if-else statements.
1984 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001985 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1986 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001987 topView.onBackPressed();
Tony Wickham49c8d292016-07-01 11:44:34 -07001988 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301989 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001990 showWorkspace(true);
1991 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301992 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08001993 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001994 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301995 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001996 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001997 } else {
1998 // Back button is a no-op here, but give at least some feedback for the button press
1999 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002000 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002001 }
2002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 * Launches the intent referred by the clicked shortcut.
2005 *
2006 * @param v The view representing the clicked shortcut.
2007 */
2008 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002009 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2010 // view has detached (it's possible for this to happen if the view is removed mid touch).
2011 if (v.getWindowToken() == null) {
2012 return;
2013 }
2014
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002015 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002016 return;
2017 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002018
Adam Cohen1697b792013-09-17 19:08:21 -07002019 if (v instanceof Workspace) {
2020 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08002021 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2022 LauncherLogProto.Action.Direction.NONE,
2023 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002024 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002025 }
2026 return;
2027 }
2028
2029 if (v instanceof CellLayout) {
2030 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002031 int page = mWorkspace.indexOfChild(v);
2032 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2033 LauncherLogProto.Action.Direction.NONE,
2034 LauncherLogProto.ContainerType.OVERVIEW, page);
2035 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002036 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002037 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002038 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002039 }
2040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002042 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002043 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002045 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002046 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002047 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07002048 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002049 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002050 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002051 } else if (tag instanceof AppInfo) {
2052 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002053 } else if (tag instanceof LauncherAppWidgetInfo) {
2054 if (v instanceof PendingAppWidgetHostView) {
2055 onClickPendingWidget((PendingAppWidgetHostView) v);
2056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058 }
2059
Sunny Goyal70660032015-05-14 00:07:08 -07002060 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002061 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002062 return false;
2063 }
2064
Michael Jurkaaf442092010-06-10 17:01:57 -07002065 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002066 * Event handler for the app widget view which has not fully restored.
2067 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002068 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002069 if (mIsSafeModeEnabled) {
2070 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2071 return;
2072 }
2073
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002074 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002075 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002076 LauncherAppWidgetProviderInfo appWidgetInfo =
2077 mAppWidgetManager.findProvider(info.providerName, info.user);
2078 if (appWidgetInfo == null) {
2079 return;
2080 }
2081 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2082
Sunny Goyald478c832016-04-01 12:04:16 -07002083 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2084 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2085 // This should not happen, as we make sure that an Id is allocated during bind.
2086 return;
2087 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002088 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2089 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002090 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002091 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002092 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002093 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002094 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002095 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002096 }
2097 }
2098
2099 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002100 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2101 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2102 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002103 *
2104 * @param v The view that was clicked.
2105 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002106 protected void onClickAllAppsButton(View v) {
2107 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002108 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302109 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2110 ControlType.ALL_APPS_BUTTON);
Sunny Goyal5606e072017-06-15 14:24:21 -07002111 showAppsView(true /* animated */, true /* updatePredictedApps */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002112 } else {
2113 showWorkspace(true);
2114 }
2115 }
2116
Sunny Goyale6e72002017-01-12 16:55:36 -08002117 private void onClickPendingAppItem(final View v, final String packageName,
2118 boolean downloadStarted) {
2119 if (downloadStarted) {
2120 // If the download has started, simply direct to the market app.
2121 startMarketIntentForPackage(v, packageName);
2122 return;
2123 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002124 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002125 .setTitle(R.string.abandoned_promises_title)
2126 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002127 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2128 @Override
2129 public void onClick(DialogInterface dialogInterface, int i) {
2130 startMarketIntentForPackage(v, packageName);
2131 }
2132 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002133 .setNeutralButton(R.string.abandoned_clean_this,
2134 new DialogInterface.OnClickListener() {
2135 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002136 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002137 mWorkspace.removeAbandonedPromise(packageName, user);
2138 }
2139 })
2140 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002141 }
2142
2143 private void startMarketIntentForPackage(View v, String packageName) {
2144 ItemInfo item = (ItemInfo) v.getTag();
2145 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2146 boolean success = startActivitySafely(v, intent, item);
2147 if (success && v instanceof BubbleTextView) {
2148 mWaitingForResume = (BubbleTextView) v;
2149 mWaitingForResume.setStayPressed(true);
2150 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002151 }
2152
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002153 /**
2154 * Event handler for an app shortcut click.
2155 *
2156 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2157 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002158 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002159 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2160 Object tag = v.getTag();
2161 if (!(tag instanceof ShortcutInfo)) {
2162 throw new IllegalArgumentException("Input must be a Shortcut");
2163 }
2164
2165 // Open shortcut
2166 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002167
2168 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002169 if ((shortcut.isDisabled &
2170 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2171 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2172 // If the app is only disabled because of the above flags, launch activity anyway.
2173 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002174 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002175 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2176 // Use a message specific to this shortcut, if it has one.
2177 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2178 return;
2179 }
2180 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002181 int error = R.string.activity_not_available;
2182 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2183 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002184 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2185 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002186 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002187 }
2188 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2189 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002190 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002191 }
2192
Chris Wren40c5ed32014-06-24 18:24:23 -04002193 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002194 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002195 String packageName = shortcut.intent.getComponent() != null ?
2196 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2197 if (!TextUtils.isEmpty(packageName)) {
2198 onClickPendingAppItem(v, packageName,
2199 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2200 return;
2201 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002202 }
2203
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002204 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002205 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002206 }
2207
Sunny Goyala7ce1662016-05-31 15:01:35 -07002208 private void startAppShortcutOrInfoActivity(View v) {
2209 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002210 Intent intent;
2211 if (item instanceof PromiseAppInfo) {
2212 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2213 intent = promiseAppInfo.getMarketIntent();
2214 } else {
2215 intent = item.getIntent();
2216 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002217 if (intent == null) {
2218 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002219 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002220 boolean success = startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002221 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002222
2223 if (success && v instanceof BubbleTextView) {
2224 mWaitingForResume = (BubbleTextView) v;
2225 mWaitingForResume.setStayPressed(true);
2226 }
2227 }
2228
2229 /**
2230 * Event handler for a folder icon click.
2231 *
2232 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2233 */
2234 protected void onClickFolderIcon(View v) {
2235 if (LOGD) Log.d(TAG, "onClickFolder");
2236 if (!(v instanceof FolderIcon)){
2237 throw new IllegalArgumentException("Input must be a FolderIcon");
2238 }
2239
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002240 Folder folder = ((FolderIcon) v).getFolder();
2241 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002242 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002243 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002244 }
Michael Jurka5130e402011-10-13 04:55:35 -07002245 }
2246
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002247 /**
2248 * Event handler for the (Add) Widgets button that appears after a long press
2249 * on the home screen.
2250 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002251 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002252 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002253 if (mIsSafeModeEnabled) {
2254 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2255 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002256 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002257 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002258 }
2259
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002260 /**
2261 * Event handler for the wallpaper picker button that appears after a long press
2262 * on the home screen.
2263 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002264 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002265 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002266 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2267 return;
2268 }
2269
Tony Wickham785f7a52015-08-31 17:28:32 -07002270 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2271 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002272 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002273 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002274 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002275
2276 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002277 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2278 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002279 intent.setPackage(pickerPackage);
2280 }
2281
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002282 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002283 try {
2284 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2285 // If there is no target package, use the default intent chooser animation
2286 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2287 } catch (ActivityNotFoundException e) {
2288 setWaitingForResult(null);
2289 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2290 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002291 }
2292
2293 /**
2294 * Event handler for a click on the settings button that appears after a long press
2295 * on the home screen.
2296 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002297 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002298 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002299 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2300 .setPackage(getPackageName());
2301 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002302 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002303 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002304 }
2305
Tony Wickhame2217252016-03-22 16:34:23 -07002306 @Override
2307 public void onAccessibilityStateChanged(boolean enabled) {
2308 mDragLayer.onAccessibilityStateChanged(enabled);
2309 }
2310
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002311 /**
2312 * Called when the user stops interacting with the launcher.
2313 * This implies that the user is now on the homescreen and is not doing housekeeping.
2314 */
Adam Cohen9211d422014-10-07 18:14:53 -07002315 protected void onInteractionEnd() {
2316 if (mLauncherCallbacks != null) {
2317 mLauncherCallbacks.onInteractionEnd();
2318 }
2319 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002320
2321 /**
2322 * Called when the user starts interacting with the launcher.
2323 * The possible interactions are:
2324 * - open all apps
2325 * - reorder an app shortcut, or a widget
2326 * - open the overview mode.
2327 * This is a good time to stop doing things that only make sense
2328 * when the user is on the homescreen and not doing housekeeping.
2329 */
Adam Cohen9211d422014-10-07 18:14:53 -07002330 protected void onInteractionBegin() {
2331 if (mLauncherCallbacks != null) {
2332 mLauncherCallbacks.onInteractionBegin();
2333 }
2334 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002335
Winson Chungcd99cd32015-04-29 11:03:24 -07002336 /** Updates the interaction state. */
2337 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002338 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002339 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002340 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2341 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002342 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002343 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002344 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002345 onInteractionEnd();
2346 }
2347 }
2348
Sunny Goyala7ce1662016-05-31 15:01:35 -07002349 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002351 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2352 try {
2353 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2354 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2355 // is enabled by default on NYC.
2356 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2357 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002358
2359 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2360 String id = ((ShortcutInfo) info).getDeepShortcutId();
2361 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302362 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002363 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002364 } else {
2365 // Could be launching some bookkeeping activity
2366 startActivity(intent, optsBundle);
2367 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002368 } finally {
2369 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002372 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2373 // corresponding permission. Show the appropriate permission prompt if that
2374 // is the case.
2375 if (intent.getComponent() == null
2376 && Intent.ACTION_CALL.equals(intent.getAction())
2377 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2378 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002379
2380 setWaitingForResult(PendingRequestArgs
2381 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002382 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2383 REQUEST_PERMISSION_CALL_PHONE);
2384 } else {
2385 // No idea why this was thrown.
2386 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 }
2389 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002390
Sunny Goyalfe770c92016-09-27 14:38:58 -07002391 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002392 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002393 if (Utilities.ATLEAST_MARSHMALLOW) {
2394 int left = 0, top = 0;
2395 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002396 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002397 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002398 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002399 if (icon != null) {
2400 Rect bounds = icon.getBounds();
2401 left = (width - bounds.width()) / 2;
2402 top = v.getPaddingTop();
2403 width = bounds.width();
2404 height = bounds.height();
2405 }
2406 }
2407 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2408 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2409 // On L devices, we use the device default slide-up transition.
2410 // On L MR1 devices, we use a custom version of the slide-up transition which
2411 // doesn't have the delay present in the device default.
2412 return ActivityOptions.makeCustomAnimation(
2413 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2414 }
2415 return null;
2416 }
2417
Tonye3c59252017-05-02 21:32:27 -07002418 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002419 int[] pos = new int[2];
2420 v.getLocationOnScreen(pos);
2421 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2422 }
2423
Sunny Goyala7ce1662016-05-31 15:01:35 -07002424 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002425 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2426 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2427 return false;
2428 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002429 // Only launch using the new animation if the shortcut has not opted out (this is a
2430 // private contract between launcher and may be ignored in the future).
2431 boolean useLaunchAnimation = (v != null) &&
2432 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2433 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2434
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002435 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002436
2437 // Prepare intent
2438 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2439 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002440 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002441 }
2442 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002443 if (Utilities.ATLEAST_MARSHMALLOW
2444 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002445 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002446 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002447 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002448 // Shortcuts need some special checks due to legacy reasons.
2449 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002450 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002451 // Could be launching some bookkeeping activity
2452 startActivity(intent, optsBundle);
2453 } else {
2454 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2455 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2456 }
2457 return true;
2458 } catch (ActivityNotFoundException|SecurityException e) {
2459 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2460 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2461 }
2462 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002463 }
2464
Tony Wickhamdadb3042016-02-24 11:07:00 -08002465 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002466 public boolean dispatchTouchEvent(MotionEvent ev) {
2467 mLastDispatchTouchEventX = ev.getX();
2468 return super.dispatchTouchEvent(ev);
2469 }
2470
2471 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002473 if (!isDraggingEnabled()) return false;
2474 if (isWorkspaceLocked()) return false;
2475 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476
Jon Miranda51f037d2016-11-07 08:37:20 -08002477 boolean ignoreLongPressToOverview =
2478 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002479
Adam Cohen1697b792013-09-17 19:08:21 -07002480 if (v instanceof Workspace) {
2481 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002482 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302483 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2484 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002485 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002486 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002487 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2488 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2489 return true;
2490 } else {
2491 return false;
2492 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002493 } else {
2494 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002495 }
Adam Cohen1697b792013-09-17 19:08:21 -07002496 }
2497
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002498 CellLayout.CellInfo longClickCellInfo = null;
2499 View itemUnderLongClick = null;
2500 if (v.getTag() instanceof ItemInfo) {
2501 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002502 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002503 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002504 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 }
2506
Winson Chung3d503fb2011-07-13 17:25:49 -07002507 // The hotseat touch handling does not go through Workspace, and we always allow long press
2508 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002509 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002510 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002511 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002512 if (mWorkspace.isInOverviewMode()) {
2513 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302514 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2515 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002516 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002517 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002518 return false;
2519 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302520 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2521 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002522 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002523 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002524 }
Jon Miranda379198e2016-09-23 08:54:40 -07002525 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2526 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002528 final boolean isAllAppsButton =
2529 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2530 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2531 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002532 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002534 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 }
2536 }
2537 }
2538 return true;
2539 }
2540
Winson Chung3d503fb2011-07-13 17:25:49 -07002541 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002542 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002543 return mHotseat != null && layout != null &&
2544 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2545 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002546
Winson Chung3d503fb2011-07-13 17:25:49 -07002547 /**
2548 * Returns the CellLayout of the specified container at the specified screen.
2549 */
Sunny Goyal92820592015-03-02 11:31:35 -08002550 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002551 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2552 if (mHotseat != null) {
2553 return mHotseat.getLayout();
2554 } else {
2555 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002556 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002557 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002558 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002559 }
2560 }
2561
Winson Chungb745afb2015-03-02 11:51:23 -08002562 /**
2563 * For overridden classes.
2564 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002565 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002566 return isAppsViewVisible();
2567 }
2568
2569 public boolean isAppsViewVisible() {
2570 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2571 }
2572
2573 public boolean isWidgetsViewVisible() {
2574 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002575 }
2576
Michael Jurkae326f182011-11-21 14:05:46 -08002577 @Override
2578 public void onTrimMemory(int level) {
2579 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002580 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2581 // The widget preview db can result in holding onto over
2582 // 3MB of memory for caching which isn't necessary.
2583 SQLiteDatabase.releaseMemory();
2584
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002585 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002586 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002587 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002588 if (mLauncherCallbacks != null) {
2589 mLauncherCallbacks.onTrimMemory(level);
2590 }
Michael Jurkae326f182011-11-21 14:05:46 -08002591 }
2592
Winson5c6bdbb2015-09-03 11:36:19 -07002593 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002594 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002595 }
2596
Winson5c6bdbb2015-09-03 11:36:19 -07002597 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002598 boolean changed = mState != State.WORKSPACE ||
2599 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002600 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002601 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002602 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002603 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002604
Michael Jurkab3e22d92011-10-31 15:58:33 -07002605 // Set focus to the AppsCustomize button
2606 if (mAllAppsButton != null) {
2607 mAllAppsButton.requestFocus();
2608 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002609 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002610
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002611 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002612 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002613
Winson Chung0f785722015-04-08 10:27:49 -07002614 if (changed) {
2615 // Send an accessibility event to announce the context change
2616 getWindow().getDecorView()
2617 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002618 }
Winson5c6bdbb2015-09-03 11:36:19 -07002619 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002620 }
2621
Winsone9f27272015-10-13 10:47:51 -07002622 /**
2623 * Shows the overview button.
2624 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002625 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002626 showOverviewMode(animated, false);
2627 }
2628
2629 /**
2630 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2631 * onto one of the overview panel buttons.
2632 */
2633 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2634 Runnable postAnimRunnable = null;
2635 if (requestButtonFocus) {
2636 postAnimRunnable = new Runnable() {
2637 @Override
2638 public void run() {
2639 // Hitting the menu button when in touch mode does not trigger touch mode to
2640 // be disabled, so if requested, force focus on one of the overview panel
2641 // buttons.
2642 mOverviewPanel.requestFocusFromTouch();
2643 }
2644 };
2645 }
Adam Cohened307df2013-10-02 09:37:31 -07002646 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002647 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002648 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002649 setState(State.WORKSPACE);
2650
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002651 // If animated from long press, then don't allow any of the controller in the drag
2652 // layer to intercept any remaining touch.
2653 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002654 }
2655
Mario Bertschleracbf5702017-03-03 10:58:06 -08002656 private void setState(State state) {
2657 this.mState = state;
2658 updateSoftInputMode();
2659 }
2660
2661 private void updateSoftInputMode() {
2662 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2663 final int mode;
2664 if (isAppsViewVisible()) {
2665 mode = SOFT_INPUT_MODE_ALL_APPS;
2666 } else {
2667 mode = SOFT_INPUT_MODE_DEFAULT;
2668 }
2669 getWindow().setSoftInputMode(mode);
2670 }
2671 }
2672
Winson Chungb745afb2015-03-02 11:51:23 -08002673 /**
2674 * Shows the apps view.
2675 */
Sunny Goyal5606e072017-06-15 14:24:21 -07002676 public void showAppsView(boolean animated, boolean updatePredictedApps) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002677 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002678 if (updatePredictedApps) {
2679 tryAndUpdatePredictedApps();
2680 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002681 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002682 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002683
Winson Chungb745afb2015-03-02 11:51:23 -08002684 /**
2685 * Shows the widgets view.
2686 */
2687 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002688 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002689 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002690 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002691 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002692 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002693
2694 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002695 @Override
2696 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002697 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002698 }
2699 });
Winson Chungb745afb2015-03-02 11:51:23 -08002700 }
2701
2702 /**
2703 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002704 *
2705 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002706 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002707 // TODO: calling method should use the return value so that when {@code false} is returned
2708 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002709 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002710 if (!(mState == State.WORKSPACE ||
2711 mState == State.APPS_SPRING_LOADED ||
2712 mState == State.WIDGETS_SPRING_LOADED ||
2713 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002714 return false;
2715 }
2716 if (toState != State.APPS && toState != State.WIDGETS) {
2717 return false;
2718 }
Winson Chungb745afb2015-03-02 11:51:23 -08002719
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002720 // This is a safe and supported transition to bypass spring_loaded mode.
2721 if (mExitSpringLoadedModeRunnable != null) {
2722 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2723 mExitSpringLoadedModeRunnable = null;
2724 }
2725
Winson Chungb745afb2015-03-02 11:51:23 -08002726 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002727 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002728 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002729 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002730 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002731
Michael Jurkab3e22d92011-10-31 15:58:33 -07002732 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002733 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002734 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002735
2736 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002737 getWindow().getDecorView()
2738 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002739 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002740 }
2741
Winson Chungcd99cd32015-04-29 11:03:24 -07002742 /**
2743 * Updates the workspace and interaction state on state change, and return the animation to this
2744 * new state.
2745 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002746 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002747 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002748 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002749 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002750 updateInteraction(fromState, toState);
2751 return anim;
2752 }
2753
Hyunyoung Song3f471442015-04-08 19:01:34 -07002754 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002755 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002756 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002757 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002758 }
Winson Chungb745afb2015-03-02 11:51:23 -08002759
Winson Chung006ee262015-08-03 14:40:11 -07002760 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002761 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002762 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002763 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002764 }
Adam Cohen7777d962011-08-18 18:58:38 -07002765
Hyunyoung Song3f471442015-04-08 19:01:34 -07002766 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002767 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002768 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002769
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002770 if (mExitSpringLoadedModeRunnable != null) {
2771 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2772 }
2773 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002774 @Override
2775 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002776 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002777 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2778 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002779 // Before we show workspace, hide all apps again because
2780 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2781 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002782 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002783 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002784 } else {
2785 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002786 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002787 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002788 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002789 };
2790 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002791 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002792
Tony Wickhame0c33232016-02-08 11:37:04 -08002793 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002794 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2795 || mState == State.WIDGETS_SPRING_LOADED;
2796 }
2797
Sunny Goyal0f76b562016-12-13 19:37:10 -08002798 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002799 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002800 showAppsView(true /* animated */, false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08002801 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002802 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002803 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2804 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002805 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002806 }
2807
Winson Chung4ac30062015-05-08 17:34:17 -07002808 /**
2809 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
2810 * resumed.
2811 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07002812 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07002813 if (mLauncherCallbacks != null) {
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002814 List<ComponentKeyMapper<AppInfo>> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07002815 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07002816 mAppsView.setPredictedApps(apps);
2817 }
2818 }
2819 }
2820
Michael Jurkad7c28052012-04-27 15:43:36 -07002821 @Override
2822 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002823 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002824 final List<CharSequence> text = event.getText();
2825 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002826 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002827 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002828 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002829 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002830 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002831 } else if (mWorkspace != null) {
2832 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002833 } else {
2834 text.add(getString(R.string.all_apps_home_button_label));
2835 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002836 return result;
2837 }
2838
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002839 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002840 * If the activity is currently paused, signal that we need to run the passed Runnable
2841 * in onResume.
2842 *
2843 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002844 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2845 * wrong when we're not running, and if the activity comes back to what the configuration was
2846 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002847 *
2848 * Implementation of the method from LauncherModel.Callbacks.
2849 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002850 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002851 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002852 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002853 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002854 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002855 if (run instanceof RunnableWithId) {
2856 // Remove any runnables which have the same id
2857 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002858 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002859 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002860 return true;
2861 } else {
2862 return false;
2863 }
2864 }
2865
Michael Jurka1e2f4652013-07-08 18:03:46 -07002866 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02002867 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002868 }
2869
Michael Jurka7607c2f2013-04-03 14:33:19 -07002870 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002871 * If the activity is currently paused, signal that we need to re-run the loader
2872 * in onResume.
2873 *
2874 * This needs to be called from incoming places where resources might have been loaded
2875 * while we are paused. That is becaues the Configuration might be wrong
2876 * when we're not running, and if it comes back to what it was when we
2877 * were paused, we are not restarted.
2878 *
2879 * Implementation of the method from LauncherModel.Callbacks.
2880 *
2881 * @return true if we are currently paused. The caller might be able to
2882 * skip some work in that case since we will come back again.
2883 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002884 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002885 public boolean setLoadOnResume() {
2886 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002887 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002888 mOnResumeNeedsLoad = true;
2889 return true;
2890 } else {
2891 return false;
2892 }
2893 }
2894
2895 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002898 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002900 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002901 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002902 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002903 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002905 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002906
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002908 * Clear any pending bind callbacks. This is called when is loader is planning to
2909 * perform a full rebind from scratch.
2910 */
2911 @Override
2912 public void clearPendingBinds() {
2913 mBindOnResumeCallbacks.clear();
2914 if (mPendingExecutor != null) {
2915 mPendingExecutor.markCompleted();
2916 mPendingExecutor = null;
2917 }
2918 }
2919
2920 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 * Refreshes the shortcuts shown on the workspace.
2922 *
2923 * Implementation of the method from LauncherModel.Callbacks.
2924 */
2925 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002926 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002927 AbstractFloatingView.closeAllOpenViews(this);
2928
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002929 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002930
Winson Chungd64d1762013-08-20 14:37:16 -07002931 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002932 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002933 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002934
Winson Chung3d503fb2011-07-13 17:25:49 -07002935 if (mHotseat != null) {
2936 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002938 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 }
2940
Adam Cohendcd297f2013-06-18 13:13:40 -07002941 @Override
2942 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002943 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002944 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2945 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002946 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2947 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002948 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002949 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2950 // If there are no screens, we need to have an empty screen
2951 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002952 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002953 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002954
Winsonc7d2e832016-07-28 12:24:55 -07002955 // After we have added all the screens, if the wallpaper was locked to the default state,
2956 // then notify to indicate that it can be released and a proper wallpaper offset can be
2957 // computed before the next layout
2958 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002959 }
2960
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002961 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002962 int count = orderedScreenIds.size();
2963 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002964 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002965 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002966 // No need to bind the first screen, as its always bound.
2967 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2968 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002969 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002970 }
2971
Sunny Goyalb23980c2017-08-17 07:45:25 -07002972 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002973 public void bindAppsAdded(final ArrayList<Long> newScreens,
2974 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002975 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002976 Runnable r = new Runnable() {
2977 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002978 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002979 }
2980 };
2981 if (waitUntilResume(r)) {
2982 return;
2983 }
2984
Winson Chungd64d1762013-08-20 14:37:16 -07002985 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002986 if (newScreens != null) {
2987 bindAddScreens(newScreens);
2988 }
Winson Chungd64d1762013-08-20 14:37:16 -07002989
2990 // We add the items without animation on non-visible pages, and with
2991 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002992 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002993 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002994 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002995 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002996 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002997 }
Winson Chungc58497e2013-09-03 17:48:37 -07002998
Winson Chung87412982013-10-03 18:34:14 -07002999 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003000 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07003001 }
3002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 * Bind the items start-end from the list.
3005 *
3006 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003008 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07003009 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003010 Runnable r = new Runnable() {
3011 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003012 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003013 }
3014 };
3015 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003016 return;
3017 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003018
Sunny Goyal3be633b2016-12-08 09:59:25 -08003019 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003020 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003021 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07003022 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003023 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003024 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07003025 int end = items.size();
3026 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003027 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003028
3029 // Short circuit if we are loading dock items for a configuration which has no dock
3030 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3031 mHotseat == null) {
3032 continue;
3033 }
3034
Sunny Goyal639e9062015-08-19 19:17:06 -07003035 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003036 switch (item.itemType) {
3037 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3038 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003039 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003040 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003041 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003042 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003043 }
3044 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003045 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003046 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003047 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003049 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07003050 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
3051 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07003052 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003053 if (view == null) {
3054 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003055 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08003056 break;
3057 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003058 default:
3059 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003061
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003062 /*
3063 * Remove colliding items.
3064 */
3065 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3066 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3067 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3068 View v = cl.getChildAt(item.cellX, item.cellY);
3069 Object tag = v.getTag();
3070 String desc = "Collision while binding workspace item: " + item
3071 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003072 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003073 throw (new RuntimeException(desc));
3074 } else {
3075 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003076 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003077 continue;
3078 }
3079 }
3080 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303081 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003082 if (animateIcons) {
3083 // Animate all the applications up now
3084 view.setAlpha(0f);
3085 view.setScaleX(0f);
3086 view.setScaleY(0f);
3087 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003088 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003091
Winson Chung997a9232013-07-24 15:33:46 -07003092 if (animateIcons) {
3093 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003094 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003095 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003096 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003097 final Runnable startBounceAnimRunnable = new Runnable() {
3098 public void run() {
3099 anim.playTogether(bounceAnims);
3100 anim.start();
3101 }
3102 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003103 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003104 // We post the animation slightly delayed to prevent slowdowns
3105 // when we are loading right after we return to launcher.
3106 mWorkspace.postDelayed(new Runnable() {
3107 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003108 if (mWorkspace != null) {
3109 mWorkspace.snapToPage(newScreenIndex);
3110 mWorkspace.postDelayed(startBounceAnimRunnable,
3111 NEW_APPS_ANIMATION_DELAY);
3112 }
Winson Chung94d67682013-09-25 16:29:40 -07003113 }
3114 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003115 } else {
3116 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003117 }
3118 }
Winson Chung64359a52013-07-08 17:17:08 -07003119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 }
3122
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 /**
3124 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003125 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003126 public void bindAppWidget(LauncherAppWidgetInfo item) {
3127 View view = inflateAppWidget(item);
3128 if (view != null) {
3129 mWorkspace.addInScreen(view, item);
3130 mWorkspace.requestLayout();
3131 }
3132 }
3133
3134 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003135 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303136 PendingAppWidgetHostView view =
3137 new PendingAppWidgetHostView(this, item, mIconCache, true);
3138 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003139 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003140 }
3141
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003142 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003144 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003145
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003146 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3147 // If the provider is not ready, bind as a pending widget.
3148 appWidgetInfo = null;
3149 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3150 // The widget id is not valid. Try to find the widget based on the provider info.
3151 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3152 } else {
3153 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3154 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003155
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003156 // If the provider is ready, but the width is not yet restored, try to restore it.
3157 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3158 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003159 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003160 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3161 + " belongs to component " + item.providerName
3162 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003163 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003164 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003165 }
Sunny Goyalff572272014-07-23 13:58:07 -07003166
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003167 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003168 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003169 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3170 // Id has not been allocated yet. Allocate a new id.
3171 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3172 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003173
Sunny Goyald478c832016-04-01 12:04:16 -07003174 // Also try to bind the widget. If the bind fails, the user will be shown
3175 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003176 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003177 pendingInfo.spanX = item.spanX;
3178 pendingInfo.spanY = item.spanY;
3179 pendingInfo.minSpanX = item.minSpanX;
3180 pendingInfo.minSpanY = item.minSpanY;
3181 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003182
3183 boolean isDirectConfig =
3184 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3185 if (isDirectConfig && item.bindOptions != null) {
3186 Bundle newOptions = item.bindOptions.getExtras();
3187 if (options != null) {
3188 newOptions.putAll(options);
3189 }
3190 options = newOptions;
3191 }
Sunny Goyald478c832016-04-01 12:04:16 -07003192 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3193 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003194
Sunny Goyal86df1382016-08-10 15:03:22 -07003195 // We tried to bind once. If we were not able to bind, we would need to
3196 // go through the permission dialog, which means we cannot skip the config
3197 // activity.
3198 item.bindOptions = null;
3199 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3200
Sunny Goyald478c832016-04-01 12:04:16 -07003201 // Bind succeeded
3202 if (success) {
3203 // If the widget has a configure activity, it is still needs to set it up,
3204 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003205 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003206 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3207 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003208 }
Sunny Goyald478c832016-04-01 12:04:16 -07003209
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003210 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003211 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003212 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003213 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003214 // The widget was marked as UI not ready, but there is no configure activity to
3215 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003216 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003217 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003218 }
Sunny Goyalff572272014-07-23 13:58:07 -07003219 }
3220
Sunny Goyald5462aa2016-11-22 16:52:39 +05303221 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003222 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003223 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003224 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003225 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003226 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003227 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003228 }
3229
Sunny Goyal233ee962015-08-03 13:05:01 -07003230 item.minSpanX = appWidgetInfo.minSpanX;
3231 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303232 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003233 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303234 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003235 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303236 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003238 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003239 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003240 }
3241
Sunny Goyalff572272014-07-23 13:58:07 -07003242 /**
3243 * Restores a pending widget.
3244 *
3245 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003246 */
Sunny Goyald478c832016-04-01 12:04:16 -07003247 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003248 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3249 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3250 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003251 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003252 }
3253
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003254 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003255 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003256 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3257 info.pendingItemInfo = null;
3258 }
Sunny Goyalff572272014-07-23 13:58:07 -07003259
3260 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003261 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003262 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003263 }
3264
Adam Cohen1462de32012-07-24 22:34:36 -07003265 public void onPageBoundSynchronously(int page) {
3266 mSynchronouslyBoundPages.add(page);
3267 }
3268
Sunny Goyal527c7d32015-08-28 15:19:36 -07003269 @Override
3270 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3271 if (mPendingExecutor != null) {
3272 mPendingExecutor.markCompleted();
3273 }
3274 mPendingExecutor = executor;
3275 executor.attachTo(this);
3276 }
3277
3278 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3279 if (mPendingExecutor == executor) {
3280 mPendingExecutor = null;
3281 }
3282 }
3283
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003284 @Override
3285 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3286 Runnable r = new Runnable() {
3287 public void run() {
3288 finishFirstPageBind(executor);
3289 }
3290 };
3291 if (waitUntilResume(r)) {
3292 return;
3293 }
3294
3295 Runnable onComplete = new Runnable() {
3296 @Override
3297 public void run() {
3298 if (executor != null) {
3299 executor.onLoadAnimationCompleted();
3300 }
3301 }
3302 };
3303 if (mDragLayer.getAlpha() < 1) {
3304 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3305 } else {
3306 onComplete.run();
3307 }
3308 }
3309
Joe Onorato9c1289c2009-08-17 11:03:03 -04003310 /**
3311 * Callback saying that there aren't any more items to bind.
3312 *
3313 * Implementation of the method from LauncherModel.Callbacks.
3314 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003315 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003316 Runnable r = new Runnable() {
3317 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003318 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003319 }
3320 };
3321 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003322 return;
3323 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003324 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003325 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003326
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003327 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003328
Sunny Goyal2100c782016-08-22 16:00:03 -07003329 if (mPendingActivityResult != null) {
3330 handleActivityResult(mPendingActivityResult.requestCode,
3331 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3332 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003333 }
3334
Sunny Goyala474a9b2017-05-04 16:47:11 -07003335 InstallShortcutReceiver.disableAndFlushInstallQueue(
3336 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003337
Tony Wickham010d2552017-01-20 08:15:28 -08003338 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3339
Adam Cohen9211d422014-10-07 18:14:53 -07003340 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003341 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003342 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003343 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003344 }
3345
Winson Chunga2413752012-04-03 14:22:34 -07003346 private boolean canRunNewAppsAnimation() {
3347 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003348 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003349 }
3350
Winson Chungc9168342013-06-26 14:54:55 -07003351 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003352 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003353 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3354 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003355 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003356 return bounceAnim;
3357 }
3358
Adam Cohen27772732013-11-11 14:00:56 +00003359 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003360 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003361 }
3362
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003363 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 * Add the icons for all apps.
3365 *
3366 * Implementation of the method from LauncherModel.Callbacks.
3367 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003368 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003369 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3370 public void run() {
3371 bindAllApplications(apps);
3372 }
3373 };
3374 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003375 return;
3376 }
3377
Winson Chungb745afb2015-03-02 11:51:23 -08003378 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003379 Executor pendingExecutor = getPendingExecutor();
3380 if (pendingExecutor != null && mState != State.APPS) {
3381 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003382 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003383 return;
3384 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003385
Winson Chungb745afb2015-03-02 11:51:23 -08003386 mAppsView.setApps(apps);
3387 }
Adam Cohen9211d422014-10-07 18:14:53 -07003388 if (mLauncherCallbacks != null) {
3389 mLauncherCallbacks.bindAllApplications(apps);
3390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 }
3392
3393 /**
Tony2917a8b2017-07-31 23:29:42 -07003394 * Returns an Executor that will run after the launcher is first drawn (including after the
3395 * initial fade in animation). Returns null if the first draw has already occurred.
3396 */
3397 public @Nullable Executor getPendingExecutor() {
3398 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3399 }
3400
3401 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003402 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3403 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3404 */
3405 @Override
3406 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003407 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003408 }
3409
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003410 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 * A package was updated.
3412 *
3413 * Implementation of the method from LauncherModel.Callbacks.
3414 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003415 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003416 Runnable r = new Runnable() {
3417 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003418 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003419 }
3420 };
3421 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003422 return;
3423 }
3424
Winson Chungb745afb2015-03-02 11:51:23 -08003425 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003426 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003427 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003428 }
3429
Sunny Goyal4390ace2014-10-13 11:33:11 -07003430 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003431 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3432 Runnable r = new Runnable() {
3433 public void run() {
3434 bindPromiseAppProgressUpdated(app);
3435 }
3436 };
3437 if (waitUntilResume(r)) {
3438 return;
3439 }
3440
3441 if (mAppsView != null) {
3442 mAppsView.updatePromiseAppProgress(app);
3443 }
3444 }
3445
3446 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003447 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3448 Runnable r = new Runnable() {
3449 public void run() {
3450 bindWidgetsRestored(widgets);
3451 }
3452 };
3453 if (waitUntilResume(r)) {
3454 return;
3455 }
3456 mWorkspace.widgetsRestored(widgets);
3457 }
3458
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003460 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003461 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003462 *
3463 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003464 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003465 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003466 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003467 Runnable r = new Runnable() {
3468 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003469 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003470 }
3471 };
3472 if (waitUntilResume(r)) {
3473 return;
3474 }
3475
Sunny Goyal4390ace2014-10-13 11:33:11 -07003476 if (!updated.isEmpty()) {
3477 mWorkspace.updateShortcuts(updated);
3478 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003479 }
3480
3481 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003482 * Update the state of a package, typically related to install state.
3483 *
3484 * Implementation of the method from LauncherModel.Callbacks.
3485 */
Sunny Goyale755d462014-07-22 13:48:29 -07003486 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003487 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3488 Runnable r = new Runnable() {
3489 public void run() {
3490 bindRestoreItemsChange(updates);
3491 }
3492 };
3493 if (waitUntilResume(r)) {
3494 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003495 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003496
Sunny Goyal756adbc2015-04-16 15:20:51 -07003497 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003498 }
3499
3500 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003501 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003502 * in addition to specific applications to remove, the reason being that
3503 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003504 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003505 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003507 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003508 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003509 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003510 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003511 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003512 }
Winson Chungd64d1762013-08-20 14:37:16 -07003513 };
3514 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003515 return;
3516 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003517 mWorkspace.removeItemsByMatcher(matcher);
3518 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003519 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003520
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003521 @Override
3522 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3523 Runnable r = new Runnable() {
3524 public void run() {
3525 bindAppInfosRemoved(appInfos);
3526 }
3527 };
3528 if (waitUntilResume(r)) {
3529 return;
Joe Onorato36115782010-06-17 13:28:48 -04003530 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003531
Winson Chungdf95eb12013-10-16 14:57:07 -07003532 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003533 if (mAppsView != null) {
3534 mAppsView.removeApps(appInfos);
Hyunyoung Song51114f22017-05-02 16:06:28 -07003535 tryAndUpdatePredictedApps();
Winson Chungc58497e2013-09-03 17:48:37 -07003536 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 }
Joe Onoratobe386092009-11-17 17:32:16 -08003538
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003539 @Override
3540 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3541 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3542 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003543 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003544 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003545 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003546 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003547 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003548 return;
3549 }
3550
Sunny Goyald164b7f2016-10-12 20:49:31 -07003551 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003552 Executor pendingExecutor = getPendingExecutor();
3553 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003554 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003555 return;
3556 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003557 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003558 }
Tony Wickham26b17462017-03-20 17:12:24 -07003559
3560 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3561 if (topView != null) {
3562 topView.onWidgetsBound();
3563 }
3564 }
3565
3566 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3567 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003568 }
3569
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003570 @Override
3571 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003572 if (mWorkspace.getState().shouldUpdateWidget) {
3573 refreshAndBindWidgetsForPackageUser(null);
3574 }
Tony Wickham26b17462017-03-20 17:12:24 -07003575 }
3576
Tony Wickham86222d22017-03-29 15:30:43 -07003577 /**
3578 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3579 * refreshes the widgets and shortcuts associated with the given package/user
3580 */
3581 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003582 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003583 }
3584
Winson Chung4b919f82012-05-01 10:44:08 -07003585 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003586 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003587 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003588 }
3589 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003590
Winson Chung4b919f82012-05-01 10:44:08 -07003591 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003592 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003593 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003594 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003595 } else {
3596 mHandler.postDelayed(new Runnable() {
3597 public void run() {
3598 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3599 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003600 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003601 }
Winson Chung4b919f82012-05-01 10:44:08 -07003602 }
Winson Chung400438b2011-01-16 17:53:48 -08003603 }
3604
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003605 private void markAppsViewShown() {
3606 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3607 return;
3608 }
3609 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3610 }
3611
3612 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003613 UserManagerCompat um = UserManagerCompat.getInstance(this);
3614 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003615 }
3616
Winson Chung80baf5a2010-08-09 16:03:15 -07003617 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003618 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003619 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003620 @Override
3621 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3622 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003623
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003624 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3625 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003626 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003627 writer.println(prefix + " Homescreen " + i);
3628
3629 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3630 for (int j = 0; j < layout.getChildCount(); j++) {
3631 Object tag = layout.getChildAt(j).getTag();
3632 if (tag != null) {
3633 writer.println(prefix + " " + tag.toString());
3634 }
3635 }
3636 }
3637
3638 writer.println(prefix + " Hotseat");
3639 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003640 for (int j = 0; j < layout.getChildCount(); j++) {
3641 Object tag = layout.getChildAt(j).getTag();
3642 if (tag != null) {
3643 writer.println(prefix + " " + tag.toString());
3644 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003645 }
Sunny Goyala1365452015-10-01 15:46:24 -07003646
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003647 try {
3648 FileLog.flushAll(writer);
3649 } catch (Exception e) {
3650 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003651 }
3652 }
3653
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003654 writer.println(prefix + "Misc:");
3655 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3656 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3657 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3658
3659 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003660
Adam Cohen9211d422014-10-07 18:14:53 -07003661 if (mLauncherCallbacks != null) {
3662 mLauncherCallbacks.dump(prefix, fd, writer, args);
3663 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003664 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003665
Sunny Goyal66b24572016-09-21 15:57:55 -07003666 @Override
3667 @TargetApi(Build.VERSION_CODES.N)
3668 public void onProvideKeyboardShortcuts(
3669 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3670
3671 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3672 if (mState == State.WORKSPACE) {
3673 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3674 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3675 }
3676 View currentFocus = getCurrentFocus();
3677 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3678 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3679 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3680 }
Tony18c4aa42017-05-10 21:23:57 -05003681 if (currentFocus.getTag() instanceof ItemInfo
3682 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003683 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3684 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3685 }
3686 if (!shortcutInfos.isEmpty()) {
3687 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3688 }
3689
3690 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3691 }
3692
3693 @Override
3694 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3695 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3696 switch (keyCode) {
3697 case KeyEvent.KEYCODE_A:
3698 if (mState == State.WORKSPACE) {
Sunny Goyal5606e072017-06-15 14:24:21 -07003699 showAppsView(true, true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003700 return true;
3701 }
3702 break;
3703 case KeyEvent.KEYCODE_S: {
3704 View focusedView = getCurrentFocus();
3705 if (focusedView instanceof BubbleTextView
3706 && focusedView.getTag() instanceof ItemInfo
3707 && mAccessibilityDelegate.performAction(focusedView,
3708 (ItemInfo) focusedView.getTag(),
3709 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003710 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003711 return true;
3712 }
3713 break;
3714 }
3715 case KeyEvent.KEYCODE_O:
3716 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3717 return true;
3718 }
3719 break;
3720 }
3721 }
3722 return super.onKeyShortcut(keyCode, event);
3723 }
3724
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003725 public static Launcher getLauncher(Context context) {
3726 if (context instanceof Launcher) {
3727 return (Launcher) context;
3728 }
3729 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3730 }
3731
Sunny Goyal5a81c382017-03-20 15:08:06 -07003732 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003733
3734 @Override
3735 public void onSharedPreferenceChanged(
3736 SharedPreferences sharedPreferences, String key) {
3737 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003738 // Recreate the activity so that it initializes the rotation preference again.
3739 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003740 }
3741 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003742 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003743}