blob: 7688b9d092027f56fe1c20b6d2d523d7323ec8f5 [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
Winson Chungcd99cd32015-04-29 11:03:24 -0700911 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -0700912 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -0700913
Sunny Goyala474a9b2017-05-04 16:47:11 -0700914 // Process any items that were added while Launcher was away.
915 InstallShortcutReceiver.disableAndFlushInstallQueue(
916 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700917
Sunny Goyala474a9b2017-05-04 16:47:11 -0700918 // Refresh shortcuts if the permission changed.
919 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700920
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700921 if (shouldShowDiscoveryBounce()) {
922 mAllAppsController.showDiscoveryBounce();
923 }
Adam Cohen9211d422014-10-07 18:14:53 -0700924 if (mLauncherCallbacks != null) {
925 mLauncherCallbacks.onResume();
926 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800927
Sunny Goyala502aa32017-10-02 16:04:06 -0700928 clearTypedText();
929
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700930 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700931 }
932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700934 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700935 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700936 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700937
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700938 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700939 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800940 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700941 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700942
Adam Cohen9211d422014-10-07 18:14:53 -0700943 if (mLauncherCallbacks != null) {
944 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700945 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700946 }
947
Adam Cohenc2d6e892014-10-16 09:49:24 -0700948 public interface LauncherOverlay {
949
950 /**
951 * Touch interaction leading to overscroll has begun
952 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700953 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700954
955 /**
956 * Touch interaction related to overscroll has ended
957 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700958 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700959
960 /**
961 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
962 * screen (or in the case of RTL, the rightmost screen).
963 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700964 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700965
966 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800967 * Called when the launcher is ready to use the overlay
968 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700969 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700970 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700971 }
972
973 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700974 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700975 }
976
977 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
978
Sunny Goyalc86df472016-02-25 09:19:38 -0800979 public void onScrollChanged(float progress) {
980 if (mWorkspace != null) {
981 mWorkspace.onOverlayScrollChanged(progress);
982 }
983 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700984 }
985
Jorim Jaggid017f882014-01-14 17:08:48 -0800986 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700987 if (mLauncherCallbacks != null) {
988 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700989 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800990 // On O and above we there is always some setting present settings (add icon to
991 // home screen or icon badging). On earlier APIs we will have the allow rotation
992 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700993 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700994 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800995 }
996
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700997 @Override
998 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001000 if (mModel.isCurrentCallbacks(this)) {
1001 mModel.stopLoader();
1002 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001003 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1004
Romain Guy13c2e7b2010-03-10 19:45:00 -08001005 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001006 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001007
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001008 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001009 @Override
1010 public void onWindowFocusChanged(boolean hasFocus) {
1011 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001012 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001013
1014 if (mLauncherCallbacks != null) {
1015 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1016 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001017 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 private boolean acceptFilter() {
1020 final InputMethodManager inputManager = (InputMethodManager)
1021 getSystemService(Context.INPUT_METHOD_SERVICE);
1022 return !inputManager.isFullscreenMode();
1023 }
1024
1025 @Override
1026 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001027 final int uniChar = event.getUnicodeChar();
1028 final boolean handled = super.onKeyDown(keyCode, event);
1029 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1030 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1032 keyCode, event);
1033 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001034 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001035 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001036 // showSearchDialog()
1037 // If there are multiple keystrokes before the search dialog takes focus,
1038 // onSearchRequested() will be called for every keystroke,
1039 // but it is idempotent, so it's fine.
1040 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042 }
1043
Joe Onorato8a9625e2010-01-28 15:55:35 -08001044 // Eat the long press event so the keyboard doesn't come up.
1045 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1046 return true;
1047 }
1048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 return handled;
1050 }
1051
Winson46163472015-09-22 17:31:56 -07001052 @Override
1053 public boolean onKeyUp(int keyCode, KeyEvent event) {
1054 if (keyCode == KeyEvent.KEYCODE_MENU) {
1055 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001056 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001057 // Close any open floating view
1058 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001059
Winson46163472015-09-22 17:31:56 -07001060 // Show the overview mode if we are on the workspace
1061 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1062 !mWorkspace.isSwitchingState()) {
1063 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001064 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001065 }
1066 }
1067 return true;
1068 }
1069 return super.onKeyUp(keyCode, event);
1070 }
1071
Karl Rosaen138a0412009-04-23 19:00:21 -07001072 private String getTypedText() {
1073 return mDefaultKeySsb.toString();
1074 }
1075
Sunny Goyal6f28e712016-09-13 14:19:29 -07001076 @Override
1077 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001078 mDefaultKeySsb.clear();
1079 mDefaultKeySsb.clearSpans();
1080 Selection.setSelection(mDefaultKeySsb, 0);
1081 }
1082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 /**
1084 * Restores the previous state, if it exists.
1085 *
1086 * @param savedState The previous state.
1087 */
1088 private void restoreState(Bundle savedState) {
1089 if (savedState == null) {
1090 return;
1091 }
1092
Sunny Goyalfe770c92016-09-27 14:38:58 -07001093 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1094 State[] stateValues = State.values();
1095 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1096 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001097 if (state == State.APPS || state == State.WIDGETS) {
1098 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001099 }
1100
Sunny Goyal2100c782016-08-22 16:00:03 -07001101 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1102 if (requestArgs != null) {
1103 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001105
1106 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
1108
1109 /**
1110 * Finds all the views we need and configure them properly.
1111 */
1112 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001113 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001114 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001115 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001116 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001117
Sunny Goyal784f9c32016-03-23 16:56:54 -07001118 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1119 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1120 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121
Winson Chung4c98d922011-05-31 16:50:48 -07001122 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001123 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001124
Winson Chung3d503fb2011-07-13 17:25:49 -07001125 // Setup the hotseat
1126 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1127 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001128 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001129 }
1130
Winsone9f27272015-10-13 10:47:51 -07001131 // Setup the overview panel
1132 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001133
Winson Chung4c98d922011-05-31 16:50:48 -07001134 // Setup the workspace
1135 mWorkspace.setHapticFeedbackEnabled(false);
1136 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001137 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001138 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1139 // default state, otherwise we will update to the wrong offsets in RTL
1140 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001141 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001142 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001143
Tony Wickham34d2c912015-09-11 09:27:58 -07001144 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001145 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001146
Winson Chungef7f8742015-06-04 17:18:17 -07001147 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001148 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001149 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001150
Winson Chung3d503fb2011-07-13 17:25:49 -07001151 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001152 mDragController.setMoveTarget(mWorkspace);
1153 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001154 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001155
Hyunyoung Songd725f642017-08-17 22:26:35 -07001156 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001157
Sunny Goyal322d5562015-06-25 19:35:49 -07001158 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1159 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 }
1162
Winsone9f27272015-10-13 10:47:51 -07001163 private void setupOverviewPanel() {
1164 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1165
Winsone9f27272015-10-13 10:47:51 -07001166 // Bind wallpaper button actions
1167 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301168 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001169 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001170 public void handleViewClick(View view) {
1171 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001172 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001173 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001174
1175 // Bind widget button actions
1176 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301177 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001178 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001179 public void handleViewClick(View view) {
1180 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001181 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001182 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001183
1184 // Bind settings actions
1185 View settingsButton = findViewById(R.id.settings_button);
1186 boolean hasSettings = hasSettings();
1187 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301188 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001189 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001190 public void handleViewClick(View view) {
1191 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001192 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001193 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001194 } else {
1195 settingsButton.setVisibility(View.GONE);
1196 }
1197
1198 mOverviewPanel.setAlpha(0f);
1199 }
1200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001202 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001203 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001204 */
1205 public void setAllAppsButton(View allAppsButton) {
1206 mAllAppsButton = allAppsButton;
1207 }
1208
Sunny Goyalbb011da2016-06-15 15:42:29 -07001209 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001210 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001211 }
1212
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001213 public View getWidgetsButton() {
1214 return mWidgetsButton;
1215 }
1216
Anjali Koppal5ad44842014-03-10 20:34:39 -07001217 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 * Creates a view representing a shortcut.
1219 *
1220 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001222 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001223 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225
1226 /**
1227 * Creates a view representing a shortcut inflated from the specified resource.
1228 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 * @param parent The group the shortcut belongs to.
1230 * @param info The data structure describing the shortcut.
1231 *
1232 * @return A View inflated from layoutResId.
1233 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001234 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001235 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1236 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001237 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001239 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 return favorite;
1241 }
1242
1243 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001244 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 *
1246 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001248 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001249 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001250 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1251 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001252 return;
1253 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001254
Jon Mirandac476d6e2017-05-04 16:30:01 -07001255 int[] cellXY = mTmpAddItemCellCoordinates;
1256 CellLayout layout = getCellLayout(container, screenId);
1257
Sunny Goyal782f0c92017-01-19 10:27:54 -08001258 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001259 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001260 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1261 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001262 }
1263
1264 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001265 // Legacy shortcuts are only supported for primary profile.
1266 info = Process.myUserHandle().equals(args.user)
1267 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001268
Sunny Goyalb57645f2017-03-20 13:57:28 -07001269 if (info == null) {
1270 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1271 return;
1272 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001273 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001274 // The app is trying to add a shortcut without sufficient permissions
1275 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1276 return;
1277 }
1278 }
1279
Jon Mirandac476d6e2017-05-04 16:30:01 -07001280 if (container < 0) {
1281 // Adding a shortcut to the Workspace.
1282 final View view = createShortcut(info);
1283 boolean foundCellSpan = false;
1284 // First we check if we already know the exact location where we want to add this item.
1285 if (cellX >= 0 && cellY >= 0) {
1286 cellXY[0] = cellX;
1287 cellXY[1] = cellY;
1288 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001289
Jon Mirandac476d6e2017-05-04 16:30:01 -07001290 // If appropriate, either create a folder or add to an existing folder
1291 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1292 true, null, null)) {
1293 return;
1294 }
1295 DragObject dragObject = new DragObject();
1296 dragObject.dragInfo = info;
1297 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1298 true)) {
1299 return;
1300 }
1301 } else {
1302 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1303 }
1304
1305 if (!foundCellSpan) {
1306 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001307 return;
1308 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001309
1310 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1311 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001312 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001313 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001314 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001315 if (folderIcon != null) {
1316 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1317 folderInfo.add(info, args.rank, false);
1318 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001319 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001320 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323
Sunny Goyale29897f52017-07-20 10:09:42 -07001324 public FolderIcon findFolderIcon(final long folderIconId) {
1325 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1326 @Override
1327 public boolean evaluate(ItemInfo info, View view) {
1328 return info != null && info.id == folderIconId;
1329 }
1330 });
1331 }
1332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001334 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001336 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001338 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001339 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1340
Adam Cohened66b2b2012-01-23 17:28:51 -08001341 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001342 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001343 }
Romain Guycbb89e42009-06-08 15:52:54 -07001344
Adam Cohen59400422014-03-05 18:07:04 -08001345 LauncherAppWidgetInfo launcherInfo;
1346 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001347 launcherInfo.spanX = itemInfo.spanX;
1348 launcherInfo.spanY = itemInfo.spanY;
1349 launcherInfo.minSpanX = itemInfo.minSpanX;
1350 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001351 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001352
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001353 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001354 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
Sunny Goyal2100c782016-08-22 16:00:03 -07001356 if (hostView == null) {
1357 // Perform actual inflation because we're live
1358 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001360 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301361 prepareAppWidget(hostView, launcherInfo);
1362 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
Romain Guycbb89e42009-06-08 15:52:54 -07001364
Sunny Goyald5462aa2016-11-22 16:52:39 +05301365 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001366 hostView.setTag(item);
1367 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001368 hostView.setFocusable(true);
1369 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001370 }
1371
Adam Cohended9f8d2010-11-03 13:25:16 -07001372 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1373 @Override
1374 public void onReceive(Context context, Intent intent) {
1375 final String action = intent.getAction();
1376 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001377 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001378 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001379 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001380 if (!showWorkspace(false)) {
1381 // If we are already on the workspace, then manually reset all apps
1382 mAppsView.reset();
1383 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001384 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001385 mShouldFadeInScrim = true;
1386 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1387 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1388 // the user unlocked and the Launcher is not in the foreground.
1389 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001390 }
1391 }
1392 };
1393
Tony Wickham010d2552017-01-20 08:15:28 -08001394 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1395 Runnable r = new Runnable() {
1396 @Override
1397 public void run() {
1398 mWorkspace.updateIconBadges(updatedBadges);
1399 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001400
1401 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1402 if (popup != null) {
1403 popup.updateNotificationHeader(updatedBadges);
1404 }
Tony Wickham010d2552017-01-20 08:15:28 -08001405 }
1406 };
1407 if (!waitUntilResume(r)) {
1408 r.run();
1409 }
1410 }
1411
Adam Cohended9f8d2010-11-03 13:25:16 -07001412 @Override
1413 public void onAttachedToWindow() {
1414 super.onAttachedToWindow();
1415
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001416 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001417 if (mLauncherCallbacks != null) {
1418 mLauncherCallbacks.onAttachedToWindow();
1419 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001420 }
1421
1422 @Override
1423 public void onDetachedFromWindow() {
1424 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001425
1426 if (mLauncherCallbacks != null) {
1427 mLauncherCallbacks.onDetachedFromWindow();
1428 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001429 }
1430
Winson Chunga6945242014-01-08 14:04:34 -08001431 public DragLayer getDragLayer() {
1432 return mDragLayer;
1433 }
1434
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001435 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001436 return mAppsView;
1437 }
1438
Hyunyoung Song3f471442015-04-08 19:01:34 -07001439 public WidgetsContainerView getWidgetsView() {
1440 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001441 }
1442
Winson Chunga6945242014-01-08 14:04:34 -08001443 public Workspace getWorkspace() {
1444 return mWorkspace;
1445 }
1446
1447 public Hotseat getHotseat() {
1448 return mHotseat;
1449 }
1450
Jorim Jaggid017f882014-01-14 17:08:48 -08001451 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001452 return mOverviewPanel;
1453 }
1454
Sunny Goyal47328fd2016-05-25 18:56:41 -07001455 public DropTargetBar getDropTargetBar() {
1456 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001457 }
1458
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001459 public LauncherAppWidgetHost getAppWidgetHost() {
1460 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
Romain Guycbb89e42009-06-08 15:52:54 -07001462
Winson Chunga9abd0e2010-10-27 17:18:37 -07001463 public LauncherModel getModel() {
1464 return mModel;
1465 }
1466
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001467 public ModelWriter getModelWriter() {
1468 return mModelWriter;
1469 }
1470
Adam Cohen79d90c52016-04-22 13:29:20 -07001471 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001472 return mSharedPrefs;
1473 }
1474
Jon Miranda6bed3502017-09-19 14:43:17 -07001475 public int getOrientation() { return mOrientation; }
1476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 @Override
1478 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001479 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 super.onNewIntent(intent);
1481
Winson15f8b172015-08-19 11:02:39 -07001482 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1483 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1484 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001485
1486 // Check this condition before handling isActionMain, as this will get reset.
1487 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001488 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001489
Winson15f8b172015-08-19 11:02:39 -07001490 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1491 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001492 if (mWorkspace == null) {
1493 // Can be cases where mWorkspace is null, this prevents a NPE
1494 return;
1495 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001496
1497 // Note: There should be at most one log per method call. This is enforced implicitly
1498 // by using if-else statements.
1499 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001500 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001501 if (topOpenView != null) {
1502 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001503 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301504 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001505 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1506 }
1507
1508 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001509 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001510 exitSpringLoadedDragMode();
1511
1512 // If we are already on home, then just animate back to the workspace,
1513 // otherwise, just wait until onResume to set the state back to Workspace
1514 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001515 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001516 } else {
1517 mOnResumeState = State.WORKSPACE;
1518 }
1519
1520 final View v = getWindow().peekDecorView();
1521 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001522 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001523 }
1524
Winson Chungb745afb2015-03-02 11:51:23 -08001525 // Reset the apps view
1526 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001527 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001528 }
1529
Hyunyoung Song3f471442015-04-08 19:01:34 -07001530 // Reset the widgets view
1531 if (!alreadyOnHome && mWidgetsView != null) {
1532 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001533 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001534
Adam Cohen9211d422014-10-07 18:14:53 -07001535 if (mLauncherCallbacks != null) {
1536 mLauncherCallbacks.onHomeIntent();
1537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001539 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001540
Adam Cohen9211d422014-10-07 18:14:53 -07001541 if (mLauncherCallbacks != null) {
1542 mLauncherCallbacks.onNewIntent(intent);
1543 }
Winson15f8b172015-08-19 11:02:39 -07001544
1545 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1546 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1547 // animation.
1548 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001549 boolean callbackAllowsMoveToDefaultScreen =
1550 mLauncherCallbacks == null || mLauncherCallbacks
1551 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001552 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1553 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001554
Winson15f8b172015-08-19 11:02:39 -07001555 mWorkspace.post(new Runnable() {
1556 @Override
1557 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001558 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001559 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001560 }
Winson15f8b172015-08-19 11:02:39 -07001561 }
1562 });
1563 }
1564 }
1565
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001566 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001567 }
1568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001570 public void onRestoreInstanceState(Bundle state) {
1571 super.onRestoreInstanceState(state);
1572 for (int page: mSynchronouslyBoundPages) {
1573 mWorkspace.restoreInstanceStateForChild(page);
1574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
1576
1577 @Override
1578 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001579 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001580 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001581
Adam Cohen21cd0022013-10-09 18:57:02 -07001582 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001583 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584
Michael Jurka883f55b2010-10-21 15:47:14 -07001585 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001586 // We close any open folders and shortcut containers since they will not be re-opened,
1587 // and we need to make sure this state is reflected.
1588 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001589
Sunny Goyal2100c782016-08-22 16:00:03 -07001590 if (mPendingRequestArgs != null) {
1591 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1592 }
1593 if (mPendingActivityResult != null) {
1594 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
1596
Adam Cohen9211d422014-10-07 18:14:53 -07001597 if (mLauncherCallbacks != null) {
1598 mLauncherCallbacks.onSaveInstanceState(outState);
1599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
1601
1602 @Override
1603 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001606 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001607 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001608
Winson Chungcd2b0142011-06-08 16:02:26 -07001609 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001610 // It's possible to receive onDestroy after a new Launcher activity has
1611 // been created. In this case, don't interfere with the new Launcher.
1612 if (mModel.isCurrentCallbacks(this)) {
1613 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001614 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001615 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001616
Sunny Goyal745bad92016-05-02 10:54:12 -07001617 if (mRotationPrefChangeHandler != null) {
1618 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001622 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001624 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001626 mAppWidgetHost = null;
1627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 TextKeyListener.getInstance().release();
1629
Tony Wickhame2217252016-03-22 16:34:23 -07001630 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1631 .removeAccessibilityStateChangeListener(this);
1632
Mario Bertschler27288382017-05-24 15:35:09 -07001633 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1634
Michael Jurka2ecf9952012-06-18 12:52:28 -07001635 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001636
Tony2917a8b2017-07-31 23:29:42 -07001637 clearPendingBinds();
1638
Adam Cohen9211d422014-10-07 18:14:53 -07001639 if (mLauncherCallbacks != null) {
1640 mLauncherCallbacks.onDestroy();
1641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
1643
Sunny Goyalae502842016-06-17 08:43:56 -07001644 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1645 return mAccessibilityDelegate;
1646 }
1647
Adam Cohena9cf38f2011-05-02 15:36:58 -07001648 public DragController getDragController() {
1649 return mDragController;
1650 }
1651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001653 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001654 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001655 }
1656
1657 @Override
1658 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1659 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001660 try {
1661 super.startIntentSenderForResult(intent, requestCode,
1662 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1663 } catch (IntentSender.SendIntentException e) {
1664 throw new ActivityNotFoundException();
1665 }
1666 }
1667
Winson Chung70d72102011-08-12 11:24:35 -07001668 /**
1669 * Indicates that we want global search for this activity by setting the globalSearch
1670 * argument for {@link #startSearch} to true.
1671 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001673 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001675
Karl Rosaen138a0412009-04-23 19:00:21 -07001676 if (initialQuery == null) {
1677 // Use any text typed in the launcher as the initial query
1678 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 if (appSearchData == null) {
1681 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001682 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001684
Sunny Goyal6f28e712016-09-13 14:19:29 -07001685 if (mLauncherCallbacks == null ||
1686 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1687 // Starting search from the callbacks failed. Start the default global search.
1688 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001689 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001690
1691 // We need to show the workspace after starting the search
1692 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001693 }
1694
Ian Parkinson047036e2014-09-01 15:40:53 +01001695 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001696 * Starts the global search activity. This code is a copied from SearchManager
1697 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001698 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001699 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001700 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001701 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1702 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1703 if (globalSearchActivity == null) {
1704 Log.w(TAG, "No global search activity found.");
1705 return;
1706 }
1707 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1708 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1709 intent.setComponent(globalSearchActivity);
1710 // Make sure that we have a Bundle to put source in
1711 if (appSearchData == null) {
1712 appSearchData = new Bundle();
1713 } else {
1714 appSearchData = new Bundle(appSearchData);
1715 }
Adam Cohen9211d422014-10-07 18:14:53 -07001716 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001717 if (!appSearchData.containsKey("source")) {
1718 appSearchData.putString("source", getPackageName());
1719 }
1720 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1721 if (!TextUtils.isEmpty(initialQuery)) {
1722 intent.putExtra(SearchManager.QUERY, initialQuery);
1723 }
1724 if (selectInitialQuery) {
1725 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1726 }
1727 intent.setSourceBounds(sourceBounds);
1728 try {
1729 startActivity(intent);
1730 } catch (ActivityNotFoundException ex) {
1731 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 }
1734
Winson Chung70d72102011-08-12 11:24:35 -07001735 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001736 public boolean onPrepareOptionsMenu(Menu menu) {
1737 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001738 if (mLauncherCallbacks != null) {
1739 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1740 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001741 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001742 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001744 @Override
1745 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001746 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001747 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001748 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001749 }
1750
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001752 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753 }
1754
Adam Cohen517a7f52014-03-01 12:12:59 -08001755 public boolean isWorkspaceLoading() {
1756 return mWorkspaceLoading;
1757 }
1758
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001759 private void setWorkspaceLoading(boolean value) {
1760 boolean isLocked = isWorkspaceLocked();
1761 mWorkspaceLoading = value;
1762 if (isLocked != isWorkspaceLocked()) {
1763 onWorkspaceLockedChanged();
1764 }
1765 }
1766
Sunny Goyal04a324a2017-01-20 21:08:59 -08001767 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001768 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001769 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001770 if (isLocked != isWorkspaceLocked()) {
1771 onWorkspaceLockedChanged();
1772 }
1773 }
1774
Adam Cohen9211d422014-10-07 18:14:53 -07001775 protected void onWorkspaceLockedChanged() {
1776 if (mLauncherCallbacks != null) {
1777 mLauncherCallbacks.onWorkspaceLockedChanged();
1778 }
1779 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001780
Sunny Goyal2100c782016-08-22 16:00:03 -07001781 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001782 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001783 if (LOGD) {
1784 Log.d(TAG, "Adding widget from drop");
1785 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001786 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001787 }
1788
Sunny Goyal2100c782016-08-22 16:00:03 -07001789 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001790 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1791 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1792 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001793
Adam Cohenad4e15c2013-10-17 16:21:35 -07001794 Runnable onComplete = new Runnable() {
1795 @Override
1796 public void run() {
1797 // Exit spring loaded mode if necessary after adding the widget
1798 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1799 null);
1800 }
1801 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001802 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001803 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
1805 }
Romain Guycbb89e42009-06-08 15:52:54 -07001806
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001807 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1808 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001809 info.container = container;
1810 info.screenId = screenId;
1811 if (cell != null) {
1812 info.cellX = cell[0];
1813 info.cellY = cell[1];
1814 }
1815 info.spanX = spanX;
1816 info.spanY = spanY;
1817
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001818 switch (info.itemType) {
1819 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1820 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001821 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001822 break;
1823 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001824 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001825 break;
1826 default:
1827 throw new IllegalStateException("Unknown item type: " + info.itemType);
1828 }
1829 }
1830
Adam Cohenfbba09b2011-07-18 21:43:05 -07001831 /**
1832 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001833 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001834 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001835 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1836 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001837 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1838 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1839 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 }
1841
Adam Cohenfbba09b2011-07-18 21:43:05 -07001842 /**
1843 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001844 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001845 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001846 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001847 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001848 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001849 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001850 // In the case where we've prebound the widget, we remove it from the DragLayer
1851 if (LOGD) {
1852 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1853 }
1854 getDragLayer().removeView(hostView);
1855
Adam Cohened66b2b2012-01-23 17:28:51 -08001856 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001857 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001858
1859 // Clear the boundWidget so that it doesn't get destroyed.
1860 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001861 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001862 // In this case, we either need to start an activity to get permission to bind
1863 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001864 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1865 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1866 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1867 this, info.componentName);
1868 } else {
1869 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1870 }
Adam Cohendd70d662012-10-04 16:53:44 -07001871 Bundle options = info.bindOptions;
1872
Sunny Goyalffe83f12014-08-14 17:39:34 -07001873 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1874 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001875 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001876 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001877 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001878 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001879 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001880 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001881 }
1882
Adam Cohendcd297f2013-06-18 13:13:40 -07001883 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001884 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001885 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 folderInfo.title = getText(R.string.folder_name);
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001889 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890
1891 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001892 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301893 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001894 // Force measure the new folder icon
1895 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1896 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001897 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
Romain Guycbb89e42009-06-08 15:52:54 -07001899
Winsonc0b52fe2015-09-09 16:38:15 -07001900 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001901 * Unbinds the view for the specified item, and removes the item and all its children.
1902 *
1903 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001904 * @param itemInfo the {@link ItemInfo} for this view.
1905 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001906 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001907 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001908 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001909 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001910 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1911 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001912 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001913 } else {
1914 mWorkspace.removeWorkspaceItem(v);
1915 }
Winsonc0b52fe2015-09-09 16:38:15 -07001916 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001917 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001918 }
1919 } else if (itemInfo instanceof FolderInfo) {
1920 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001921 if (v instanceof FolderIcon) {
1922 ((FolderIcon) v).removeListeners();
1923 }
Winsonc0b52fe2015-09-09 16:38:15 -07001924 mWorkspace.removeWorkspaceItem(v);
1925 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001926 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001927 }
1928 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1929 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001930 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001931 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001932 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001933 }
1934 } else {
1935 return false;
1936 }
1937 return true;
1938 }
1939
1940 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001941 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001942 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001943 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001944 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001945 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001946 // Deleting an app widget ID is a void call but writes to disk before returning
1947 // to the caller...
1948 new AsyncTask<Void, Void, Void>() {
1949 public Void doInBackground(Void ... args) {
1950 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1951 return null;
1952 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001953 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001954 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001955 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001956 }
1957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 @Override
1959 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001960 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962
Joe Onorato88ec0992009-11-19 13:16:06 -08001963 @Override
1964 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001965 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1966 return;
1967 }
1968
Sunny Goyal45478022015-06-08 16:52:41 -07001969 if (mDragController.isDragging()) {
1970 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001971 return;
1972 }
1973
Jon Mirandafeba90f2016-10-06 10:53:29 -07001974 // Note: There should be at most one log per method call. This is enforced implicitly
1975 // by using if-else statements.
1976 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001977 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1978 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001979 topView.onBackPressed();
Tony Wickham49c8d292016-07-01 11:44:34 -07001980 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301981 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001982 showWorkspace(true);
1983 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301984 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08001985 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001986 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301987 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001988 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001989 } else {
1990 // Back button is a no-op here, but give at least some feedback for the button press
1991 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001992 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001993 }
1994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 * Launches the intent referred by the clicked shortcut.
1997 *
1998 * @param v The view representing the clicked shortcut.
1999 */
2000 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002001 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2002 // view has detached (it's possible for this to happen if the view is removed mid touch).
2003 if (v.getWindowToken() == null) {
2004 return;
2005 }
2006
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002007 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002008 return;
2009 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002010
Adam Cohen1697b792013-09-17 19:08:21 -07002011 if (v instanceof Workspace) {
2012 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08002013 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2014 LauncherLogProto.Action.Direction.NONE,
2015 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002016 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002017 }
2018 return;
2019 }
2020
2021 if (v instanceof CellLayout) {
2022 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002023 int page = mWorkspace.indexOfChild(v);
2024 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2025 LauncherLogProto.Action.Direction.NONE,
2026 LauncherLogProto.ContainerType.OVERVIEW, page);
2027 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002028 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002029 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002030 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002031 }
2032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002034 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002035 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002037 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002038 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002039 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07002040 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002041 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002042 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002043 } else if (tag instanceof AppInfo) {
2044 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002045 } else if (tag instanceof LauncherAppWidgetInfo) {
2046 if (v instanceof PendingAppWidgetHostView) {
2047 onClickPendingWidget((PendingAppWidgetHostView) v);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050 }
2051
Sunny Goyal70660032015-05-14 00:07:08 -07002052 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002053 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002054 return false;
2055 }
2056
Michael Jurkaaf442092010-06-10 17:01:57 -07002057 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002058 * Event handler for the app widget view which has not fully restored.
2059 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002060 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002061 if (mIsSafeModeEnabled) {
2062 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2063 return;
2064 }
2065
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002066 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002067 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002068 LauncherAppWidgetProviderInfo appWidgetInfo =
2069 mAppWidgetManager.findProvider(info.providerName, info.user);
2070 if (appWidgetInfo == null) {
2071 return;
2072 }
2073 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2074
Sunny Goyald478c832016-04-01 12:04:16 -07002075 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2076 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2077 // This should not happen, as we make sure that an Id is allocated during bind.
2078 return;
2079 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002080 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2081 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002082 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002083 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002084 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002085 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002086 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002087 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002088 }
2089 }
2090
2091 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002092 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2093 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2094 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002095 *
2096 * @param v The view that was clicked.
2097 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002098 protected void onClickAllAppsButton(View v) {
2099 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002100 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302101 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2102 ControlType.ALL_APPS_BUTTON);
Sunny Goyal5606e072017-06-15 14:24:21 -07002103 showAppsView(true /* animated */, true /* updatePredictedApps */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002104 } else {
2105 showWorkspace(true);
2106 }
2107 }
2108
Sunny Goyale6e72002017-01-12 16:55:36 -08002109 private void onClickPendingAppItem(final View v, final String packageName,
2110 boolean downloadStarted) {
2111 if (downloadStarted) {
2112 // If the download has started, simply direct to the market app.
2113 startMarketIntentForPackage(v, packageName);
2114 return;
2115 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002116 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002117 .setTitle(R.string.abandoned_promises_title)
2118 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002119 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2120 @Override
2121 public void onClick(DialogInterface dialogInterface, int i) {
2122 startMarketIntentForPackage(v, packageName);
2123 }
2124 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002125 .setNeutralButton(R.string.abandoned_clean_this,
2126 new DialogInterface.OnClickListener() {
2127 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002128 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002129 mWorkspace.removeAbandonedPromise(packageName, user);
2130 }
2131 })
2132 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002133 }
2134
2135 private void startMarketIntentForPackage(View v, String packageName) {
2136 ItemInfo item = (ItemInfo) v.getTag();
2137 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2138 boolean success = startActivitySafely(v, intent, item);
2139 if (success && v instanceof BubbleTextView) {
2140 mWaitingForResume = (BubbleTextView) v;
2141 mWaitingForResume.setStayPressed(true);
2142 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002143 }
2144
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002145 /**
2146 * Event handler for an app shortcut click.
2147 *
2148 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2149 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002150 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002151 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2152 Object tag = v.getTag();
2153 if (!(tag instanceof ShortcutInfo)) {
2154 throw new IllegalArgumentException("Input must be a Shortcut");
2155 }
2156
2157 // Open shortcut
2158 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002159
2160 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002161 if ((shortcut.isDisabled &
2162 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2163 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2164 // If the app is only disabled because of the above flags, launch activity anyway.
2165 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002166 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002167 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2168 // Use a message specific to this shortcut, if it has one.
2169 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2170 return;
2171 }
2172 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002173 int error = R.string.activity_not_available;
2174 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2175 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002176 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2177 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002178 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002179 }
2180 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2181 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002182 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002183 }
2184
Chris Wren40c5ed32014-06-24 18:24:23 -04002185 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002186 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002187 String packageName = shortcut.intent.getComponent() != null ?
2188 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2189 if (!TextUtils.isEmpty(packageName)) {
2190 onClickPendingAppItem(v, packageName,
2191 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2192 return;
2193 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002194 }
2195
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002196 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002197 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002198 }
2199
Sunny Goyala7ce1662016-05-31 15:01:35 -07002200 private void startAppShortcutOrInfoActivity(View v) {
2201 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002202 Intent intent;
2203 if (item instanceof PromiseAppInfo) {
2204 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2205 intent = promiseAppInfo.getMarketIntent();
2206 } else {
2207 intent = item.getIntent();
2208 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002209 if (intent == null) {
2210 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002211 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002212 boolean success = startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002213 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002214
2215 if (success && v instanceof BubbleTextView) {
2216 mWaitingForResume = (BubbleTextView) v;
2217 mWaitingForResume.setStayPressed(true);
2218 }
2219 }
2220
2221 /**
2222 * Event handler for a folder icon click.
2223 *
2224 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2225 */
2226 protected void onClickFolderIcon(View v) {
2227 if (LOGD) Log.d(TAG, "onClickFolder");
2228 if (!(v instanceof FolderIcon)){
2229 throw new IllegalArgumentException("Input must be a FolderIcon");
2230 }
2231
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002232 Folder folder = ((FolderIcon) v).getFolder();
2233 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002234 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002235 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002236 }
Michael Jurka5130e402011-10-13 04:55:35 -07002237 }
2238
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002239 /**
2240 * Event handler for the (Add) Widgets button that appears after a long press
2241 * on the home screen.
2242 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002243 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002244 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002245 if (mIsSafeModeEnabled) {
2246 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2247 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002248 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002249 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002250 }
2251
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002252 /**
2253 * Event handler for the wallpaper picker button that appears after a long press
2254 * on the home screen.
2255 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002256 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002257 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002258 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2259 return;
2260 }
2261
Tony Wickham785f7a52015-08-31 17:28:32 -07002262 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2263 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002264 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002265 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002266 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002267
2268 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002269 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2270 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002271 intent.setPackage(pickerPackage);
2272 }
2273
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002274 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002275 try {
2276 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2277 // If there is no target package, use the default intent chooser animation
2278 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2279 } catch (ActivityNotFoundException e) {
2280 setWaitingForResult(null);
2281 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2282 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002283 }
2284
2285 /**
2286 * Event handler for a click on the settings button that appears after a long press
2287 * on the home screen.
2288 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002289 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002290 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002291 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2292 .setPackage(getPackageName());
2293 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002294 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002295 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002296 }
2297
Tony Wickhame2217252016-03-22 16:34:23 -07002298 @Override
2299 public void onAccessibilityStateChanged(boolean enabled) {
2300 mDragLayer.onAccessibilityStateChanged(enabled);
2301 }
2302
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002303 /**
2304 * Called when the user stops interacting with the launcher.
2305 * This implies that the user is now on the homescreen and is not doing housekeeping.
2306 */
Adam Cohen9211d422014-10-07 18:14:53 -07002307 protected void onInteractionEnd() {
2308 if (mLauncherCallbacks != null) {
2309 mLauncherCallbacks.onInteractionEnd();
2310 }
2311 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002312
2313 /**
2314 * Called when the user starts interacting with the launcher.
2315 * The possible interactions are:
2316 * - open all apps
2317 * - reorder an app shortcut, or a widget
2318 * - open the overview mode.
2319 * This is a good time to stop doing things that only make sense
2320 * when the user is on the homescreen and not doing housekeeping.
2321 */
Adam Cohen9211d422014-10-07 18:14:53 -07002322 protected void onInteractionBegin() {
2323 if (mLauncherCallbacks != null) {
2324 mLauncherCallbacks.onInteractionBegin();
2325 }
2326 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002327
Winson Chungcd99cd32015-04-29 11:03:24 -07002328 /** Updates the interaction state. */
2329 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002330 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002331 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002332 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2333 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002334 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002335 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002336 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002337 onInteractionEnd();
2338 }
2339 }
2340
Sunny Goyala7ce1662016-05-31 15:01:35 -07002341 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002343 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2344 try {
2345 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2346 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2347 // is enabled by default on NYC.
2348 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2349 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002350
2351 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2352 String id = ((ShortcutInfo) info).getDeepShortcutId();
2353 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302354 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002355 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002356 } else {
2357 // Could be launching some bookkeeping activity
2358 startActivity(intent, optsBundle);
2359 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002360 } finally {
2361 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002364 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2365 // corresponding permission. Show the appropriate permission prompt if that
2366 // is the case.
2367 if (intent.getComponent() == null
2368 && Intent.ACTION_CALL.equals(intent.getAction())
2369 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2370 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002371
2372 setWaitingForResult(PendingRequestArgs
2373 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002374 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2375 REQUEST_PERMISSION_CALL_PHONE);
2376 } else {
2377 // No idea why this was thrown.
2378 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 }
2381 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002382
Sunny Goyalfe770c92016-09-27 14:38:58 -07002383 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002384 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002385 if (Utilities.ATLEAST_MARSHMALLOW) {
2386 int left = 0, top = 0;
2387 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002388 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002389 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002390 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002391 if (icon != null) {
2392 Rect bounds = icon.getBounds();
2393 left = (width - bounds.width()) / 2;
2394 top = v.getPaddingTop();
2395 width = bounds.width();
2396 height = bounds.height();
2397 }
2398 }
2399 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2400 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2401 // On L devices, we use the device default slide-up transition.
2402 // On L MR1 devices, we use a custom version of the slide-up transition which
2403 // doesn't have the delay present in the device default.
2404 return ActivityOptions.makeCustomAnimation(
2405 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2406 }
2407 return null;
2408 }
2409
Tonye3c59252017-05-02 21:32:27 -07002410 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002411 int[] pos = new int[2];
2412 v.getLocationOnScreen(pos);
2413 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2414 }
2415
Sunny Goyala7ce1662016-05-31 15:01:35 -07002416 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002417 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2418 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2419 return false;
2420 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002421 // Only launch using the new animation if the shortcut has not opted out (this is a
2422 // private contract between launcher and may be ignored in the future).
2423 boolean useLaunchAnimation = (v != null) &&
2424 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2425 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2426
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002427 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002428
2429 // Prepare intent
2430 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2431 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002432 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002433 }
2434 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002435 if (Utilities.ATLEAST_MARSHMALLOW
2436 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002437 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002438 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002439 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002440 // Shortcuts need some special checks due to legacy reasons.
2441 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002442 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002443 // Could be launching some bookkeeping activity
2444 startActivity(intent, optsBundle);
2445 } else {
2446 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2447 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2448 }
2449 return true;
2450 } catch (ActivityNotFoundException|SecurityException e) {
2451 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2452 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2453 }
2454 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002455 }
2456
Tony Wickhamdadb3042016-02-24 11:07:00 -08002457 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002458 public boolean dispatchTouchEvent(MotionEvent ev) {
2459 mLastDispatchTouchEventX = ev.getX();
2460 return super.dispatchTouchEvent(ev);
2461 }
2462
2463 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002465 if (!isDraggingEnabled()) return false;
2466 if (isWorkspaceLocked()) return false;
2467 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468
Jon Miranda51f037d2016-11-07 08:37:20 -08002469 boolean ignoreLongPressToOverview =
2470 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002471
Adam Cohen1697b792013-09-17 19:08:21 -07002472 if (v instanceof Workspace) {
2473 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002474 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302475 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2476 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002477 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002478 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002479 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2480 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2481 return true;
2482 } else {
2483 return false;
2484 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002485 } else {
2486 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002487 }
Adam Cohen1697b792013-09-17 19:08:21 -07002488 }
2489
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002490 CellLayout.CellInfo longClickCellInfo = null;
2491 View itemUnderLongClick = null;
2492 if (v.getTag() instanceof ItemInfo) {
2493 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002494 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002495 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002496 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 }
2498
Winson Chung3d503fb2011-07-13 17:25:49 -07002499 // The hotseat touch handling does not go through Workspace, and we always allow long press
2500 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002501 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002502 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002503 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002504 if (mWorkspace.isInOverviewMode()) {
2505 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302506 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2507 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002508 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002509 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002510 return false;
2511 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302512 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2513 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002514 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002515 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002516 }
Jon Miranda379198e2016-09-23 08:54:40 -07002517 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2518 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002520 final boolean isAllAppsButton =
2521 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2522 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2523 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002524 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002526 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 }
2528 }
2529 }
2530 return true;
2531 }
2532
Winson Chung3d503fb2011-07-13 17:25:49 -07002533 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002534 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002535 return mHotseat != null && layout != null &&
2536 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2537 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002538
Winson Chung3d503fb2011-07-13 17:25:49 -07002539 /**
2540 * Returns the CellLayout of the specified container at the specified screen.
2541 */
Sunny Goyal92820592015-03-02 11:31:35 -08002542 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002543 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2544 if (mHotseat != null) {
2545 return mHotseat.getLayout();
2546 } else {
2547 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002548 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002549 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002550 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002551 }
2552 }
2553
Winson Chungb745afb2015-03-02 11:51:23 -08002554 /**
2555 * For overridden classes.
2556 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002557 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002558 return isAppsViewVisible();
2559 }
2560
2561 public boolean isAppsViewVisible() {
2562 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2563 }
2564
2565 public boolean isWidgetsViewVisible() {
2566 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002567 }
2568
Michael Jurkae326f182011-11-21 14:05:46 -08002569 @Override
2570 public void onTrimMemory(int level) {
2571 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002572 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2573 // The widget preview db can result in holding onto over
2574 // 3MB of memory for caching which isn't necessary.
2575 SQLiteDatabase.releaseMemory();
2576
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002577 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002578 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002579 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002580 if (mLauncherCallbacks != null) {
2581 mLauncherCallbacks.onTrimMemory(level);
2582 }
Michael Jurkae326f182011-11-21 14:05:46 -08002583 }
2584
Winson5c6bdbb2015-09-03 11:36:19 -07002585 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002586 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002587 }
2588
Winson5c6bdbb2015-09-03 11:36:19 -07002589 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002590 boolean changed = mState != State.WORKSPACE ||
2591 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002592 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002593 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002594 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002595 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002596
Michael Jurkab3e22d92011-10-31 15:58:33 -07002597 // Set focus to the AppsCustomize button
2598 if (mAllAppsButton != null) {
2599 mAllAppsButton.requestFocus();
2600 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002601 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002602
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002603 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002604 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002605
Winson Chung0f785722015-04-08 10:27:49 -07002606 if (changed) {
2607 // Send an accessibility event to announce the context change
2608 getWindow().getDecorView()
2609 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002610 }
Winson5c6bdbb2015-09-03 11:36:19 -07002611 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002612 }
2613
Winsone9f27272015-10-13 10:47:51 -07002614 /**
2615 * Shows the overview button.
2616 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002617 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002618 showOverviewMode(animated, false);
2619 }
2620
2621 /**
2622 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2623 * onto one of the overview panel buttons.
2624 */
2625 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2626 Runnable postAnimRunnable = null;
2627 if (requestButtonFocus) {
2628 postAnimRunnable = new Runnable() {
2629 @Override
2630 public void run() {
2631 // Hitting the menu button when in touch mode does not trigger touch mode to
2632 // be disabled, so if requested, force focus on one of the overview panel
2633 // buttons.
2634 mOverviewPanel.requestFocusFromTouch();
2635 }
2636 };
2637 }
Adam Cohened307df2013-10-02 09:37:31 -07002638 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002639 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002640 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002641 setState(State.WORKSPACE);
2642
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002643 // If animated from long press, then don't allow any of the controller in the drag
2644 // layer to intercept any remaining touch.
2645 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002646 }
2647
Mario Bertschleracbf5702017-03-03 10:58:06 -08002648 private void setState(State state) {
2649 this.mState = state;
2650 updateSoftInputMode();
2651 }
2652
2653 private void updateSoftInputMode() {
2654 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2655 final int mode;
2656 if (isAppsViewVisible()) {
2657 mode = SOFT_INPUT_MODE_ALL_APPS;
2658 } else {
2659 mode = SOFT_INPUT_MODE_DEFAULT;
2660 }
2661 getWindow().setSoftInputMode(mode);
2662 }
2663 }
2664
Winson Chungb745afb2015-03-02 11:51:23 -08002665 /**
2666 * Shows the apps view.
2667 */
Sunny Goyal5606e072017-06-15 14:24:21 -07002668 public void showAppsView(boolean animated, boolean updatePredictedApps) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002669 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002670 if (updatePredictedApps) {
2671 tryAndUpdatePredictedApps();
2672 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002673 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002674 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002675
Winson Chungb745afb2015-03-02 11:51:23 -08002676 /**
2677 * Shows the widgets view.
2678 */
2679 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002680 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002681 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002682 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002683 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002684 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002685
2686 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002687 @Override
2688 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002689 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002690 }
2691 });
Winson Chungb745afb2015-03-02 11:51:23 -08002692 }
2693
2694 /**
2695 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002696 *
2697 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002698 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002699 // TODO: calling method should use the return value so that when {@code false} is returned
2700 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002701 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002702 if (!(mState == State.WORKSPACE ||
2703 mState == State.APPS_SPRING_LOADED ||
2704 mState == State.WIDGETS_SPRING_LOADED ||
2705 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002706 return false;
2707 }
2708 if (toState != State.APPS && toState != State.WIDGETS) {
2709 return false;
2710 }
Winson Chungb745afb2015-03-02 11:51:23 -08002711
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002712 // This is a safe and supported transition to bypass spring_loaded mode.
2713 if (mExitSpringLoadedModeRunnable != null) {
2714 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2715 mExitSpringLoadedModeRunnable = null;
2716 }
2717
Winson Chungb745afb2015-03-02 11:51:23 -08002718 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002719 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002720 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002721 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002722 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002723
Michael Jurkab3e22d92011-10-31 15:58:33 -07002724 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002725 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002726 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002727
2728 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002729 getWindow().getDecorView()
2730 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002731 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002732 }
2733
Winson Chungcd99cd32015-04-29 11:03:24 -07002734 /**
2735 * Updates the workspace and interaction state on state change, and return the animation to this
2736 * new state.
2737 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002738 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002739 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002740 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002741 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002742 updateInteraction(fromState, toState);
2743 return anim;
2744 }
2745
Hyunyoung Song3f471442015-04-08 19:01:34 -07002746 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002747 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002748 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002749 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002750 }
Winson Chungb745afb2015-03-02 11:51:23 -08002751
Winson Chung006ee262015-08-03 14:40:11 -07002752 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002753 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002754 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002755 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002756 }
Adam Cohen7777d962011-08-18 18:58:38 -07002757
Hyunyoung Song3f471442015-04-08 19:01:34 -07002758 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002759 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002760 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002761
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002762 if (mExitSpringLoadedModeRunnable != null) {
2763 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2764 }
2765 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002766 @Override
2767 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002768 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002769 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2770 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002771 // Before we show workspace, hide all apps again because
2772 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2773 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002774 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002775 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002776 } else {
2777 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002778 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002779 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002780 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002781 };
2782 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002783 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002784
Tony Wickhame0c33232016-02-08 11:37:04 -08002785 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002786 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2787 || mState == State.WIDGETS_SPRING_LOADED;
2788 }
2789
Sunny Goyal0f76b562016-12-13 19:37:10 -08002790 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002791 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002792 showAppsView(true /* animated */, false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08002793 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002794 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002795 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2796 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002797 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002798 }
2799
Winson Chung4ac30062015-05-08 17:34:17 -07002800 /**
2801 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
2802 * resumed.
2803 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07002804 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07002805 if (mLauncherCallbacks != null) {
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002806 List<ComponentKeyMapper<AppInfo>> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07002807 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07002808 mAppsView.setPredictedApps(apps);
2809 }
2810 }
2811 }
2812
Michael Jurkad7c28052012-04-27 15:43:36 -07002813 @Override
2814 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002815 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002816 final List<CharSequence> text = event.getText();
2817 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002818 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002819 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002820 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002821 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002822 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002823 } else if (mWorkspace != null) {
2824 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002825 } else {
2826 text.add(getString(R.string.all_apps_home_button_label));
2827 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002828 return result;
2829 }
2830
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002831 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002832 * If the activity is currently paused, signal that we need to run the passed Runnable
2833 * in onResume.
2834 *
2835 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002836 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2837 * wrong when we're not running, and if the activity comes back to what the configuration was
2838 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002839 *
2840 * Implementation of the method from LauncherModel.Callbacks.
2841 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002842 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002843 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002844 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002845 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002846 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002847 if (run instanceof RunnableWithId) {
2848 // Remove any runnables which have the same id
2849 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002850 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002851 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002852 return true;
2853 } else {
2854 return false;
2855 }
2856 }
2857
Michael Jurka1e2f4652013-07-08 18:03:46 -07002858 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02002859 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002860 }
2861
Michael Jurka7607c2f2013-04-03 14:33:19 -07002862 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002863 * If the activity is currently paused, signal that we need to re-run the loader
2864 * in onResume.
2865 *
2866 * This needs to be called from incoming places where resources might have been loaded
2867 * while we are paused. That is becaues the Configuration might be wrong
2868 * when we're not running, and if it comes back to what it was when we
2869 * were paused, we are not restarted.
2870 *
2871 * Implementation of the method from LauncherModel.Callbacks.
2872 *
2873 * @return true if we are currently paused. The caller might be able to
2874 * skip some work in that case since we will come back again.
2875 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002876 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002877 public boolean setLoadOnResume() {
2878 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002879 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002880 mOnResumeNeedsLoad = true;
2881 return true;
2882 } else {
2883 return false;
2884 }
2885 }
2886
2887 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002890 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002892 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002893 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002894 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002895 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002898
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002900 * Clear any pending bind callbacks. This is called when is loader is planning to
2901 * perform a full rebind from scratch.
2902 */
2903 @Override
2904 public void clearPendingBinds() {
2905 mBindOnResumeCallbacks.clear();
2906 if (mPendingExecutor != null) {
2907 mPendingExecutor.markCompleted();
2908 mPendingExecutor = null;
2909 }
2910 }
2911
2912 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 * Refreshes the shortcuts shown on the workspace.
2914 *
2915 * Implementation of the method from LauncherModel.Callbacks.
2916 */
2917 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002918 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002919 AbstractFloatingView.closeAllOpenViews(this);
2920
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002921 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002922
Winson Chungd64d1762013-08-20 14:37:16 -07002923 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002924 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002925 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002926
Winson Chung3d503fb2011-07-13 17:25:49 -07002927 if (mHotseat != null) {
2928 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002930 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 }
2932
Adam Cohendcd297f2013-06-18 13:13:40 -07002933 @Override
2934 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002935 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002936 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2937 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002938 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2939 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002940 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002941 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2942 // If there are no screens, we need to have an empty screen
2943 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002944 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002945 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002946
Winsonc7d2e832016-07-28 12:24:55 -07002947 // After we have added all the screens, if the wallpaper was locked to the default state,
2948 // then notify to indicate that it can be released and a proper wallpaper offset can be
2949 // computed before the next layout
2950 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002951 }
2952
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002953 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002954 int count = orderedScreenIds.size();
2955 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002956 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002957 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002958 // No need to bind the first screen, as its always bound.
2959 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2960 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002961 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002962 }
2963
Sunny Goyalb23980c2017-08-17 07:45:25 -07002964 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002965 public void bindAppsAdded(final ArrayList<Long> newScreens,
2966 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002967 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002968 Runnable r = new Runnable() {
2969 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002970 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002971 }
2972 };
2973 if (waitUntilResume(r)) {
2974 return;
2975 }
2976
Winson Chungd64d1762013-08-20 14:37:16 -07002977 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002978 if (newScreens != null) {
2979 bindAddScreens(newScreens);
2980 }
Winson Chungd64d1762013-08-20 14:37:16 -07002981
2982 // We add the items without animation on non-visible pages, and with
2983 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002984 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002985 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002986 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002987 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002988 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002989 }
Winson Chungc58497e2013-09-03 17:48:37 -07002990
Winson Chung87412982013-10-03 18:34:14 -07002991 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002992 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002993 }
2994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002996 * Bind the items start-end from the list.
2997 *
2998 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003000 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07003001 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003002 Runnable r = new Runnable() {
3003 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003004 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003005 }
3006 };
3007 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003008 return;
3009 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003010
Sunny Goyal3be633b2016-12-08 09:59:25 -08003011 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003012 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003013 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07003014 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003015 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003016 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07003017 int end = items.size();
3018 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003019 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003020
3021 // Short circuit if we are loading dock items for a configuration which has no dock
3022 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3023 mHotseat == null) {
3024 continue;
3025 }
3026
Sunny Goyal639e9062015-08-19 19:17:06 -07003027 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003028 switch (item.itemType) {
3029 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3030 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003031 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003032 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003033 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003034 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003035 }
3036 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003037 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003038 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003039 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003041 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07003042 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
3043 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07003044 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003045 if (view == null) {
3046 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003047 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08003048 break;
3049 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003050 default:
3051 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003053
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003054 /*
3055 * Remove colliding items.
3056 */
3057 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3058 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3059 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3060 View v = cl.getChildAt(item.cellX, item.cellY);
3061 Object tag = v.getTag();
3062 String desc = "Collision while binding workspace item: " + item
3063 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003064 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003065 throw (new RuntimeException(desc));
3066 } else {
3067 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003068 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003069 continue;
3070 }
3071 }
3072 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303073 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003074 if (animateIcons) {
3075 // Animate all the applications up now
3076 view.setAlpha(0f);
3077 view.setScaleX(0f);
3078 view.setScaleY(0f);
3079 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003080 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003083
Winson Chung997a9232013-07-24 15:33:46 -07003084 if (animateIcons) {
3085 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003086 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003087 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003088 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003089 final Runnable startBounceAnimRunnable = new Runnable() {
3090 public void run() {
3091 anim.playTogether(bounceAnims);
3092 anim.start();
3093 }
3094 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003095 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003096 // We post the animation slightly delayed to prevent slowdowns
3097 // when we are loading right after we return to launcher.
3098 mWorkspace.postDelayed(new Runnable() {
3099 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003100 if (mWorkspace != null) {
3101 mWorkspace.snapToPage(newScreenIndex);
3102 mWorkspace.postDelayed(startBounceAnimRunnable,
3103 NEW_APPS_ANIMATION_DELAY);
3104 }
Winson Chung94d67682013-09-25 16:29:40 -07003105 }
3106 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003107 } else {
3108 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003109 }
3110 }
Winson Chung64359a52013-07-08 17:17:08 -07003111 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113 }
3114
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 /**
3116 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003118 public void bindAppWidget(LauncherAppWidgetInfo item) {
3119 View view = inflateAppWidget(item);
3120 if (view != null) {
3121 mWorkspace.addInScreen(view, item);
3122 mWorkspace.requestLayout();
3123 }
3124 }
3125
3126 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003127 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303128 PendingAppWidgetHostView view =
3129 new PendingAppWidgetHostView(this, item, mIconCache, true);
3130 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003131 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003132 }
3133
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003134 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003135
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003136 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003137
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003138 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3139 // If the provider is not ready, bind as a pending widget.
3140 appWidgetInfo = null;
3141 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3142 // The widget id is not valid. Try to find the widget based on the provider info.
3143 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3144 } else {
3145 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3146 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003147
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003148 // If the provider is ready, but the width is not yet restored, try to restore it.
3149 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3150 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003151 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003152 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3153 + " belongs to component " + item.providerName
3154 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003155 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003156 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003157 }
Sunny Goyalff572272014-07-23 13:58:07 -07003158
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003159 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003160 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003161 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3162 // Id has not been allocated yet. Allocate a new id.
3163 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3164 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003165
Sunny Goyald478c832016-04-01 12:04:16 -07003166 // Also try to bind the widget. If the bind fails, the user will be shown
3167 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003168 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003169 pendingInfo.spanX = item.spanX;
3170 pendingInfo.spanY = item.spanY;
3171 pendingInfo.minSpanX = item.minSpanX;
3172 pendingInfo.minSpanY = item.minSpanY;
3173 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003174
3175 boolean isDirectConfig =
3176 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3177 if (isDirectConfig && item.bindOptions != null) {
3178 Bundle newOptions = item.bindOptions.getExtras();
3179 if (options != null) {
3180 newOptions.putAll(options);
3181 }
3182 options = newOptions;
3183 }
Sunny Goyald478c832016-04-01 12:04:16 -07003184 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3185 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003186
Sunny Goyal86df1382016-08-10 15:03:22 -07003187 // We tried to bind once. If we were not able to bind, we would need to
3188 // go through the permission dialog, which means we cannot skip the config
3189 // activity.
3190 item.bindOptions = null;
3191 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3192
Sunny Goyald478c832016-04-01 12:04:16 -07003193 // Bind succeeded
3194 if (success) {
3195 // If the widget has a configure activity, it is still needs to set it up,
3196 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003197 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003198 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3199 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003200 }
Sunny Goyald478c832016-04-01 12:04:16 -07003201
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003202 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003203 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003204 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003205 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003206 // The widget was marked as UI not ready, but there is no configure activity to
3207 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003208 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003209 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003210 }
Sunny Goyalff572272014-07-23 13:58:07 -07003211 }
3212
Sunny Goyald5462aa2016-11-22 16:52:39 +05303213 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003214 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003215 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003216 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003217 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003218 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003219 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003220 }
3221
Sunny Goyal233ee962015-08-03 13:05:01 -07003222 item.minSpanX = appWidgetInfo.minSpanX;
3223 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303224 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003225 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303226 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003227 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303228 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003229
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003230 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003231 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 }
3233
Sunny Goyalff572272014-07-23 13:58:07 -07003234 /**
3235 * Restores a pending widget.
3236 *
3237 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003238 */
Sunny Goyald478c832016-04-01 12:04:16 -07003239 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003240 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3241 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3242 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003243 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003244 }
3245
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003246 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003247 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003248 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3249 info.pendingItemInfo = null;
3250 }
Sunny Goyalff572272014-07-23 13:58:07 -07003251
Sunny Goyalba47faa2017-10-04 16:50:57 -07003252 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3253 view.reinflate();
3254 }
3255
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003256 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003257 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003258 }
3259
Adam Cohen1462de32012-07-24 22:34:36 -07003260 public void onPageBoundSynchronously(int page) {
3261 mSynchronouslyBoundPages.add(page);
3262 }
3263
Sunny Goyal527c7d32015-08-28 15:19:36 -07003264 @Override
3265 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3266 if (mPendingExecutor != null) {
3267 mPendingExecutor.markCompleted();
3268 }
3269 mPendingExecutor = executor;
3270 executor.attachTo(this);
3271 }
3272
3273 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3274 if (mPendingExecutor == executor) {
3275 mPendingExecutor = null;
3276 }
3277 }
3278
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003279 @Override
3280 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3281 Runnable r = new Runnable() {
3282 public void run() {
3283 finishFirstPageBind(executor);
3284 }
3285 };
3286 if (waitUntilResume(r)) {
3287 return;
3288 }
3289
3290 Runnable onComplete = new Runnable() {
3291 @Override
3292 public void run() {
3293 if (executor != null) {
3294 executor.onLoadAnimationCompleted();
3295 }
3296 }
3297 };
3298 if (mDragLayer.getAlpha() < 1) {
3299 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3300 } else {
3301 onComplete.run();
3302 }
3303 }
3304
Joe Onorato9c1289c2009-08-17 11:03:03 -04003305 /**
3306 * Callback saying that there aren't any more items to bind.
3307 *
3308 * Implementation of the method from LauncherModel.Callbacks.
3309 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003310 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003311 Runnable r = new Runnable() {
3312 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003313 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003314 }
3315 };
3316 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003317 return;
3318 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003319 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003320 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003321
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003322 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003323
Sunny Goyal2100c782016-08-22 16:00:03 -07003324 if (mPendingActivityResult != null) {
3325 handleActivityResult(mPendingActivityResult.requestCode,
3326 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3327 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003328 }
3329
Sunny Goyala474a9b2017-05-04 16:47:11 -07003330 InstallShortcutReceiver.disableAndFlushInstallQueue(
3331 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003332
Tony Wickham010d2552017-01-20 08:15:28 -08003333 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3334
Adam Cohen9211d422014-10-07 18:14:53 -07003335 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003336 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003337 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003338 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003339 }
3340
Winson Chunga2413752012-04-03 14:22:34 -07003341 private boolean canRunNewAppsAnimation() {
3342 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003343 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003344 }
3345
Winson Chungc9168342013-06-26 14:54:55 -07003346 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003347 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003348 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3349 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003350 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003351 return bounceAnim;
3352 }
3353
Adam Cohen27772732013-11-11 14:00:56 +00003354 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003355 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003356 }
3357
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003358 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 * Add the icons for all apps.
3360 *
3361 * Implementation of the method from LauncherModel.Callbacks.
3362 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003363 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003364 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3365 public void run() {
3366 bindAllApplications(apps);
3367 }
3368 };
3369 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003370 return;
3371 }
3372
Winson Chungb745afb2015-03-02 11:51:23 -08003373 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003374 Executor pendingExecutor = getPendingExecutor();
3375 if (pendingExecutor != null && mState != State.APPS) {
3376 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003377 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003378 return;
3379 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003380
Winson Chungb745afb2015-03-02 11:51:23 -08003381 mAppsView.setApps(apps);
3382 }
Adam Cohen9211d422014-10-07 18:14:53 -07003383 if (mLauncherCallbacks != null) {
3384 mLauncherCallbacks.bindAllApplications(apps);
3385 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386 }
3387
3388 /**
Tony2917a8b2017-07-31 23:29:42 -07003389 * Returns an Executor that will run after the launcher is first drawn (including after the
3390 * initial fade in animation). Returns null if the first draw has already occurred.
3391 */
3392 public @Nullable Executor getPendingExecutor() {
3393 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3394 }
3395
3396 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003397 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3398 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3399 */
3400 @Override
3401 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003402 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003403 }
3404
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003405 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 * A package was updated.
3407 *
3408 * Implementation of the method from LauncherModel.Callbacks.
3409 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003410 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003411 Runnable r = new Runnable() {
3412 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003413 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003414 }
3415 };
3416 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003417 return;
3418 }
3419
Winson Chungb745afb2015-03-02 11:51:23 -08003420 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003421 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 }
3424
Sunny Goyal4390ace2014-10-13 11:33:11 -07003425 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003426 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3427 Runnable r = new Runnable() {
3428 public void run() {
3429 bindPromiseAppProgressUpdated(app);
3430 }
3431 };
3432 if (waitUntilResume(r)) {
3433 return;
3434 }
3435
3436 if (mAppsView != null) {
3437 mAppsView.updatePromiseAppProgress(app);
3438 }
3439 }
3440
3441 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003442 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3443 Runnable r = new Runnable() {
3444 public void run() {
3445 bindWidgetsRestored(widgets);
3446 }
3447 };
3448 if (waitUntilResume(r)) {
3449 return;
3450 }
3451 mWorkspace.widgetsRestored(widgets);
3452 }
3453
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003455 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003456 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003457 *
3458 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003459 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003460 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003461 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003462 Runnable r = new Runnable() {
3463 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003464 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003465 }
3466 };
3467 if (waitUntilResume(r)) {
3468 return;
3469 }
3470
Sunny Goyal4390ace2014-10-13 11:33:11 -07003471 if (!updated.isEmpty()) {
3472 mWorkspace.updateShortcuts(updated);
3473 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003474 }
3475
3476 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003477 * Update the state of a package, typically related to install state.
3478 *
3479 * Implementation of the method from LauncherModel.Callbacks.
3480 */
Sunny Goyale755d462014-07-22 13:48:29 -07003481 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003482 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3483 Runnable r = new Runnable() {
3484 public void run() {
3485 bindRestoreItemsChange(updates);
3486 }
3487 };
3488 if (waitUntilResume(r)) {
3489 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003490 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003491
Sunny Goyal756adbc2015-04-16 15:20:51 -07003492 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003493 }
3494
3495 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003496 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003497 * in addition to specific applications to remove, the reason being that
3498 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003499 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003500 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003502 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003503 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003504 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003505 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003506 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003507 }
Winson Chungd64d1762013-08-20 14:37:16 -07003508 };
3509 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003510 return;
3511 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003512 mWorkspace.removeItemsByMatcher(matcher);
3513 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003514 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003515
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003516 @Override
3517 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3518 Runnable r = new Runnable() {
3519 public void run() {
3520 bindAppInfosRemoved(appInfos);
3521 }
3522 };
3523 if (waitUntilResume(r)) {
3524 return;
Joe Onorato36115782010-06-17 13:28:48 -04003525 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003526
Winson Chungdf95eb12013-10-16 14:57:07 -07003527 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003528 if (mAppsView != null) {
3529 mAppsView.removeApps(appInfos);
Hyunyoung Song51114f22017-05-02 16:06:28 -07003530 tryAndUpdatePredictedApps();
Winson Chungc58497e2013-09-03 17:48:37 -07003531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 }
Joe Onoratobe386092009-11-17 17:32:16 -08003533
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003534 @Override
3535 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3536 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3537 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003538 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003539 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003540 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003541 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003542 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003543 return;
3544 }
3545
Sunny Goyald164b7f2016-10-12 20:49:31 -07003546 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003547 Executor pendingExecutor = getPendingExecutor();
3548 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003549 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003550 return;
3551 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003552 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003553 }
Tony Wickham26b17462017-03-20 17:12:24 -07003554
3555 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3556 if (topView != null) {
3557 topView.onWidgetsBound();
3558 }
3559 }
3560
3561 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3562 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003563 }
3564
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003565 @Override
3566 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003567 if (mWorkspace.getState().shouldUpdateWidget) {
3568 refreshAndBindWidgetsForPackageUser(null);
3569 }
Tony Wickham26b17462017-03-20 17:12:24 -07003570 }
3571
Tony Wickham86222d22017-03-29 15:30:43 -07003572 /**
3573 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3574 * refreshes the widgets and shortcuts associated with the given package/user
3575 */
3576 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003577 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003578 }
3579
Winson Chung4b919f82012-05-01 10:44:08 -07003580 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003581 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003582 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003583 }
3584 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003585
Winson Chung4b919f82012-05-01 10:44:08 -07003586 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003587 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003588 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003589 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003590 } else {
3591 mHandler.postDelayed(new Runnable() {
3592 public void run() {
3593 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3594 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003595 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003596 }
Winson Chung4b919f82012-05-01 10:44:08 -07003597 }
Winson Chung400438b2011-01-16 17:53:48 -08003598 }
3599
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003600 private void markAppsViewShown() {
3601 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3602 return;
3603 }
3604 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3605 }
3606
3607 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003608 UserManagerCompat um = UserManagerCompat.getInstance(this);
3609 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003610 }
3611
Winson Chung80baf5a2010-08-09 16:03:15 -07003612 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003613 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003614 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003615 @Override
3616 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3617 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003618
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003619 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3620 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003621 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003622 writer.println(prefix + " Homescreen " + i);
3623
3624 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3625 for (int j = 0; j < layout.getChildCount(); j++) {
3626 Object tag = layout.getChildAt(j).getTag();
3627 if (tag != null) {
3628 writer.println(prefix + " " + tag.toString());
3629 }
3630 }
3631 }
3632
3633 writer.println(prefix + " Hotseat");
3634 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003635 for (int j = 0; j < layout.getChildCount(); j++) {
3636 Object tag = layout.getChildAt(j).getTag();
3637 if (tag != null) {
3638 writer.println(prefix + " " + tag.toString());
3639 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003640 }
Sunny Goyala1365452015-10-01 15:46:24 -07003641
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003642 try {
3643 FileLog.flushAll(writer);
3644 } catch (Exception e) {
3645 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003646 }
3647 }
3648
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003649 writer.println(prefix + "Misc:");
3650 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3651 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3652 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3653
3654 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003655
Adam Cohen9211d422014-10-07 18:14:53 -07003656 if (mLauncherCallbacks != null) {
3657 mLauncherCallbacks.dump(prefix, fd, writer, args);
3658 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003659 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003660
Sunny Goyal66b24572016-09-21 15:57:55 -07003661 @Override
3662 @TargetApi(Build.VERSION_CODES.N)
3663 public void onProvideKeyboardShortcuts(
3664 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3665
3666 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3667 if (mState == State.WORKSPACE) {
3668 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3669 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3670 }
3671 View currentFocus = getCurrentFocus();
3672 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3673 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3674 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3675 }
Tony18c4aa42017-05-10 21:23:57 -05003676 if (currentFocus.getTag() instanceof ItemInfo
3677 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003678 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3679 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3680 }
3681 if (!shortcutInfos.isEmpty()) {
3682 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3683 }
3684
3685 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3686 }
3687
3688 @Override
3689 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3690 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3691 switch (keyCode) {
3692 case KeyEvent.KEYCODE_A:
3693 if (mState == State.WORKSPACE) {
Sunny Goyal5606e072017-06-15 14:24:21 -07003694 showAppsView(true, true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003695 return true;
3696 }
3697 break;
3698 case KeyEvent.KEYCODE_S: {
3699 View focusedView = getCurrentFocus();
3700 if (focusedView instanceof BubbleTextView
3701 && focusedView.getTag() instanceof ItemInfo
3702 && mAccessibilityDelegate.performAction(focusedView,
3703 (ItemInfo) focusedView.getTag(),
3704 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003705 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003706 return true;
3707 }
3708 break;
3709 }
3710 case KeyEvent.KEYCODE_O:
3711 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3712 return true;
3713 }
3714 break;
3715 }
3716 }
3717 return super.onKeyShortcut(keyCode, event);
3718 }
3719
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003720 public static Launcher getLauncher(Context context) {
3721 if (context instanceof Launcher) {
3722 return (Launcher) context;
3723 }
3724 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3725 }
3726
Sunny Goyal5a81c382017-03-20 15:08:06 -07003727 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003728
3729 @Override
3730 public void onSharedPreferenceChanged(
3731 SharedPreferences sharedPreferences, String key) {
3732 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003733 // Recreate the activity so that it initializes the rotation preference again.
3734 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003735 }
3736 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003738}