blob: 775dad2c8003512c42ef4cccf779a4ee86d0a24b [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 Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyalaeb16432017-10-16 11:46:41 -070022import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_NEXT_FRAME;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070023import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyalaeb16432017-10-16 11:46:41 -070024import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070025import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
26import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
27
Sunny Goyal28c6b962015-10-12 11:42:05 -070028import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070030import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070032import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070034import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000035import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070036import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040037import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080039import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070040import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080043import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040044import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070046import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040047import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070049import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070050import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070051import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070052import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070054import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070055import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070062import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070065import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070066import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070072import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070076import android.view.KeyboardShortcutGroup;
77import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080078import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070085import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal651077b2014-06-30 14:15:31 -070090import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070091import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070092import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070093import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070096import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010097import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070098import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -070099import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700101import com.android.launcher3.dragndrop.DragController;
102import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700103import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800105import com.android.launcher3.dragndrop.PinItemDragListener;
Mario Bertschler27288382017-05-24 15:35:09 -0700106import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000107import com.android.launcher3.folder.Folder;
108import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700109import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700110import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700111import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700112import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700115import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700116import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800117import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700119import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700120import com.android.launcher3.states.AllAppsState;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800121import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530122import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
123import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
124import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700125import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700126import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700127import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700128import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700130import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800131import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700132import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700134import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700135import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700136import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700137import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700138import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700139import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700140import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800141import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700142import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800143import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700144import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700145import com.android.launcher3.widget.WidgetListRowEntry;
146import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700147import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700148
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700149import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700150import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700151import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700152import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700153import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700154import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800155import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700156import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158/**
159 * Default launcher application.
160 */
Sunny Goyal27835952017-01-13 12:15:53 -0800161public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700162 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
163 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700164 AccessibilityManager.AccessibilityStateChangeListener,
165 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700166 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700167 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chunga2413752012-04-03 14:22:34 -0700169 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700175 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Michael Jurka8b805b12012-04-18 14:23:14 -0700177 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700178 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
179 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700180
Jon Mirandac476d6e2017-05-04 16:30:01 -0700181 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700182
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700183 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
184
Mathew Inwood876a8462013-06-14 14:12:41 +0100185 /**
186 * IntentStarter uses request codes starting with this. This must be greater than all activity
187 * request codes used internally.
188 */
189 protected static final int REQUEST_LAST = 100;
190
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";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700203 // Type: SparseArray<Parcelable>
204 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Sunny Goyalbe93f262017-10-20 17:05:27 -0700206 private LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chunga2413752012-04-03 14:22:34 -0700212 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700213 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
214 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
215 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700218 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800220 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700221
222 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700223
Sunny Goyalffe83f12014-08-14 17:39:34 -0700224 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700227 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700228
Sunny Goyal316490e2015-06-02 09:38:28 -0700229 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800230 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700231
Michael Jurka838a4ca2011-02-07 13:33:06 -0800232 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700233
Sunny Goyal47328fd2016-05-25 18:56:41 -0700234 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700235
236 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700237 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700238 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700239
Jon Miranda6bed3502017-09-19 14:43:17 -0700240 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
241 // that results in widgets being inflated in the wrong orientation.
242 private int mOrientation;
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private SpannableStringBuilder mDefaultKeySsb = null;
245
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800248 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700249 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700251 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700252 private OnResumeCallback mOnResumeCallback;
253
Sunny Goyal527c7d32015-08-28 15:19:36 -0700254 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700255
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800257 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800258 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700259 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700260 private final Handler mHandler = new Handler();
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700261 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262
Jon Miranda2d89ea82017-05-04 11:47:53 -0700263 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700264 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700265
Tony Wickham010d2552017-01-20 08:15:28 -0800266 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700267
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700268 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700269
Winson Chung46353de2012-02-16 14:05:10 -0800270 // We only want to get the SharedPreferences once since it does an FS stat each time we get
271 // it from the context.
272 private SharedPreferences mSharedPrefs;
273
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700274 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
275 // state transition. If another state transition happened during this delay,
276 // simply unregister this runnable.
277 private Runnable mExitSpringLoadedModeRunnable;
278
Sunny Goyal2100c782016-08-22 16:00:03 -0700279 // Activity result which needs to be processed after workspace has loaded.
280 private ActivityResultInfo mPendingActivityResult;
281 /**
282 * Holds extra information required to handle a result from an external call, like
283 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
284 */
285 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800286
Jon Miranda379198e2016-09-23 08:54:40 -0700287 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700288
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700289 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700290 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700291 private boolean mAppLaunchSuccess;
292
Sunny Goyal745bad92016-05-02 10:54:12 -0700293 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 @Override
296 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700297 if (DEBUG_STRICT_MODE) {
298 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
299 .detectDiskReads()
300 .detectDiskWrites()
301 .detectNetwork() // or .detectAll() for all detectable problems
302 .penaltyLog()
303 .build());
304 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
305 .detectLeakedSqlLiteObjects()
306 .detectLeakedClosableObjects()
307 .penaltyLog()
308 .penaltyDeath()
309 .build());
310 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700311 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700312
Adam Cohen9211d422014-10-07 18:14:53 -0700313 if (mLauncherCallbacks != null) {
314 mLauncherCallbacks.preOnCreate();
315 }
316
Mario Bertschler27288382017-05-24 15:35:09 -0700317 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
318 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700319 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700320
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700322 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400323
Sunny Goyal87f784c2017-01-11 10:48:34 -0800324 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700325
Adam Cohen2e6da152015-05-06 11:42:25 -0700326 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800327 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700328 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700329 Display display = getWindowManager().getDefaultDisplay();
330 Point mwSize = new Point();
331 display.getSize(mwSize);
332 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
333 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700334
Jon Miranda6bed3502017-09-19 14:43:17 -0700335 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700336 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700337 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800338 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800339 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800340 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700341 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700342
Joe Onorato41a12d22009-10-31 18:30:00 -0400343 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700344 mAllAppsController = new AllAppsTransitionController(this);
345 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700346
Sunny Goyalffe83f12014-08-14 17:39:34 -0700347 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700348
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700349 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700350 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700351
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700352 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
353 // this also ensures that any synchronous binding below doesn't re-trigger another
354 // LauncherModel load.
355 mPaused = false;
356
Sunny Goyal60820d72017-05-09 12:40:11 -0700357 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 setupViews();
Winson1f064272016-07-18 17:18:02 -0700360 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361
Tony Wickham010d2552017-01-20 08:15:28 -0800362 mPopupDataProvider = new PopupDataProvider(this);
363
Tony Wickhame2217252016-03-22 16:34:23 -0700364 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
365 .addAccessibilityStateChangeListener(this);
366
Sunny Goyalfe770c92016-09-27 14:38:58 -0700367 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368
Sunny Goyal2100c782016-08-22 16:00:03 -0700369 // We only load the page synchronously if the user rotates (or triggers a
370 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700371 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
372 if (savedInstanceState != null) {
373 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
374 }
375 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700376 // If we are not binding synchronously, show a fade in animation when
377 // the first page bind completes.
378 mDragLayer.setAlpha(0);
379 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700380 // Pages bound synchronously.
381 mWorkspace.setCurrentPage(currentScreen);
382
Sunny Goyal2100c782016-08-22 16:00:03 -0700383 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385
386 // For handling default keys
387 mDefaultKeySsb = new SpannableStringBuilder();
388 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800389
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800390 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700391 // In case we are on a device with locked rotation, we should look at preferences to check
392 // if the user has specifically allowed rotation.
393 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700394 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700395 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
396 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700397 }
398
Sunny Goyal5a81c382017-03-20 15:08:06 -0700399 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
400 // Temporarily enable the rotation
401 mRotationEnabled = true;
402 }
403
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700404 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
405 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700406 setRequestedOrientation(mRotationEnabled
407 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700408
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800409 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700410
Jon Miranda7fda2852017-07-19 16:07:01 -0700411 // Listen for broadcasts
412 IntentFilter filter = new IntentFilter();
413 filter.addAction(Intent.ACTION_SCREEN_OFF);
414 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
415 registerReceiver(mReceiver, filter);
416 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700417
Sunny Goyal8392c822017-06-20 10:03:56 -0700418 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
419 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700420
421 if (mLauncherCallbacks != null) {
422 mLauncherCallbacks.onCreate(savedInstanceState);
423 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700424
425 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700426 }
427
Sunny Goyal7779d622015-06-11 16:18:39 -0700428 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700429 public void onThemeChanged() {
430 recreate();
431 }
432
Sunny Goyalbe93f262017-10-20 17:05:27 -0700433 public LauncherStateTransitionAnimation getStateTransition() {
434 return mStateTransitionAnimation;
435 }
436
Mario Bertschlera6936942017-05-31 14:48:19 -0700437 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700438 if (isDark) {
439 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700440 } else if (supportsDarkText) {
441 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700442 }
443 }
444
445 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700446 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800447 return mLauncherView.findViewById(id);
448 }
449
450 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700451 public void onAppWidgetHostReset() {
452 if (mAppWidgetHost != null) {
453 mAppWidgetHost.startListening();
454 }
455 }
456
Adam Cohen9211d422014-10-07 18:14:53 -0700457 private LauncherCallbacks mLauncherCallbacks;
458
Winson1f064272016-07-18 17:18:02 -0700459 public void onInsetsChanged(Rect insets) {
460 mDeviceProfile.updateInsets(insets);
461 mDeviceProfile.layout(this, true /* notifyListeners */);
462 }
463
Sunny Goyal32554d12015-12-03 15:31:25 -0800464 /**
465 * Call this after onCreate to set or clear overlay.
466 */
467 public void setLauncherOverlay(LauncherOverlay overlay) {
468 if (overlay != null) {
469 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
470 }
471 mWorkspace.setLauncherOverlay(overlay);
472 }
473
Adam Cohen9211d422014-10-07 18:14:53 -0700474 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
475 mLauncherCallbacks = callbacks;
476 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700477 }
478
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700479 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700480 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700481 if (mLauncherCallbacks != null) {
482 mLauncherCallbacks.onLauncherProviderChange();
483 }
484 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700485
Hyunyoung Song3f471442015-04-08 19:01:34 -0700486 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700487 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
488 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700489 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700490 }
491
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000492 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700493 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
494 // This cast is safe as long as the id < 0x00FFFFFF
495 // Since we jail all the dynamically generated views, there should be no clashes
496 // with any other views.
497 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000498 }
499
Tony Wickham010d2552017-01-20 08:15:28 -0800500 public PopupDataProvider getPopupDataProvider() {
501 return mPopupDataProvider;
502 }
503
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000504 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700505 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
506 * a configuration step, this allows the proper animations to run after other transitions.
507 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 private long completeAdd(
509 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
510 long screenId = info.screenId;
511 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700512 // When the screen id represents an actual screen (as opposed to a rank) we make sure
513 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700515 }
Adam Cohendb364c32014-05-20 14:23:40 -0700516
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700519 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700520 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700523 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700524 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700525 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700526 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700527 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700528 int widgetId = appWidgetId;
529 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700530 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700531 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700532 // Since the view was just bound, also launch the configure activity if needed
533 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
534 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800535 if (provider != null) {
536 new WidgetAddFlowHandler(provider)
537 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700538 }
539 }
540 break;
541 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800542 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700543
Adam Cohendb364c32014-05-20 14:23:40 -0700544 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800545 }
546
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800547 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700548 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700549 if (isWorkspaceLoading()) {
550 // process the result once the workspace has loaded.
551 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
552 return;
553 }
554 mPendingActivityResult = null;
555
Winson Chunge341d302013-08-16 14:31:00 -0700556 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700557 final PendingRequestArgs requestArgs = mPendingRequestArgs;
558 setWaitingForResult(null);
559 if (requestArgs == null) {
560 return;
561 }
562
563 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700564
Adam Cohenad4e15c2013-10-17 16:21:35 -0700565 Runnable exitSpringLoaded = new Runnable() {
566 @Override
567 public void run() {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700568 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700569 }
570 };
571
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700573 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
576 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700577 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700578 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700579 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700581 addAppWidgetImpl(
582 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800583 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700584 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 }
586 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200587 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyalf9403d92017-10-18 10:55:56 -0700588 if (resultCode == RESULT_OK && isInState(LauncherState.OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700589 // User could have free-scrolled between pages before picking a wallpaper; make sure
590 // we move to the closest one now.
591 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700592 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200593 }
594 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700595 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200596
Adam Cohened66b2b2012-01-23 17:28:51 -0800597 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700598 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700599
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 // We have special handling for widgets
601 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800602 final int appWidgetId;
603 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
604 : -1;
605 if (widgetId < 0) {
606 appWidgetId = pendingAddWidgetId;
607 } else {
608 appWidgetId = widgetId;
609 }
610
Adam Cohenad4e15c2013-10-17 16:21:35 -0700611 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800612 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700613 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
614 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700615 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700616 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700617 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700618 @Override
619 public void run() {
Sunny Goyalaeb16432017-10-16 11:46:41 -0700620 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_NEXT_FRAME);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700621 }
622 };
Adam Cohendb364c32014-05-20 14:23:40 -0700623
Sunny Goyal2100c782016-08-22 16:00:03 -0700624 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
625 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
626 } else {
627 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
628 // When the screen id represents an actual screen (as opposed to a rank)
629 // we make sure that the drop page actually exists.
630 requestArgs.screenId =
631 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700632 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700633 final CellLayout dropLayout =
634 mWorkspace.getScreenWithId(requestArgs.screenId);
635
636 dropLayout.setDropPending(true);
637 final Runnable onComplete = new Runnable() {
638 @Override
639 public void run() {
640 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
641 dropLayout.setDropPending(false);
642 }
643 };
644 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
645 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700646 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 return;
648 }
649
Sunny Goyald478c832016-04-01 12:04:16 -0700650 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
651 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700652 if (resultCode == RESULT_OK) {
653 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700655 }
656 // Leave the widget in the pending state if the user canceled the configure.
657 return;
658 }
659
Sunny Goyalaad90582015-07-09 14:22:50 -0700660 if (requestCode == REQUEST_CREATE_SHORTCUT) {
661 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
663 completeAdd(requestCode, data, -1, requestArgs);
664 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
665 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
666
Sunny Goyalaad90582015-07-09 14:22:50 -0700667 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700668 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
669 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800673 }
674
675 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700676 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800677 final int requestCode, final int resultCode, final Intent data) {
678 handleActivityResult(requestCode, resultCode, data);
679 if (mLauncherCallbacks != null) {
680 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
681 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 }
683
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700684 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700685 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600686 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 PendingRequestArgs pendingArgs = mPendingRequestArgs;
688 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
689 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
690 setWaitingForResult(null);
691
Sunny Goyal28c6b962015-10-12 11:42:05 -0700692 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700693 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700694 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700696 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 Intent intent = pendingArgs.getPendingIntent();
698
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 if (grantResults.length > 0
700 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700701 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700702 } else {
703 // TODO: Show a snack bar with link to settings
704 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700705 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700706 }
707 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600708 if (mLauncherCallbacks != null) {
709 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
710 grantResults);
711 }
712 }
713
Adam Cohendb364c32014-05-20 14:23:40 -0700714 /**
715 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
716 *
717 * @param screenId the screen id to check
718 * @return the new screen, or screenId if it exists
719 */
720 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800721 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700722 if (dropLayout == null) {
723 // it's possible that the add screen was removed because it was
724 // empty and a re-bind occurred
725 mWorkspace.addExtraEmptyScreen();
726 return mWorkspace.commitExtraEmptyScreen();
727 } else {
728 return screenId;
729 }
730 }
731
Sunny Goyal2100c782016-08-22 16:00:03 -0700732 @Thunk void completeTwoStageWidgetDrop(
733 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
734 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 Runnable onCompleteRunnable = null;
736 int animationType = 0;
737
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700738 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 if (resultCode == RESULT_OK) {
740 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
741 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800742 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 onCompleteRunnable = new Runnable() {
745 @Override
746 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700747 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700748 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 }
750 };
751 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800752 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700755 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700756 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700757 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
758 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700760 // The animated view may be null in the case of a rotation during widget configuration
761 onCompleteRunnable.run();
762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
764
765 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700766 protected void onStop() {
767 super.onStop();
768 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700769
770 if (mLauncherCallbacks != null) {
771 mLauncherCallbacks.onStop();
772 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700773
Sunny Goyalf5e37442016-11-02 10:31:24 -0700774 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700775 mAppWidgetHost.stopListening();
776 }
Tony Wickham010d2552017-01-20 08:15:28 -0800777
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700778 if (!mAppLaunchSuccess) {
779 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
780 mWorkspace.getState().containerType);
781 }
Tony Wickham010d2552017-01-20 08:15:28 -0800782 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700783 }
784
785 @Override
786 protected void onStart() {
787 super.onStart();
788 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700789
790 if (mLauncherCallbacks != null) {
791 mLauncherCallbacks.onStart();
792 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700793
Sunny Goyalf5e37442016-11-02 10:31:24 -0700794 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700795 mAppWidgetHost.startListening();
796 }
Tony Wickham010d2552017-01-20 08:15:28 -0800797
798 if (!isWorkspaceLoading()) {
799 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
800 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700801
Jon Miranda7fda2852017-07-19 16:07:01 -0700802 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700803 if (mScrimAnimator != null) {
804 mScrimAnimator.cancel();
805 }
806 mDragLayer.getBackground().setAlpha(0);
807 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
808 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
809 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
810 @Override
811 public void onAnimationEnd(Animator animation) {
812 mScrimAnimator = null;
813 }
814 });
815 mScrimAnimator.setDuration(600);
816 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
817 mScrimAnimator.start();
818 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700819 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700820 }
821
822 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700824 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700826 TraceHelper.partitionSection("ON_RESUME", "superCall");
827
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700828 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700829 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800830 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700831 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700832 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700833 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700834 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700836 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200837 // We might have postponed some bind calls until onResume (see waitUntilResume) --
838 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700839 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
840 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200841 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700842 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200843 }
Winson Chunge4e50662012-01-23 14:45:13 -0800844
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700845 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700846 // Process any items that were added while Launcher was away.
847 InstallShortcutReceiver.disableAndFlushInstallQueue(
848 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700849
Sunny Goyala474a9b2017-05-04 16:47:11 -0700850 // Refresh shortcuts if the permission changed.
851 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700852
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700853 if (shouldShowDiscoveryBounce()) {
854 mAllAppsController.showDiscoveryBounce();
855 }
Adam Cohen9211d422014-10-07 18:14:53 -0700856 if (mLauncherCallbacks != null) {
857 mLauncherCallbacks.onResume();
858 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800859
Sunny Goyala502aa32017-10-02 16:04:06 -0700860 clearTypedText();
861
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700862 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700863 }
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700866 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700867 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700868 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700869
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700870 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700871 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800872 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700873 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700874
Adam Cohen9211d422014-10-07 18:14:53 -0700875 if (mLauncherCallbacks != null) {
876 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700877 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700878 }
879
Adam Cohenc2d6e892014-10-16 09:49:24 -0700880 public interface LauncherOverlay {
881
882 /**
883 * Touch interaction leading to overscroll has begun
884 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700885 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700886
887 /**
888 * Touch interaction related to overscroll has ended
889 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700890 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700891
892 /**
893 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
894 * screen (or in the case of RTL, the rightmost screen).
895 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700896 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700897
898 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800899 * Called when the launcher is ready to use the overlay
900 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700901 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700902 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700903 }
904
905 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700906 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700907 }
908
909 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
910
Sunny Goyalc86df472016-02-25 09:19:38 -0800911 public void onScrollChanged(float progress) {
912 if (mWorkspace != null) {
913 mWorkspace.onOverlayScrollChanged(progress);
914 }
915 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700916 }
917
Jorim Jaggid017f882014-01-14 17:08:48 -0800918 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700919 if (mLauncherCallbacks != null) {
920 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700921 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800922 // On O and above we there is always some setting present settings (add icon to
923 // home screen or icon badging). On earlier APIs we will have the allow rotation
924 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700925 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700926 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800927 }
928
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700929 @Override
930 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700932 if (mModel.isCurrentCallbacks(this)) {
933 mModel.stopLoader();
934 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700935 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
936
Romain Guy13c2e7b2010-03-10 19:45:00 -0800937 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700938 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700939
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800940 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800941 @Override
942 public void onWindowFocusChanged(boolean hasFocus) {
943 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700944 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700945
946 if (mLauncherCallbacks != null) {
947 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
948 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800949 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 private boolean acceptFilter() {
952 final InputMethodManager inputManager = (InputMethodManager)
953 getSystemService(Context.INPUT_METHOD_SERVICE);
954 return !inputManager.isFullscreenMode();
955 }
956
957 @Override
958 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700959 final int uniChar = event.getUnicodeChar();
960 final boolean handled = super.onKeyDown(keyCode, event);
961 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
962 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
964 keyCode, event);
965 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700967 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 // showSearchDialog()
969 // If there are multiple keystrokes before the search dialog takes focus,
970 // onSearchRequested() will be called for every keystroke,
971 // but it is idempotent, so it's fine.
972 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974 }
975
Joe Onorato8a9625e2010-01-28 15:55:35 -0800976 // Eat the long press event so the keyboard doesn't come up.
977 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
978 return true;
979 }
980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 return handled;
982 }
983
Winson46163472015-09-22 17:31:56 -0700984 @Override
985 public boolean onKeyUp(int keyCode, KeyEvent event) {
986 if (keyCode == KeyEvent.KEYCODE_MENU) {
987 // Ignore the menu key if we are currently dragging or are on the custom content screen
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700988 if (!mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700989 // Close any open floating view
990 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -0700991
Winson46163472015-09-22 17:31:56 -0700992 // Show the overview mode if we are on the workspace
Sunny Goyalf9403d92017-10-18 10:55:56 -0700993 if (isInState(LauncherState.NORMAL) && !mWorkspace.isSwitchingState()) {
Winson46163472015-09-22 17:31:56 -0700994 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -0700995 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -0700996 }
997 }
998 return true;
999 }
1000 return super.onKeyUp(keyCode, event);
1001 }
1002
Karl Rosaen138a0412009-04-23 19:00:21 -07001003 private String getTypedText() {
1004 return mDefaultKeySsb.toString();
1005 }
1006
Sunny Goyal6f28e712016-09-13 14:19:29 -07001007 @Override
1008 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001009 mDefaultKeySsb.clear();
1010 mDefaultKeySsb.clearSpans();
1011 Selection.setSelection(mDefaultKeySsb, 0);
1012 }
1013
Sunny Goyalf9403d92017-10-18 10:55:56 -07001014 public boolean isInState(LauncherState state) {
1015 return mWorkspace.getState() == state;
1016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 /**
1019 * Restores the previous state, if it exists.
1020 *
1021 * @param savedState The previous state.
1022 */
1023 private void restoreState(Bundle savedState) {
1024 if (savedState == null) {
1025 return;
1026 }
1027
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07001028 int stateOrdinal = savedState.getInt(RUNTIME_STATE, LauncherState.NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001029 LauncherState[] stateValues = LauncherState.values();
1030 LauncherState state = stateValues[stateOrdinal];
1031 if (!state.doNotRestore) {
1032 if (state == LauncherState.ALL_APPS) {
1033 showAppsView(false /* animated */);
1034 } else {
1035 // TODO: Add logic for other states
1036 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001037 }
1038
Sunny Goyal2100c782016-08-22 16:00:03 -07001039 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1040 if (requestArgs != null) {
1041 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001043
1044 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001045
1046 SparseArray<Parcelable> widgetsState =
1047 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1048 if (widgetsState != null) {
1049 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 }
1052
1053 /**
1054 * Finds all the views we need and configure them properly.
1055 */
1056 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001057 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001058 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001059 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001060 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001061
Sunny Goyal784f9c32016-03-23 16:56:54 -07001062 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1063 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1064 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065
Winson Chung4c98d922011-05-31 16:50:48 -07001066 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001067 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001068
Winson Chung3d503fb2011-07-13 17:25:49 -07001069 // Setup the hotseat
1070 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1071 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001072 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001073 }
1074
Winsone9f27272015-10-13 10:47:51 -07001075 // Setup the overview panel
1076 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001077
Winson Chung4c98d922011-05-31 16:50:48 -07001078 // Setup the workspace
1079 mWorkspace.setHapticFeedbackEnabled(false);
1080 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001081 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001082 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1083 // default state, otherwise we will update to the wrong offsets in RTL
1084 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001085 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001086 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001087
Tony Wickham34d2c912015-09-11 09:27:58 -07001088 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001089 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001090
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001091 // Setup Apps
1092 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001093
Winson Chung3d503fb2011-07-13 17:25:49 -07001094 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001095 mDragController.setMoveTarget(mWorkspace);
1096 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001097 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001098
Hyunyoung Songd725f642017-08-17 22:26:35 -07001099 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001100
Sunny Goyal322d5562015-06-25 19:35:49 -07001101 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1102 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
1105
Winsone9f27272015-10-13 10:47:51 -07001106 private void setupOverviewPanel() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001107 mOverviewPanel = findViewById(R.id.overview_panel);
Winsone9f27272015-10-13 10:47:51 -07001108
Winsone9f27272015-10-13 10:47:51 -07001109 // Bind wallpaper button actions
1110 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301111 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001112 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001113 public void handleViewClick(View view) {
1114 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001115 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001116 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001117
1118 // Bind widget button actions
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301119 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001120 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001121 public void handleViewClick(View view) {
1122 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001123 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001124 }.attachTo(findViewById(R.id.widget_button));
Winsone9f27272015-10-13 10:47:51 -07001125
1126 // Bind settings actions
1127 View settingsButton = findViewById(R.id.settings_button);
1128 boolean hasSettings = hasSettings();
1129 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301130 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001131 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001132 public void handleViewClick(View view) {
1133 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001134 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001135 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001136 } else {
1137 settingsButton.setVisibility(View.GONE);
1138 }
1139
1140 mOverviewPanel.setAlpha(0f);
1141 }
1142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001144 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001145 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001146 */
1147 public void setAllAppsButton(View allAppsButton) {
1148 mAllAppsButton = allAppsButton;
1149 }
1150
Anjali Koppal5ad44842014-03-10 20:34:39 -07001151 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 * Creates a view representing a shortcut.
1153 *
1154 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001156 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001157 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 }
1159
1160 /**
1161 * Creates a view representing a shortcut inflated from the specified resource.
1162 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 * @param parent The group the shortcut belongs to.
1164 * @param info The data structure describing the shortcut.
1165 *
1166 * @return A View inflated from layoutResId.
1167 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001168 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001169 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1170 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001171 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001173 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 return favorite;
1175 }
1176
1177 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001178 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 *
1180 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001182 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001183 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001184 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1185 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001186 return;
1187 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001188
Jon Mirandac476d6e2017-05-04 16:30:01 -07001189 int[] cellXY = mTmpAddItemCellCoordinates;
1190 CellLayout layout = getCellLayout(container, screenId);
1191
Sunny Goyal782f0c92017-01-19 10:27:54 -08001192 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001193 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001194 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1195 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001196 }
1197
1198 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001199 // Legacy shortcuts are only supported for primary profile.
1200 info = Process.myUserHandle().equals(args.user)
1201 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001202
Sunny Goyalb57645f2017-03-20 13:57:28 -07001203 if (info == null) {
1204 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1205 return;
1206 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001207 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001208 // The app is trying to add a shortcut without sufficient permissions
1209 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1210 return;
1211 }
1212 }
1213
Jon Mirandac476d6e2017-05-04 16:30:01 -07001214 if (container < 0) {
1215 // Adding a shortcut to the Workspace.
1216 final View view = createShortcut(info);
1217 boolean foundCellSpan = false;
1218 // First we check if we already know the exact location where we want to add this item.
1219 if (cellX >= 0 && cellY >= 0) {
1220 cellXY[0] = cellX;
1221 cellXY[1] = cellY;
1222 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001223
Jon Mirandac476d6e2017-05-04 16:30:01 -07001224 // If appropriate, either create a folder or add to an existing folder
1225 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001226 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001227 return;
1228 }
1229 DragObject dragObject = new DragObject();
1230 dragObject.dragInfo = info;
1231 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1232 true)) {
1233 return;
1234 }
1235 } else {
1236 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1237 }
1238
1239 if (!foundCellSpan) {
1240 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001241 return;
1242 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001243
1244 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1245 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001246 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001247 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001248 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001249 if (folderIcon != null) {
1250 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1251 folderInfo.add(info, args.rank, false);
1252 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001253 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001254 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257
Sunny Goyale29897f52017-07-20 10:09:42 -07001258 public FolderIcon findFolderIcon(final long folderIconId) {
1259 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1260 @Override
1261 public boolean evaluate(ItemInfo info, View view) {
1262 return info != null && info.id == folderIconId;
1263 }
1264 });
1265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001268 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001270 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001272 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001273 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1274
Adam Cohened66b2b2012-01-23 17:28:51 -08001275 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001276 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001277 }
Romain Guycbb89e42009-06-08 15:52:54 -07001278
Adam Cohen59400422014-03-05 18:07:04 -08001279 LauncherAppWidgetInfo launcherInfo;
1280 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001281 launcherInfo.spanX = itemInfo.spanX;
1282 launcherInfo.spanY = itemInfo.spanY;
1283 launcherInfo.minSpanX = itemInfo.minSpanX;
1284 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001285 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001286
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001287 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001288 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289
Sunny Goyal2100c782016-08-22 16:00:03 -07001290 if (hostView == null) {
1291 // Perform actual inflation because we're live
1292 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001294 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301295 prepareAppWidget(hostView, launcherInfo);
1296 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
Romain Guycbb89e42009-06-08 15:52:54 -07001298
Sunny Goyald5462aa2016-11-22 16:52:39 +05301299 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001300 hostView.setTag(item);
1301 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001302 hostView.setFocusable(true);
1303 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001304 }
1305
Adam Cohended9f8d2010-11-03 13:25:16 -07001306 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1307 @Override
1308 public void onReceive(Context context, Intent intent) {
1309 final String action = intent.getAction();
1310 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001311 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001312 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001313 if (mAppsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001314 if (!showWorkspace(false)) {
1315 // If we are already on the workspace, then manually reset all apps
1316 mAppsView.reset();
1317 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001318 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001319 mShouldFadeInScrim = true;
1320 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1321 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1322 // the user unlocked and the Launcher is not in the foreground.
1323 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001324 }
1325 }
1326 };
1327
Tony Wickham010d2552017-01-20 08:15:28 -08001328 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1329 Runnable r = new Runnable() {
1330 @Override
1331 public void run() {
1332 mWorkspace.updateIconBadges(updatedBadges);
1333 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001334
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001335 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1336 if (popup instanceof PopupContainerWithArrow) {
1337 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001338 }
Tony Wickham010d2552017-01-20 08:15:28 -08001339 }
1340 };
1341 if (!waitUntilResume(r)) {
1342 r.run();
1343 }
1344 }
1345
Adam Cohended9f8d2010-11-03 13:25:16 -07001346 @Override
1347 public void onAttachedToWindow() {
1348 super.onAttachedToWindow();
1349
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001350 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001351 if (mLauncherCallbacks != null) {
1352 mLauncherCallbacks.onAttachedToWindow();
1353 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 }
1355
1356 @Override
1357 public void onDetachedFromWindow() {
1358 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001359
1360 if (mLauncherCallbacks != null) {
1361 mLauncherCallbacks.onDetachedFromWindow();
1362 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 }
1364
Winson Chunga6945242014-01-08 14:04:34 -08001365 public DragLayer getDragLayer() {
1366 return mDragLayer;
1367 }
1368
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001369 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001370 return mAppsView;
1371 }
1372
Winson Chunga6945242014-01-08 14:04:34 -08001373 public Workspace getWorkspace() {
1374 return mWorkspace;
1375 }
1376
1377 public Hotseat getHotseat() {
1378 return mHotseat;
1379 }
1380
Jorim Jaggid017f882014-01-14 17:08:48 -08001381 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001382 return mOverviewPanel;
1383 }
1384
Sunny Goyal47328fd2016-05-25 18:56:41 -07001385 public DropTargetBar getDropTargetBar() {
1386 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001387 }
1388
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001389 public LauncherAppWidgetHost getAppWidgetHost() {
1390 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
Romain Guycbb89e42009-06-08 15:52:54 -07001392
Winson Chunga9abd0e2010-10-27 17:18:37 -07001393 public LauncherModel getModel() {
1394 return mModel;
1395 }
1396
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001397 public ModelWriter getModelWriter() {
1398 return mModelWriter;
1399 }
1400
Adam Cohen79d90c52016-04-22 13:29:20 -07001401 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001402 return mSharedPrefs;
1403 }
1404
Jon Miranda6bed3502017-09-19 14:43:17 -07001405 public int getOrientation() { return mOrientation; }
1406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 @Override
1408 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001409 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 super.onNewIntent(intent);
1411
Winson15f8b172015-08-19 11:02:39 -07001412 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1413 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1414 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001415
1416 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyalf9403d92017-10-18 10:55:56 -07001417 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(LauncherState.NORMAL)
1418 && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001419
Winson15f8b172015-08-19 11:02:39 -07001420 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1421 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001422 if (mWorkspace == null) {
1423 // Can be cases where mWorkspace is null, this prevents a NPE
1424 return;
1425 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001426
1427 // Note: There should be at most one log per method call. This is enforced implicitly
1428 // by using if-else statements.
1429 UserEventDispatcher ued = getUserEventDispatcher();
Jon Mirandafeba90f2016-10-06 10:53:29 -07001430 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Sunny Goyal37920962017-09-28 13:43:24 -07001431 if (topOpenView != null) {
1432 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001433 } else if (alreadyOnHome) {
Sunny Goyalaeb16432017-10-16 11:46:41 -07001434 Target target = newContainerTarget(mWorkspace.getState().containerType);
1435 target.pageIndex = mWorkspace.getCurrentPage();
1436 ued.logActionCommand(Action.Command.HOME_INTENT, target);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001437 }
1438
1439 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001440 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Sunny Goyal53b99362017-10-05 14:58:56 -07001441 showWorkspace(alreadyOnHome /* animated */);
Adam Cohen6fecd412013-10-02 17:41:50 -07001442
1443 final View v = getWindow().peekDecorView();
1444 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001445 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001446 }
1447
Winson Chungb745afb2015-03-02 11:51:23 -08001448 // Reset the apps view
1449 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001450 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001451 }
1452
Adam Cohen9211d422014-10-07 18:14:53 -07001453 if (mLauncherCallbacks != null) {
1454 mLauncherCallbacks.onHomeIntent();
1455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001457 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001458
Adam Cohen9211d422014-10-07 18:14:53 -07001459 if (mLauncherCallbacks != null) {
1460 mLauncherCallbacks.onNewIntent(intent);
1461 }
Winson15f8b172015-08-19 11:02:39 -07001462
1463 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1464 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1465 // animation.
1466 if (isActionMain) {
Sunny Goyal00c95b82017-10-03 10:29:23 -07001467 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001468
Winson15f8b172015-08-19 11:02:39 -07001469 mWorkspace.post(new Runnable() {
1470 @Override
1471 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001472 if (mWorkspace != null) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001473 mWorkspace.moveToDefaultScreen();
Tonyc17390962015-10-25 17:39:37 -07001474 }
Winson15f8b172015-08-19 11:02:39 -07001475 }
1476 });
1477 }
1478 }
1479
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001480 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001481 }
1482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001484 public void onRestoreInstanceState(Bundle state) {
1485 super.onRestoreInstanceState(state);
1486 for (int page: mSynchronouslyBoundPages) {
1487 mWorkspace.restoreInstanceStateForChild(page);
1488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 @Override
1492 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001493 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001494 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001495
Adam Cohen21cd0022013-10-09 18:57:02 -07001496 }
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07001497 outState.putInt(RUNTIME_STATE, mWorkspace.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001498
1499
1500 AbstractFloatingView widgets = AbstractFloatingView
1501 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1502 if (widgets != null) {
1503 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1504 widgets.saveHierarchyState(widgetsState);
1505 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1506 } else {
1507 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1508 }
1509
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001510 // We close any open folders and shortcut containers since they will not be re-opened,
1511 // and we need to make sure this state is reflected.
1512 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001513
Sunny Goyal2100c782016-08-22 16:00:03 -07001514 if (mPendingRequestArgs != null) {
1515 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1516 }
1517 if (mPendingActivityResult != null) {
1518 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001521 super.onSaveInstanceState(outState);
1522
Adam Cohen9211d422014-10-07 18:14:53 -07001523 if (mLauncherCallbacks != null) {
1524 mLauncherCallbacks.onSaveInstanceState(outState);
1525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527
1528 @Override
1529 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001532 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001533 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001534
Winson Chungcd2b0142011-06-08 16:02:26 -07001535 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001536 // It's possible to receive onDestroy after a new Launcher activity has
1537 // been created. In this case, don't interfere with the new Launcher.
1538 if (mModel.isCurrentCallbacks(this)) {
1539 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001540 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001541 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001542
Sunny Goyal745bad92016-05-02 10:54:12 -07001543 if (mRotationPrefChangeHandler != null) {
1544 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001548 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001550 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001552 mAppWidgetHost = null;
1553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 TextKeyListener.getInstance().release();
1555
Tony Wickhame2217252016-03-22 16:34:23 -07001556 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1557 .removeAccessibilityStateChangeListener(this);
1558
Mario Bertschler27288382017-05-24 15:35:09 -07001559 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1560
Michael Jurka2ecf9952012-06-18 12:52:28 -07001561 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001562
Tony2917a8b2017-07-31 23:29:42 -07001563 clearPendingBinds();
1564
Adam Cohen9211d422014-10-07 18:14:53 -07001565 if (mLauncherCallbacks != null) {
1566 mLauncherCallbacks.onDestroy();
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569
Sunny Goyalae502842016-06-17 08:43:56 -07001570 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1571 return mAccessibilityDelegate;
1572 }
1573
Adam Cohena9cf38f2011-05-02 15:36:58 -07001574 public DragController getDragController() {
1575 return mDragController;
1576 }
1577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001579 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001580 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001581 }
1582
1583 @Override
1584 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1585 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001586 try {
1587 super.startIntentSenderForResult(intent, requestCode,
1588 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1589 } catch (IntentSender.SendIntentException e) {
1590 throw new ActivityNotFoundException();
1591 }
1592 }
1593
Winson Chung70d72102011-08-12 11:24:35 -07001594 /**
1595 * Indicates that we want global search for this activity by setting the globalSearch
1596 * argument for {@link #startSearch} to true.
1597 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001599 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001601
Karl Rosaen138a0412009-04-23 19:00:21 -07001602 if (initialQuery == null) {
1603 // Use any text typed in the launcher as the initial query
1604 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 if (appSearchData == null) {
1607 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001608 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001610
Sunny Goyal6f28e712016-09-13 14:19:29 -07001611 if (mLauncherCallbacks == null ||
1612 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1613 // Starting search from the callbacks failed. Start the default global search.
1614 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001615 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001616
1617 // We need to show the workspace after starting the search
1618 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001619 }
1620
Ian Parkinson047036e2014-09-01 15:40:53 +01001621 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001622 * Starts the global search activity. This code is a copied from SearchManager
1623 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001624 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001625 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001626 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001627 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1628 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1629 if (globalSearchActivity == null) {
1630 Log.w(TAG, "No global search activity found.");
1631 return;
1632 }
1633 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1634 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1635 intent.setComponent(globalSearchActivity);
1636 // Make sure that we have a Bundle to put source in
1637 if (appSearchData == null) {
1638 appSearchData = new Bundle();
1639 } else {
1640 appSearchData = new Bundle(appSearchData);
1641 }
Adam Cohen9211d422014-10-07 18:14:53 -07001642 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001643 if (!appSearchData.containsKey("source")) {
1644 appSearchData.putString("source", getPackageName());
1645 }
1646 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1647 if (!TextUtils.isEmpty(initialQuery)) {
1648 intent.putExtra(SearchManager.QUERY, initialQuery);
1649 }
1650 if (selectInitialQuery) {
1651 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1652 }
1653 intent.setSourceBounds(sourceBounds);
1654 try {
1655 startActivity(intent);
1656 } catch (ActivityNotFoundException ex) {
1657 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 }
1660
Winson Chung70d72102011-08-12 11:24:35 -07001661 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001662 public boolean onPrepareOptionsMenu(Menu menu) {
1663 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001664 if (mLauncherCallbacks != null) {
1665 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1666 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001667 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001670 @Override
1671 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001672 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001673 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001674 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001675 }
1676
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001678 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679 }
1680
Adam Cohen517a7f52014-03-01 12:12:59 -08001681 public boolean isWorkspaceLoading() {
1682 return mWorkspaceLoading;
1683 }
1684
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001685 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001686 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001687 }
1688
Sunny Goyal04a324a2017-01-20 21:08:59 -08001689 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001690 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001691 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001692
Sunny Goyal2100c782016-08-22 16:00:03 -07001693 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001694 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001695 if (LOGD) {
1696 Log.d(TAG, "Adding widget from drop");
1697 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001698 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001699 }
1700
Sunny Goyal2100c782016-08-22 16:00:03 -07001701 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001702 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1703 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1704 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001705
Adam Cohenad4e15c2013-10-17 16:21:35 -07001706 Runnable onComplete = new Runnable() {
1707 @Override
1708 public void run() {
1709 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal4c7f2152017-10-17 17:17:16 -07001710 exitSpringLoadedDragMode(SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001711 }
1712 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001713 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001714 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 }
1716 }
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001718 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1719 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001720 info.container = container;
1721 info.screenId = screenId;
1722 if (cell != null) {
1723 info.cellX = cell[0];
1724 info.cellY = cell[1];
1725 }
1726 info.spanX = spanX;
1727 info.spanY = spanY;
1728
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001729 switch (info.itemType) {
1730 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1731 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001732 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001733 break;
1734 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001735 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001736 break;
1737 default:
1738 throw new IllegalStateException("Unknown item type: " + info.itemType);
1739 }
1740 }
1741
Adam Cohenfbba09b2011-07-18 21:43:05 -07001742 /**
1743 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001744 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001745 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001746 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1747 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001748 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1749 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1750 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001751 }
1752
Adam Cohenfbba09b2011-07-18 21:43:05 -07001753 /**
1754 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001755 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001756 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001757 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001758 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001759 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001760 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001761 // In the case where we've prebound the widget, we remove it from the DragLayer
1762 if (LOGD) {
1763 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1764 }
1765 getDragLayer().removeView(hostView);
1766
Adam Cohened66b2b2012-01-23 17:28:51 -08001767 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001768 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001769
1770 // Clear the boundWidget so that it doesn't get destroyed.
1771 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001772 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001773 // In this case, we either need to start an activity to get permission to bind
1774 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001775 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1776 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1777 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1778 this, info.componentName);
1779 } else {
1780 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1781 }
Adam Cohendd70d662012-10-04 16:53:44 -07001782 Bundle options = info.bindOptions;
1783
Sunny Goyalffe83f12014-08-14 17:39:34 -07001784 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1785 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001786 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001787 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001788 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001789 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001790 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001791 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001792 }
1793
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001795 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001796 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 folderInfo.title = getText(R.string.folder_name);
1798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001800 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801
1802 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001803 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301804 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001805 // Force measure the new folder icon
1806 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1807 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001808 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Winsonc0b52fe2015-09-09 16:38:15 -07001811 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001812 * Unbinds the view for the specified item, and removes the item and all its children.
1813 *
1814 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001815 * @param itemInfo the {@link ItemInfo} for this view.
1816 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001817 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001818 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001819 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001820 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001821 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1822 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001823 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001824 } else {
1825 mWorkspace.removeWorkspaceItem(v);
1826 }
Winsonc0b52fe2015-09-09 16:38:15 -07001827 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001828 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001829 }
1830 } else if (itemInfo instanceof FolderInfo) {
1831 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001832 if (v instanceof FolderIcon) {
1833 ((FolderIcon) v).removeListeners();
1834 }
Winsonc0b52fe2015-09-09 16:38:15 -07001835 mWorkspace.removeWorkspaceItem(v);
1836 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001837 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001838 }
1839 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1840 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001841 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001842 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001843 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001844 }
1845 } else {
1846 return false;
1847 }
1848 return true;
1849 }
1850
1851 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001852 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001853 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001854 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001855 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001856 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001857 // Deleting an app widget ID is a void call but writes to disk before returning
1858 // to the caller...
1859 new AsyncTask<Void, Void, Void>() {
1860 public Void doInBackground(Void ... args) {
1861 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1862 return null;
1863 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001864 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001865 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001866 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 @Override
1870 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001871 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873
Joe Onorato88ec0992009-11-19 13:16:06 -08001874 @Override
1875 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001876 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1877 return;
1878 }
1879
Sunny Goyal45478022015-06-08 16:52:41 -07001880 if (mDragController.isDragging()) {
1881 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001882 return;
1883 }
1884
Jon Mirandafeba90f2016-10-06 10:53:29 -07001885 // Note: There should be at most one log per method call. This is enforced implicitly
1886 // by using if-else statements.
1887 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001888 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1889 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001890 topView.onBackPressed();
Sunny Goyalc99cb172017-10-19 16:15:09 -07001891 } else if (!isInState(LauncherState.NORMAL)) {
1892 ued.logActionCommand(Action.Command.BACK, mWorkspace.getState().containerType);
Winson Chungdc61c4d2015-04-20 18:26:57 -07001893 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001894 } else {
1895 // Back button is a no-op here, but give at least some feedback for the button press
1896 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001897 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 * Launches the intent referred by the clicked shortcut.
1902 *
1903 * @param v The view representing the clicked shortcut.
1904 */
1905 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001906 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1907 // view has detached (it's possible for this to happen if the view is removed mid touch).
1908 if (v.getWindowToken() == null) {
1909 return;
1910 }
1911
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001912 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001913 return;
1914 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001915
Adam Cohen1697b792013-09-17 19:08:21 -07001916 if (v instanceof Workspace) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07001917 if (isInState(LauncherState.OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001918 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1919 LauncherLogProto.Action.Direction.NONE,
1920 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07001921 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001922 }
1923 return;
1924 }
1925
1926 if (v instanceof CellLayout) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07001927 if (isInState(LauncherState.OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001928 int page = mWorkspace.indexOfChild(v);
1929 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1930 LauncherLogProto.Action.Direction.NONE,
1931 LauncherLogProto.ContainerType.OVERVIEW, page);
1932 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001933 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001934 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001935 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001939 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001940 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001942 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001943 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001944 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001945 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001946 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001947 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001948 } else if (tag instanceof AppInfo) {
1949 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001950 } else if (tag instanceof LauncherAppWidgetInfo) {
1951 if (v instanceof PendingAppWidgetHostView) {
1952 onClickPendingWidget((PendingAppWidgetHostView) v);
1953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955 }
1956
Sunny Goyal70660032015-05-14 00:07:08 -07001957 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001958 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001959 return false;
1960 }
1961
Michael Jurkaaf442092010-06-10 17:01:57 -07001962 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001963 * Event handler for the app widget view which has not fully restored.
1964 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001965 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001966 if (mIsSafeModeEnabled) {
1967 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1968 return;
1969 }
1970
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001971 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001972 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001973 LauncherAppWidgetProviderInfo appWidgetInfo =
1974 mAppWidgetManager.findProvider(info.providerName, info.user);
1975 if (appWidgetInfo == null) {
1976 return;
1977 }
1978 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1979
Sunny Goyald478c832016-04-01 12:04:16 -07001980 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1981 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1982 // This should not happen, as we make sure that an Id is allocated during bind.
1983 return;
1984 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001985 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1986 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001987 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001988 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001989 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001990 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001991 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001992 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001993 }
1994 }
1995
1996 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001997 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1998 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1999 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002000 *
2001 * @param v The view that was clicked.
2002 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002003 protected void onClickAllAppsButton(View v) {
2004 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyalf9403d92017-10-18 10:55:56 -07002005 if (!isInState(LauncherState.ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302006 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2007 ControlType.ALL_APPS_BUTTON);
Sunny Goyald0d07032017-10-04 10:40:28 -07002008 showAppsView(true /* animated */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002009 } else {
2010 showWorkspace(true);
2011 }
2012 }
2013
Sunny Goyale6e72002017-01-12 16:55:36 -08002014 private void onClickPendingAppItem(final View v, final String packageName,
2015 boolean downloadStarted) {
2016 if (downloadStarted) {
2017 // If the download has started, simply direct to the market app.
2018 startMarketIntentForPackage(v, packageName);
2019 return;
2020 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002021 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002022 .setTitle(R.string.abandoned_promises_title)
2023 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002024 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2025 @Override
2026 public void onClick(DialogInterface dialogInterface, int i) {
2027 startMarketIntentForPackage(v, packageName);
2028 }
2029 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002030 .setNeutralButton(R.string.abandoned_clean_this,
2031 new DialogInterface.OnClickListener() {
2032 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002033 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002034 mWorkspace.removeAbandonedPromise(packageName, user);
2035 }
2036 })
2037 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002038 }
2039
2040 private void startMarketIntentForPackage(View v, String packageName) {
2041 ItemInfo item = (ItemInfo) v.getTag();
2042 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002043 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002044 }
2045
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002046 /**
2047 * Event handler for an app shortcut click.
2048 *
2049 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2050 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002051 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002052 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2053 Object tag = v.getTag();
2054 if (!(tag instanceof ShortcutInfo)) {
2055 throw new IllegalArgumentException("Input must be a Shortcut");
2056 }
2057
2058 // Open shortcut
2059 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002060
2061 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002062 if ((shortcut.isDisabled &
2063 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2064 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2065 // If the app is only disabled because of the above flags, launch activity anyway.
2066 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002067 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002068 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2069 // Use a message specific to this shortcut, if it has one.
2070 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2071 return;
2072 }
2073 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002074 int error = R.string.activity_not_available;
2075 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2076 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002077 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2078 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002079 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002080 }
2081 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2082 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002083 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002084 }
2085
Chris Wren40c5ed32014-06-24 18:24:23 -04002086 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07002087 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08002088 String packageName = shortcut.intent.getComponent() != null ?
2089 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2090 if (!TextUtils.isEmpty(packageName)) {
2091 onClickPendingAppItem(v, packageName,
2092 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2093 return;
2094 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002095 }
2096
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002097 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002098 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002099 }
2100
Sunny Goyala7ce1662016-05-31 15:01:35 -07002101 private void startAppShortcutOrInfoActivity(View v) {
2102 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002103 Intent intent;
2104 if (item instanceof PromiseAppInfo) {
2105 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2106 intent = promiseAppInfo.getMarketIntent();
2107 } else {
2108 intent = item.getIntent();
2109 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002110 if (intent == null) {
2111 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002112 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002113 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002114 }
2115
2116 /**
2117 * Event handler for a folder icon click.
2118 *
2119 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2120 */
2121 protected void onClickFolderIcon(View v) {
2122 if (LOGD) Log.d(TAG, "onClickFolder");
2123 if (!(v instanceof FolderIcon)){
2124 throw new IllegalArgumentException("Input must be a FolderIcon");
2125 }
2126
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002127 Folder folder = ((FolderIcon) v).getFolder();
2128 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002129 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002130 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002131 }
Michael Jurka5130e402011-10-13 04:55:35 -07002132 }
2133
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002134 /**
2135 * Event handler for the (Add) Widgets button that appears after a long press
2136 * on the home screen.
2137 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002138 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002139 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002140 if (mIsSafeModeEnabled) {
2141 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2142 } else {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002143 WidgetsFullSheet.show(this, true /* animated */);
Adam Cohen9211d422014-10-07 18:14:53 -07002144 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002145 }
2146
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002147 /**
2148 * Event handler for the wallpaper picker button that appears after a long press
2149 * on the home screen.
2150 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002151 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002152 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002153 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2154 return;
2155 }
2156
Tony Wickham785f7a52015-08-31 17:28:32 -07002157 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2158 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002159 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002160 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002161 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002162
2163 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002164 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2165 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002166 intent.setPackage(pickerPackage);
2167 }
2168
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002169 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002170 try {
2171 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2172 // If there is no target package, use the default intent chooser animation
2173 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2174 } catch (ActivityNotFoundException e) {
2175 setWaitingForResult(null);
2176 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2177 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002178 }
2179
2180 /**
2181 * Event handler for a click on the settings button that appears after a long press
2182 * on the home screen.
2183 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002184 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002185 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002186 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2187 .setPackage(getPackageName());
2188 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002189 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002190 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002191 }
2192
Tony Wickhame2217252016-03-22 16:34:23 -07002193 @Override
2194 public void onAccessibilityStateChanged(boolean enabled) {
2195 mDragLayer.onAccessibilityStateChanged(enabled);
2196 }
2197
Sunny Goyala7ce1662016-05-31 15:01:35 -07002198 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002200 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2201 try {
2202 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2203 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2204 // is enabled by default on NYC.
2205 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2206 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002207
2208 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2209 String id = ((ShortcutInfo) info).getDeepShortcutId();
2210 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302211 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002212 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002213 } else {
2214 // Could be launching some bookkeeping activity
2215 startActivity(intent, optsBundle);
2216 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002217 } finally {
2218 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002221 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2222 // corresponding permission. Show the appropriate permission prompt if that
2223 // is the case.
2224 if (intent.getComponent() == null
2225 && Intent.ACTION_CALL.equals(intent.getAction())
2226 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2227 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002228
2229 setWaitingForResult(PendingRequestArgs
2230 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002231 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2232 REQUEST_PERMISSION_CALL_PHONE);
2233 } else {
2234 // No idea why this was thrown.
2235 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 }
2238 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002239
Sunny Goyalfe770c92016-09-27 14:38:58 -07002240 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002241 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002242 if (Utilities.ATLEAST_MARSHMALLOW) {
2243 int left = 0, top = 0;
2244 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002245 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002246 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002247 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002248 if (icon != null) {
2249 Rect bounds = icon.getBounds();
2250 left = (width - bounds.width()) / 2;
2251 top = v.getPaddingTop();
2252 width = bounds.width();
2253 height = bounds.height();
2254 }
2255 }
2256 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2257 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2258 // On L devices, we use the device default slide-up transition.
2259 // On L MR1 devices, we use a custom version of the slide-up transition which
2260 // doesn't have the delay present in the device default.
2261 return ActivityOptions.makeCustomAnimation(
2262 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2263 }
2264 return null;
2265 }
2266
Tonye3c59252017-05-02 21:32:27 -07002267 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002268 int[] pos = new int[2];
2269 v.getLocationOnScreen(pos);
2270 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2271 }
2272
Sunny Goyala7ce1662016-05-31 15:01:35 -07002273 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002274 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002275 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2276 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002277 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002278 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002279 // Only launch using the new animation if the shortcut has not opted out (this is a
2280 // private contract between launcher and may be ignored in the future).
2281 boolean useLaunchAnimation = (v != null) &&
2282 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2283 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2284
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002285 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002286
2287 // Prepare intent
2288 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2289 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002290 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002291 }
2292 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002293 if (Utilities.ATLEAST_MARSHMALLOW
2294 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002295 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002296 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002297 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002298 // Shortcuts need some special checks due to legacy reasons.
2299 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002300 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002301 // Could be launching some bookkeeping activity
2302 startActivity(intent, optsBundle);
2303 } else {
2304 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2305 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2306 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002307
2308 if (v instanceof BubbleTextView) {
2309 // This is set to the view that launched the activity that navigated the user away
2310 // from launcher. Since there is no callback for when the activity has finished
2311 // launching, enable the press state and keep this reference to reset the press
2312 // state when we return to launcher.
2313 BubbleTextView btv = (BubbleTextView) v;
2314 btv.setStayPressed(true);
2315 setOnResumeCallback(btv);
2316 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002317 mAppLaunchSuccess = true;
2318 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002319 } catch (ActivityNotFoundException|SecurityException e) {
2320 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2321 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2322 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002323 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002324 }
2325
Tony Wickhamdadb3042016-02-24 11:07:00 -08002326 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002327 public boolean dispatchTouchEvent(MotionEvent ev) {
2328 mLastDispatchTouchEventX = ev.getX();
2329 return super.dispatchTouchEvent(ev);
2330 }
2331
2332 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002334 if (!isDraggingEnabled()) return false;
2335 if (isWorkspaceLocked()) return false;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07002336 if (!isInState(LauncherState.NORMAL) && !isInState(LauncherState.OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337
Jon Miranda51f037d2016-11-07 08:37:20 -08002338 boolean ignoreLongPressToOverview =
2339 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002340
Adam Cohen1697b792013-09-17 19:08:21 -07002341 if (v instanceof Workspace) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002342 if (!isInState(LauncherState.OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002343 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302344 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2345 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002346 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002347 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002348 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2349 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2350 return true;
2351 } else {
2352 return false;
2353 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002354 } else {
2355 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002356 }
Adam Cohen1697b792013-09-17 19:08:21 -07002357 }
2358
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002359 CellLayout.CellInfo longClickCellInfo = null;
2360 View itemUnderLongClick = null;
2361 if (v.getTag() instanceof ItemInfo) {
2362 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002363 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002364 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002365 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
2367
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 // The hotseat touch handling does not go through Workspace, and we always allow long press
2369 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002370 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002371 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002372 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002373 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002374 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302375 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2376 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002377 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002378 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002379 return false;
2380 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302381 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2382 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002383 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002384 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002385 }
Jon Miranda379198e2016-09-23 08:54:40 -07002386 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2387 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002389 final boolean isAllAppsButton =
2390 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2391 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2392 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002393 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002395 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 }
2397 }
2398 }
2399 return true;
2400 }
2401
Winson Chung3d503fb2011-07-13 17:25:49 -07002402 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002403 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002404 return mHotseat != null && layout != null &&
2405 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2406 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002407
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 /**
2409 * Returns the CellLayout of the specified container at the specified screen.
2410 */
Sunny Goyal92820592015-03-02 11:31:35 -08002411 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002412 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2413 if (mHotseat != null) {
2414 return mHotseat.getLayout();
2415 } else {
2416 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002417 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002419 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002420 }
2421 }
2422
Michael Jurkae326f182011-11-21 14:05:46 -08002423 @Override
2424 public void onTrimMemory(int level) {
2425 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002426 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2427 // The widget preview db can result in holding onto over
2428 // 3MB of memory for caching which isn't necessary.
2429 SQLiteDatabase.releaseMemory();
2430
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002431 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002432 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002433 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002434 if (mLauncherCallbacks != null) {
2435 mLauncherCallbacks.onTrimMemory(level);
2436 }
Michael Jurkae326f182011-11-21 14:05:46 -08002437 }
2438
Winson5c6bdbb2015-09-03 11:36:19 -07002439 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002440 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002441 }
2442
Winson5c6bdbb2015-09-03 11:36:19 -07002443 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002444 boolean changed = !isInState(LauncherState.NORMAL);
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002445 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002446 mWorkspace.setVisibility(View.VISIBLE);
Sunny Goyalbe93f262017-10-20 17:05:27 -07002447 mStateTransitionAnimation.goToState(LauncherState.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002448
Michael Jurkab3e22d92011-10-31 15:58:33 -07002449 // Set focus to the AppsCustomize button
2450 if (mAllAppsButton != null) {
2451 mAllAppsButton.requestFocus();
2452 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002453 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002454
Winson Chung0f785722015-04-08 10:27:49 -07002455 if (changed) {
2456 // Send an accessibility event to announce the context change
2457 getWindow().getDecorView()
2458 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002459 }
Winson5c6bdbb2015-09-03 11:36:19 -07002460 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002461 }
2462
Winsone9f27272015-10-13 10:47:51 -07002463 /**
2464 * Shows the overview button.
2465 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002466 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002467 showOverviewMode(animated, false);
2468 }
2469
2470 /**
2471 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2472 * onto one of the overview panel buttons.
2473 */
2474 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2475 Runnable postAnimRunnable = null;
2476 if (requestButtonFocus) {
2477 postAnimRunnable = new Runnable() {
2478 @Override
2479 public void run() {
2480 // Hitting the menu button when in touch mode does not trigger touch mode to
2481 // be disabled, so if requested, force focus on one of the overview panel
2482 // buttons.
2483 mOverviewPanel.requestFocusFromTouch();
2484 }
2485 };
2486 }
Adam Cohened307df2013-10-02 09:37:31 -07002487 mWorkspace.setVisibility(View.VISIBLE);
Sunny Goyalbe93f262017-10-20 17:05:27 -07002488 mStateTransitionAnimation.goToState(LauncherState.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002489
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002490 // If animated from long press, then don't allow any of the controller in the drag
2491 // layer to intercept any remaining touch.
2492 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002493 }
2494
Winson Chungb745afb2015-03-02 11:51:23 -08002495 /**
2496 * Shows the apps view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002497 *
2498 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002499 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002500 // TODO: calling method should use the return value so that when {@code false} is returned
2501 // the workspace transition doesn't fall into invalid state.
Sunny Goyalaeb16432017-10-16 11:46:41 -07002502 public boolean showAppsView(boolean animated) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002503 if (!(isInState(LauncherState.NORMAL) ||
2504 (isInState(LauncherState.ALL_APPS) && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002505 return false;
2506 }
Winson Chungb745afb2015-03-02 11:51:23 -08002507
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002508 // This is a safe and supported transition to bypass spring_loaded mode.
2509 if (mExitSpringLoadedModeRunnable != null) {
2510 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2511 mExitSpringLoadedModeRunnable = null;
2512 }
2513
Sunny Goyalbe93f262017-10-20 17:05:27 -07002514 mStateTransitionAnimation.goToState(LauncherState.ALL_APPS, animated, null);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002515
Michael Jurkab3e22d92011-10-31 15:58:33 -07002516 // Change the state *after* we've called all the transition code
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002517 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002518
2519 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002520 getWindow().getDecorView()
2521 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002522 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002523 }
2524
Hyunyoung Song3f471442015-04-08 19:01:34 -07002525 public void enterSpringLoadedDragMode() {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002526 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -07002527 mWorkspace.getState().ordinal));
Sunny Goyalf9403d92017-10-18 10:55:56 -07002528 if (isInState(LauncherState.SPRING_LOADED)) {
Winson Chungb745afb2015-03-02 11:51:23 -08002529 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002530 }
Sunny Goyalbe93f262017-10-20 17:05:27 -07002531 mStateTransitionAnimation.goToState(LauncherState.SPRING_LOADED, true, null);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002532 }
Adam Cohen7777d962011-08-18 18:58:38 -07002533
Sunny Goyalaeb16432017-10-16 11:46:41 -07002534 public void exitSpringLoadedDragMode(int delay) {
2535 exitSpringLoadedDragMode(delay, null);
Sunny Goyal1797af42017-10-06 13:29:57 -07002536 }
2537
Sunny Goyalaeb16432017-10-16 11:46:41 -07002538 public void exitSpringLoadedDragMode(int delay, final Runnable onCompleteRunnable) {
Sunny Goyalf9403d92017-10-18 10:55:56 -07002539 if (!isInState(LauncherState.SPRING_LOADED)) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002540
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002541 if (mExitSpringLoadedModeRunnable != null) {
2542 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2543 }
2544 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002545 @Override
2546 public void run() {
Sunny Goyalaeb16432017-10-16 11:46:41 -07002547 showWorkspace(true, onCompleteRunnable);
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002548 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07002549 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002550 };
2551 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07002552 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002553
Michael Jurkad7c28052012-04-27 15:43:36 -07002554 @Override
2555 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002556 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002557 final List<CharSequence> text = event.getText();
2558 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002559 // Populate event with a fake title based on the current state.
Sunny Goyalf9403d92017-10-18 10:55:56 -07002560 if (isInState(LauncherState.ALL_APPS)) {
Sunny Goyalc525d802015-04-29 11:05:34 -07002561 text.add(getString(R.string.all_apps_button_label));
Hyunyoung Song166e6482015-06-25 21:06:07 -07002562 } else if (mWorkspace != null) {
2563 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07002564 } else {
2565 text.add(getString(R.string.all_apps_home_button_label));
2566 }
Michael Jurkad7c28052012-04-27 15:43:36 -07002567 return result;
2568 }
2569
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002570 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002571 * If the activity is currently paused, signal that we need to run the passed Runnable
2572 * in onResume.
2573 *
2574 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002575 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2576 * wrong when we're not running, and if the activity comes back to what the configuration was
2577 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002578 *
2579 * Implementation of the method from LauncherModel.Callbacks.
2580 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002581 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002582 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002583 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002584 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002585 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002586 if (run instanceof RunnableWithId) {
2587 // Remove any runnables which have the same id
2588 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002589 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002590 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002591 return true;
2592 } else {
2593 return false;
2594 }
2595 }
2596
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002597 public void setOnResumeCallback(OnResumeCallback callback) {
2598 if (mOnResumeCallback != null) {
2599 mOnResumeCallback.onLauncherResume();
2600 }
2601 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002602 }
2603
Michael Jurka7607c2f2013-04-03 14:33:19 -07002604 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002605 * If the activity is currently paused, signal that we need to re-run the loader
2606 * in onResume.
2607 *
2608 * This needs to be called from incoming places where resources might have been loaded
2609 * while we are paused. That is becaues the Configuration might be wrong
2610 * when we're not running, and if it comes back to what it was when we
2611 * were paused, we are not restarted.
2612 *
2613 * Implementation of the method from LauncherModel.Callbacks.
2614 *
2615 * @return true if we are currently paused. The caller might be able to
2616 * skip some work in that case since we will come back again.
2617 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002618 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002619 public boolean setLoadOnResume() {
2620 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002621 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002622 mOnResumeNeedsLoad = true;
2623 return true;
2624 } else {
2625 return false;
2626 }
2627 }
2628
2629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002632 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002633 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002634 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002635 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002636 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002637 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002638 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002639 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002640
Joe Onorato9c1289c2009-08-17 11:03:03 -04002641 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002642 * Clear any pending bind callbacks. This is called when is loader is planning to
2643 * perform a full rebind from scratch.
2644 */
2645 @Override
2646 public void clearPendingBinds() {
2647 mBindOnResumeCallbacks.clear();
2648 if (mPendingExecutor != null) {
2649 mPendingExecutor.markCompleted();
2650 mPendingExecutor = null;
2651 }
2652 }
2653
2654 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002655 * Refreshes the shortcuts shown on the workspace.
2656 *
2657 * Implementation of the method from LauncherModel.Callbacks.
2658 */
2659 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002660 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002661 // Floating panels (except the full widget sheet) are associated with individual icons. If
2662 // we are starting a fresh bind, close all such panels as all the icons are about
2663 // to go away.
2664 AbstractFloatingView.closeOpenViews(this, true,
2665 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002666
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002667 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002668
Winson Chungd64d1762013-08-20 14:37:16 -07002669 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002670 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002671 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002672
Winson Chung3d503fb2011-07-13 17:25:49 -07002673 if (mHotseat != null) {
2674 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002675 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002676 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677 }
2678
Adam Cohendcd297f2013-06-18 13:13:40 -07002679 @Override
2680 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002681 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002682 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2683 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002684 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2685 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002686 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002687 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2688 // If there are no screens, we need to have an empty screen
2689 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002690 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002691 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002692
Winsonc7d2e832016-07-28 12:24:55 -07002693 // After we have added all the screens, if the wallpaper was locked to the default state,
2694 // then notify to indicate that it can be released and a proper wallpaper offset can be
2695 // computed before the next layout
2696 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002697 }
2698
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002699 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002700 int count = orderedScreenIds.size();
2701 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002702 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002703 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002704 // No need to bind the first screen, as its always bound.
2705 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2706 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002707 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002708 }
2709
Sunny Goyalb23980c2017-08-17 07:45:25 -07002710 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002711 public void bindAppsAdded(final ArrayList<Long> newScreens,
2712 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002713 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002714 Runnable r = new Runnable() {
2715 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002716 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002717 }
2718 };
2719 if (waitUntilResume(r)) {
2720 return;
2721 }
2722
Winson Chungd64d1762013-08-20 14:37:16 -07002723 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002724 if (newScreens != null) {
2725 bindAddScreens(newScreens);
2726 }
Winson Chungd64d1762013-08-20 14:37:16 -07002727
2728 // We add the items without animation on non-visible pages, and with
2729 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002730 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002731 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002732 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002733 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002734 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002735 }
Winson Chungc58497e2013-09-03 17:48:37 -07002736
Winson Chung87412982013-10-03 18:34:14 -07002737 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002738 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002739 }
2740
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002742 * Bind the items start-end from the list.
2743 *
2744 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002746 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002747 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002748 Runnable r = new Runnable() {
2749 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002750 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002751 }
2752 };
2753 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002754 return;
2755 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002756
Sunny Goyal3be633b2016-12-08 09:59:25 -08002757 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002758 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002759 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002760 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002761 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002762 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002763 int end = items.size();
2764 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002765 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002766
2767 // Short circuit if we are loading dock items for a configuration which has no dock
2768 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2769 mHotseat == null) {
2770 continue;
2771 }
2772
Sunny Goyal639e9062015-08-19 19:17:06 -07002773 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002774 switch (item.itemType) {
2775 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2776 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002777 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002778 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002779 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002781 }
2782 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002783 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002784 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002785 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002786 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002787 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002788 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2789 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002790 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002791 if (view == null) {
2792 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002793 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002794 break;
2795 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002796 default:
2797 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002799
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002800 /*
2801 * Remove colliding items.
2802 */
2803 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2804 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2805 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2806 View v = cl.getChildAt(item.cellX, item.cellY);
2807 Object tag = v.getTag();
2808 String desc = "Collision while binding workspace item: " + item
2809 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002810 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002811 throw (new RuntimeException(desc));
2812 } else {
2813 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002814 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002815 continue;
2816 }
2817 }
2818 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302819 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002820 if (animateIcons) {
2821 // Animate all the applications up now
2822 view.setAlpha(0f);
2823 view.setScaleX(0f);
2824 view.setScaleY(0f);
2825 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002826 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002828 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002829
Winson Chung997a9232013-07-24 15:33:46 -07002830 if (animateIcons) {
2831 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002832 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002833 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002834 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002835 final Runnable startBounceAnimRunnable = new Runnable() {
2836 public void run() {
2837 anim.playTogether(bounceAnims);
2838 anim.start();
2839 }
2840 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002841 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002842 // We post the animation slightly delayed to prevent slowdowns
2843 // when we are loading right after we return to launcher.
2844 mWorkspace.postDelayed(new Runnable() {
2845 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002846 if (mWorkspace != null) {
2847 mWorkspace.snapToPage(newScreenIndex);
2848 mWorkspace.postDelayed(startBounceAnimRunnable,
2849 NEW_APPS_ANIMATION_DELAY);
2850 }
Winson Chung94d67682013-09-25 16:29:40 -07002851 }
2852 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002853 } else {
2854 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002855 }
2856 }
Winson Chung64359a52013-07-08 17:17:08 -07002857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 }
2860
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 /**
2862 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002863 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002864 public void bindAppWidget(LauncherAppWidgetInfo item) {
2865 View view = inflateAppWidget(item);
2866 if (view != null) {
2867 mWorkspace.addInScreen(view, item);
2868 mWorkspace.requestLayout();
2869 }
2870 }
2871
2872 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002873 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302874 PendingAppWidgetHostView view =
2875 new PendingAppWidgetHostView(this, item, mIconCache, true);
2876 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002877 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002878 }
2879
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002880 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002882 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002883
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002884 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2885 // If the provider is not ready, bind as a pending widget.
2886 appWidgetInfo = null;
2887 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2888 // The widget id is not valid. Try to find the widget based on the provider info.
2889 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2890 } else {
2891 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2892 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002893
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002894 // If the provider is ready, but the width is not yet restored, try to restore it.
2895 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2896 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002897 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002898 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2899 + " belongs to component " + item.providerName
2900 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002901 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002902 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002903 }
Sunny Goyalff572272014-07-23 13:58:07 -07002904
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002905 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002906 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002907 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2908 // Id has not been allocated yet. Allocate a new id.
2909 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2910 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002911
Sunny Goyald478c832016-04-01 12:04:16 -07002912 // Also try to bind the widget. If the bind fails, the user will be shown
2913 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002914 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002915 pendingInfo.spanX = item.spanX;
2916 pendingInfo.spanY = item.spanY;
2917 pendingInfo.minSpanX = item.minSpanX;
2918 pendingInfo.minSpanY = item.minSpanY;
2919 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002920
2921 boolean isDirectConfig =
2922 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2923 if (isDirectConfig && item.bindOptions != null) {
2924 Bundle newOptions = item.bindOptions.getExtras();
2925 if (options != null) {
2926 newOptions.putAll(options);
2927 }
2928 options = newOptions;
2929 }
Sunny Goyald478c832016-04-01 12:04:16 -07002930 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2931 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002932
Sunny Goyal86df1382016-08-10 15:03:22 -07002933 // We tried to bind once. If we were not able to bind, we would need to
2934 // go through the permission dialog, which means we cannot skip the config
2935 // activity.
2936 item.bindOptions = null;
2937 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2938
Sunny Goyald478c832016-04-01 12:04:16 -07002939 // Bind succeeded
2940 if (success) {
2941 // If the widget has a configure activity, it is still needs to set it up,
2942 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002943 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002944 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2945 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002946 }
Sunny Goyald478c832016-04-01 12:04:16 -07002947
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002948 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002949 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002950 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002951 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002952 // The widget was marked as UI not ready, but there is no configure activity to
2953 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002954 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002955 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002956 }
Sunny Goyalff572272014-07-23 13:58:07 -07002957 }
2958
Sunny Goyald5462aa2016-11-22 16:52:39 +05302959 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002960 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002961 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002962 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002963 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002964 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002965 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002966 }
2967
Sunny Goyal233ee962015-08-03 13:05:01 -07002968 item.minSpanX = appWidgetInfo.minSpanX;
2969 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302970 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002971 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302972 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002973 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302974 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002976 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002977 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 }
2979
Sunny Goyalff572272014-07-23 13:58:07 -07002980 /**
2981 * Restores a pending widget.
2982 *
2983 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002984 */
Sunny Goyald478c832016-04-01 12:04:16 -07002985 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002986 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2987 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2988 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002989 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002990 }
2991
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002992 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002993 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002994 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2995 info.pendingItemInfo = null;
2996 }
Sunny Goyalff572272014-07-23 13:58:07 -07002997
Sunny Goyalba47faa2017-10-04 16:50:57 -07002998 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2999 view.reinflate();
3000 }
3001
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003002 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003003 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003004 }
3005
Adam Cohen1462de32012-07-24 22:34:36 -07003006 public void onPageBoundSynchronously(int page) {
3007 mSynchronouslyBoundPages.add(page);
3008 }
3009
Sunny Goyal527c7d32015-08-28 15:19:36 -07003010 @Override
3011 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3012 if (mPendingExecutor != null) {
3013 mPendingExecutor.markCompleted();
3014 }
3015 mPendingExecutor = executor;
3016 executor.attachTo(this);
3017 }
3018
3019 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3020 if (mPendingExecutor == executor) {
3021 mPendingExecutor = null;
3022 }
3023 }
3024
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003025 @Override
3026 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3027 Runnable r = new Runnable() {
3028 public void run() {
3029 finishFirstPageBind(executor);
3030 }
3031 };
3032 if (waitUntilResume(r)) {
3033 return;
3034 }
3035
3036 Runnable onComplete = new Runnable() {
3037 @Override
3038 public void run() {
3039 if (executor != null) {
3040 executor.onLoadAnimationCompleted();
3041 }
3042 }
3043 };
3044 if (mDragLayer.getAlpha() < 1) {
3045 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3046 } else {
3047 onComplete.run();
3048 }
3049 }
3050
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 /**
3052 * Callback saying that there aren't any more items to bind.
3053 *
3054 * Implementation of the method from LauncherModel.Callbacks.
3055 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003056 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003057 Runnable r = new Runnable() {
3058 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003059 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003060 }
3061 };
3062 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003063 return;
3064 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003065 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07003066 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003068 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003069
Sunny Goyal2100c782016-08-22 16:00:03 -07003070 if (mPendingActivityResult != null) {
3071 handleActivityResult(mPendingActivityResult.requestCode,
3072 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3073 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003074 }
3075
Sunny Goyala474a9b2017-05-04 16:47:11 -07003076 InstallShortcutReceiver.disableAndFlushInstallQueue(
3077 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003078
Tony Wickham010d2552017-01-20 08:15:28 -08003079 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07003080 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07003081 }
3082
Winson Chunga2413752012-04-03 14:22:34 -07003083 private boolean canRunNewAppsAnimation() {
3084 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003085 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003086 }
3087
Winson Chungc9168342013-06-26 14:54:55 -07003088 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003089 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003090 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3091 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003092 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003093 return bounceAnim;
3094 }
3095
Adam Cohen27772732013-11-11 14:00:56 +00003096 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003097 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003098 }
3099
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003100 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 * Add the icons for all apps.
3102 *
3103 * Implementation of the method from LauncherModel.Callbacks.
3104 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003105 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003106 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3107 public void run() {
3108 bindAllApplications(apps);
3109 }
3110 };
3111 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003112 return;
3113 }
3114
Winson Chungb745afb2015-03-02 11:51:23 -08003115 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003116 Executor pendingExecutor = getPendingExecutor();
Sunny Goyalf9403d92017-10-18 10:55:56 -07003117 if (pendingExecutor != null && !isInState(LauncherState.ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07003118 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003119 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003120 return;
3121 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003122
Winson Chungb745afb2015-03-02 11:51:23 -08003123 mAppsView.setApps(apps);
3124 }
Adam Cohen9211d422014-10-07 18:14:53 -07003125 if (mLauncherCallbacks != null) {
3126 mLauncherCallbacks.bindAllApplications(apps);
3127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003128 }
3129
3130 /**
Tony2917a8b2017-07-31 23:29:42 -07003131 * Returns an Executor that will run after the launcher is first drawn (including after the
3132 * initial fade in animation). Returns null if the first draw has already occurred.
3133 */
3134 public @Nullable Executor getPendingExecutor() {
3135 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3136 }
3137
3138 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003139 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3140 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3141 */
3142 @Override
3143 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003144 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003145 }
3146
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003147 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003148 * A package was updated.
3149 *
3150 * Implementation of the method from LauncherModel.Callbacks.
3151 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003152 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003153 Runnable r = new Runnable() {
3154 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003155 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003156 }
3157 };
3158 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003159 return;
3160 }
3161
Winson Chungb745afb2015-03-02 11:51:23 -08003162 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003163 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003164 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 }
3166
Sunny Goyal4390ace2014-10-13 11:33:11 -07003167 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003168 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3169 Runnable r = new Runnable() {
3170 public void run() {
3171 bindPromiseAppProgressUpdated(app);
3172 }
3173 };
3174 if (waitUntilResume(r)) {
3175 return;
3176 }
3177
3178 if (mAppsView != null) {
3179 mAppsView.updatePromiseAppProgress(app);
3180 }
3181 }
3182
3183 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003184 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3185 Runnable r = new Runnable() {
3186 public void run() {
3187 bindWidgetsRestored(widgets);
3188 }
3189 };
3190 if (waitUntilResume(r)) {
3191 return;
3192 }
3193 mWorkspace.widgetsRestored(widgets);
3194 }
3195
Joe Onorato9c1289c2009-08-17 11:03:03 -04003196 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003197 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003198 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003199 *
3200 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003201 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003202 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003203 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003204 Runnable r = new Runnable() {
3205 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003206 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003207 }
3208 };
3209 if (waitUntilResume(r)) {
3210 return;
3211 }
3212
Sunny Goyal4390ace2014-10-13 11:33:11 -07003213 if (!updated.isEmpty()) {
3214 mWorkspace.updateShortcuts(updated);
3215 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003216 }
3217
3218 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003219 * Update the state of a package, typically related to install state.
3220 *
3221 * Implementation of the method from LauncherModel.Callbacks.
3222 */
Sunny Goyale755d462014-07-22 13:48:29 -07003223 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003224 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3225 Runnable r = new Runnable() {
3226 public void run() {
3227 bindRestoreItemsChange(updates);
3228 }
3229 };
3230 if (waitUntilResume(r)) {
3231 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003232 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003233
Sunny Goyal756adbc2015-04-16 15:20:51 -07003234 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003235 }
3236
3237 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003238 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003239 * in addition to specific applications to remove, the reason being that
3240 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003241 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003242 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003244 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003245 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003246 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003247 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003248 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003249 }
Winson Chungd64d1762013-08-20 14:37:16 -07003250 };
3251 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003252 return;
3253 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003254 mWorkspace.removeItemsByMatcher(matcher);
3255 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003256 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003257
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003258 @Override
3259 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3260 Runnable r = new Runnable() {
3261 public void run() {
3262 bindAppInfosRemoved(appInfos);
3263 }
3264 };
3265 if (waitUntilResume(r)) {
3266 return;
Joe Onorato36115782010-06-17 13:28:48 -04003267 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003268
Winson Chungdf95eb12013-10-16 14:57:07 -07003269 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003270 if (mAppsView != null) {
3271 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003272 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 }
Joe Onoratobe386092009-11-17 17:32:16 -08003274
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003275 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003276 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3277 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003278 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3279 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003280 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003281 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003282 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003283 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003284 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003285 return;
3286 }
3287
Tony Wickham26b17462017-03-20 17:12:24 -07003288 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3289 if (topView != null) {
3290 topView.onWidgetsBound();
3291 }
3292 }
3293
Tony Wickham86222d22017-03-29 15:30:43 -07003294 /**
3295 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3296 * refreshes the widgets and shortcuts associated with the given package/user
3297 */
3298 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003299 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003300 }
3301
Sunny Goyalc99cb172017-10-19 16:15:09 -07003302 public boolean isRotationEnabled () {
3303 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003304 }
3305
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003306 private boolean shouldShowDiscoveryBounce() {
Sunny Goyalbe93f262017-10-20 17:05:27 -07003307 return isInState(LauncherState.NORMAL)
3308 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003309 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003310 }
3311
Winson Chung80baf5a2010-08-09 16:03:15 -07003312 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003313 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003314 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003315 @Override
3316 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3317 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003318
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003319 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3320 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003321 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003322 writer.println(prefix + " Homescreen " + i);
3323
3324 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3325 for (int j = 0; j < layout.getChildCount(); j++) {
3326 Object tag = layout.getChildAt(j).getTag();
3327 if (tag != null) {
3328 writer.println(prefix + " " + tag.toString());
3329 }
3330 }
3331 }
3332
3333 writer.println(prefix + " Hotseat");
3334 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003335 for (int j = 0; j < layout.getChildCount(); j++) {
3336 Object tag = layout.getChildAt(j).getTag();
3337 if (tag != null) {
3338 writer.println(prefix + " " + tag.toString());
3339 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003340 }
Sunny Goyala1365452015-10-01 15:46:24 -07003341
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003342 try {
3343 FileLog.flushAll(writer);
3344 } catch (Exception e) {
3345 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003346 }
3347 }
3348
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003349 writer.println(prefix + "Misc:");
3350 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3351 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3352 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3353
3354 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003355
Adam Cohen9211d422014-10-07 18:14:53 -07003356 if (mLauncherCallbacks != null) {
3357 mLauncherCallbacks.dump(prefix, fd, writer, args);
3358 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003359 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003360
Sunny Goyal66b24572016-09-21 15:57:55 -07003361 @Override
3362 @TargetApi(Build.VERSION_CODES.N)
3363 public void onProvideKeyboardShortcuts(
3364 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3365
3366 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyalf9403d92017-10-18 10:55:56 -07003367 if (isInState(LauncherState.NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003368 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3369 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3370 }
3371 View currentFocus = getCurrentFocus();
3372 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3373 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3374 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3375 }
Tony18c4aa42017-05-10 21:23:57 -05003376 if (currentFocus.getTag() instanceof ItemInfo
3377 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003378 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3379 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3380 }
3381 if (!shortcutInfos.isEmpty()) {
3382 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3383 }
3384
3385 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3386 }
3387
3388 @Override
3389 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3390 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3391 switch (keyCode) {
3392 case KeyEvent.KEYCODE_A:
Sunny Goyalf9403d92017-10-18 10:55:56 -07003393 if (isInState(LauncherState.NORMAL)) {
Sunny Goyald0d07032017-10-04 10:40:28 -07003394 showAppsView(true);
Sunny Goyal66b24572016-09-21 15:57:55 -07003395 return true;
3396 }
3397 break;
3398 case KeyEvent.KEYCODE_S: {
3399 View focusedView = getCurrentFocus();
3400 if (focusedView instanceof BubbleTextView
3401 && focusedView.getTag() instanceof ItemInfo
3402 && mAccessibilityDelegate.performAction(focusedView,
3403 (ItemInfo) focusedView.getTag(),
3404 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003405 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003406 return true;
3407 }
3408 break;
3409 }
3410 case KeyEvent.KEYCODE_O:
3411 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3412 return true;
3413 }
3414 break;
3415 }
3416 }
3417 return super.onKeyShortcut(keyCode, event);
3418 }
3419
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003420 public static Launcher getLauncher(Context context) {
3421 if (context instanceof Launcher) {
3422 return (Launcher) context;
3423 }
3424 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3425 }
3426
Sunny Goyal5a81c382017-03-20 15:08:06 -07003427 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003428
3429 @Override
3430 public void onSharedPreferenceChanged(
3431 SharedPreferences sharedPreferences, String key) {
3432 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003433 // Recreate the activity so that it initializes the rotation preference again.
3434 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003435 }
3436 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003437 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003438
3439 /**
3440 * Callback for listening for onResume
3441 */
3442 public interface OnResumeCallback {
3443
3444 void onLauncherResume();
3445 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003446}