blob: 5d63694d87855d3d73c1cddc16c795c1ef947b8d [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;
Tony Wickham827cef22016-03-17 15:39:39 -0700101import com.android.launcher3.dynamicui.ExtractedColors;
Mario Bertschler27288382017-05-24 15:35:09 -0700102import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000103import com.android.launcher3.folder.Folder;
104import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700105import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700106import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700107import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700108import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.model.ModelWriter;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700110import com.android.launcher3.model.PackageItemInfo;
111import com.android.launcher3.model.WidgetItem;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700113import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800114import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700116import com.android.launcher3.shortcuts.DeepShortcutManager;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800117import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530118import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
119import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700121import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700122import com.android.launcher3.util.ComponentKey;
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700123import com.android.launcher3.util.ComponentKeyMapper;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700124import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700125import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700129import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700130import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700131import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700132import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700133import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700134import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700135import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700136import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800137import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800139import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700140import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700142import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700143
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700146import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700147import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700148import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700149import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800150import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700151import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153/**
154 * Default launcher application.
155 */
Sunny Goyal27835952017-01-13 12:15:53 -0800156public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700157 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
158 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700159 AccessibilityManager.AccessibilityStateChangeListener,
160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700170 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
Mario Bertschleracbf5702017-03-03 10:58:06 -0800186 private static final int SOFT_INPUT_MODE_DEFAULT =
Jon Mirandafd516322017-03-09 13:14:50 -0800187 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800188 private static final int SOFT_INPUT_MODE_ALL_APPS =
Jon Mirandafd516322017-03-09 13:14:50 -0800189 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800190
Winson Chung2672ff92012-05-04 16:22:30 -0700191 // The Intent extra that defines whether to ignore the launch animation
192 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400193 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 // Type: int
196 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700197 // Type: int
198 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700199 // Type: PendingRequestArgs
200 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
201 // Type: ActivityResultInfo
202 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700204 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700207 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
208 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Adam Cohen4f8071b2016-02-13 16:06:05 -0800215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Winson Chunga2413752012-04-03 14:22:34 -0700218 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700219 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
220 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
221 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700222
Sunny Goyal60820d72017-05-09 12:40:11 -0700223 private final ExtractedColors mExtractedColors = new ExtractedColors();
224
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700226 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800228 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700229
230 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Sunny Goyalffe83f12014-08-14 17:39:34 -0700232 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700235 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700236
Sunny Goyal316490e2015-06-02 09:38:28 -0700237 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800238 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700239
Michael Jurka838a4ca2011-02-07 13:33:06 -0800240 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700241 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700242
Sunny Goyal47328fd2016-05-25 18:56:41 -0700243 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700244
245 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700246 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700247 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700248
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700249 // Main container view and the model for the widget tray screen.
250 @Thunk WidgetsContainerView mWidgetsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700251
Jon Miranda6bed3502017-09-19 14:43:17 -0700252 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
253 // that results in widgets being inflated in the wrong orientation.
254 private int mOrientation;
255
Winson Chung4a2afa32012-07-19 14:53:05 -0700256 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
257 // scroll issues (because the workspace may not have been measured yet) and extra work.
258 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
259 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
261 private SpannableStringBuilder mDefaultKeySsb = null;
262
Adam Cohen091440a2015-03-18 14:16:05 -0700263 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800265 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700266 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700268 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
269 private final ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700270 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700271
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800273 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800274 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700275 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700276 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700277 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278
Jon Miranda2d89ea82017-05-04 11:47:53 -0700279 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700280 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700281
Tony Wickham010d2552017-01-20 08:15:28 -0800282 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700283
Winson Chung400438b2011-01-16 17:53:48 -0800284 // Determines how long to wait after a rotation before restoring the screen orientation to
285 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700286 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800287
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700288 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700289
Winson Chung46353de2012-02-16 14:05:10 -0800290 // We only want to get the SharedPreferences once since it does an FS stat each time we get
291 // it from the context.
292 private SharedPreferences mSharedPrefs;
293
Winson Chung13eb5272015-05-11 16:30:13 -0700294 // This is set to the view that launched the activity that navigated the user away from
295 // launcher. Since there is no callback for when the activity has finished launching, enable
296 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800297 private BubbleTextView mWaitingForResume;
298
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700299 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
300 // state transition. If another state transition happened during this delay,
301 // simply unregister this runnable.
302 private Runnable mExitSpringLoadedModeRunnable;
303
Sunny Goyal2100c782016-08-22 16:00:03 -0700304 // Activity result which needs to be processed after workspace has loaded.
305 private ActivityResultInfo mPendingActivityResult;
306 /**
307 * Holds extra information required to handle a result from an external call, like
308 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
309 */
310 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800311
Jon Miranda379198e2016-09-23 08:54:40 -0700312 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700313
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700314 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700315 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400316
317 @Thunk void setOrientation() {
318 if (mRotationEnabled) {
319 unlockScreenOrientation(true);
320 } else {
321 setRequestedOrientation(
322 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700323 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400324 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700325
Sunny Goyal745bad92016-05-02 10:54:12 -0700326 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700327
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 @Override
329 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700330 if (DEBUG_STRICT_MODE) {
331 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
332 .detectDiskReads()
333 .detectDiskWrites()
334 .detectNetwork() // or .detectAll() for all detectable problems
335 .penaltyLog()
336 .build());
337 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
338 .detectLeakedSqlLiteObjects()
339 .detectLeakedClosableObjects()
340 .penaltyLog()
341 .penaltyDeath()
342 .build());
343 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700344 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700345
Adam Cohen9211d422014-10-07 18:14:53 -0700346 if (mLauncherCallbacks != null) {
347 mLauncherCallbacks.preOnCreate();
348 }
349
Mario Bertschler27288382017-05-24 15:35:09 -0700350 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
351 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700352 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700355 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400356
Sunny Goyal87f784c2017-01-11 10:48:34 -0800357 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700358
Adam Cohen2e6da152015-05-06 11:42:25 -0700359 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800360 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700361 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700362 Display display = getWindowManager().getDefaultDisplay();
363 Point mwSize = new Point();
364 display.getSize(mwSize);
365 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
366 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700367
Jon Miranda6bed3502017-09-19 14:43:17 -0700368 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700369 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700370 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800371 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800372 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800373 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700374 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700375
Joe Onorato41a12d22009-10-31 18:30:00 -0400376 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700377 mAllAppsController = new AllAppsTransitionController(this);
378 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700379
Sunny Goyalffe83f12014-08-14 17:39:34 -0700380 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700381
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700382 mAppWidgetHost = new LauncherAppWidgetHost(this);
383 if (Utilities.ATLEAST_MARSHMALLOW) {
384 mAppWidgetHost.addProviderChangeListener(this);
385 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700387
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700388 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
389 // this also ensures that any synchronous binding below doesn't re-trigger another
390 // LauncherModel load.
391 mPaused = false;
392
Sunny Goyal60820d72017-05-09 12:40:11 -0700393 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 setupViews();
Winson1f064272016-07-18 17:18:02 -0700396 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700397 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398
Tony Wickham010d2552017-01-20 08:15:28 -0800399 mPopupDataProvider = new PopupDataProvider(this);
400
Tony Wickhame2217252016-03-22 16:34:23 -0700401 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
402 .addAccessibilityStateChangeListener(this);
403
Sunny Goyalfe770c92016-09-27 14:38:58 -0700404 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405
Sunny Goyal2100c782016-08-22 16:00:03 -0700406 // We only load the page synchronously if the user rotates (or triggers a
407 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700408 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
409 if (savedInstanceState != null) {
410 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
411 }
412 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700413 // If we are not binding synchronously, show a fade in animation when
414 // the first page bind completes.
415 mDragLayer.setAlpha(0);
416 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700417 // Pages bound synchronously.
418 mWorkspace.setCurrentPage(currentScreen);
419
Sunny Goyal2100c782016-08-22 16:00:03 -0700420 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 }
422
423 // For handling default keys
424 mDefaultKeySsb = new SpannableStringBuilder();
425 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800426
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800427 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700428 // In case we are on a device with locked rotation, we should look at preferences to check
429 // if the user has specifically allowed rotation.
430 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700431 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700432 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
433 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700434 }
435
Sunny Goyal5a81c382017-03-20 15:08:06 -0700436 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
437 // Temporarily enable the rotation
438 mRotationEnabled = true;
439 }
440
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700441 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
442 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400443 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700444
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800445 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700446
Jon Miranda7fda2852017-07-19 16:07:01 -0700447 // Listen for broadcasts
448 IntentFilter filter = new IntentFilter();
449 filter.addAction(Intent.ACTION_SCREEN_OFF);
450 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
451 registerReceiver(mReceiver, filter);
452 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700453
Sunny Goyal8392c822017-06-20 10:03:56 -0700454 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
455 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700456
457 if (mLauncherCallbacks != null) {
458 mLauncherCallbacks.onCreate(savedInstanceState);
459 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700460
461 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700462 }
463
Sunny Goyal7779d622015-06-11 16:18:39 -0700464 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700465 public void onThemeChanged() {
466 recreate();
467 }
468
Mario Bertschlera6936942017-05-31 14:48:19 -0700469 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700470 if (isDark) {
471 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700472 } else if (supportsDarkText) {
473 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700474 }
475 }
476
477 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700478 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800479 return mLauncherView.findViewById(id);
480 }
481
482 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700483 public void onExtractedColorsChanged() {
484 loadExtractedColorsAndColorItems();
Sunny Goyal60820d72017-05-09 12:40:11 -0700485 mExtractedColors.notifyChange();
Sunny Goyal5e7b2382017-04-25 23:09:06 -0700486 }
487
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700488 @Override
489 public void onAppWidgetHostReset() {
490 if (mAppWidgetHost != null) {
491 mAppWidgetHost.startListening();
492 }
493 }
494
Tony Wickham827cef22016-03-17 15:39:39 -0700495 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700496 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700497 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700498 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700499 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700500 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700501 }
502 }
503
Adam Cohen9211d422014-10-07 18:14:53 -0700504 private LauncherCallbacks mLauncherCallbacks;
505
506 public void onPostCreate(Bundle savedInstanceState) {
507 super.onPostCreate(savedInstanceState);
508 if (mLauncherCallbacks != null) {
509 mLauncherCallbacks.onPostCreate(savedInstanceState);
510 }
511 }
512
Winson1f064272016-07-18 17:18:02 -0700513 public void onInsetsChanged(Rect insets) {
514 mDeviceProfile.updateInsets(insets);
515 mDeviceProfile.layout(this, true /* notifyListeners */);
516 }
517
Sunny Goyal32554d12015-12-03 15:31:25 -0800518 /**
519 * Call this after onCreate to set or clear overlay.
520 */
521 public void setLauncherOverlay(LauncherOverlay overlay) {
522 if (overlay != null) {
523 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
524 }
525 mWorkspace.setLauncherOverlay(overlay);
526 }
527
Adam Cohen9211d422014-10-07 18:14:53 -0700528 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
529 mLauncherCallbacks = callbacks;
530 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700531 }
532
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700533 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700534 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700535 if (mLauncherCallbacks != null) {
536 mLauncherCallbacks.onLauncherProviderChange();
537 }
538 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700539
Hyunyoung Song3f471442015-04-08 19:01:34 -0700540 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700541 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
542 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700543 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700544 }
545
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000546 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700547 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
548 // This cast is safe as long as the id < 0x00FFFFFF
549 // Since we jail all the dynamically generated views, there should be no clashes
550 // with any other views.
551 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000552 }
553
Tony Wickham010d2552017-01-20 08:15:28 -0800554 public PopupDataProvider getPopupDataProvider() {
555 return mPopupDataProvider;
556 }
557
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000558 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700559 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
560 * a configuration step, this allows the proper animations to run after other transitions.
561 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700562 private long completeAdd(
563 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
564 long screenId = info.screenId;
565 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700566 // When the screen id represents an actual screen (as opposed to a rank) we make sure
567 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700568 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700569 }
Adam Cohendb364c32014-05-20 14:23:40 -0700570
Sunny Goyal2100c782016-08-22 16:00:03 -0700571 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800572 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700573 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700574 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800575 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700576 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700577 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700578 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700579 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700580 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700581 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700582 int widgetId = appWidgetId;
583 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700584 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700585 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700586 // Since the view was just bound, also launch the configure activity if needed
587 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
588 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800589 if (provider != null) {
590 new WidgetAddFlowHandler(provider)
591 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700592 }
593 }
594 break;
595 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800596 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700597
Adam Cohendb364c32014-05-20 14:23:40 -0700598 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 }
600
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800601 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700602 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700603 if (isWorkspaceLoading()) {
604 // process the result once the workspace has loaded.
605 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
606 return;
607 }
608 mPendingActivityResult = null;
609
Winson Chunge341d302013-08-16 14:31:00 -0700610 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700611 final PendingRequestArgs requestArgs = mPendingRequestArgs;
612 setWaitingForResult(null);
613 if (requestArgs == null) {
614 return;
615 }
616
617 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700618
Adam Cohenad4e15c2013-10-17 16:21:35 -0700619 Runnable exitSpringLoaded = new Runnable() {
620 @Override
621 public void run() {
622 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
623 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
624 }
625 };
626
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700628 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700629 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700630 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
631 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700632 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700633 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700634 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700635 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700636 addAppWidgetImpl(
637 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800638 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700639 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700640 }
641 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200642 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
643 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700644 // User could have free-scrolled between pages before picking a wallpaper; make sure
645 // we move to the closest one now.
646 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700647 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200648 }
649 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700650 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200651
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700653 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700654
Adam Cohened66b2b2012-01-23 17:28:51 -0800655 // We have special handling for widgets
656 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800657 final int appWidgetId;
658 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
659 : -1;
660 if (widgetId < 0) {
661 appWidgetId = pendingAddWidgetId;
662 } else {
663 appWidgetId = widgetId;
664 }
665
Adam Cohenad4e15c2013-10-17 16:21:35 -0700666 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800667 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700668 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
669 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700670 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700672 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700673 @Override
674 public void run() {
675 exitSpringLoadedDragModeDelayed(false, 0, null);
676 }
677 };
Adam Cohendb364c32014-05-20 14:23:40 -0700678
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
680 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
681 } else {
682 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
683 // When the screen id represents an actual screen (as opposed to a rank)
684 // we make sure that the drop page actually exists.
685 requestArgs.screenId =
686 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700687 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 final CellLayout dropLayout =
689 mWorkspace.getScreenWithId(requestArgs.screenId);
690
691 dropLayout.setDropPending(true);
692 final Runnable onComplete = new Runnable() {
693 @Override
694 public void run() {
695 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
696 dropLayout.setDropPending(false);
697 }
698 };
699 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
700 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700701 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 return;
703 }
704
Sunny Goyald478c832016-04-01 12:04:16 -0700705 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
706 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700707 if (resultCode == RESULT_OK) {
708 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700709 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700710 }
711 // Leave the widget in the pending state if the user canceled the configure.
712 return;
713 }
714
Sunny Goyalaad90582015-07-09 14:22:50 -0700715 if (requestCode == REQUEST_CREATE_SHORTCUT) {
716 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700717 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
718 completeAdd(requestCode, data, -1, requestArgs);
719 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
720 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
721
Sunny Goyalaad90582015-07-09 14:22:50 -0700722 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700723 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
724 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800728 }
729
730 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700731 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800732 final int requestCode, final int resultCode, final Intent data) {
733 handleActivityResult(requestCode, resultCode, data);
734 if (mLauncherCallbacks != null) {
735 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
736 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 }
738
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700739 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700740 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600741 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 PendingRequestArgs pendingArgs = mPendingRequestArgs;
743 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
744 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
745 setWaitingForResult(null);
746
Sunny Goyal28c6b962015-10-12 11:42:05 -0700747 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700748 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700749 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700750 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700751 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 Intent intent = pendingArgs.getPendingIntent();
753
Sunny Goyal28c6b962015-10-12 11:42:05 -0700754 if (grantResults.length > 0
755 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700756 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700757 } else {
758 // TODO: Show a snack bar with link to settings
759 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700760 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700761 }
762 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600763 if (mLauncherCallbacks != null) {
764 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
765 grantResults);
766 }
767 }
768
Adam Cohendb364c32014-05-20 14:23:40 -0700769 /**
770 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
771 *
772 * @param screenId the screen id to check
773 * @return the new screen, or screenId if it exists
774 */
775 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800776 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700777 if (dropLayout == null) {
778 // it's possible that the add screen was removed because it was
779 // empty and a re-bind occurred
780 mWorkspace.addExtraEmptyScreen();
781 return mWorkspace.commitExtraEmptyScreen();
782 } else {
783 return screenId;
784 }
785 }
786
Sunny Goyal2100c782016-08-22 16:00:03 -0700787 @Thunk void completeTwoStageWidgetDrop(
788 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
789 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800790 Runnable onCompleteRunnable = null;
791 int animationType = 0;
792
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700793 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800794 if (resultCode == RESULT_OK) {
795 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
796 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800797 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700798 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 onCompleteRunnable = new Runnable() {
800 @Override
801 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700802 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
804 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 }
806 };
807 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800808 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800809 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700811 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700812 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700813 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
814 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700816 // The animated view may be null in the case of a rotation during widget configuration
817 onCompleteRunnable.run();
818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
820
821 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700822 protected void onStop() {
823 super.onStop();
824 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700825
826 if (mLauncherCallbacks != null) {
827 mLauncherCallbacks.onStop();
828 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700829
Sunny Goyalf5e37442016-11-02 10:31:24 -0700830 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700831 mAppWidgetHost.stopListening();
832 }
Tony Wickham010d2552017-01-20 08:15:28 -0800833
834 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700835 }
836
837 @Override
838 protected void onStart() {
839 super.onStart();
840 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700841
842 if (mLauncherCallbacks != null) {
843 mLauncherCallbacks.onStart();
844 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700845
Sunny Goyalf5e37442016-11-02 10:31:24 -0700846 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700847 mAppWidgetHost.startListening();
848 }
Tony Wickham010d2552017-01-20 08:15:28 -0800849
850 if (!isWorkspaceLoading()) {
851 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
852 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700853
Jon Miranda7fda2852017-07-19 16:07:01 -0700854 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700855 if (mScrimAnimator != null) {
856 mScrimAnimator.cancel();
857 }
858 mDragLayer.getBackground().setAlpha(0);
859 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
860 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
861 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
862 @Override
863 public void onAnimationEnd(Animator animation) {
864 mScrimAnimator = null;
865 }
866 });
867 mScrimAnimator.setDuration(600);
868 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
869 mScrimAnimator.start();
870 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700871 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700872 }
873
874 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700876 TraceHelper.beginSection("ON_RESUME");
Adam Cohen9211d422014-10-07 18:14:53 -0700877 if (mLauncherCallbacks != null) {
878 mLauncherCallbacks.preOnResume();
879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700881 TraceHelper.partitionSection("ON_RESUME", "superCall");
882
Hyunyoung Songaa953652016-04-19 18:30:24 -0700883 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700884
Winson Chung4a2afa32012-07-19 14:53:05 -0700885 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700886 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700887 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800888 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700889 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700890 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700891 // view after launching an app, as they may be depending on the UI to be static to
892 // switch to another app, otherwise, if it was
Sunny Goyal5606e072017-06-15 14:24:21 -0700893 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800894 } else if (mOnResumeState == State.WIDGETS) {
895 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700896 }
Jon Miranda7653a602017-07-28 10:09:34 -0700897 if (mOnResumeState != State.APPS) {
898 tryAndUpdatePredictedApps();
899 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700900 mOnResumeState = State.NONE;
901
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800902 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700903 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700904 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700905 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700906 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700908 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200909 // We might have postponed some bind calls until onResume (see waitUntilResume) --
910 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700911 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
912 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200913 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700914 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200915 }
Michael Jurka54554252013-08-01 12:52:23 +0200916 if (mOnResumeCallbacks.size() > 0) {
917 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
918 mOnResumeCallbacks.get(i).run();
919 }
920 mOnResumeCallbacks.clear();
921 }
Winson Chunge4e50662012-01-23 14:45:13 -0800922
923 // Reset the pressed state of icons that were locked in the press state while activities
924 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800925 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800926 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800927 mWaitingForResume.setStayPressed(false);
928 }
Winson Chung82963d52013-10-09 11:20:57 -0700929
Adam Cohen06dff352012-06-01 17:17:08 -0700930 // It is possible that widgets can receive updates while launcher is not in the foreground.
931 // Consequently, the widgets will be inflated in the orientation of the foreground activity
932 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
933 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -0700934 if (!isWorkspaceLoading()) {
935 getWorkspace().reinflateWidgetsIfNecessary();
936 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700937
Winson Chungcd99cd32015-04-29 11:03:24 -0700938 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -0700939 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -0700940
Sunny Goyala474a9b2017-05-04 16:47:11 -0700941 // Process any items that were added while Launcher was away.
942 InstallShortcutReceiver.disableAndFlushInstallQueue(
943 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700944
Sunny Goyala474a9b2017-05-04 16:47:11 -0700945 // Refresh shortcuts if the permission changed.
946 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700947
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700948 if (shouldShowDiscoveryBounce()) {
949 mAllAppsController.showDiscoveryBounce();
950 }
Adam Cohen9211d422014-10-07 18:14:53 -0700951 if (mLauncherCallbacks != null) {
952 mLauncherCallbacks.onResume();
953 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800954
Sunny Goyala502aa32017-10-02 16:04:06 -0700955 clearTypedText();
956
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700957 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700958 }
959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700961 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700962 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700963 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700964
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700965 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700966 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800967 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700968 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700969
Adam Cohen9211d422014-10-07 18:14:53 -0700970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700972 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700973 }
974
Adam Cohenc2d6e892014-10-16 09:49:24 -0700975 public interface LauncherOverlay {
976
977 /**
978 * Touch interaction leading to overscroll has begun
979 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700980 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700981
982 /**
983 * Touch interaction related to overscroll has ended
984 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700985 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700986
987 /**
988 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
989 * screen (or in the case of RTL, the rightmost screen).
990 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700991 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700992
993 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800994 * Called when the launcher is ready to use the overlay
995 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700996 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700997 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700998 }
999
1000 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001001 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001002 }
1003
1004 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1005
Sunny Goyalc86df472016-02-25 09:19:38 -08001006 public void onScrollChanged(float progress) {
1007 if (mWorkspace != null) {
1008 mWorkspace.onOverlayScrollChanged(progress);
1009 }
1010 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001011 }
1012
Jorim Jaggid017f882014-01-14 17:08:48 -08001013 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001014 if (mLauncherCallbacks != null) {
1015 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001016 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -08001017 // On O and above we there is always some setting present settings (add icon to
1018 // home screen or icon badging). On earlier APIs we will have the allow rotation
1019 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001020 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001021 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001022 }
1023
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001024 @Override
1025 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001027 if (mModel.isCurrentCallbacks(this)) {
1028 mModel.stopLoader();
1029 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001030 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1031
Romain Guy13c2e7b2010-03-10 19:45:00 -08001032 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001033 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001034
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001035 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001036 @Override
1037 public void onWindowFocusChanged(boolean hasFocus) {
1038 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001039 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001040
1041 if (mLauncherCallbacks != null) {
1042 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1043 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001044 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 private boolean acceptFilter() {
1047 final InputMethodManager inputManager = (InputMethodManager)
1048 getSystemService(Context.INPUT_METHOD_SERVICE);
1049 return !inputManager.isFullscreenMode();
1050 }
1051
1052 @Override
1053 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001054 final int uniChar = event.getUnicodeChar();
1055 final boolean handled = super.onKeyDown(keyCode, event);
1056 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1057 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1059 keyCode, event);
1060 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001061 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001062 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001063 // showSearchDialog()
1064 // If there are multiple keystrokes before the search dialog takes focus,
1065 // onSearchRequested() will be called for every keystroke,
1066 // but it is idempotent, so it's fine.
1067 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 }
1069 }
1070
Joe Onorato8a9625e2010-01-28 15:55:35 -08001071 // Eat the long press event so the keyboard doesn't come up.
1072 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1073 return true;
1074 }
1075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 return handled;
1077 }
1078
Winson46163472015-09-22 17:31:56 -07001079 @Override
1080 public boolean onKeyUp(int keyCode, KeyEvent event) {
1081 if (keyCode == KeyEvent.KEYCODE_MENU) {
1082 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001083 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001084 // Close any open floating view
1085 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001086
Winson46163472015-09-22 17:31:56 -07001087 // Stop resizing any widgets
1088 mWorkspace.exitWidgetResizeMode();
1089
1090 // Show the overview mode if we are on the workspace
1091 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1092 !mWorkspace.isSwitchingState()) {
1093 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001094 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001095 }
1096 }
1097 return true;
1098 }
1099 return super.onKeyUp(keyCode, event);
1100 }
1101
Karl Rosaen138a0412009-04-23 19:00:21 -07001102 private String getTypedText() {
1103 return mDefaultKeySsb.toString();
1104 }
1105
Sunny Goyal6f28e712016-09-13 14:19:29 -07001106 @Override
1107 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001108 mDefaultKeySsb.clear();
1109 mDefaultKeySsb.clearSpans();
1110 Selection.setSelection(mDefaultKeySsb, 0);
1111 }
1112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 /**
1114 * Restores the previous state, if it exists.
1115 *
1116 * @param savedState The previous state.
1117 */
1118 private void restoreState(Bundle savedState) {
1119 if (savedState == null) {
1120 return;
1121 }
1122
Sunny Goyalfe770c92016-09-27 14:38:58 -07001123 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1124 State[] stateValues = State.values();
1125 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1126 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001127 if (state == State.APPS || state == State.WIDGETS) {
1128 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001129 }
1130
Sunny Goyal2100c782016-08-22 16:00:03 -07001131 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1132 if (requestArgs != null) {
1133 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001135
1136 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138
1139 /**
1140 * Finds all the views we need and configure them properly.
1141 */
1142 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001143 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001144 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001145 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001146 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001147
Sunny Goyal784f9c32016-03-23 16:56:54 -07001148 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1149 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1150 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
Winson Chung4c98d922011-05-31 16:50:48 -07001152 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001153 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001154
Winson Chung3d503fb2011-07-13 17:25:49 -07001155 // Setup the hotseat
1156 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1157 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001158 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001159 }
1160
Winsone9f27272015-10-13 10:47:51 -07001161 // Setup the overview panel
1162 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001163
Winson Chung4c98d922011-05-31 16:50:48 -07001164 // Setup the workspace
1165 mWorkspace.setHapticFeedbackEnabled(false);
1166 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001167 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001168 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1169 // default state, otherwise we will update to the wrong offsets in RTL
1170 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001171 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001172 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001173
Tony Wickham34d2c912015-09-11 09:27:58 -07001174 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001175 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001176
Winson Chungef7f8742015-06-04 17:18:17 -07001177 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001178 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001179 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001180
Winson Chung3d503fb2011-07-13 17:25:49 -07001181 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001182 mDragController.setMoveTarget(mWorkspace);
1183 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001184 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001185
Hyunyoung Songd725f642017-08-17 22:26:35 -07001186 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001187
Sunny Goyal322d5562015-06-25 19:35:49 -07001188 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1189 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192
Winsone9f27272015-10-13 10:47:51 -07001193 private void setupOverviewPanel() {
1194 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1195
Winsone9f27272015-10-13 10:47:51 -07001196 // Bind wallpaper button actions
1197 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301198 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001199 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001200 public void handleViewClick(View view) {
1201 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001202 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001203 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001204
1205 // Bind widget button actions
1206 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301207 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001208 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001209 public void handleViewClick(View view) {
1210 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001211 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001212 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001213
1214 // Bind settings actions
1215 View settingsButton = findViewById(R.id.settings_button);
1216 boolean hasSettings = hasSettings();
1217 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301218 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001219 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001220 public void handleViewClick(View view) {
1221 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001222 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001223 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001224 } else {
1225 settingsButton.setVisibility(View.GONE);
1226 }
1227
1228 mOverviewPanel.setAlpha(0f);
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001232 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001233 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001234 */
1235 public void setAllAppsButton(View allAppsButton) {
1236 mAllAppsButton = allAppsButton;
1237 }
1238
Sunny Goyalbb011da2016-06-15 15:42:29 -07001239 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001240 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001241 }
1242
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001243 public View getWidgetsButton() {
1244 return mWidgetsButton;
1245 }
1246
Anjali Koppal5ad44842014-03-10 20:34:39 -07001247 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 * Creates a view representing a shortcut.
1249 *
1250 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001252 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001253 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
1256 /**
1257 * Creates a view representing a shortcut inflated from the specified resource.
1258 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 * @param parent The group the shortcut belongs to.
1260 * @param info The data structure describing the shortcut.
1261 *
1262 * @return A View inflated from layoutResId.
1263 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001264 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001265 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1266 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001267 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001269 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 return favorite;
1271 }
1272
1273 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001274 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 *
1276 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001278 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001279 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001280 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1281 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001282 return;
1283 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001284
Jon Mirandac476d6e2017-05-04 16:30:01 -07001285 int[] cellXY = mTmpAddItemCellCoordinates;
1286 CellLayout layout = getCellLayout(container, screenId);
1287
Sunny Goyal782f0c92017-01-19 10:27:54 -08001288 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001289 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001290 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1291 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001292 }
1293
1294 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001295 // Legacy shortcuts are only supported for primary profile.
1296 info = Process.myUserHandle().equals(args.user)
1297 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001298
Sunny Goyalb57645f2017-03-20 13:57:28 -07001299 if (info == null) {
1300 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1301 return;
1302 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001303 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001304 // The app is trying to add a shortcut without sufficient permissions
1305 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1306 return;
1307 }
1308 }
1309
Jon Mirandac476d6e2017-05-04 16:30:01 -07001310 if (container < 0) {
1311 // Adding a shortcut to the Workspace.
1312 final View view = createShortcut(info);
1313 boolean foundCellSpan = false;
1314 // First we check if we already know the exact location where we want to add this item.
1315 if (cellX >= 0 && cellY >= 0) {
1316 cellXY[0] = cellX;
1317 cellXY[1] = cellY;
1318 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001319
Jon Mirandac476d6e2017-05-04 16:30:01 -07001320 // If appropriate, either create a folder or add to an existing folder
1321 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1322 true, null, null)) {
1323 return;
1324 }
1325 DragObject dragObject = new DragObject();
1326 dragObject.dragInfo = info;
1327 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1328 true)) {
1329 return;
1330 }
1331 } else {
1332 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1333 }
1334
1335 if (!foundCellSpan) {
1336 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001337 return;
1338 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001339
1340 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1341 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001342 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001343 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001344 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001345 if (folderIcon != null) {
1346 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1347 folderInfo.add(info, args.rank, false);
1348 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001349 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001350 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353
Sunny Goyale29897f2017-07-20 10:09:42 -07001354 public FolderIcon findFolderIcon(final long folderIconId) {
1355 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1356 @Override
1357 public boolean evaluate(ItemInfo info, View view) {
1358 return info != null && info.id == folderIconId;
1359 }
1360 });
1361 }
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001364 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001366 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001368 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001369 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1370
Adam Cohened66b2b2012-01-23 17:28:51 -08001371 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001372 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001373 }
Romain Guycbb89e42009-06-08 15:52:54 -07001374
Adam Cohen59400422014-03-05 18:07:04 -08001375 LauncherAppWidgetInfo launcherInfo;
1376 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001377 launcherInfo.spanX = itemInfo.spanX;
1378 launcherInfo.spanY = itemInfo.spanY;
1379 launcherInfo.minSpanX = itemInfo.minSpanX;
1380 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001381 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001382
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001383 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001384 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385
Sunny Goyal2100c782016-08-22 16:00:03 -07001386 if (hostView == null) {
1387 // Perform actual inflation because we're live
1388 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001390 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301391 prepareAppWidget(hostView, launcherInfo);
1392 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
Romain Guycbb89e42009-06-08 15:52:54 -07001394
Sunny Goyald5462aa2016-11-22 16:52:39 +05301395 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001396 hostView.setTag(item);
1397 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001398 hostView.setFocusable(true);
1399 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001400 }
1401
Adam Cohended9f8d2010-11-03 13:25:16 -07001402 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1403 @Override
1404 public void onReceive(Context context, Intent intent) {
1405 final String action = intent.getAction();
1406 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001407 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001408
Winson Chungc100e8e2011-08-09 16:02:43 -07001409 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001410 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001411 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001412 if (!showWorkspace(false)) {
1413 // If we are already on the workspace, then manually reset all apps
1414 mAppsView.reset();
1415 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001416 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001417 mShouldFadeInScrim = true;
1418 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1419 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1420 // the user unlocked and the Launcher is not in the foreground.
1421 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001422 }
1423 }
1424 };
1425
Tony Wickham010d2552017-01-20 08:15:28 -08001426 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1427 Runnable r = new Runnable() {
1428 @Override
1429 public void run() {
1430 mWorkspace.updateIconBadges(updatedBadges);
1431 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001432
1433 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1434 if (popup != null) {
1435 popup.updateNotificationHeader(updatedBadges);
1436 }
Tony Wickham010d2552017-01-20 08:15:28 -08001437 }
1438 };
1439 if (!waitUntilResume(r)) {
1440 r.run();
1441 }
1442 }
1443
Adam Cohended9f8d2010-11-03 13:25:16 -07001444 @Override
1445 public void onAttachedToWindow() {
1446 super.onAttachedToWindow();
1447
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001448 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001449 if (mLauncherCallbacks != null) {
1450 mLauncherCallbacks.onAttachedToWindow();
1451 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001452 }
1453
1454 @Override
1455 public void onDetachedFromWindow() {
1456 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001457
1458 if (mLauncherCallbacks != null) {
1459 mLauncherCallbacks.onDetachedFromWindow();
1460 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001461 }
1462
Winson Chunga6945242014-01-08 14:04:34 -08001463 public DragLayer getDragLayer() {
1464 return mDragLayer;
1465 }
1466
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001467 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001468 return mAppsView;
1469 }
1470
Hyunyoung Song3f471442015-04-08 19:01:34 -07001471 public WidgetsContainerView getWidgetsView() {
1472 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001473 }
1474
Winson Chunga6945242014-01-08 14:04:34 -08001475 public Workspace getWorkspace() {
1476 return mWorkspace;
1477 }
1478
1479 public Hotseat getHotseat() {
1480 return mHotseat;
1481 }
1482
Jorim Jaggid017f882014-01-14 17:08:48 -08001483 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001484 return mOverviewPanel;
1485 }
1486
Sunny Goyal47328fd2016-05-25 18:56:41 -07001487 public DropTargetBar getDropTargetBar() {
1488 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001489 }
1490
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001491 public LauncherAppWidgetHost getAppWidgetHost() {
1492 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
Romain Guycbb89e42009-06-08 15:52:54 -07001494
Winson Chunga9abd0e2010-10-27 17:18:37 -07001495 public LauncherModel getModel() {
1496 return mModel;
1497 }
1498
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001499 public ModelWriter getModelWriter() {
1500 return mModelWriter;
1501 }
1502
Adam Cohen79d90c52016-04-22 13:29:20 -07001503 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001504 return mSharedPrefs;
1505 }
1506
Jon Miranda6bed3502017-09-19 14:43:17 -07001507 public int getOrientation() { return mOrientation; }
1508
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 @Override
1510 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001511 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 super.onNewIntent(intent);
1513
Winson15f8b172015-08-19 11:02:39 -07001514 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1515 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1516 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001517
1518 // Check this condition before handling isActionMain, as this will get reset.
1519 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001520 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001521
Winson15f8b172015-08-19 11:02:39 -07001522 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1523 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001524 if (mWorkspace == null) {
1525 // Can be cases where mWorkspace is null, this prevents a NPE
1526 return;
1527 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001528
1529 // Note: There should be at most one log per method call. This is enforced implicitly
1530 // by using if-else statements.
1531 UserEventDispatcher ued = getUserEventDispatcher();
1532
1533 // TODO: Log this case.
Adam Cohen6fecd412013-10-02 17:41:50 -07001534 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001535
Jon Mirandafeba90f2016-10-06 10:53:29 -07001536 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham540913e2017-01-23 11:47:51 -08001537 if (topOpenView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301538 ued.logActionCommand(Action.Command.HOME_INTENT,
1539 topOpenView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001540 } else if (topOpenView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301541 ued.logActionCommand(Action.Command.HOME_INTENT,
1542 ((Folder) topOpenView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001543 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301544 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001545 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1546 }
1547
1548 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001549 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001550 exitSpringLoadedDragMode();
1551
1552 // If we are already on home, then just animate back to the workspace,
1553 // otherwise, just wait until onResume to set the state back to Workspace
1554 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001555 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001556 } else {
1557 mOnResumeState = State.WORKSPACE;
1558 }
1559
1560 final View v = getWindow().peekDecorView();
1561 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001562 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001563 }
1564
Winson Chungb745afb2015-03-02 11:51:23 -08001565 // Reset the apps view
1566 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001567 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001568 }
1569
Hyunyoung Song3f471442015-04-08 19:01:34 -07001570 // Reset the widgets view
1571 if (!alreadyOnHome && mWidgetsView != null) {
1572 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001573 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001574
Adam Cohen9211d422014-10-07 18:14:53 -07001575 if (mLauncherCallbacks != null) {
1576 mLauncherCallbacks.onHomeIntent();
1577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001579 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001580
Adam Cohen9211d422014-10-07 18:14:53 -07001581 if (mLauncherCallbacks != null) {
1582 mLauncherCallbacks.onNewIntent(intent);
1583 }
Winson15f8b172015-08-19 11:02:39 -07001584
1585 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1586 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1587 // animation.
1588 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001589 boolean callbackAllowsMoveToDefaultScreen =
1590 mLauncherCallbacks == null || mLauncherCallbacks
1591 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001592 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1593 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001594
Winson15f8b172015-08-19 11:02:39 -07001595 mWorkspace.post(new Runnable() {
1596 @Override
1597 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001598 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001599 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001600 }
Winson15f8b172015-08-19 11:02:39 -07001601 }
1602 });
1603 }
1604 }
1605
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001606 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001607 }
1608
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001610 public void onRestoreInstanceState(Bundle state) {
1611 super.onRestoreInstanceState(state);
1612 for (int page: mSynchronouslyBoundPages) {
1613 mWorkspace.restoreInstanceStateForChild(page);
1614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 }
1616
1617 @Override
1618 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001619 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001620 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001621
Adam Cohen21cd0022013-10-09 18:57:02 -07001622 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001623 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624
Michael Jurka883f55b2010-10-21 15:47:14 -07001625 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001626 // We close any open folders and shortcut containers since they will not be re-opened,
1627 // and we need to make sure this state is reflected.
1628 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001629
Sunny Goyal2100c782016-08-22 16:00:03 -07001630 if (mPendingRequestArgs != null) {
1631 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1632 }
1633 if (mPendingActivityResult != null) {
1634 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
1636
Adam Cohen9211d422014-10-07 18:14:53 -07001637 if (mLauncherCallbacks != null) {
1638 mLauncherCallbacks.onSaveInstanceState(outState);
1639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
1641
1642 @Override
1643 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001646 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001647 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001648
Winson Chungcd2b0142011-06-08 16:02:26 -07001649 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001650 // It's possible to receive onDestroy after a new Launcher activity has
1651 // been created. In this case, don't interfere with the new Launcher.
1652 if (mModel.isCurrentCallbacks(this)) {
1653 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001654 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001655 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001656
Sunny Goyal745bad92016-05-02 10:54:12 -07001657 if (mRotationPrefChangeHandler != null) {
1658 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001662 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001664 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001666 mAppWidgetHost = null;
1667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 TextKeyListener.getInstance().release();
1669
Tony Wickhame2217252016-03-22 16:34:23 -07001670 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1671 .removeAccessibilityStateChangeListener(this);
1672
Mario Bertschler27288382017-05-24 15:35:09 -07001673 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1674
Michael Jurka2ecf9952012-06-18 12:52:28 -07001675 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001676
Tony2917a8b2017-07-31 23:29:42 -07001677 clearPendingBinds();
1678
Adam Cohen9211d422014-10-07 18:14:53 -07001679 if (mLauncherCallbacks != null) {
1680 mLauncherCallbacks.onDestroy();
1681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
1683
Sunny Goyalae502842016-06-17 08:43:56 -07001684 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1685 return mAccessibilityDelegate;
1686 }
1687
Adam Cohena9cf38f2011-05-02 15:36:58 -07001688 public DragController getDragController() {
1689 return mDragController;
1690 }
1691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001693 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001694 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001695 }
1696
1697 @Override
1698 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1699 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001700 try {
1701 super.startIntentSenderForResult(intent, requestCode,
1702 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1703 } catch (IntentSender.SendIntentException e) {
1704 throw new ActivityNotFoundException();
1705 }
1706 }
1707
Winson Chung70d72102011-08-12 11:24:35 -07001708 /**
1709 * Indicates that we want global search for this activity by setting the globalSearch
1710 * argument for {@link #startSearch} to true.
1711 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001713 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001715
Karl Rosaen138a0412009-04-23 19:00:21 -07001716 if (initialQuery == null) {
1717 // Use any text typed in the launcher as the initial query
1718 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 if (appSearchData == null) {
1721 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001722 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001724
Sunny Goyal6f28e712016-09-13 14:19:29 -07001725 if (mLauncherCallbacks == null ||
1726 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1727 // Starting search from the callbacks failed. Start the default global search.
1728 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001729 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001730
1731 // We need to show the workspace after starting the search
1732 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001733 }
1734
Ian Parkinson047036e2014-09-01 15:40:53 +01001735 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001736 * Starts the global search activity. This code is a copied from SearchManager
1737 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001738 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001739 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001740 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001741 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1742 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1743 if (globalSearchActivity == null) {
1744 Log.w(TAG, "No global search activity found.");
1745 return;
1746 }
1747 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1748 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1749 intent.setComponent(globalSearchActivity);
1750 // Make sure that we have a Bundle to put source in
1751 if (appSearchData == null) {
1752 appSearchData = new Bundle();
1753 } else {
1754 appSearchData = new Bundle(appSearchData);
1755 }
Adam Cohen9211d422014-10-07 18:14:53 -07001756 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001757 if (!appSearchData.containsKey("source")) {
1758 appSearchData.putString("source", getPackageName());
1759 }
1760 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1761 if (!TextUtils.isEmpty(initialQuery)) {
1762 intent.putExtra(SearchManager.QUERY, initialQuery);
1763 }
1764 if (selectInitialQuery) {
1765 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1766 }
1767 intent.setSourceBounds(sourceBounds);
1768 try {
1769 startActivity(intent);
1770 } catch (ActivityNotFoundException ex) {
1771 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
1774
Winson Chung70d72102011-08-12 11:24:35 -07001775 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001776 public boolean onPrepareOptionsMenu(Menu menu) {
1777 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001778 if (mLauncherCallbacks != null) {
1779 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1780 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001781 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001784 @Override
1785 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001786 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001787 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001788 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001789 }
1790
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001792 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793 }
1794
Adam Cohen517a7f52014-03-01 12:12:59 -08001795 public boolean isWorkspaceLoading() {
1796 return mWorkspaceLoading;
1797 }
1798
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001799 private void setWorkspaceLoading(boolean value) {
1800 boolean isLocked = isWorkspaceLocked();
1801 mWorkspaceLoading = value;
1802 if (isLocked != isWorkspaceLocked()) {
1803 onWorkspaceLockedChanged();
1804 }
1805 }
1806
Sunny Goyal04a324a2017-01-20 21:08:59 -08001807 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001808 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001809 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001810 if (isLocked != isWorkspaceLocked()) {
1811 onWorkspaceLockedChanged();
1812 }
1813 }
1814
Adam Cohen9211d422014-10-07 18:14:53 -07001815 protected void onWorkspaceLockedChanged() {
1816 if (mLauncherCallbacks != null) {
1817 mLauncherCallbacks.onWorkspaceLockedChanged();
1818 }
1819 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001820
Sunny Goyal2100c782016-08-22 16:00:03 -07001821 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001822 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001823 if (LOGD) {
1824 Log.d(TAG, "Adding widget from drop");
1825 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001826 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001827 }
1828
Sunny Goyal2100c782016-08-22 16:00:03 -07001829 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001830 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1831 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1832 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001833
Adam Cohenad4e15c2013-10-17 16:21:35 -07001834 Runnable onComplete = new Runnable() {
1835 @Override
1836 public void run() {
1837 // Exit spring loaded mode if necessary after adding the widget
1838 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1839 null);
1840 }
1841 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001842 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001843 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845 }
Romain Guycbb89e42009-06-08 15:52:54 -07001846
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001847 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1848 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001849 info.container = container;
1850 info.screenId = screenId;
1851 if (cell != null) {
1852 info.cellX = cell[0];
1853 info.cellY = cell[1];
1854 }
1855 info.spanX = spanX;
1856 info.spanY = spanY;
1857
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001858 switch (info.itemType) {
1859 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1860 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001861 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001862 break;
1863 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001864 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001865 break;
1866 default:
1867 throw new IllegalStateException("Unknown item type: " + info.itemType);
1868 }
1869 }
1870
Adam Cohenfbba09b2011-07-18 21:43:05 -07001871 /**
1872 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001873 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001874 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001875 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1876 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001877 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1878 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1879 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001880 }
1881
Adam Cohenfbba09b2011-07-18 21:43:05 -07001882 /**
1883 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001884 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001885 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001886 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001887 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001888 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001889 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001890 // In the case where we've prebound the widget, we remove it from the DragLayer
1891 if (LOGD) {
1892 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1893 }
1894 getDragLayer().removeView(hostView);
1895
Adam Cohened66b2b2012-01-23 17:28:51 -08001896 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001897 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001898
1899 // Clear the boundWidget so that it doesn't get destroyed.
1900 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001901 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001902 // In this case, we either need to start an activity to get permission to bind
1903 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001904 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1905 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1906 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1907 this, info.componentName);
1908 } else {
1909 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1910 }
Adam Cohendd70d662012-10-04 16:53:44 -07001911 Bundle options = info.bindOptions;
1912
Sunny Goyalffe83f12014-08-14 17:39:34 -07001913 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1914 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001915 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001916 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001917 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001918 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001919 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001920 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001921 }
1922
Adam Cohendcd297f2013-06-18 13:13:40 -07001923 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001924 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001925 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 folderInfo.title = getText(R.string.folder_name);
1927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001929 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930
1931 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001932 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301933 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001934 // Force measure the new folder icon
1935 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1936 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001937 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
Romain Guycbb89e42009-06-08 15:52:54 -07001939
Winsonc0b52fe2015-09-09 16:38:15 -07001940 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001941 * Unbinds the view for the specified item, and removes the item and all its children.
1942 *
1943 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001944 * @param itemInfo the {@link ItemInfo} for this view.
1945 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001946 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001947 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001948 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001949 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001950 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1951 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001952 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001953 } else {
1954 mWorkspace.removeWorkspaceItem(v);
1955 }
Winsonc0b52fe2015-09-09 16:38:15 -07001956 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001957 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001958 }
1959 } else if (itemInfo instanceof FolderInfo) {
1960 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001961 if (v instanceof FolderIcon) {
1962 ((FolderIcon) v).removeListeners();
1963 }
Winsonc0b52fe2015-09-09 16:38:15 -07001964 mWorkspace.removeWorkspaceItem(v);
1965 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001966 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001967 }
1968 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1969 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001970 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001971 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001972 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001973 }
1974 } else {
1975 return false;
1976 }
1977 return true;
1978 }
1979
1980 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001981 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001982 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001983 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001984 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001985 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001986 // Deleting an app widget ID is a void call but writes to disk before returning
1987 // to the caller...
1988 new AsyncTask<Void, Void, Void>() {
1989 public Void doInBackground(Void ... args) {
1990 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1991 return null;
1992 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001993 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001994 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001995 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 @Override
1999 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002000 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
2002
Joe Onorato88ec0992009-11-19 13:16:06 -08002003 @Override
2004 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002005 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2006 return;
2007 }
2008
Sunny Goyal45478022015-06-08 16:52:41 -07002009 if (mDragController.isDragging()) {
2010 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002011 return;
2012 }
2013
Jon Mirandafeba90f2016-10-06 10:53:29 -07002014 // Note: There should be at most one log per method call. This is enforced implicitly
2015 // by using if-else statements.
2016 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002017 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2018 if (topView != null) {
2019 if (topView.getActiveTextView() != null) {
2020 topView.getActiveTextView().dispatchBackKey();
2021 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002022 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302023 ued.logActionCommand(Action.Command.BACK,
2024 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002025 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302026 ued.logActionCommand(Action.Command.BACK,
2027 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002028 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002029 topView.close(true);
2030 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002031 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302032 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002033 showWorkspace(true);
2034 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302035 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002036 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002037 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302038 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002039 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002040 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002041 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002042 mWorkspace.exitWidgetResizeMode();
2043
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002044 // Back button is a no-op here, but give at least some feedback for the button press
2045 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002046 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002047 }
2048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 * Launches the intent referred by the clicked shortcut.
2051 *
2052 * @param v The view representing the clicked shortcut.
2053 */
2054 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002055 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2056 // view has detached (it's possible for this to happen if the view is removed mid touch).
2057 if (v.getWindowToken() == null) {
2058 return;
2059 }
2060
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002061 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002062 return;
2063 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002064
Adam Cohen1697b792013-09-17 19:08:21 -07002065 if (v instanceof Workspace) {
2066 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08002067 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2068 LauncherLogProto.Action.Direction.NONE,
2069 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002070 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002071 }
2072 return;
2073 }
2074
2075 if (v instanceof CellLayout) {
2076 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002077 int page = mWorkspace.indexOfChild(v);
2078 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2079 LauncherLogProto.Action.Direction.NONE,
2080 LauncherLogProto.ContainerType.OVERVIEW, page);
2081 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002082 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002083 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002084 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002085 }
2086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002088 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002089 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002091 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002092 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002093 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07002094 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002095 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002096 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002097 } else if (tag instanceof AppInfo) {
2098 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002099 } else if (tag instanceof LauncherAppWidgetInfo) {
2100 if (v instanceof PendingAppWidgetHostView) {
2101 onClickPendingWidget((PendingAppWidgetHostView) v);
2102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104 }
2105
Sunny Goyal70660032015-05-14 00:07:08 -07002106 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002107 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002108 return false;
2109 }
2110
Michael Jurkaaf442092010-06-10 17:01:57 -07002111 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002112 * Event handler for the app widget view which has not fully restored.
2113 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002114 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002115 if (mIsSafeModeEnabled) {
2116 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2117 return;
2118 }
2119
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002120 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002121 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002122 LauncherAppWidgetProviderInfo appWidgetInfo =
2123 mAppWidgetManager.findProvider(info.providerName, info.user);
2124 if (appWidgetInfo == null) {
2125 return;
2126 }
2127 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2128
Sunny Goyald478c832016-04-01 12:04:16 -07002129 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2130 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2131 // This should not happen, as we make sure that an Id is allocated during bind.
2132 return;
2133 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002134 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2135 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002136 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002137 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002138 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002139 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002140 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002141 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002142 }
2143 }
2144
2145 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002146 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2147 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2148 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002149 *
2150 * @param v The view that was clicked.
2151 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002152 protected void onClickAllAppsButton(View v) {
2153 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002154 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302155 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2156 ControlType.ALL_APPS_BUTTON);
Sunny Goyal5606e072017-06-15 14:24:21 -07002157 showAppsView(true /* animated */, true /* updatePredictedApps */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002158 } else {
2159 showWorkspace(true);
2160 }
2161 }
2162
Sunny Goyale6e72002017-01-12 16:55:36 -08002163 private void onClickPendingAppItem(final View v, final String packageName,
2164 boolean downloadStarted) {
2165 if (downloadStarted) {
2166 // If the download has started, simply direct to the market app.
2167 startMarketIntentForPackage(v, packageName);
2168 return;
2169 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002170 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002171 .setTitle(R.string.abandoned_promises_title)
2172 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002173 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2174 @Override
2175 public void onClick(DialogInterface dialogInterface, int i) {
2176 startMarketIntentForPackage(v, packageName);
2177 }
2178 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002179 .setNeutralButton(R.string.abandoned_clean_this,
2180 new DialogInterface.OnClickListener() {
2181 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002182 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002183 mWorkspace.removeAbandonedPromise(packageName, user);
2184 }
2185 })
2186 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002187 }
2188
2189 private void startMarketIntentForPackage(View v, String packageName) {
2190 ItemInfo item = (ItemInfo) v.getTag();
2191 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2192 boolean success = startActivitySafely(v, intent, item);
2193 if (success && v instanceof BubbleTextView) {
2194 mWaitingForResume = (BubbleTextView) v;
2195 mWaitingForResume.setStayPressed(true);
2196 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002197 }
2198
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002199 /**
2200 * Event handler for an app shortcut click.
2201 *
2202 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2203 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002204 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002205 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2206 Object tag = v.getTag();
2207 if (!(tag instanceof ShortcutInfo)) {
2208 throw new IllegalArgumentException("Input must be a Shortcut");
2209 }
2210
2211 // Open shortcut
2212 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002213
2214 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002215 if ((shortcut.isDisabled &
2216 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2217 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2218 // If the app is only disabled because of the above flags, launch activity anyway.
2219 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002220 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002221 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2222 // Use a message specific to this shortcut, if it has one.
2223 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2224 return;
2225 }
2226 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002227 int error = R.string.activity_not_available;
2228 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2229 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002230 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2231 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002232 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002233 }
2234 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2235 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002236 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002237 }
2238
Chris Wren40c5ed32014-06-24 18:24:23 -04002239 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002240 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002241 String packageName = shortcut.intent.getComponent() != null ?
2242 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2243 if (!TextUtils.isEmpty(packageName)) {
2244 onClickPendingAppItem(v, packageName,
2245 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2246 return;
2247 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002248 }
2249
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002250 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002251 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002252 }
2253
Sunny Goyala7ce1662016-05-31 15:01:35 -07002254 private void startAppShortcutOrInfoActivity(View v) {
2255 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002256 Intent intent;
2257 if (item instanceof PromiseAppInfo) {
2258 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2259 intent = promiseAppInfo.getMarketIntent();
2260 } else {
2261 intent = item.getIntent();
2262 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002263 if (intent == null) {
2264 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002265 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002266 boolean success = startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002267 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002268
2269 if (success && v instanceof BubbleTextView) {
2270 mWaitingForResume = (BubbleTextView) v;
2271 mWaitingForResume.setStayPressed(true);
2272 }
2273 }
2274
2275 /**
2276 * Event handler for a folder icon click.
2277 *
2278 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2279 */
2280 protected void onClickFolderIcon(View v) {
2281 if (LOGD) Log.d(TAG, "onClickFolder");
2282 if (!(v instanceof FolderIcon)){
2283 throw new IllegalArgumentException("Input must be a FolderIcon");
2284 }
2285
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002286 Folder folder = ((FolderIcon) v).getFolder();
2287 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002288 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002289 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002290 }
Michael Jurka5130e402011-10-13 04:55:35 -07002291 }
2292
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002293 /**
2294 * Event handler for the (Add) Widgets button that appears after a long press
2295 * on the home screen.
2296 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002297 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002298 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002299 if (mIsSafeModeEnabled) {
2300 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2301 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002302 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002303 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002304 }
2305
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002306 /**
2307 * Event handler for the wallpaper picker button that appears after a long press
2308 * on the home screen.
2309 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002310 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002311 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002312 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2313 return;
2314 }
2315
Tony Wickham785f7a52015-08-31 17:28:32 -07002316 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2317 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002318 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002319 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002320 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002321
2322 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002323 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2324 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002325 intent.setPackage(pickerPackage);
2326 }
2327
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002328 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002329 try {
2330 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2331 // If there is no target package, use the default intent chooser animation
2332 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2333 } catch (ActivityNotFoundException e) {
2334 setWaitingForResult(null);
2335 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2336 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002337 }
2338
2339 /**
2340 * Event handler for a click on the settings button that appears after a long press
2341 * on the home screen.
2342 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002343 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002344 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002345 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2346 .setPackage(getPackageName());
2347 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002348 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002349 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002350 }
2351
Tony Wickhame2217252016-03-22 16:34:23 -07002352 @Override
2353 public void onAccessibilityStateChanged(boolean enabled) {
2354 mDragLayer.onAccessibilityStateChanged(enabled);
2355 }
2356
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002357 /**
2358 * Called when the user stops interacting with the launcher.
2359 * This implies that the user is now on the homescreen and is not doing housekeeping.
2360 */
Adam Cohen9211d422014-10-07 18:14:53 -07002361 protected void onInteractionEnd() {
2362 if (mLauncherCallbacks != null) {
2363 mLauncherCallbacks.onInteractionEnd();
2364 }
2365 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002366
2367 /**
2368 * Called when the user starts interacting with the launcher.
2369 * The possible interactions are:
2370 * - open all apps
2371 * - reorder an app shortcut, or a widget
2372 * - open the overview mode.
2373 * This is a good time to stop doing things that only make sense
2374 * when the user is on the homescreen and not doing housekeeping.
2375 */
Adam Cohen9211d422014-10-07 18:14:53 -07002376 protected void onInteractionBegin() {
2377 if (mLauncherCallbacks != null) {
2378 mLauncherCallbacks.onInteractionBegin();
2379 }
2380 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002381
Winson Chungcd99cd32015-04-29 11:03:24 -07002382 /** Updates the interaction state. */
2383 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002384 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002385 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002386 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2387 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002388 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002389 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002390 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002391 onInteractionEnd();
2392 }
2393 }
2394
Sunny Goyala7ce1662016-05-31 15:01:35 -07002395 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002397 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2398 try {
2399 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2400 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2401 // is enabled by default on NYC.
2402 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2403 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002404
2405 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2406 String id = ((ShortcutInfo) info).getDeepShortcutId();
2407 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302408 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002409 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002410 } else {
2411 // Could be launching some bookkeeping activity
2412 startActivity(intent, optsBundle);
2413 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002414 } finally {
2415 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002418 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2419 // corresponding permission. Show the appropriate permission prompt if that
2420 // is the case.
2421 if (intent.getComponent() == null
2422 && Intent.ACTION_CALL.equals(intent.getAction())
2423 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2424 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002425
2426 setWaitingForResult(PendingRequestArgs
2427 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002428 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2429 REQUEST_PERMISSION_CALL_PHONE);
2430 } else {
2431 // No idea why this was thrown.
2432 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002433 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 }
2435 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002436
Sunny Goyalfe770c92016-09-27 14:38:58 -07002437 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002438 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002439 if (Utilities.ATLEAST_MARSHMALLOW) {
2440 int left = 0, top = 0;
2441 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002442 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002443 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002444 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002445 if (icon != null) {
2446 Rect bounds = icon.getBounds();
2447 left = (width - bounds.width()) / 2;
2448 top = v.getPaddingTop();
2449 width = bounds.width();
2450 height = bounds.height();
2451 }
2452 }
2453 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2454 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2455 // On L devices, we use the device default slide-up transition.
2456 // On L MR1 devices, we use a custom version of the slide-up transition which
2457 // doesn't have the delay present in the device default.
2458 return ActivityOptions.makeCustomAnimation(
2459 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2460 }
2461 return null;
2462 }
2463
Tonye3c59252017-05-02 21:32:27 -07002464 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002465 int[] pos = new int[2];
2466 v.getLocationOnScreen(pos);
2467 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2468 }
2469
Sunny Goyala7ce1662016-05-31 15:01:35 -07002470 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002471 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2472 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2473 return false;
2474 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002475 // Only launch using the new animation if the shortcut has not opted out (this is a
2476 // private contract between launcher and may be ignored in the future).
2477 boolean useLaunchAnimation = (v != null) &&
2478 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2479 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2480
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002481 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002482
2483 // Prepare intent
2484 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2485 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002486 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002487 }
2488 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002489 if (Utilities.ATLEAST_MARSHMALLOW
2490 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002491 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002492 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002493 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002494 // Shortcuts need some special checks due to legacy reasons.
2495 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002496 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002497 // Could be launching some bookkeeping activity
2498 startActivity(intent, optsBundle);
2499 } else {
2500 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2501 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2502 }
2503 return true;
2504 } catch (ActivityNotFoundException|SecurityException e) {
2505 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2506 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2507 }
2508 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002509 }
2510
Tony Wickhamdadb3042016-02-24 11:07:00 -08002511 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002512 public boolean dispatchTouchEvent(MotionEvent ev) {
2513 mLastDispatchTouchEventX = ev.getX();
2514 return super.dispatchTouchEvent(ev);
2515 }
2516
2517 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002519 if (!isDraggingEnabled()) return false;
2520 if (isWorkspaceLocked()) return false;
2521 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522
Jon Miranda51f037d2016-11-07 08:37:20 -08002523 boolean ignoreLongPressToOverview =
2524 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002525
Adam Cohen1697b792013-09-17 19:08:21 -07002526 if (v instanceof Workspace) {
2527 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002528 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302529 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2530 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002531 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002532 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002533 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2534 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2535 return true;
2536 } else {
2537 return false;
2538 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002539 } else {
2540 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002541 }
Adam Cohen1697b792013-09-17 19:08:21 -07002542 }
2543
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002544 CellLayout.CellInfo longClickCellInfo = null;
2545 View itemUnderLongClick = null;
2546 if (v.getTag() instanceof ItemInfo) {
2547 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002548 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002549 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002550 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552
Winson Chung3d503fb2011-07-13 17:25:49 -07002553 // The hotseat touch handling does not go through Workspace, and we always allow long press
2554 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002555 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002556 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002557 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002558 if (mWorkspace.isInOverviewMode()) {
2559 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302560 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2561 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002562 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002563 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002564 return false;
2565 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302566 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2567 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002568 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002569 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002570 }
Jon Miranda379198e2016-09-23 08:54:40 -07002571 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2572 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002574 final boolean isAllAppsButton =
2575 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2576 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2577 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002578 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002580 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 }
2582 }
2583 }
2584 return true;
2585 }
2586
Winson Chung3d503fb2011-07-13 17:25:49 -07002587 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002588 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002589 return mHotseat != null && layout != null &&
2590 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2591 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002592
Winson Chung3d503fb2011-07-13 17:25:49 -07002593 /**
2594 * Returns the CellLayout of the specified container at the specified screen.
2595 */
Sunny Goyal92820592015-03-02 11:31:35 -08002596 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002597 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2598 if (mHotseat != null) {
2599 return mHotseat.getLayout();
2600 } else {
2601 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002602 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002603 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002604 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002605 }
2606 }
2607
Winson Chungb745afb2015-03-02 11:51:23 -08002608 /**
2609 * For overridden classes.
2610 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002611 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002612 return isAppsViewVisible();
2613 }
2614
2615 public boolean isAppsViewVisible() {
2616 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2617 }
2618
2619 public boolean isWidgetsViewVisible() {
2620 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002621 }
2622
Michael Jurkae326f182011-11-21 14:05:46 -08002623 @Override
2624 public void onTrimMemory(int level) {
2625 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002626 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2627 // The widget preview db can result in holding onto over
2628 // 3MB of memory for caching which isn't necessary.
2629 SQLiteDatabase.releaseMemory();
2630
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002631 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002632 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002633 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002634 if (mLauncherCallbacks != null) {
2635 mLauncherCallbacks.onTrimMemory(level);
2636 }
Michael Jurkae326f182011-11-21 14:05:46 -08002637 }
2638
Winson5c6bdbb2015-09-03 11:36:19 -07002639 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002640 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002641 }
2642
Winson5c6bdbb2015-09-03 11:36:19 -07002643 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002644 boolean changed = mState != State.WORKSPACE ||
2645 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002646 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002647 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002648 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002649 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002650
Michael Jurkab3e22d92011-10-31 15:58:33 -07002651 // Set focus to the AppsCustomize button
2652 if (mAllAppsButton != null) {
2653 mAllAppsButton.requestFocus();
2654 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002655 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002656
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002657 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002658 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002659
Winson Chung0f785722015-04-08 10:27:49 -07002660 if (changed) {
2661 // Send an accessibility event to announce the context change
2662 getWindow().getDecorView()
2663 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002664 }
Winson5c6bdbb2015-09-03 11:36:19 -07002665 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002666 }
2667
Winsone9f27272015-10-13 10:47:51 -07002668 /**
2669 * Shows the overview button.
2670 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002671 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002672 showOverviewMode(animated, false);
2673 }
2674
2675 /**
2676 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2677 * onto one of the overview panel buttons.
2678 */
2679 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2680 Runnable postAnimRunnable = null;
2681 if (requestButtonFocus) {
2682 postAnimRunnable = new Runnable() {
2683 @Override
2684 public void run() {
2685 // Hitting the menu button when in touch mode does not trigger touch mode to
2686 // be disabled, so if requested, force focus on one of the overview panel
2687 // buttons.
2688 mOverviewPanel.requestFocusFromTouch();
2689 }
2690 };
2691 }
Adam Cohened307df2013-10-02 09:37:31 -07002692 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002693 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002694 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002695 setState(State.WORKSPACE);
2696
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002697 // If animated from long press, then don't allow any of the controller in the drag
2698 // layer to intercept any remaining touch.
2699 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002700 }
2701
Mario Bertschleracbf5702017-03-03 10:58:06 -08002702 private void setState(State state) {
2703 this.mState = state;
2704 updateSoftInputMode();
2705 }
2706
2707 private void updateSoftInputMode() {
2708 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2709 final int mode;
2710 if (isAppsViewVisible()) {
2711 mode = SOFT_INPUT_MODE_ALL_APPS;
2712 } else {
2713 mode = SOFT_INPUT_MODE_DEFAULT;
2714 }
2715 getWindow().setSoftInputMode(mode);
2716 }
2717 }
2718
Winson Chungb745afb2015-03-02 11:51:23 -08002719 /**
2720 * Shows the apps view.
2721 */
Sunny Goyal5606e072017-06-15 14:24:21 -07002722 public void showAppsView(boolean animated, boolean updatePredictedApps) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002723 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002724 if (updatePredictedApps) {
2725 tryAndUpdatePredictedApps();
2726 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002727 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002728 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002729
Winson Chungb745afb2015-03-02 11:51:23 -08002730 /**
2731 * Shows the widgets view.
2732 */
2733 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002734 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002735 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002736 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002737 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002738 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002739
2740 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002741 @Override
2742 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002743 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002744 }
2745 });
Winson Chungb745afb2015-03-02 11:51:23 -08002746 }
2747
2748 /**
2749 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002750 *
2751 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002752 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002753 // TODO: calling method should use the return value so that when {@code false} is returned
2754 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002755 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002756 if (!(mState == State.WORKSPACE ||
2757 mState == State.APPS_SPRING_LOADED ||
2758 mState == State.WIDGETS_SPRING_LOADED ||
2759 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002760 return false;
2761 }
2762 if (toState != State.APPS && toState != State.WIDGETS) {
2763 return false;
2764 }
Winson Chungb745afb2015-03-02 11:51:23 -08002765
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002766 // This is a safe and supported transition to bypass spring_loaded mode.
2767 if (mExitSpringLoadedModeRunnable != null) {
2768 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2769 mExitSpringLoadedModeRunnable = null;
2770 }
2771
Winson Chungb745afb2015-03-02 11:51:23 -08002772 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002773 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002774 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002775 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002776 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002777
Michael Jurkab3e22d92011-10-31 15:58:33 -07002778 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002779 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002780 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002781
2782 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002783 getWindow().getDecorView()
2784 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002785 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002786 }
2787
Winson Chungcd99cd32015-04-29 11:03:24 -07002788 /**
2789 * Updates the workspace and interaction state on state change, and return the animation to this
2790 * new state.
2791 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002792 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002793 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002794 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002795 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002796 updateInteraction(fromState, toState);
2797 return anim;
2798 }
2799
Hyunyoung Song3f471442015-04-08 19:01:34 -07002800 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002801 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002802 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002803 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002804 }
Winson Chungb745afb2015-03-02 11:51:23 -08002805
Winson Chung006ee262015-08-03 14:40:11 -07002806 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002807 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002808 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002809 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002810 }
Adam Cohen7777d962011-08-18 18:58:38 -07002811
Hyunyoung Song3f471442015-04-08 19:01:34 -07002812 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002813 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002814 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002815
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002816 if (mExitSpringLoadedModeRunnable != null) {
2817 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2818 }
2819 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002820 @Override
2821 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002822 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002823 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2824 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002825 // Before we show workspace, hide all apps again because
2826 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2827 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07002828 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002829 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002830 } else {
2831 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002832 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002833 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002834 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002835 };
2836 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002837 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002838
Tony Wickhame0c33232016-02-08 11:37:04 -08002839 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07002840 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
2841 || mState == State.WIDGETS_SPRING_LOADED;
2842 }
2843
Sunny Goyal0f76b562016-12-13 19:37:10 -08002844 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08002845 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002846 showAppsView(true /* animated */, false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08002847 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002848 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07002849 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
2850 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002851 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002852 }
2853
Winson Chung4ac30062015-05-08 17:34:17 -07002854 /**
2855 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
2856 * resumed.
2857 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07002858 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07002859 if (mLauncherCallbacks != null) {
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002860 List<ComponentKeyMapper<AppInfo>> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07002861 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07002862 mAppsView.setPredictedApps(apps);
2863 }
2864 }
2865 }
2866
Michael Jurkad7c28052012-04-27 15:43:36 -07002867 @Override
2868 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002869 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002870 final List<CharSequence> text = event.getText();
2871 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002872 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08002873 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002874 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08002875 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002876 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002877 } else if (mWorkspace != null) {
2878 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002879 } else {
2880 text.add(getString(R.string.all_apps_home_button_label));
2881 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002882 return result;
2883 }
2884
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002885 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002886 * If the activity is currently paused, signal that we need to run the passed Runnable
2887 * in onResume.
2888 *
2889 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002890 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2891 * wrong when we're not running, and if the activity comes back to what the configuration was
2892 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002893 *
2894 * Implementation of the method from LauncherModel.Callbacks.
2895 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002896 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002897 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002898 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002899 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002900 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002901 if (run instanceof RunnableWithId) {
2902 // Remove any runnables which have the same id
2903 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002904 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002905 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002906 return true;
2907 } else {
2908 return false;
2909 }
2910 }
2911
Michael Jurka1e2f4652013-07-08 18:03:46 -07002912 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02002913 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002914 }
2915
Michael Jurka7607c2f2013-04-03 14:33:19 -07002916 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002917 * If the activity is currently paused, signal that we need to re-run the loader
2918 * in onResume.
2919 *
2920 * This needs to be called from incoming places where resources might have been loaded
2921 * while we are paused. That is becaues the Configuration might be wrong
2922 * when we're not running, and if it comes back to what it was when we
2923 * were paused, we are not restarted.
2924 *
2925 * Implementation of the method from LauncherModel.Callbacks.
2926 *
2927 * @return true if we are currently paused. The caller might be able to
2928 * skip some work in that case since we will come back again.
2929 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002930 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002931 public boolean setLoadOnResume() {
2932 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002933 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002934 mOnResumeNeedsLoad = true;
2935 return true;
2936 } else {
2937 return false;
2938 }
2939 }
2940
2941 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002942 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002944 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002946 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002947 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002948 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002949 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002951 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002952
Joe Onorato9c1289c2009-08-17 11:03:03 -04002953 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002954 * Clear any pending bind callbacks. This is called when is loader is planning to
2955 * perform a full rebind from scratch.
2956 */
2957 @Override
2958 public void clearPendingBinds() {
2959 mBindOnResumeCallbacks.clear();
2960 if (mPendingExecutor != null) {
2961 mPendingExecutor.markCompleted();
2962 mPendingExecutor = null;
2963 }
2964 }
2965
2966 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967 * Refreshes the shortcuts shown on the workspace.
2968 *
2969 * Implementation of the method from LauncherModel.Callbacks.
2970 */
2971 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002972 TraceHelper.beginSection("startBinding");
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002973 AbstractFloatingView.closeAllOpenViews(this);
2974
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002975 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002976
Winson Chungd64d1762013-08-20 14:37:16 -07002977 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002978 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002979 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002980
Winson Chung3d503fb2011-07-13 17:25:49 -07002981 if (mHotseat != null) {
2982 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002984 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 }
2986
Adam Cohendcd297f2013-06-18 13:13:40 -07002987 @Override
2988 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002989 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002990 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2991 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002992 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2993 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002994 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002995 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2996 // If there are no screens, we need to have an empty screen
2997 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002998 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002999 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003000
Winsonc7d2e832016-07-28 12:24:55 -07003001 // After we have added all the screens, if the wallpaper was locked to the default state,
3002 // then notify to indicate that it can be released and a proper wallpaper offset can be
3003 // computed before the next layout
3004 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003005 }
3006
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003007 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003008 int count = orderedScreenIds.size();
3009 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003010 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003011 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003012 // No need to bind the first screen, as its always bound.
3013 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3014 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003015 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003016 }
3017
Sunny Goyalb23980c2017-08-17 07:45:25 -07003018 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07003019 public void bindAppsAdded(final ArrayList<Long> newScreens,
3020 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07003021 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07003022 Runnable r = new Runnable() {
3023 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003024 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07003025 }
3026 };
3027 if (waitUntilResume(r)) {
3028 return;
3029 }
3030
Winson Chungd64d1762013-08-20 14:37:16 -07003031 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003032 if (newScreens != null) {
3033 bindAddScreens(newScreens);
3034 }
Winson Chungd64d1762013-08-20 14:37:16 -07003035
3036 // We add the items without animation on non-visible pages, and with
3037 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003038 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003039 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07003040 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003041 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003042 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07003043 }
Winson Chungc58497e2013-09-03 17:48:37 -07003044
Winson Chung87412982013-10-03 18:34:14 -07003045 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003046 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07003047 }
3048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003050 * Bind the items start-end from the list.
3051 *
3052 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003054 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07003055 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003056 Runnable r = new Runnable() {
3057 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003058 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003059 }
3060 };
3061 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003062 return;
3063 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003064
Sunny Goyal3be633b2016-12-08 09:59:25 -08003065 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003066 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003067 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07003068 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003069 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003070 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07003071 int end = items.size();
3072 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003073 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003074
3075 // Short circuit if we are loading dock items for a configuration which has no dock
3076 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3077 mHotseat == null) {
3078 continue;
3079 }
3080
Sunny Goyal639e9062015-08-19 19:17:06 -07003081 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 switch (item.itemType) {
3083 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3084 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003085 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003086 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003087 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003089 }
3090 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003091 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003092 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003093 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003095 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07003096 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
3097 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07003098 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003099 if (view == null) {
3100 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003101 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08003102 break;
3103 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003104 default:
3105 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003107
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003108 /*
3109 * Remove colliding items.
3110 */
3111 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3112 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3113 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3114 View v = cl.getChildAt(item.cellX, item.cellY);
3115 Object tag = v.getTag();
3116 String desc = "Collision while binding workspace item: " + item
3117 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003118 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003119 throw (new RuntimeException(desc));
3120 } else {
3121 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003122 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003123 continue;
3124 }
3125 }
3126 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303127 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003128 if (animateIcons) {
3129 // Animate all the applications up now
3130 view.setAlpha(0f);
3131 view.setScaleX(0f);
3132 view.setScaleY(0f);
3133 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003134 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003137
Winson Chung997a9232013-07-24 15:33:46 -07003138 if (animateIcons) {
3139 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003140 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003141 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003142 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003143 final Runnable startBounceAnimRunnable = new Runnable() {
3144 public void run() {
3145 anim.playTogether(bounceAnims);
3146 anim.start();
3147 }
3148 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003149 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003150 // We post the animation slightly delayed to prevent slowdowns
3151 // when we are loading right after we return to launcher.
3152 mWorkspace.postDelayed(new Runnable() {
3153 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003154 if (mWorkspace != null) {
3155 mWorkspace.snapToPage(newScreenIndex);
3156 mWorkspace.postDelayed(startBounceAnimRunnable,
3157 NEW_APPS_ANIMATION_DELAY);
3158 }
Winson Chung94d67682013-09-25 16:29:40 -07003159 }
3160 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003161 } else {
3162 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003163 }
3164 }
Winson Chung64359a52013-07-08 17:17:08 -07003165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 }
3168
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 /**
3170 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003172 public void bindAppWidget(LauncherAppWidgetInfo item) {
3173 View view = inflateAppWidget(item);
3174 if (view != null) {
3175 mWorkspace.addInScreen(view, item);
3176 mWorkspace.requestLayout();
3177 }
3178 }
3179
3180 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003181 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303182 PendingAppWidgetHostView view =
3183 new PendingAppWidgetHostView(this, item, mIconCache, true);
3184 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003185 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003186 }
3187
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003188 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003189
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003190 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003191
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003192 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3193 // If the provider is not ready, bind as a pending widget.
3194 appWidgetInfo = null;
3195 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3196 // The widget id is not valid. Try to find the widget based on the provider info.
3197 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3198 } else {
3199 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3200 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003201
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003202 // If the provider is ready, but the width is not yet restored, try to restore it.
3203 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3204 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003205 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003206 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3207 + " belongs to component " + item.providerName
3208 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003209 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003210 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003211 }
Sunny Goyalff572272014-07-23 13:58:07 -07003212
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003213 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003214 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003215 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3216 // Id has not been allocated yet. Allocate a new id.
3217 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3218 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003219
Sunny Goyald478c832016-04-01 12:04:16 -07003220 // Also try to bind the widget. If the bind fails, the user will be shown
3221 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003222 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003223 pendingInfo.spanX = item.spanX;
3224 pendingInfo.spanY = item.spanY;
3225 pendingInfo.minSpanX = item.minSpanX;
3226 pendingInfo.minSpanY = item.minSpanY;
3227 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003228
3229 boolean isDirectConfig =
3230 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3231 if (isDirectConfig && item.bindOptions != null) {
3232 Bundle newOptions = item.bindOptions.getExtras();
3233 if (options != null) {
3234 newOptions.putAll(options);
3235 }
3236 options = newOptions;
3237 }
Sunny Goyald478c832016-04-01 12:04:16 -07003238 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3239 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003240
Sunny Goyal86df1382016-08-10 15:03:22 -07003241 // We tried to bind once. If we were not able to bind, we would need to
3242 // go through the permission dialog, which means we cannot skip the config
3243 // activity.
3244 item.bindOptions = null;
3245 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3246
Sunny Goyald478c832016-04-01 12:04:16 -07003247 // Bind succeeded
3248 if (success) {
3249 // If the widget has a configure activity, it is still needs to set it up,
3250 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003251 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003252 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3253 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003254 }
Sunny Goyald478c832016-04-01 12:04:16 -07003255
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003256 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003257 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003258 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003259 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003260 // The widget was marked as UI not ready, but there is no configure activity to
3261 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003262 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003263 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003264 }
Sunny Goyalff572272014-07-23 13:58:07 -07003265 }
3266
Sunny Goyald5462aa2016-11-22 16:52:39 +05303267 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003268 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07003269 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003270 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003271 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003272 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003273 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003274 }
3275
Sunny Goyal233ee962015-08-03 13:05:01 -07003276 item.minSpanX = appWidgetInfo.minSpanX;
3277 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303278 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003279 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303280 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003281 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303282 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003283
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003284 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003285 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003286 }
3287
Sunny Goyalff572272014-07-23 13:58:07 -07003288 /**
3289 * Restores a pending widget.
3290 *
3291 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003292 */
Sunny Goyald478c832016-04-01 12:04:16 -07003293 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003294 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3295 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3296 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003297 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003298 }
3299
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003300 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003301 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003302 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3303 info.pendingItemInfo = null;
3304 }
Sunny Goyalff572272014-07-23 13:58:07 -07003305
3306 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003307 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003308 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003309 }
3310
Adam Cohen1462de32012-07-24 22:34:36 -07003311 public void onPageBoundSynchronously(int page) {
3312 mSynchronouslyBoundPages.add(page);
3313 }
3314
Sunny Goyal527c7d32015-08-28 15:19:36 -07003315 @Override
3316 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3317 if (mPendingExecutor != null) {
3318 mPendingExecutor.markCompleted();
3319 }
3320 mPendingExecutor = executor;
3321 executor.attachTo(this);
3322 }
3323
3324 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3325 if (mPendingExecutor == executor) {
3326 mPendingExecutor = null;
3327 }
3328 }
3329
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003330 @Override
3331 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3332 Runnable r = new Runnable() {
3333 public void run() {
3334 finishFirstPageBind(executor);
3335 }
3336 };
3337 if (waitUntilResume(r)) {
3338 return;
3339 }
3340
3341 Runnable onComplete = new Runnable() {
3342 @Override
3343 public void run() {
3344 if (executor != null) {
3345 executor.onLoadAnimationCompleted();
3346 }
3347 }
3348 };
3349 if (mDragLayer.getAlpha() < 1) {
3350 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3351 } else {
3352 onComplete.run();
3353 }
3354 }
3355
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 /**
3357 * Callback saying that there aren't any more items to bind.
3358 *
3359 * Implementation of the method from LauncherModel.Callbacks.
3360 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003361 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003362 Runnable r = new Runnable() {
3363 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003364 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003365 }
3366 };
3367 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003368 return;
3369 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003370 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003371 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003373 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003374
Sunny Goyal2100c782016-08-22 16:00:03 -07003375 if (mPendingActivityResult != null) {
3376 handleActivityResult(mPendingActivityResult.requestCode,
3377 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3378 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003379 }
3380
Sunny Goyala474a9b2017-05-04 16:47:11 -07003381 InstallShortcutReceiver.disableAndFlushInstallQueue(
3382 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003383
Tony Wickham010d2552017-01-20 08:15:28 -08003384 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3385
Adam Cohen9211d422014-10-07 18:14:53 -07003386 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003387 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003388 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003389 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003390 }
3391
Winson Chunga2413752012-04-03 14:22:34 -07003392 private boolean canRunNewAppsAnimation() {
3393 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003394 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003395 }
3396
Winson Chungc9168342013-06-26 14:54:55 -07003397 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003398 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003399 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3400 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003401 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003402 return bounceAnim;
3403 }
3404
Adam Cohen27772732013-11-11 14:00:56 +00003405 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003406 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003407 }
3408
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003409 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 * Add the icons for all apps.
3411 *
3412 * Implementation of the method from LauncherModel.Callbacks.
3413 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003414 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003415 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3416 public void run() {
3417 bindAllApplications(apps);
3418 }
3419 };
3420 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003421 return;
3422 }
3423
Winson Chungb745afb2015-03-02 11:51:23 -08003424 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003425 Executor pendingExecutor = getPendingExecutor();
3426 if (pendingExecutor != null && mState != State.APPS) {
3427 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003428 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003429 return;
3430 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003431
Winson Chungb745afb2015-03-02 11:51:23 -08003432 mAppsView.setApps(apps);
3433 }
Adam Cohen9211d422014-10-07 18:14:53 -07003434 if (mLauncherCallbacks != null) {
3435 mLauncherCallbacks.bindAllApplications(apps);
3436 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 }
3438
3439 /**
Tony2917a8b2017-07-31 23:29:42 -07003440 * Returns an Executor that will run after the launcher is first drawn (including after the
3441 * initial fade in animation). Returns null if the first draw has already occurred.
3442 */
3443 public @Nullable Executor getPendingExecutor() {
3444 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3445 }
3446
3447 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003448 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3449 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3450 */
3451 @Override
3452 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003453 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003454 }
3455
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003456 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003457 * A package was updated.
3458 *
3459 * Implementation of the method from LauncherModel.Callbacks.
3460 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003461 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003462 Runnable r = new Runnable() {
3463 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003464 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003465 }
3466 };
3467 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003468 return;
3469 }
3470
Winson Chungb745afb2015-03-02 11:51:23 -08003471 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003472 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003473 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 }
3475
Sunny Goyal4390ace2014-10-13 11:33:11 -07003476 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003477 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3478 Runnable r = new Runnable() {
3479 public void run() {
3480 bindPromiseAppProgressUpdated(app);
3481 }
3482 };
3483 if (waitUntilResume(r)) {
3484 return;
3485 }
3486
3487 if (mAppsView != null) {
3488 mAppsView.updatePromiseAppProgress(app);
3489 }
3490 }
3491
3492 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003493 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3494 Runnable r = new Runnable() {
3495 public void run() {
3496 bindWidgetsRestored(widgets);
3497 }
3498 };
3499 if (waitUntilResume(r)) {
3500 return;
3501 }
3502 mWorkspace.widgetsRestored(widgets);
3503 }
3504
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003506 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003507 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003508 *
3509 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003510 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003511 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003512 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003513 Runnable r = new Runnable() {
3514 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003515 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003516 }
3517 };
3518 if (waitUntilResume(r)) {
3519 return;
3520 }
3521
Sunny Goyal4390ace2014-10-13 11:33:11 -07003522 if (!updated.isEmpty()) {
3523 mWorkspace.updateShortcuts(updated);
3524 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003525 }
3526
3527 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003528 * Update the state of a package, typically related to install state.
3529 *
3530 * Implementation of the method from LauncherModel.Callbacks.
3531 */
Sunny Goyale755d462014-07-22 13:48:29 -07003532 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003533 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3534 Runnable r = new Runnable() {
3535 public void run() {
3536 bindRestoreItemsChange(updates);
3537 }
3538 };
3539 if (waitUntilResume(r)) {
3540 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003541 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003542
Sunny Goyal756adbc2015-04-16 15:20:51 -07003543 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003544 }
3545
3546 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003547 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003548 * in addition to specific applications to remove, the reason being that
3549 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003550 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003551 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003553 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003554 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003555 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003556 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003557 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003558 }
Winson Chungd64d1762013-08-20 14:37:16 -07003559 };
3560 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003561 return;
3562 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003563 mWorkspace.removeItemsByMatcher(matcher);
3564 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003565 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003566
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003567 @Override
3568 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3569 Runnable r = new Runnable() {
3570 public void run() {
3571 bindAppInfosRemoved(appInfos);
3572 }
3573 };
3574 if (waitUntilResume(r)) {
3575 return;
Joe Onorato36115782010-06-17 13:28:48 -04003576 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003577
Winson Chungdf95eb12013-10-16 14:57:07 -07003578 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003579 if (mAppsView != null) {
3580 mAppsView.removeApps(appInfos);
Hyunyoung Song51114f22017-05-02 16:06:28 -07003581 tryAndUpdatePredictedApps();
Winson Chungc58497e2013-09-03 17:48:37 -07003582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 }
Joe Onoratobe386092009-11-17 17:32:16 -08003584
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003585 @Override
3586 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3587 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3588 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003589 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003590 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003591 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003592 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003593 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003594 return;
3595 }
3596
Sunny Goyald164b7f2016-10-12 20:49:31 -07003597 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003598 Executor pendingExecutor = getPendingExecutor();
3599 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003600 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003601 return;
3602 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003603 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003604 }
Tony Wickham26b17462017-03-20 17:12:24 -07003605
3606 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3607 if (topView != null) {
3608 topView.onWidgetsBound();
3609 }
3610 }
3611
3612 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3613 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003614 }
3615
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003616 @Override
3617 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003618 if (mWorkspace.getState().shouldUpdateWidget) {
3619 refreshAndBindWidgetsForPackageUser(null);
3620 }
Tony Wickham26b17462017-03-20 17:12:24 -07003621 }
3622
Tony Wickham86222d22017-03-29 15:30:43 -07003623 /**
3624 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3625 * refreshes the widgets and shortcuts associated with the given package/user
3626 */
3627 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003628 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003629 }
3630
Winson Chung4b919f82012-05-01 10:44:08 -07003631 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003632 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003633 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003634 }
3635 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003636
Winson Chung4b919f82012-05-01 10:44:08 -07003637 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003638 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003639 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003640 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003641 } else {
3642 mHandler.postDelayed(new Runnable() {
3643 public void run() {
3644 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3645 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003646 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003647 }
Winson Chung4b919f82012-05-01 10:44:08 -07003648 }
Winson Chung400438b2011-01-16 17:53:48 -08003649 }
3650
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003651 private void markAppsViewShown() {
3652 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3653 return;
3654 }
3655 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3656 }
3657
3658 private boolean shouldShowDiscoveryBounce() {
Adam Cohen39933482017-09-29 16:43:51 -07003659 UserManagerCompat um = UserManagerCompat.getInstance(this);
3660 return mState == State.WORKSPACE && !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false) && !um.isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003661 }
3662
Winson Chung80baf5a2010-08-09 16:03:15 -07003663 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003664 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003665 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003666 @Override
3667 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3668 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003669
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003670 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3671 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003672 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003673 writer.println(prefix + " Homescreen " + i);
3674
3675 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3676 for (int j = 0; j < layout.getChildCount(); j++) {
3677 Object tag = layout.getChildAt(j).getTag();
3678 if (tag != null) {
3679 writer.println(prefix + " " + tag.toString());
3680 }
3681 }
3682 }
3683
3684 writer.println(prefix + " Hotseat");
3685 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003686 for (int j = 0; j < layout.getChildCount(); j++) {
3687 Object tag = layout.getChildAt(j).getTag();
3688 if (tag != null) {
3689 writer.println(prefix + " " + tag.toString());
3690 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003691 }
Sunny Goyala1365452015-10-01 15:46:24 -07003692
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003693 try {
3694 FileLog.flushAll(writer);
3695 } catch (Exception e) {
3696 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003697 }
3698 }
3699
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003700 writer.println(prefix + "Misc:");
3701 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3702 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3703 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3704
3705 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003706
Adam Cohen9211d422014-10-07 18:14:53 -07003707 if (mLauncherCallbacks != null) {
3708 mLauncherCallbacks.dump(prefix, fd, writer, args);
3709 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003710 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003711
Sunny Goyal66b24572016-09-21 15:57:55 -07003712 @Override
3713 @TargetApi(Build.VERSION_CODES.N)
3714 public void onProvideKeyboardShortcuts(
3715 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3716
3717 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3718 if (mState == State.WORKSPACE) {
3719 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3720 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3721 }
3722 View currentFocus = getCurrentFocus();
3723 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3724 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3725 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3726 }
Tony18c4aa42017-05-10 21:23:57 -05003727 if (currentFocus.getTag() instanceof ItemInfo
3728 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003729 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3730 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3731 }
3732 if (!shortcutInfos.isEmpty()) {
3733 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3734 }
3735
3736 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3737 }
3738
3739 @Override
3740 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3741 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3742 switch (keyCode) {
3743 case KeyEvent.KEYCODE_A:
3744 if (mState == State.WORKSPACE) {
Sunny Goyal5606e072017-06-15 14:24:21 -07003745 showAppsView(true, true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003746 return true;
3747 }
3748 break;
3749 case KeyEvent.KEYCODE_S: {
3750 View focusedView = getCurrentFocus();
3751 if (focusedView instanceof BubbleTextView
3752 && focusedView.getTag() instanceof ItemInfo
3753 && mAccessibilityDelegate.performAction(focusedView,
3754 (ItemInfo) focusedView.getTag(),
3755 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08003756 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003757 return true;
3758 }
3759 break;
3760 }
3761 case KeyEvent.KEYCODE_O:
3762 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3763 return true;
3764 }
3765 break;
3766 }
3767 }
3768 return super.onKeyShortcut(keyCode, event);
3769 }
3770
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003771 public static Launcher getLauncher(Context context) {
3772 if (context instanceof Launcher) {
3773 return (Launcher) context;
3774 }
3775 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3776 }
3777
Sunny Goyal5a81c382017-03-20 15:08:06 -07003778 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003779
3780 @Override
3781 public void onSharedPreferenceChanged(
3782 SharedPreferences sharedPreferences, String key) {
3783 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003784 // Recreate the activity so that it initializes the rotation preference again.
3785 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003786 }
3787 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003789}