blob: ba31926c28eac0bcaa3f55b58c49a128c731f1ca [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) {
Sunny Goyald0d07032017-10-04 10:40:28 -0700870 showAppsView(false /* animated */);
Winson Chungb745afb2015-03-02 11:51:23 -0800871 } else if (mOnResumeState == State.WIDGETS) {
872 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700873 }
874 mOnResumeState = State.NONE;
875
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800876 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700877 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700878 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700879 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700880 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700882 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200883 // We might have postponed some bind calls until onResume (see waitUntilResume) --
884 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700885 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
886 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200887 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700888 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200889 }
Michael Jurka54554252013-08-01 12:52:23 +0200890 if (mOnResumeCallbacks.size() > 0) {
891 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
892 mOnResumeCallbacks.get(i).run();
893 }
894 mOnResumeCallbacks.clear();
895 }
Winson Chunge4e50662012-01-23 14:45:13 -0800896
897 // Reset the pressed state of icons that were locked in the press state while activities
898 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800899 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800900 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800901 mWaitingForResume.setStayPressed(false);
902 }
Winson Chung82963d52013-10-09 11:20:57 -0700903
Winson Chungcd99cd32015-04-29 11:03:24 -0700904 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Sunny Goyale755d462014-07-22 13:48:29 -0700905
Sunny Goyala474a9b2017-05-04 16:47:11 -0700906 // Process any items that were added while Launcher was away.
907 InstallShortcutReceiver.disableAndFlushInstallQueue(
908 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700909
Sunny Goyala474a9b2017-05-04 16:47:11 -0700910 // Refresh shortcuts if the permission changed.
911 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700912
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700913 if (shouldShowDiscoveryBounce()) {
914 mAllAppsController.showDiscoveryBounce();
915 }
Adam Cohen9211d422014-10-07 18:14:53 -0700916 if (mLauncherCallbacks != null) {
917 mLauncherCallbacks.onResume();
918 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800919
Sunny Goyala502aa32017-10-02 16:04:06 -0700920 clearTypedText();
921
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700922 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700923 }
924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700926 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700927 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700928 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700929
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700930 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700931 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800932 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700933 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700934
Adam Cohen9211d422014-10-07 18:14:53 -0700935 if (mLauncherCallbacks != null) {
936 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700937 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700938 }
939
Adam Cohenc2d6e892014-10-16 09:49:24 -0700940 public interface LauncherOverlay {
941
942 /**
943 * Touch interaction leading to overscroll has begun
944 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700945 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700946
947 /**
948 * Touch interaction related to overscroll has ended
949 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700950 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700951
952 /**
953 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
954 * screen (or in the case of RTL, the rightmost screen).
955 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700956 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700957
958 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800959 * Called when the launcher is ready to use the overlay
960 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700961 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700962 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700963 }
964
965 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700966 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700967 }
968
969 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
970
Sunny Goyalc86df472016-02-25 09:19:38 -0800971 public void onScrollChanged(float progress) {
972 if (mWorkspace != null) {
973 mWorkspace.onOverlayScrollChanged(progress);
974 }
975 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700976 }
977
Jorim Jaggid017f882014-01-14 17:08:48 -0800978 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700979 if (mLauncherCallbacks != null) {
980 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700981 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800982 // On O and above we there is always some setting present settings (add icon to
983 // home screen or icon badging). On earlier APIs we will have the allow rotation
984 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700985 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700986 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800987 }
988
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700989 @Override
990 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700992 if (mModel.isCurrentCallbacks(this)) {
993 mModel.stopLoader();
994 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700995 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
996
Romain Guy13c2e7b2010-03-10 19:45:00 -0800997 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700998 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700999
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001000 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001001 @Override
1002 public void onWindowFocusChanged(boolean hasFocus) {
1003 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001004 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001005
1006 if (mLauncherCallbacks != null) {
1007 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1008 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001009 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 private boolean acceptFilter() {
1012 final InputMethodManager inputManager = (InputMethodManager)
1013 getSystemService(Context.INPUT_METHOD_SERVICE);
1014 return !inputManager.isFullscreenMode();
1015 }
1016
1017 @Override
1018 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001019 final int uniChar = event.getUnicodeChar();
1020 final boolean handled = super.onKeyDown(keyCode, event);
1021 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1022 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1024 keyCode, event);
1025 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001026 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001027 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001028 // showSearchDialog()
1029 // If there are multiple keystrokes before the search dialog takes focus,
1030 // onSearchRequested() will be called for every keystroke,
1031 // but it is idempotent, so it's fine.
1032 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 }
1034 }
1035
Joe Onorato8a9625e2010-01-28 15:55:35 -08001036 // Eat the long press event so the keyboard doesn't come up.
1037 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1038 return true;
1039 }
1040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 return handled;
1042 }
1043
Winson46163472015-09-22 17:31:56 -07001044 @Override
1045 public boolean onKeyUp(int keyCode, KeyEvent event) {
1046 if (keyCode == KeyEvent.KEYCODE_MENU) {
1047 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001048 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001049 // Close any open floating view
1050 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001051
Winson46163472015-09-22 17:31:56 -07001052 // Show the overview mode if we are on the workspace
1053 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1054 !mWorkspace.isSwitchingState()) {
1055 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001056 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001057 }
1058 }
1059 return true;
1060 }
1061 return super.onKeyUp(keyCode, event);
1062 }
1063
Karl Rosaen138a0412009-04-23 19:00:21 -07001064 private String getTypedText() {
1065 return mDefaultKeySsb.toString();
1066 }
1067
Sunny Goyal6f28e712016-09-13 14:19:29 -07001068 @Override
1069 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 mDefaultKeySsb.clear();
1071 mDefaultKeySsb.clearSpans();
1072 Selection.setSelection(mDefaultKeySsb, 0);
1073 }
1074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 /**
1076 * Restores the previous state, if it exists.
1077 *
1078 * @param savedState The previous state.
1079 */
1080 private void restoreState(Bundle savedState) {
1081 if (savedState == null) {
1082 return;
1083 }
1084
Sunny Goyalfe770c92016-09-27 14:38:58 -07001085 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1086 State[] stateValues = State.values();
1087 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1088 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001089 if (state == State.APPS || state == State.WIDGETS) {
1090 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001091 }
1092
Sunny Goyal2100c782016-08-22 16:00:03 -07001093 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1094 if (requestArgs != null) {
1095 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001097
1098 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100
1101 /**
1102 * Finds all the views we need and configure them properly.
1103 */
1104 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001105 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001106 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001107 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001108 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001109
Sunny Goyal784f9c32016-03-23 16:56:54 -07001110 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1111 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1112 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
Winson Chung4c98d922011-05-31 16:50:48 -07001114 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001115 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001116
Winson Chung3d503fb2011-07-13 17:25:49 -07001117 // Setup the hotseat
1118 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1119 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001120 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 }
1122
Winsone9f27272015-10-13 10:47:51 -07001123 // Setup the overview panel
1124 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001125
Winson Chung4c98d922011-05-31 16:50:48 -07001126 // Setup the workspace
1127 mWorkspace.setHapticFeedbackEnabled(false);
1128 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001129 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001130 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1131 // default state, otherwise we will update to the wrong offsets in RTL
1132 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001133 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001134 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001135
Tony Wickham34d2c912015-09-11 09:27:58 -07001136 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001137 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001138
Winson Chungef7f8742015-06-04 17:18:17 -07001139 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001140 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001141 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001142
Winson Chung3d503fb2011-07-13 17:25:49 -07001143 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001144 mDragController.setMoveTarget(mWorkspace);
1145 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001146 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001147
Hyunyoung Songd725f642017-08-17 22:26:35 -07001148 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001149
Sunny Goyal322d5562015-06-25 19:35:49 -07001150 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1151 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154
Winsone9f27272015-10-13 10:47:51 -07001155 private void setupOverviewPanel() {
1156 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1157
Winsone9f27272015-10-13 10:47:51 -07001158 // Bind wallpaper button actions
1159 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301160 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001161 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001162 public void handleViewClick(View view) {
1163 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001164 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001165 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001166
1167 // Bind widget button actions
1168 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301169 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001170 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001171 public void handleViewClick(View view) {
1172 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001173 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001174 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001175
1176 // Bind settings actions
1177 View settingsButton = findViewById(R.id.settings_button);
1178 boolean hasSettings = hasSettings();
1179 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301180 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001181 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001182 public void handleViewClick(View view) {
1183 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001184 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001185 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001186 } else {
1187 settingsButton.setVisibility(View.GONE);
1188 }
1189
1190 mOverviewPanel.setAlpha(0f);
1191 }
1192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001194 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001195 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001196 */
1197 public void setAllAppsButton(View allAppsButton) {
1198 mAllAppsButton = allAppsButton;
1199 }
1200
Sunny Goyalbb011da2016-06-15 15:42:29 -07001201 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001202 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001203 }
1204
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001205 public View getWidgetsButton() {
1206 return mWidgetsButton;
1207 }
1208
Anjali Koppal5ad44842014-03-10 20:34:39 -07001209 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 * Creates a view representing a shortcut.
1211 *
1212 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001214 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001215 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217
1218 /**
1219 * Creates a view representing a shortcut inflated from the specified resource.
1220 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 * @param parent The group the shortcut belongs to.
1222 * @param info The data structure describing the shortcut.
1223 *
1224 * @return A View inflated from layoutResId.
1225 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001226 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001227 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1228 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001229 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001231 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 return favorite;
1233 }
1234
1235 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001236 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 *
1238 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001240 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001241 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001242 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1243 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001244 return;
1245 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001246
Jon Mirandac476d6e2017-05-04 16:30:01 -07001247 int[] cellXY = mTmpAddItemCellCoordinates;
1248 CellLayout layout = getCellLayout(container, screenId);
1249
Sunny Goyal782f0c92017-01-19 10:27:54 -08001250 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001251 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001252 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1253 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001254 }
1255
1256 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001257 // Legacy shortcuts are only supported for primary profile.
1258 info = Process.myUserHandle().equals(args.user)
1259 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001260
Sunny Goyalb57645f2017-03-20 13:57:28 -07001261 if (info == null) {
1262 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1263 return;
1264 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001265 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001266 // The app is trying to add a shortcut without sufficient permissions
1267 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1268 return;
1269 }
1270 }
1271
Jon Mirandac476d6e2017-05-04 16:30:01 -07001272 if (container < 0) {
1273 // Adding a shortcut to the Workspace.
1274 final View view = createShortcut(info);
1275 boolean foundCellSpan = false;
1276 // First we check if we already know the exact location where we want to add this item.
1277 if (cellX >= 0 && cellY >= 0) {
1278 cellXY[0] = cellX;
1279 cellXY[1] = cellY;
1280 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001281
Jon Mirandac476d6e2017-05-04 16:30:01 -07001282 // If appropriate, either create a folder or add to an existing folder
1283 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1284 true, null, null)) {
1285 return;
1286 }
1287 DragObject dragObject = new DragObject();
1288 dragObject.dragInfo = info;
1289 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1290 true)) {
1291 return;
1292 }
1293 } else {
1294 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1295 }
1296
1297 if (!foundCellSpan) {
1298 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001299 return;
1300 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001301
1302 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1303 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001304 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001305 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001306 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001307 if (folderIcon != null) {
1308 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1309 folderInfo.add(info, args.rank, false);
1310 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001311 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001312 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
Sunny Goyale29897f2017-07-20 10:09:42 -07001316 public FolderIcon findFolderIcon(final long folderIconId) {
1317 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1318 @Override
1319 public boolean evaluate(ItemInfo info, View view) {
1320 return info != null && info.id == folderIconId;
1321 }
1322 });
1323 }
1324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001326 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001328 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001330 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001331 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1332
Adam Cohened66b2b2012-01-23 17:28:51 -08001333 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001334 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001335 }
Romain Guycbb89e42009-06-08 15:52:54 -07001336
Adam Cohen59400422014-03-05 18:07:04 -08001337 LauncherAppWidgetInfo launcherInfo;
1338 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001339 launcherInfo.spanX = itemInfo.spanX;
1340 launcherInfo.spanY = itemInfo.spanY;
1341 launcherInfo.minSpanX = itemInfo.minSpanX;
1342 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001343 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001344
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001345 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001346 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347
Sunny Goyal2100c782016-08-22 16:00:03 -07001348 if (hostView == null) {
1349 // Perform actual inflation because we're live
1350 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001352 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301353 prepareAppWidget(hostView, launcherInfo);
1354 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
Romain Guycbb89e42009-06-08 15:52:54 -07001356
Sunny Goyald5462aa2016-11-22 16:52:39 +05301357 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001358 hostView.setTag(item);
1359 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001360 hostView.setFocusable(true);
1361 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001362 }
1363
Adam Cohended9f8d2010-11-03 13:25:16 -07001364 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1365 @Override
1366 public void onReceive(Context context, Intent intent) {
1367 final String action = intent.getAction();
1368 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001369 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001370 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001371 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001372 if (!showWorkspace(false)) {
1373 // If we are already on the workspace, then manually reset all apps
1374 mAppsView.reset();
1375 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001376 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001377 mShouldFadeInScrim = true;
1378 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1379 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1380 // the user unlocked and the Launcher is not in the foreground.
1381 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001382 }
1383 }
1384 };
1385
Tony Wickham010d2552017-01-20 08:15:28 -08001386 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1387 Runnable r = new Runnable() {
1388 @Override
1389 public void run() {
1390 mWorkspace.updateIconBadges(updatedBadges);
1391 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001392
1393 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1394 if (popup != null) {
1395 popup.updateNotificationHeader(updatedBadges);
1396 }
Tony Wickham010d2552017-01-20 08:15:28 -08001397 }
1398 };
1399 if (!waitUntilResume(r)) {
1400 r.run();
1401 }
1402 }
1403
Adam Cohended9f8d2010-11-03 13:25:16 -07001404 @Override
1405 public void onAttachedToWindow() {
1406 super.onAttachedToWindow();
1407
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001408 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001409 if (mLauncherCallbacks != null) {
1410 mLauncherCallbacks.onAttachedToWindow();
1411 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001412 }
1413
1414 @Override
1415 public void onDetachedFromWindow() {
1416 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001417
1418 if (mLauncherCallbacks != null) {
1419 mLauncherCallbacks.onDetachedFromWindow();
1420 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001421 }
1422
Winson Chunga6945242014-01-08 14:04:34 -08001423 public DragLayer getDragLayer() {
1424 return mDragLayer;
1425 }
1426
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001427 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001428 return mAppsView;
1429 }
1430
Hyunyoung Song3f471442015-04-08 19:01:34 -07001431 public WidgetsContainerView getWidgetsView() {
1432 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001433 }
1434
Winson Chunga6945242014-01-08 14:04:34 -08001435 public Workspace getWorkspace() {
1436 return mWorkspace;
1437 }
1438
1439 public Hotseat getHotseat() {
1440 return mHotseat;
1441 }
1442
Jorim Jaggid017f882014-01-14 17:08:48 -08001443 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001444 return mOverviewPanel;
1445 }
1446
Sunny Goyal47328fd2016-05-25 18:56:41 -07001447 public DropTargetBar getDropTargetBar() {
1448 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001449 }
1450
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001451 public LauncherAppWidgetHost getAppWidgetHost() {
1452 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
Romain Guycbb89e42009-06-08 15:52:54 -07001454
Winson Chunga9abd0e2010-10-27 17:18:37 -07001455 public LauncherModel getModel() {
1456 return mModel;
1457 }
1458
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001459 public ModelWriter getModelWriter() {
1460 return mModelWriter;
1461 }
1462
Adam Cohen79d90c52016-04-22 13:29:20 -07001463 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001464 return mSharedPrefs;
1465 }
1466
Jon Miranda6bed3502017-09-19 14:43:17 -07001467 public int getOrientation() { return mOrientation; }
1468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 @Override
1470 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001471 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 super.onNewIntent(intent);
1473
Winson15f8b172015-08-19 11:02:39 -07001474 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1475 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1476 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001477
1478 // Check this condition before handling isActionMain, as this will get reset.
1479 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001480 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001481
Winson15f8b172015-08-19 11:02:39 -07001482 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1483 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001484 if (mWorkspace == null) {
1485 // Can be cases where mWorkspace is null, this prevents a NPE
1486 return;
1487 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001488
1489 // Note: There should be at most one log per method call. This is enforced implicitly
1490 // by using if-else statements.
1491 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001492 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001493 if (topOpenView != null) {
1494 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001495 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301496 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001497 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1498 }
1499
1500 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001501 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001502 exitSpringLoadedDragMode();
1503
1504 // If we are already on home, then just animate back to the workspace,
1505 // otherwise, just wait until onResume to set the state back to Workspace
1506 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001507 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001508 } else {
1509 mOnResumeState = State.WORKSPACE;
1510 }
1511
1512 final View v = getWindow().peekDecorView();
1513 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001514 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001515 }
1516
Winson Chungb745afb2015-03-02 11:51:23 -08001517 // Reset the apps view
1518 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001519 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001520 }
1521
Hyunyoung Song3f471442015-04-08 19:01:34 -07001522 // Reset the widgets view
1523 if (!alreadyOnHome && mWidgetsView != null) {
1524 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001525 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001526
Adam Cohen9211d422014-10-07 18:14:53 -07001527 if (mLauncherCallbacks != null) {
1528 mLauncherCallbacks.onHomeIntent();
1529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001531 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001532
Adam Cohen9211d422014-10-07 18:14:53 -07001533 if (mLauncherCallbacks != null) {
1534 mLauncherCallbacks.onNewIntent(intent);
1535 }
Winson15f8b172015-08-19 11:02:39 -07001536
1537 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1538 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1539 // animation.
1540 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001541 boolean callbackAllowsMoveToDefaultScreen =
1542 mLauncherCallbacks == null || mLauncherCallbacks
1543 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001544 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1545 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001546
Winson15f8b172015-08-19 11:02:39 -07001547 mWorkspace.post(new Runnable() {
1548 @Override
1549 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001550 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001551 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001552 }
Winson15f8b172015-08-19 11:02:39 -07001553 }
1554 });
1555 }
1556 }
1557
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001558 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001559 }
1560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001562 public void onRestoreInstanceState(Bundle state) {
1563 super.onRestoreInstanceState(state);
1564 for (int page: mSynchronouslyBoundPages) {
1565 mWorkspace.restoreInstanceStateForChild(page);
1566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568
1569 @Override
1570 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001571 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001572 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001573
Adam Cohen21cd0022013-10-09 18:57:02 -07001574 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001575 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576
Michael Jurka883f55b2010-10-21 15:47:14 -07001577 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001578 // We close any open folders and shortcut containers since they will not be re-opened,
1579 // and we need to make sure this state is reflected.
1580 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001581
Sunny Goyal2100c782016-08-22 16:00:03 -07001582 if (mPendingRequestArgs != null) {
1583 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1584 }
1585 if (mPendingActivityResult != null) {
1586 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588
Adam Cohen9211d422014-10-07 18:14:53 -07001589 if (mLauncherCallbacks != null) {
1590 mLauncherCallbacks.onSaveInstanceState(outState);
1591 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
1593
1594 @Override
1595 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001597
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001598 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001599 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001600
Winson Chungcd2b0142011-06-08 16:02:26 -07001601 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001602 // It's possible to receive onDestroy after a new Launcher activity has
1603 // been created. In this case, don't interfere with the new Launcher.
1604 if (mModel.isCurrentCallbacks(this)) {
1605 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001606 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001607 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001608
Sunny Goyal745bad92016-05-02 10:54:12 -07001609 if (mRotationPrefChangeHandler != null) {
1610 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1611 }
1612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001614 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001616 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001618 mAppWidgetHost = null;
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 TextKeyListener.getInstance().release();
1621
Tony Wickhame2217252016-03-22 16:34:23 -07001622 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1623 .removeAccessibilityStateChangeListener(this);
1624
Mario Bertschler27288382017-05-24 15:35:09 -07001625 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1626
Michael Jurka2ecf9952012-06-18 12:52:28 -07001627 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001628
Tony2917a8b2017-07-31 23:29:42 -07001629 clearPendingBinds();
1630
Adam Cohen9211d422014-10-07 18:14:53 -07001631 if (mLauncherCallbacks != null) {
1632 mLauncherCallbacks.onDestroy();
1633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
1635
Sunny Goyalae502842016-06-17 08:43:56 -07001636 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1637 return mAccessibilityDelegate;
1638 }
1639
Adam Cohena9cf38f2011-05-02 15:36:58 -07001640 public DragController getDragController() {
1641 return mDragController;
1642 }
1643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001645 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001646 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001647 }
1648
1649 @Override
1650 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1651 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001652 try {
1653 super.startIntentSenderForResult(intent, requestCode,
1654 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1655 } catch (IntentSender.SendIntentException e) {
1656 throw new ActivityNotFoundException();
1657 }
1658 }
1659
Winson Chung70d72102011-08-12 11:24:35 -07001660 /**
1661 * Indicates that we want global search for this activity by setting the globalSearch
1662 * argument for {@link #startSearch} to true.
1663 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001665 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001667
Karl Rosaen138a0412009-04-23 19:00:21 -07001668 if (initialQuery == null) {
1669 // Use any text typed in the launcher as the initial query
1670 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 if (appSearchData == null) {
1673 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001674 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001676
Sunny Goyal6f28e712016-09-13 14:19:29 -07001677 if (mLauncherCallbacks == null ||
1678 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1679 // Starting search from the callbacks failed. Start the default global search.
1680 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001681 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001682
1683 // We need to show the workspace after starting the search
1684 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001685 }
1686
Ian Parkinson047036e2014-09-01 15:40:53 +01001687 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001688 * Starts the global search activity. This code is a copied from SearchManager
1689 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001690 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001691 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001692 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001693 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1694 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1695 if (globalSearchActivity == null) {
1696 Log.w(TAG, "No global search activity found.");
1697 return;
1698 }
1699 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1700 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1701 intent.setComponent(globalSearchActivity);
1702 // Make sure that we have a Bundle to put source in
1703 if (appSearchData == null) {
1704 appSearchData = new Bundle();
1705 } else {
1706 appSearchData = new Bundle(appSearchData);
1707 }
Adam Cohen9211d422014-10-07 18:14:53 -07001708 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001709 if (!appSearchData.containsKey("source")) {
1710 appSearchData.putString("source", getPackageName());
1711 }
1712 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1713 if (!TextUtils.isEmpty(initialQuery)) {
1714 intent.putExtra(SearchManager.QUERY, initialQuery);
1715 }
1716 if (selectInitialQuery) {
1717 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1718 }
1719 intent.setSourceBounds(sourceBounds);
1720 try {
1721 startActivity(intent);
1722 } catch (ActivityNotFoundException ex) {
1723 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 }
1726
Winson Chung70d72102011-08-12 11:24:35 -07001727 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001728 public boolean onPrepareOptionsMenu(Menu menu) {
1729 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001730 if (mLauncherCallbacks != null) {
1731 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1732 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001733 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001736 @Override
1737 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001738 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001739 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001740 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001741 }
1742
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001744 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 }
1746
Adam Cohen517a7f52014-03-01 12:12:59 -08001747 public boolean isWorkspaceLoading() {
1748 return mWorkspaceLoading;
1749 }
1750
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001751 private void setWorkspaceLoading(boolean value) {
1752 boolean isLocked = isWorkspaceLocked();
1753 mWorkspaceLoading = value;
1754 if (isLocked != isWorkspaceLocked()) {
1755 onWorkspaceLockedChanged();
1756 }
1757 }
1758
Sunny Goyal04a324a2017-01-20 21:08:59 -08001759 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001760 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001761 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001762 if (isLocked != isWorkspaceLocked()) {
1763 onWorkspaceLockedChanged();
1764 }
1765 }
1766
Adam Cohen9211d422014-10-07 18:14:53 -07001767 protected void onWorkspaceLockedChanged() {
1768 if (mLauncherCallbacks != null) {
1769 mLauncherCallbacks.onWorkspaceLockedChanged();
1770 }
1771 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001772
Sunny Goyal2100c782016-08-22 16:00:03 -07001773 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001774 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001775 if (LOGD) {
1776 Log.d(TAG, "Adding widget from drop");
1777 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001778 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001779 }
1780
Sunny Goyal2100c782016-08-22 16:00:03 -07001781 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001782 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1783 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1784 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001785
Adam Cohenad4e15c2013-10-17 16:21:35 -07001786 Runnable onComplete = new Runnable() {
1787 @Override
1788 public void run() {
1789 // Exit spring loaded mode if necessary after adding the widget
1790 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1791 null);
1792 }
1793 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001794 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001795 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001799 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1800 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001801 info.container = container;
1802 info.screenId = screenId;
1803 if (cell != null) {
1804 info.cellX = cell[0];
1805 info.cellY = cell[1];
1806 }
1807 info.spanX = spanX;
1808 info.spanY = spanY;
1809
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001810 switch (info.itemType) {
1811 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1812 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001813 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001814 break;
1815 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001816 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001817 break;
1818 default:
1819 throw new IllegalStateException("Unknown item type: " + info.itemType);
1820 }
1821 }
1822
Adam Cohenfbba09b2011-07-18 21:43:05 -07001823 /**
1824 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001825 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001826 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001827 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1828 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001829 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1830 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1831 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001832 }
1833
Adam Cohenfbba09b2011-07-18 21:43:05 -07001834 /**
1835 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001836 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001837 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001838 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001839 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001840 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001841 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001842 // In the case where we've prebound the widget, we remove it from the DragLayer
1843 if (LOGD) {
1844 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1845 }
1846 getDragLayer().removeView(hostView);
1847
Adam Cohened66b2b2012-01-23 17:28:51 -08001848 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001849 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001850
1851 // Clear the boundWidget so that it doesn't get destroyed.
1852 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001853 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001854 // In this case, we either need to start an activity to get permission to bind
1855 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001856 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1857 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1858 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1859 this, info.componentName);
1860 } else {
1861 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1862 }
Adam Cohendd70d662012-10-04 16:53:44 -07001863 Bundle options = info.bindOptions;
1864
Sunny Goyalffe83f12014-08-14 17:39:34 -07001865 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1866 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001867 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001868 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001869 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001870 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001871 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001872 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001873 }
1874
Adam Cohendcd297f2013-06-18 13:13:40 -07001875 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001876 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001877 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 folderInfo.title = getText(R.string.folder_name);
1879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001881 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882
1883 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001884 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301885 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001886 // Force measure the new folder icon
1887 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1888 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001889 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
Romain Guycbb89e42009-06-08 15:52:54 -07001891
Winsonc0b52fe2015-09-09 16:38:15 -07001892 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001893 * Unbinds the view for the specified item, and removes the item and all its children.
1894 *
1895 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001896 * @param itemInfo the {@link ItemInfo} for this view.
1897 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001898 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001899 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001900 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001901 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001902 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1903 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001904 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001905 } else {
1906 mWorkspace.removeWorkspaceItem(v);
1907 }
Winsonc0b52fe2015-09-09 16:38:15 -07001908 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001909 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001910 }
1911 } else if (itemInfo instanceof FolderInfo) {
1912 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001913 if (v instanceof FolderIcon) {
1914 ((FolderIcon) v).removeListeners();
1915 }
Winsonc0b52fe2015-09-09 16:38:15 -07001916 mWorkspace.removeWorkspaceItem(v);
1917 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001918 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001919 }
1920 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1921 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001922 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001923 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001924 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001925 }
1926 } else {
1927 return false;
1928 }
1929 return true;
1930 }
1931
1932 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001933 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001934 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001935 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001936 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001937 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001938 // Deleting an app widget ID is a void call but writes to disk before returning
1939 // to the caller...
1940 new AsyncTask<Void, Void, Void>() {
1941 public Void doInBackground(Void ... args) {
1942 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1943 return null;
1944 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001945 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001946 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001947 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001948 }
1949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 @Override
1951 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001952 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
Joe Onorato88ec0992009-11-19 13:16:06 -08001955 @Override
1956 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001957 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1958 return;
1959 }
1960
Sunny Goyal45478022015-06-08 16:52:41 -07001961 if (mDragController.isDragging()) {
1962 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001963 return;
1964 }
1965
Jon Mirandafeba90f2016-10-06 10:53:29 -07001966 // Note: There should be at most one log per method call. This is enforced implicitly
1967 // by using if-else statements.
1968 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001969 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1970 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001971 topView.onBackPressed();
Tony Wickham49c8d292016-07-01 11:44:34 -07001972 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301973 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08001974 showWorkspace(true);
1975 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301976 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08001977 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001978 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301979 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001980 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001981 } else {
1982 // Back button is a no-op here, but give at least some feedback for the button press
1983 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001984 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 * Launches the intent referred by the clicked shortcut.
1989 *
1990 * @param v The view representing the clicked shortcut.
1991 */
1992 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001993 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1994 // view has detached (it's possible for this to happen if the view is removed mid touch).
1995 if (v.getWindowToken() == null) {
1996 return;
1997 }
1998
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001999 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002000 return;
2001 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002002
Adam Cohen1697b792013-09-17 19:08:21 -07002003 if (v instanceof Workspace) {
2004 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08002005 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2006 LauncherLogProto.Action.Direction.NONE,
2007 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002008 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002009 }
2010 return;
2011 }
2012
2013 if (v instanceof CellLayout) {
2014 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002015 int page = mWorkspace.indexOfChild(v);
2016 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2017 LauncherLogProto.Action.Direction.NONE,
2018 LauncherLogProto.ContainerType.OVERVIEW, page);
2019 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002020 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002021 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002022 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002023 }
2024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002026 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002027 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002029 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002030 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002031 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07002032 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002033 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002034 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002035 } else if (tag instanceof AppInfo) {
2036 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002037 } else if (tag instanceof LauncherAppWidgetInfo) {
2038 if (v instanceof PendingAppWidgetHostView) {
2039 onClickPendingWidget((PendingAppWidgetHostView) v);
2040 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
2042 }
2043
Sunny Goyal70660032015-05-14 00:07:08 -07002044 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002045 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002046 return false;
2047 }
2048
Michael Jurkaaf442092010-06-10 17:01:57 -07002049 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002050 * Event handler for the app widget view which has not fully restored.
2051 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002052 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002053 if (mIsSafeModeEnabled) {
2054 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2055 return;
2056 }
2057
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002058 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002059 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002060 LauncherAppWidgetProviderInfo appWidgetInfo =
2061 mAppWidgetManager.findProvider(info.providerName, info.user);
2062 if (appWidgetInfo == null) {
2063 return;
2064 }
2065 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2066
Sunny Goyald478c832016-04-01 12:04:16 -07002067 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2068 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2069 // This should not happen, as we make sure that an Id is allocated during bind.
2070 return;
2071 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002072 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2073 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002074 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002075 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002076 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002077 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002078 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002079 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002080 }
2081 }
2082
2083 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002084 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2085 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2086 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002087 *
2088 * @param v The view that was clicked.
2089 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002090 protected void onClickAllAppsButton(View v) {
2091 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002092 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302093 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2094 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002095 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002096 } else {
2097 showWorkspace(true);
2098 }
2099 }
2100
Sunny Goyale6e72002017-01-12 16:55:36 -08002101 private void onClickPendingAppItem(final View v, final String packageName,
2102 boolean downloadStarted) {
2103 if (downloadStarted) {
2104 // If the download has started, simply direct to the market app.
2105 startMarketIntentForPackage(v, packageName);
2106 return;
2107 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002108 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002109 .setTitle(R.string.abandoned_promises_title)
2110 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002111 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2112 @Override
2113 public void onClick(DialogInterface dialogInterface, int i) {
2114 startMarketIntentForPackage(v, packageName);
2115 }
2116 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002117 .setNeutralButton(R.string.abandoned_clean_this,
2118 new DialogInterface.OnClickListener() {
2119 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002120 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002121 mWorkspace.removeAbandonedPromise(packageName, user);
2122 }
2123 })
2124 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002125 }
2126
2127 private void startMarketIntentForPackage(View v, String packageName) {
2128 ItemInfo item = (ItemInfo) v.getTag();
2129 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2130 boolean success = startActivitySafely(v, intent, item);
2131 if (success && v instanceof BubbleTextView) {
2132 mWaitingForResume = (BubbleTextView) v;
2133 mWaitingForResume.setStayPressed(true);
2134 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002135 }
2136
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002137 /**
2138 * Event handler for an app shortcut click.
2139 *
2140 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2141 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002142 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002143 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2144 Object tag = v.getTag();
2145 if (!(tag instanceof ShortcutInfo)) {
2146 throw new IllegalArgumentException("Input must be a Shortcut");
2147 }
2148
2149 // Open shortcut
2150 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002151
2152 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002153 if ((shortcut.isDisabled &
2154 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2155 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2156 // If the app is only disabled because of the above flags, launch activity anyway.
2157 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002158 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002159 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2160 // Use a message specific to this shortcut, if it has one.
2161 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2162 return;
2163 }
2164 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002165 int error = R.string.activity_not_available;
2166 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2167 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002168 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2169 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002170 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002171 }
2172 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2173 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002174 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002175 }
2176
Chris Wren40c5ed32014-06-24 18:24:23 -04002177 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002178 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002179 String packageName = shortcut.intent.getComponent() != null ?
2180 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2181 if (!TextUtils.isEmpty(packageName)) {
2182 onClickPendingAppItem(v, packageName,
2183 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2184 return;
2185 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002186 }
2187
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002188 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002189 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002190 }
2191
Sunny Goyala7ce1662016-05-31 15:01:35 -07002192 private void startAppShortcutOrInfoActivity(View v) {
2193 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002194 Intent intent;
2195 if (item instanceof PromiseAppInfo) {
2196 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2197 intent = promiseAppInfo.getMarketIntent();
2198 } else {
2199 intent = item.getIntent();
2200 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002201 if (intent == null) {
2202 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002203 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002204 boolean success = startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002205 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002206
2207 if (success && v instanceof BubbleTextView) {
2208 mWaitingForResume = (BubbleTextView) v;
2209 mWaitingForResume.setStayPressed(true);
2210 }
2211 }
2212
2213 /**
2214 * Event handler for a folder icon click.
2215 *
2216 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2217 */
2218 protected void onClickFolderIcon(View v) {
2219 if (LOGD) Log.d(TAG, "onClickFolder");
2220 if (!(v instanceof FolderIcon)){
2221 throw new IllegalArgumentException("Input must be a FolderIcon");
2222 }
2223
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002224 Folder folder = ((FolderIcon) v).getFolder();
2225 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002226 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002227 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002228 }
Michael Jurka5130e402011-10-13 04:55:35 -07002229 }
2230
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002231 /**
2232 * Event handler for the (Add) Widgets button that appears after a long press
2233 * on the home screen.
2234 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002235 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002236 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002237 if (mIsSafeModeEnabled) {
2238 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2239 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002240 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002241 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002242 }
2243
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002244 /**
2245 * Event handler for the wallpaper picker button that appears after a long press
2246 * on the home screen.
2247 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002248 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002249 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002250 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2251 return;
2252 }
2253
Tony Wickham785f7a52015-08-31 17:28:32 -07002254 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2255 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002256 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002257 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002258 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002259
2260 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002261 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2262 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002263 intent.setPackage(pickerPackage);
2264 }
2265
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002266 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002267 try {
2268 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2269 // If there is no target package, use the default intent chooser animation
2270 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2271 } catch (ActivityNotFoundException e) {
2272 setWaitingForResult(null);
2273 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2274 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002275 }
2276
2277 /**
2278 * Event handler for a click on the settings button that appears after a long press
2279 * on the home screen.
2280 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002281 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002282 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002283 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2284 .setPackage(getPackageName());
2285 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002286 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002287 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002288 }
2289
Tony Wickhame2217252016-03-22 16:34:23 -07002290 @Override
2291 public void onAccessibilityStateChanged(boolean enabled) {
2292 mDragLayer.onAccessibilityStateChanged(enabled);
2293 }
2294
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002295 /**
2296 * Called when the user stops interacting with the launcher.
2297 * This implies that the user is now on the homescreen and is not doing housekeeping.
2298 */
Adam Cohen9211d422014-10-07 18:14:53 -07002299 protected void onInteractionEnd() {
2300 if (mLauncherCallbacks != null) {
2301 mLauncherCallbacks.onInteractionEnd();
2302 }
2303 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002304
2305 /**
2306 * Called when the user starts interacting with the launcher.
2307 * The possible interactions are:
2308 * - open all apps
2309 * - reorder an app shortcut, or a widget
2310 * - open the overview mode.
2311 * This is a good time to stop doing things that only make sense
2312 * when the user is on the homescreen and not doing housekeeping.
2313 */
Adam Cohen9211d422014-10-07 18:14:53 -07002314 protected void onInteractionBegin() {
2315 if (mLauncherCallbacks != null) {
2316 mLauncherCallbacks.onInteractionBegin();
2317 }
2318 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002319
Winson Chungcd99cd32015-04-29 11:03:24 -07002320 /** Updates the interaction state. */
2321 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002322 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002323 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002324 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2325 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002326 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002327 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002328 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002329 onInteractionEnd();
2330 }
2331 }
2332
Sunny Goyala7ce1662016-05-31 15:01:35 -07002333 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002335 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2336 try {
2337 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2338 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2339 // is enabled by default on NYC.
2340 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2341 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002342
2343 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2344 String id = ((ShortcutInfo) info).getDeepShortcutId();
2345 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302346 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002347 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002348 } else {
2349 // Could be launching some bookkeeping activity
2350 startActivity(intent, optsBundle);
2351 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002352 } finally {
2353 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002356 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2357 // corresponding permission. Show the appropriate permission prompt if that
2358 // is the case.
2359 if (intent.getComponent() == null
2360 && Intent.ACTION_CALL.equals(intent.getAction())
2361 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2362 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002363
2364 setWaitingForResult(PendingRequestArgs
2365 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002366 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2367 REQUEST_PERMISSION_CALL_PHONE);
2368 } else {
2369 // No idea why this was thrown.
2370 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002371 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 }
2373 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002374
Sunny Goyalfe770c92016-09-27 14:38:58 -07002375 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002376 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002377 if (Utilities.ATLEAST_MARSHMALLOW) {
2378 int left = 0, top = 0;
2379 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002380 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002381 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002382 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002383 if (icon != null) {
2384 Rect bounds = icon.getBounds();
2385 left = (width - bounds.width()) / 2;
2386 top = v.getPaddingTop();
2387 width = bounds.width();
2388 height = bounds.height();
2389 }
2390 }
2391 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2392 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2393 // On L devices, we use the device default slide-up transition.
2394 // On L MR1 devices, we use a custom version of the slide-up transition which
2395 // doesn't have the delay present in the device default.
2396 return ActivityOptions.makeCustomAnimation(
2397 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2398 }
2399 return null;
2400 }
2401
Tonye3c59252017-05-02 21:32:27 -07002402 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002403 int[] pos = new int[2];
2404 v.getLocationOnScreen(pos);
2405 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2406 }
2407
Sunny Goyala7ce1662016-05-31 15:01:35 -07002408 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002409 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2410 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2411 return false;
2412 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002413 // Only launch using the new animation if the shortcut has not opted out (this is a
2414 // private contract between launcher and may be ignored in the future).
2415 boolean useLaunchAnimation = (v != null) &&
2416 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2417 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2418
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002419 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002420
2421 // Prepare intent
2422 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2423 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002424 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002425 }
2426 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002427 if (Utilities.ATLEAST_MARSHMALLOW
2428 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002429 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002430 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002431 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002432 // Shortcuts need some special checks due to legacy reasons.
2433 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002434 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002435 // Could be launching some bookkeeping activity
2436 startActivity(intent, optsBundle);
2437 } else {
2438 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2439 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2440 }
2441 return true;
2442 } catch (ActivityNotFoundException|SecurityException e) {
2443 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2444 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2445 }
2446 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002447 }
2448
Tony Wickhamdadb3042016-02-24 11:07:00 -08002449 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002450 public boolean dispatchTouchEvent(MotionEvent ev) {
2451 mLastDispatchTouchEventX = ev.getX();
2452 return super.dispatchTouchEvent(ev);
2453 }
2454
2455 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002457 if (!isDraggingEnabled()) return false;
2458 if (isWorkspaceLocked()) return false;
2459 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460
Jon Miranda51f037d2016-11-07 08:37:20 -08002461 boolean ignoreLongPressToOverview =
2462 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002463
Adam Cohen1697b792013-09-17 19:08:21 -07002464 if (v instanceof Workspace) {
2465 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002466 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302467 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2468 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002469 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002470 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002471 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2472 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2473 return true;
2474 } else {
2475 return false;
2476 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002477 } else {
2478 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002479 }
Adam Cohen1697b792013-09-17 19:08:21 -07002480 }
2481
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002482 CellLayout.CellInfo longClickCellInfo = null;
2483 View itemUnderLongClick = null;
2484 if (v.getTag() instanceof ItemInfo) {
2485 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002486 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002487 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002488 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 }
2490
Winson Chung3d503fb2011-07-13 17:25:49 -07002491 // The hotseat touch handling does not go through Workspace, and we always allow long press
2492 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002493 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002494 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002495 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002496 if (mWorkspace.isInOverviewMode()) {
2497 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302498 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2499 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002500 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002501 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002502 return false;
2503 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302504 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2505 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002506 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002507 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002508 }
Jon Miranda379198e2016-09-23 08:54:40 -07002509 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2510 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002512 final boolean isAllAppsButton =
2513 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2514 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2515 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002516 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002518 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 }
2520 }
2521 }
2522 return true;
2523 }
2524
Winson Chung3d503fb2011-07-13 17:25:49 -07002525 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002526 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002527 return mHotseat != null && layout != null &&
2528 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2529 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002530
Winson Chung3d503fb2011-07-13 17:25:49 -07002531 /**
2532 * Returns the CellLayout of the specified container at the specified screen.
2533 */
Sunny Goyal92820592015-03-02 11:31:35 -08002534 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002535 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2536 if (mHotseat != null) {
2537 return mHotseat.getLayout();
2538 } else {
2539 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002540 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002541 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002542 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002543 }
2544 }
2545
Winson Chungb745afb2015-03-02 11:51:23 -08002546 /**
2547 * For overridden classes.
2548 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002549 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002550 return isAppsViewVisible();
2551 }
2552
2553 public boolean isAppsViewVisible() {
2554 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2555 }
2556
2557 public boolean isWidgetsViewVisible() {
2558 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002559 }
2560
Michael Jurkae326f182011-11-21 14:05:46 -08002561 @Override
2562 public void onTrimMemory(int level) {
2563 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002564 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2565 // The widget preview db can result in holding onto over
2566 // 3MB of memory for caching which isn't necessary.
2567 SQLiteDatabase.releaseMemory();
2568
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002569 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002570 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002571 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002572 if (mLauncherCallbacks != null) {
2573 mLauncherCallbacks.onTrimMemory(level);
2574 }
Michael Jurkae326f182011-11-21 14:05:46 -08002575 }
2576
Winson5c6bdbb2015-09-03 11:36:19 -07002577 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002578 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002579 }
2580
Winson5c6bdbb2015-09-03 11:36:19 -07002581 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002582 boolean changed = mState != State.WORKSPACE ||
2583 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002584 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002585 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002586 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002587 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002588
Michael Jurkab3e22d92011-10-31 15:58:33 -07002589 // Set focus to the AppsCustomize button
2590 if (mAllAppsButton != null) {
2591 mAllAppsButton.requestFocus();
2592 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002593 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002594
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002595 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002596 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002597
Winson Chung0f785722015-04-08 10:27:49 -07002598 if (changed) {
2599 // Send an accessibility event to announce the context change
2600 getWindow().getDecorView()
2601 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002602 }
Winson5c6bdbb2015-09-03 11:36:19 -07002603 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002604 }
2605
Winsone9f27272015-10-13 10:47:51 -07002606 /**
2607 * Shows the overview button.
2608 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002609 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002610 showOverviewMode(animated, false);
2611 }
2612
2613 /**
2614 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2615 * onto one of the overview panel buttons.
2616 */
2617 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2618 Runnable postAnimRunnable = null;
2619 if (requestButtonFocus) {
2620 postAnimRunnable = new Runnable() {
2621 @Override
2622 public void run() {
2623 // Hitting the menu button when in touch mode does not trigger touch mode to
2624 // be disabled, so if requested, force focus on one of the overview panel
2625 // buttons.
2626 mOverviewPanel.requestFocusFromTouch();
2627 }
2628 };
2629 }
Adam Cohened307df2013-10-02 09:37:31 -07002630 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002631 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002632 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002633 setState(State.WORKSPACE);
2634
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002635 // If animated from long press, then don't allow any of the controller in the drag
2636 // layer to intercept any remaining touch.
2637 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002638 }
2639
Mario Bertschleracbf5702017-03-03 10:58:06 -08002640 private void setState(State state) {
2641 this.mState = state;
2642 updateSoftInputMode();
2643 }
2644
2645 private void updateSoftInputMode() {
2646 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2647 final int mode;
2648 if (isAppsViewVisible()) {
2649 mode = SOFT_INPUT_MODE_ALL_APPS;
2650 } else {
2651 mode = SOFT_INPUT_MODE_DEFAULT;
2652 }
2653 getWindow().setSoftInputMode(mode);
2654 }
2655 }
2656
Winson Chungb745afb2015-03-02 11:51:23 -08002657 /**
2658 * Shows the apps view.
2659 */
Sunny Goyald0d07032017-10-04 10:40:28 -07002660 public void showAppsView(boolean animated) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002661 markAppsViewShown();
Sunny Goyal5606e072017-06-15 14:24:21 -07002662 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002663 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002664
Winson Chungb745afb2015-03-02 11:51:23 -08002665 /**
2666 * Shows the widgets view.
2667 */
2668 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002669 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002670 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002671 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002672 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002673 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002674
2675 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002676 @Override
2677 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002678 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002679 }
2680 });
Winson Chungb745afb2015-03-02 11:51:23 -08002681 }
2682
2683 /**
2684 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002685 *
2686 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002687 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002688 // TODO: calling method should use the return value so that when {@code false} is returned
2689 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002690 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002691 if (!(mState == State.WORKSPACE ||
2692 mState == State.APPS_SPRING_LOADED ||
2693 mState == State.WIDGETS_SPRING_LOADED ||
2694 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002695 return false;
2696 }
2697 if (toState != State.APPS && toState != State.WIDGETS) {
2698 return false;
2699 }
Winson Chungb745afb2015-03-02 11:51:23 -08002700
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002701 // This is a safe and supported transition to bypass spring_loaded mode.
2702 if (mExitSpringLoadedModeRunnable != null) {
2703 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2704 mExitSpringLoadedModeRunnable = null;
2705 }
2706
Winson Chungb745afb2015-03-02 11:51:23 -08002707 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002708 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002709 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002710 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002711 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002712
Michael Jurkab3e22d92011-10-31 15:58:33 -07002713 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002714 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002715 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002716
2717 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002718 getWindow().getDecorView()
2719 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002720 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002721 }
2722
Winson Chungcd99cd32015-04-29 11:03:24 -07002723 /**
2724 * Updates the workspace and interaction state on state change, and return the animation to this
2725 * new state.
2726 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002727 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002728 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002729 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002730 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002731 updateInteraction(fromState, toState);
2732 return anim;
2733 }
2734
Hyunyoung Song3f471442015-04-08 19:01:34 -07002735 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002736 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002737 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002738 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002739 }
Winson Chungb745afb2015-03-02 11:51:23 -08002740
Winson Chung006ee262015-08-03 14:40:11 -07002741 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002742 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002743 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002744 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002745 }
Adam Cohen7777d962011-08-18 18:58:38 -07002746
Hyunyoung Song3f471442015-04-08 19:01:34 -07002747 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002748 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002749 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002750
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002751 if (mExitSpringLoadedModeRunnable != null) {
2752 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2753 }
2754 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002755 @Override
2756 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002757 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002758 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2759 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002760 // Before we show workspace, hide all apps again because
2761 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2762 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002763 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002764 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002765 } else {
2766 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002767 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002768 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002769 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002770 };
2771 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002772 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002773
Tony Wickhame0c33232016-02-08 11:37:04 -08002774 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002775 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2776 || mState == State.WIDGETS_SPRING_LOADED;
2777 }
2778
Sunny Goyal0f76b562016-12-13 19:37:10 -08002779 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002780 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyald0d07032017-10-04 10:40:28 -07002781 showAppsView(true /* animated */);
Winson Chungb745afb2015-03-02 11:51:23 -08002782 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002783 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002784 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2785 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002786 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002787 }
2788
Michael Jurkad7c28052012-04-27 15:43:36 -07002789 @Override
2790 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002791 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002792 final List<CharSequence> text = event.getText();
2793 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002794 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002795 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002796 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002797 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002798 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002799 } else if (mWorkspace != null) {
2800 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002801 } else {
2802 text.add(getString(R.string.all_apps_home_button_label));
2803 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002804 return result;
2805 }
2806
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002807 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002808 * If the activity is currently paused, signal that we need to run the passed Runnable
2809 * in onResume.
2810 *
2811 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002812 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2813 * wrong when we're not running, and if the activity comes back to what the configuration was
2814 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002815 *
2816 * Implementation of the method from LauncherModel.Callbacks.
2817 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002818 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002819 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002820 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002821 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002822 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002823 if (run instanceof RunnableWithId) {
2824 // Remove any runnables which have the same id
2825 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002826 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002827 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002828 return true;
2829 } else {
2830 return false;
2831 }
2832 }
2833
Michael Jurka1e2f4652013-07-08 18:03:46 -07002834 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02002835 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002836 }
2837
Michael Jurka7607c2f2013-04-03 14:33:19 -07002838 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002839 * If the activity is currently paused, signal that we need to re-run the loader
2840 * in onResume.
2841 *
2842 * This needs to be called from incoming places where resources might have been loaded
2843 * while we are paused. That is becaues the Configuration might be wrong
2844 * when we're not running, and if it comes back to what it was when we
2845 * were paused, we are not restarted.
2846 *
2847 * Implementation of the method from LauncherModel.Callbacks.
2848 *
2849 * @return true if we are currently paused. The caller might be able to
2850 * skip some work in that case since we will come back again.
2851 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002852 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002853 public boolean setLoadOnResume() {
2854 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002855 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002856 mOnResumeNeedsLoad = true;
2857 return true;
2858 } else {
2859 return false;
2860 }
2861 }
2862
2863 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002866 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002868 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002869 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002870 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002871 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002872 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002874
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002876 * Clear any pending bind callbacks. This is called when is loader is planning to
2877 * perform a full rebind from scratch.
2878 */
2879 @Override
2880 public void clearPendingBinds() {
2881 mBindOnResumeCallbacks.clear();
2882 if (mPendingExecutor != null) {
2883 mPendingExecutor.markCompleted();
2884 mPendingExecutor = null;
2885 }
2886 }
2887
2888 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002889 * Refreshes the shortcuts shown on the workspace.
2890 *
2891 * Implementation of the method from LauncherModel.Callbacks.
2892 */
2893 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002894 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002895 AbstractFloatingView.closeAllOpenViews(this);
2896
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002897 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002898
Winson Chungd64d1762013-08-20 14:37:16 -07002899 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002900 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002901 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002902
Winson Chung3d503fb2011-07-13 17:25:49 -07002903 if (mHotseat != null) {
2904 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002906 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 }
2908
Adam Cohendcd297f2013-06-18 13:13:40 -07002909 @Override
2910 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002911 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002912 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2913 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002914 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2915 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002916 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002917 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2918 // If there are no screens, we need to have an empty screen
2919 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002920 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002921 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002922
Winsonc7d2e832016-07-28 12:24:55 -07002923 // After we have added all the screens, if the wallpaper was locked to the default state,
2924 // then notify to indicate that it can be released and a proper wallpaper offset can be
2925 // computed before the next layout
2926 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002927 }
2928
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002929 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002930 int count = orderedScreenIds.size();
2931 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002932 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002933 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002934 // No need to bind the first screen, as its always bound.
2935 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2936 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002937 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002938 }
2939
Sunny Goyalb23980c2017-08-17 07:45:25 -07002940 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002941 public void bindAppsAdded(final ArrayList<Long> newScreens,
2942 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002943 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002944 Runnable r = new Runnable() {
2945 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002946 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002947 }
2948 };
2949 if (waitUntilResume(r)) {
2950 return;
2951 }
2952
Winson Chungd64d1762013-08-20 14:37:16 -07002953 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002954 if (newScreens != null) {
2955 bindAddScreens(newScreens);
2956 }
Winson Chungd64d1762013-08-20 14:37:16 -07002957
2958 // We add the items without animation on non-visible pages, and with
2959 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002960 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002961 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002962 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002963 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002964 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002965 }
Winson Chungc58497e2013-09-03 17:48:37 -07002966
Winson Chung87412982013-10-03 18:34:14 -07002967 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002968 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002969 }
2970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972 * Bind the items start-end from the list.
2973 *
2974 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002976 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002977 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002978 Runnable r = new Runnable() {
2979 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002980 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002981 }
2982 };
2983 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002984 return;
2985 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002986
Sunny Goyal3be633b2016-12-08 09:59:25 -08002987 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002988 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002989 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002990 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002991 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002992 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002993 int end = items.size();
2994 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002995 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002996
2997 // Short circuit if we are loading dock items for a configuration which has no dock
2998 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2999 mHotseat == null) {
3000 continue;
3001 }
3002
Sunny Goyal639e9062015-08-19 19:17:06 -07003003 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 switch (item.itemType) {
3005 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3006 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003007 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003008 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003009 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003010 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003011 }
3012 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003013 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003014 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003015 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003016 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003017 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07003018 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
3019 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07003020 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003021 if (view == null) {
3022 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003023 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08003024 break;
3025 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003026 default:
3027 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003028 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003029
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003030 /*
3031 * Remove colliding items.
3032 */
3033 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3034 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3035 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3036 View v = cl.getChildAt(item.cellX, item.cellY);
3037 Object tag = v.getTag();
3038 String desc = "Collision while binding workspace item: " + item
3039 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003040 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003041 throw (new RuntimeException(desc));
3042 } else {
3043 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003044 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003045 continue;
3046 }
3047 }
3048 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303049 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003050 if (animateIcons) {
3051 // Animate all the applications up now
3052 view.setAlpha(0f);
3053 view.setScaleX(0f);
3054 view.setScaleY(0f);
3055 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003056 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003059
Winson Chung997a9232013-07-24 15:33:46 -07003060 if (animateIcons) {
3061 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003062 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003063 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003064 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003065 final Runnable startBounceAnimRunnable = new Runnable() {
3066 public void run() {
3067 anim.playTogether(bounceAnims);
3068 anim.start();
3069 }
3070 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003071 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003072 // We post the animation slightly delayed to prevent slowdowns
3073 // when we are loading right after we return to launcher.
3074 mWorkspace.postDelayed(new Runnable() {
3075 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003076 if (mWorkspace != null) {
3077 mWorkspace.snapToPage(newScreenIndex);
3078 mWorkspace.postDelayed(startBounceAnimRunnable,
3079 NEW_APPS_ANIMATION_DELAY);
3080 }
Winson Chung94d67682013-09-25 16:29:40 -07003081 }
3082 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003083 } else {
3084 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003085 }
3086 }
Winson Chung64359a52013-07-08 17:17:08 -07003087 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 }
3090
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 /**
3092 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003094 public void bindAppWidget(LauncherAppWidgetInfo item) {
3095 View view = inflateAppWidget(item);
3096 if (view != null) {
3097 mWorkspace.addInScreen(view, item);
3098 mWorkspace.requestLayout();
3099 }
3100 }
3101
3102 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003103 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303104 PendingAppWidgetHostView view =
3105 new PendingAppWidgetHostView(this, item, mIconCache, true);
3106 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003107 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003108 }
3109
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003110 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003112 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003113
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003114 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3115 // If the provider is not ready, bind as a pending widget.
3116 appWidgetInfo = null;
3117 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3118 // The widget id is not valid. Try to find the widget based on the provider info.
3119 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3120 } else {
3121 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3122 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003123
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003124 // If the provider is ready, but the width is not yet restored, try to restore it.
3125 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3126 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003127 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003128 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3129 + " belongs to component " + item.providerName
3130 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003131 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003132 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003133 }
Sunny Goyalff572272014-07-23 13:58:07 -07003134
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003135 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003136 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003137 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3138 // Id has not been allocated yet. Allocate a new id.
3139 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3140 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003141
Sunny Goyald478c832016-04-01 12:04:16 -07003142 // Also try to bind the widget. If the bind fails, the user will be shown
3143 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003144 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003145 pendingInfo.spanX = item.spanX;
3146 pendingInfo.spanY = item.spanY;
3147 pendingInfo.minSpanX = item.minSpanX;
3148 pendingInfo.minSpanY = item.minSpanY;
3149 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003150
3151 boolean isDirectConfig =
3152 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3153 if (isDirectConfig && item.bindOptions != null) {
3154 Bundle newOptions = item.bindOptions.getExtras();
3155 if (options != null) {
3156 newOptions.putAll(options);
3157 }
3158 options = newOptions;
3159 }
Sunny Goyald478c832016-04-01 12:04:16 -07003160 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3161 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003162
Sunny Goyal86df1382016-08-10 15:03:22 -07003163 // We tried to bind once. If we were not able to bind, we would need to
3164 // go through the permission dialog, which means we cannot skip the config
3165 // activity.
3166 item.bindOptions = null;
3167 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3168
Sunny Goyald478c832016-04-01 12:04:16 -07003169 // Bind succeeded
3170 if (success) {
3171 // If the widget has a configure activity, it is still needs to set it up,
3172 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003173 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003174 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3175 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003176 }
Sunny Goyald478c832016-04-01 12:04:16 -07003177
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003178 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003179 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003180 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003181 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003182 // The widget was marked as UI not ready, but there is no configure activity to
3183 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003184 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003185 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003186 }
Sunny Goyalff572272014-07-23 13:58:07 -07003187 }
3188
Sunny Goyald5462aa2016-11-22 16:52:39 +05303189 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003190 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003191 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003192 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003193 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003194 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003195 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003196 }
3197
Sunny Goyal233ee962015-08-03 13:05:01 -07003198 item.minSpanX = appWidgetInfo.minSpanX;
3199 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303200 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003201 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303202 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003203 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303204 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003206 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003207 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003208 }
3209
Sunny Goyalff572272014-07-23 13:58:07 -07003210 /**
3211 * Restores a pending widget.
3212 *
3213 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003214 */
Sunny Goyald478c832016-04-01 12:04:16 -07003215 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003216 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3217 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3218 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003219 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003220 }
3221
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003222 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003223 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003224 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3225 info.pendingItemInfo = null;
3226 }
Sunny Goyalff572272014-07-23 13:58:07 -07003227
Sunny Goyalba47faa2017-10-04 16:50:57 -07003228 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
3229 view.reinflate();
3230 }
3231
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003232 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003233 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003234 }
3235
Adam Cohen1462de32012-07-24 22:34:36 -07003236 public void onPageBoundSynchronously(int page) {
3237 mSynchronouslyBoundPages.add(page);
3238 }
3239
Sunny Goyal527c7d32015-08-28 15:19:36 -07003240 @Override
3241 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3242 if (mPendingExecutor != null) {
3243 mPendingExecutor.markCompleted();
3244 }
3245 mPendingExecutor = executor;
3246 executor.attachTo(this);
3247 }
3248
3249 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3250 if (mPendingExecutor == executor) {
3251 mPendingExecutor = null;
3252 }
3253 }
3254
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003255 @Override
3256 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3257 Runnable r = new Runnable() {
3258 public void run() {
3259 finishFirstPageBind(executor);
3260 }
3261 };
3262 if (waitUntilResume(r)) {
3263 return;
3264 }
3265
3266 Runnable onComplete = new Runnable() {
3267 @Override
3268 public void run() {
3269 if (executor != null) {
3270 executor.onLoadAnimationCompleted();
3271 }
3272 }
3273 };
3274 if (mDragLayer.getAlpha() < 1) {
3275 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3276 } else {
3277 onComplete.run();
3278 }
3279 }
3280
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 /**
3282 * Callback saying that there aren't any more items to bind.
3283 *
3284 * Implementation of the method from LauncherModel.Callbacks.
3285 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003286 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003287 Runnable r = new Runnable() {
3288 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003289 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003290 }
3291 };
3292 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003293 return;
3294 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003295 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003296 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003297
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003298 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003299
Sunny Goyal2100c782016-08-22 16:00:03 -07003300 if (mPendingActivityResult != null) {
3301 handleActivityResult(mPendingActivityResult.requestCode,
3302 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3303 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003304 }
3305
Sunny Goyala474a9b2017-05-04 16:47:11 -07003306 InstallShortcutReceiver.disableAndFlushInstallQueue(
3307 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003308
Tony Wickham010d2552017-01-20 08:15:28 -08003309 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3310
Adam Cohen9211d422014-10-07 18:14:53 -07003311 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003312 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003313 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003314 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003315 }
3316
Winson Chunga2413752012-04-03 14:22:34 -07003317 private boolean canRunNewAppsAnimation() {
3318 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003319 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003320 }
3321
Winson Chungc9168342013-06-26 14:54:55 -07003322 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003323 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003324 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3325 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003326 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003327 return bounceAnim;
3328 }
3329
Adam Cohen27772732013-11-11 14:00:56 +00003330 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003331 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003332 }
3333
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003334 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 * Add the icons for all apps.
3336 *
3337 * Implementation of the method from LauncherModel.Callbacks.
3338 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003339 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003340 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3341 public void run() {
3342 bindAllApplications(apps);
3343 }
3344 };
3345 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003346 return;
3347 }
3348
Winson Chungb745afb2015-03-02 11:51:23 -08003349 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003350 Executor pendingExecutor = getPendingExecutor();
3351 if (pendingExecutor != null && mState != State.APPS) {
3352 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003353 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003354 return;
3355 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003356
Winson Chungb745afb2015-03-02 11:51:23 -08003357 mAppsView.setApps(apps);
3358 }
Adam Cohen9211d422014-10-07 18:14:53 -07003359 if (mLauncherCallbacks != null) {
3360 mLauncherCallbacks.bindAllApplications(apps);
3361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 }
3363
3364 /**
Tony2917a8b2017-07-31 23:29:42 -07003365 * Returns an Executor that will run after the launcher is first drawn (including after the
3366 * initial fade in animation). Returns null if the first draw has already occurred.
3367 */
3368 public @Nullable Executor getPendingExecutor() {
3369 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3370 }
3371
3372 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003373 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3374 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3375 */
3376 @Override
3377 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003378 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003379 }
3380
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003381 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 * A package was updated.
3383 *
3384 * Implementation of the method from LauncherModel.Callbacks.
3385 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003386 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003387 Runnable r = new Runnable() {
3388 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003389 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003390 }
3391 };
3392 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003393 return;
3394 }
3395
Winson Chungb745afb2015-03-02 11:51:23 -08003396 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003397 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 }
3400
Sunny Goyal4390ace2014-10-13 11:33:11 -07003401 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003402 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3403 Runnable r = new Runnable() {
3404 public void run() {
3405 bindPromiseAppProgressUpdated(app);
3406 }
3407 };
3408 if (waitUntilResume(r)) {
3409 return;
3410 }
3411
3412 if (mAppsView != null) {
3413 mAppsView.updatePromiseAppProgress(app);
3414 }
3415 }
3416
3417 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003418 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3419 Runnable r = new Runnable() {
3420 public void run() {
3421 bindWidgetsRestored(widgets);
3422 }
3423 };
3424 if (waitUntilResume(r)) {
3425 return;
3426 }
3427 mWorkspace.widgetsRestored(widgets);
3428 }
3429
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003431 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003432 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003433 *
3434 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003435 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003436 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003437 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003438 Runnable r = new Runnable() {
3439 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003440 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003441 }
3442 };
3443 if (waitUntilResume(r)) {
3444 return;
3445 }
3446
Sunny Goyal4390ace2014-10-13 11:33:11 -07003447 if (!updated.isEmpty()) {
3448 mWorkspace.updateShortcuts(updated);
3449 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003450 }
3451
3452 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003453 * Update the state of a package, typically related to install state.
3454 *
3455 * Implementation of the method from LauncherModel.Callbacks.
3456 */
Sunny Goyale755d462014-07-22 13:48:29 -07003457 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003458 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3459 Runnable r = new Runnable() {
3460 public void run() {
3461 bindRestoreItemsChange(updates);
3462 }
3463 };
3464 if (waitUntilResume(r)) {
3465 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003466 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003467
Sunny Goyal756adbc2015-04-16 15:20:51 -07003468 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003469 }
3470
3471 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003472 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003473 * in addition to specific applications to remove, the reason being that
3474 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003475 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003476 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003478 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003479 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003480 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003481 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003482 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003483 }
Winson Chungd64d1762013-08-20 14:37:16 -07003484 };
3485 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003486 return;
3487 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003488 mWorkspace.removeItemsByMatcher(matcher);
3489 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003490 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003491
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003492 @Override
3493 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3494 Runnable r = new Runnable() {
3495 public void run() {
3496 bindAppInfosRemoved(appInfos);
3497 }
3498 };
3499 if (waitUntilResume(r)) {
3500 return;
Joe Onorato36115782010-06-17 13:28:48 -04003501 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003502
Winson Chungdf95eb12013-10-16 14:57:07 -07003503 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003504 if (mAppsView != null) {
3505 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003506 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 }
Joe Onoratobe386092009-11-17 17:32:16 -08003508
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003509 @Override
3510 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3511 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3512 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003513 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003514 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003515 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003516 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003517 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003518 return;
3519 }
3520
Sunny Goyald164b7f2016-10-12 20:49:31 -07003521 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003522 Executor pendingExecutor = getPendingExecutor();
3523 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003524 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003525 return;
3526 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003527 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003528 }
Tony Wickham26b17462017-03-20 17:12:24 -07003529
3530 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3531 if (topView != null) {
3532 topView.onWidgetsBound();
3533 }
3534 }
3535
3536 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3537 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003538 }
3539
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003540 @Override
3541 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003542 if (mWorkspace.getState().shouldUpdateWidget) {
3543 refreshAndBindWidgetsForPackageUser(null);
3544 }
Tony Wickham26b17462017-03-20 17:12:24 -07003545 }
3546
Tony Wickham86222d22017-03-29 15:30:43 -07003547 /**
3548 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3549 * refreshes the widgets and shortcuts associated with the given package/user
3550 */
3551 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003552 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003553 }
3554
Winson Chung4b919f82012-05-01 10:44:08 -07003555 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003556 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003557 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003558 }
3559 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003560
Winson Chung4b919f82012-05-01 10:44:08 -07003561 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003562 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003563 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003564 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003565 } else {
3566 mHandler.postDelayed(new Runnable() {
3567 public void run() {
3568 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3569 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003570 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003571 }
Winson Chung4b919f82012-05-01 10:44:08 -07003572 }
Winson Chung400438b2011-01-16 17:53:48 -08003573 }
3574
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003575 private void markAppsViewShown() {
3576 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3577 return;
3578 }
3579 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3580 }
3581
3582 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003583 UserManagerCompat um = UserManagerCompat.getInstance(this);
3584 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003585 }
3586
Winson Chung80baf5a2010-08-09 16:03:15 -07003587 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003588 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003589 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003590 @Override
3591 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3592 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003593
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003594 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3595 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003596 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003597 writer.println(prefix + " Homescreen " + i);
3598
3599 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3600 for (int j = 0; j < layout.getChildCount(); j++) {
3601 Object tag = layout.getChildAt(j).getTag();
3602 if (tag != null) {
3603 writer.println(prefix + " " + tag.toString());
3604 }
3605 }
3606 }
3607
3608 writer.println(prefix + " Hotseat");
3609 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003610 for (int j = 0; j < layout.getChildCount(); j++) {
3611 Object tag = layout.getChildAt(j).getTag();
3612 if (tag != null) {
3613 writer.println(prefix + " " + tag.toString());
3614 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003615 }
Sunny Goyala1365452015-10-01 15:46:24 -07003616
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003617 try {
3618 FileLog.flushAll(writer);
3619 } catch (Exception e) {
3620 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003621 }
3622 }
3623
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003624 writer.println(prefix + "Misc:");
3625 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3626 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3627 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3628
3629 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003630
Adam Cohen9211d422014-10-07 18:14:53 -07003631 if (mLauncherCallbacks != null) {
3632 mLauncherCallbacks.dump(prefix, fd, writer, args);
3633 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003634 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003635
Sunny Goyal66b24572016-09-21 15:57:55 -07003636 @Override
3637 @TargetApi(Build.VERSION_CODES.N)
3638 public void onProvideKeyboardShortcuts(
3639 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3640
3641 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3642 if (mState == State.WORKSPACE) {
3643 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3644 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3645 }
3646 View currentFocus = getCurrentFocus();
3647 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3648 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3649 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3650 }
Tony18c4aa42017-05-10 21:23:57 -05003651 if (currentFocus.getTag() instanceof ItemInfo
3652 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003653 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3654 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3655 }
3656 if (!shortcutInfos.isEmpty()) {
3657 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3658 }
3659
3660 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3661 }
3662
3663 @Override
3664 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3665 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3666 switch (keyCode) {
3667 case KeyEvent.KEYCODE_A:
3668 if (mState == State.WORKSPACE) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003669 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003670 return true;
3671 }
3672 break;
3673 case KeyEvent.KEYCODE_S: {
3674 View focusedView = getCurrentFocus();
3675 if (focusedView instanceof BubbleTextView
3676 && focusedView.getTag() instanceof ItemInfo
3677 && mAccessibilityDelegate.performAction(focusedView,
3678 (ItemInfo) focusedView.getTag(),
3679 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003680 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003681 return true;
3682 }
3683 break;
3684 }
3685 case KeyEvent.KEYCODE_O:
3686 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3687 return true;
3688 }
3689 break;
3690 }
3691 }
3692 return super.onKeyShortcut(keyCode, event);
3693 }
3694
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003695 public static Launcher getLauncher(Context context) {
3696 if (context instanceof Launcher) {
3697 return (Launcher) context;
3698 }
3699 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3700 }
3701
Sunny Goyal5a81c382017-03-20 15:08:06 -07003702 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003703
3704 @Override
3705 public void onSharedPreferenceChanged(
3706 SharedPreferences sharedPreferences, String key) {
3707 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003708 // Recreate the activity so that it initializes the rotation preference again.
3709 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003710 }
3711 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003713}