blob: adfb44cd7dd78b184d445d38546466ce1f9d317c [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 Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Sunny Goyal28c6b962015-10-12 11:42:05 -070033import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070035import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070039import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070041import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070049import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040050import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070052import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070053import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070054import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070055import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070057import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070058import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070068import android.support.annotation.Nullable;
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;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -080073import android.view.ActionMode;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070077import android.view.KeyboardShortcutGroup;
78import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080079import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070088
Sunny Goyal651077b2014-06-30 14:15:31 -070089import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070091import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070092import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070093import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070094import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080095import com.android.launcher3.allapps.DiscoveryBounce;
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;
Tony Wickhame0c33232016-02-08 11:37:04 -080099import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700100import com.android.launcher3.dragndrop.DragController;
101import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700102import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700103import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700104import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000105import com.android.launcher3.folder.Folder;
106import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700107import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700108import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700109import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700110import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800116import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800117import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530119import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700121import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700124import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700125import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700129import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700132import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700133import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700134import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800135import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800136import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800138import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800139import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700140import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700141import com.android.launcher3.widget.WidgetListRowEntry;
142import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700143import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700144
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700146import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700147import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700148import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700149import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700150import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800151import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700152import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154/**
155 * Default launcher application.
156 */
Sunny Goyal27835952017-01-13 12:15:53 -0800157public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700158 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
159 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chunga2413752012-04-03 14:22:34 -0700164 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700167 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700170 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
174 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175
Jon Mirandac476d6e2017-05-04 16:30:01 -0700176 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700177
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700178 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
179
Mathew Inwood876a8462013-06-14 14:12:41 +0100180 /**
181 * IntentStarter uses request codes starting with this. This must be greater than all activity
182 * request codes used internally.
183 */
184 protected static final int REQUEST_LAST = 100;
185
Winson Chung2672ff92012-05-04 16:22:30 -0700186 // The Intent extra that defines whether to ignore the launch animation
187 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400188 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
196 // Type: ActivityResultInfo
197 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700198 // Type: SparseArray<Parcelable>
199 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800201 // When starting an action mode, setting this tag will cause the action mode to be cancelled
202 // automatically when user interacts with the launcher.
203 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
204
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700205 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700212 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
213 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
214 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700215
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700217 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800219 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700220
Sunny Goyalffe83f12014-08-14 17:39:34 -0700221 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700222 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700223
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700224 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700225
Sunny Goyal316490e2015-06-02 09:38:28 -0700226 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700227
Sunny Goyal47328fd2016-05-25 18:56:41 -0700228 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700229
230 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700231 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700232 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700233
Winson Chung8ae41982017-11-10 11:42:13 -0800234 // UI and state for the overview panel
235 private ViewGroup mOverviewPanel;
236
Jon Miranda6bed3502017-09-19 14:43:17 -0700237 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
238 // that results in widgets being inflated in the wrong orientation.
239 private int mOrientation;
240
Adam Cohen091440a2015-03-18 14:16:05 -0700241 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700243 private OnResumeCallback mOnResumeCallback;
244
Sunny Goyal527c7d32015-08-28 15:19:36 -0700245 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700246
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800248 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700250 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Jon Miranda2d89ea82017-05-04 11:47:53 -0700252 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700253 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700254
Tony Wickham010d2552017-01-20 08:15:28 -0800255 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700256
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800257 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700258
Winson Chung46353de2012-02-16 14:05:10 -0800259 // We only want to get the SharedPreferences once since it does an FS stat each time we get
260 // it from the context.
261 private SharedPreferences mSharedPrefs;
262
Sunny Goyal2100c782016-08-22 16:00:03 -0700263 // Activity result which needs to be processed after workspace has loaded.
264 private ActivityResultInfo mPendingActivityResult;
265 /**
266 * Holds extra information required to handle a result from an external call, like
267 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
268 */
269 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800270
Jon Miranda379198e2016-09-23 08:54:40 -0700271 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700272
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700273 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700274 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700275 private boolean mAppLaunchSuccess;
276
Sunny Goyal745bad92016-05-02 10:54:12 -0700277 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800278 private ActionMode mCurrentActionMode;
Sunny Goyal7779d622015-06-11 16:18:39 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 @Override
281 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700282 if (DEBUG_STRICT_MODE) {
283 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
284 .detectDiskReads()
285 .detectDiskWrites()
286 .detectNetwork() // or .detectAll() for all detectable problems
287 .penaltyLog()
288 .build());
289 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
290 .detectLeakedSqlLiteObjects()
291 .detectLeakedClosableObjects()
292 .penaltyLog()
293 .penaltyDeath()
294 .build());
295 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700296 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700297
Adam Cohen9211d422014-10-07 18:14:53 -0700298 if (mLauncherCallbacks != null) {
299 mLauncherCallbacks.preOnCreate();
300 }
301
Mario Bertschler27288382017-05-24 15:35:09 -0700302 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
303 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700304 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700307 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400308
Sunny Goyal87f784c2017-01-11 10:48:34 -0800309 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700310
Adam Cohen2e6da152015-05-06 11:42:25 -0700311 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800312 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700313 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700314 Display display = getWindowManager().getDefaultDisplay();
315 Point mwSize = new Point();
316 display.getSize(mwSize);
317 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
318 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700319
Jon Miranda6bed3502017-09-19 14:43:17 -0700320 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700321 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700322 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800323 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800324 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800325 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700326 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700327
Joe Onorato41a12d22009-10-31 18:30:00 -0400328 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700329 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800330 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700331
Sunny Goyalffe83f12014-08-14 17:39:34 -0700332 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700333
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700334 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700335 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700336
Sunny Goyal60820d72017-05-09 12:40:11 -0700337 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 setupViews();
Winson1f064272016-07-18 17:18:02 -0700340 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341
Tony Wickham010d2552017-01-20 08:15:28 -0800342 mPopupDataProvider = new PopupDataProvider(this);
343
Sunny Goyald3864fa2017-11-14 15:06:36 -0800344 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
345 // In case we are on a device with locked rotation, we should look at preferences to check
346 // if the user has specifically allowed rotation.
347 if (!mRotationEnabled) {
348 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
349 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
350 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352
Sunny Goyald3864fa2017-11-14 15:06:36 -0800353 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
354 if (internalStateHandled) {
355 // Temporarily enable the rotation
356 mRotationEnabled = true;
357
358 if (savedInstanceState != null) {
359 // InternalStateHandler has already set the appropriate state.
360 // We dont need to do anything.
361 savedInstanceState.remove(RUNTIME_STATE);
362 }
363 }
364 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800365
Sunny Goyal2100c782016-08-22 16:00:03 -0700366 // We only load the page synchronously if the user rotates (or triggers a
367 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700368 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
369 if (savedInstanceState != null) {
370 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
371 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800372
Sunny Goyalfe770c92016-09-27 14:38:58 -0700373 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800374 if (!internalStateHandled) {
375 // If we are not binding synchronously, show a fade in animation when
376 // the first page bind completes.
377 mDragLayer.setAlpha(0);
378 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700379 } 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
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800387 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800388
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700389 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
390 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700391 setRequestedOrientation(mRotationEnabled
392 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700393
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800394 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700395
Jon Miranda7fda2852017-07-19 16:07:01 -0700396 // Listen for broadcasts
397 IntentFilter filter = new IntentFilter();
398 filter.addAction(Intent.ACTION_SCREEN_OFF);
399 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
400 registerReceiver(mReceiver, filter);
401 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700402
Sunny Goyal8392c822017-06-20 10:03:56 -0700403 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
404 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700405
406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.onCreate(savedInstanceState);
408 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700409
410 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700411 }
412
Sunny Goyal7779d622015-06-11 16:18:39 -0700413 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700414 public void onThemeChanged() {
415 recreate();
416 }
417
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700418 public LauncherStateManager getStateManager() {
419 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700420 }
421
Mario Bertschlera6936942017-05-31 14:48:19 -0700422 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700423 if (isDark) {
424 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700425 } else if (supportsDarkText) {
426 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700427 }
428 }
429
430 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700431 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800432 return mLauncherView.findViewById(id);
433 }
434
435 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700436 public void onAppWidgetHostReset() {
437 if (mAppWidgetHost != null) {
438 mAppWidgetHost.startListening();
439 }
440 }
441
Adam Cohen9211d422014-10-07 18:14:53 -0700442 private LauncherCallbacks mLauncherCallbacks;
443
Winson1f064272016-07-18 17:18:02 -0700444 public void onInsetsChanged(Rect insets) {
445 mDeviceProfile.updateInsets(insets);
446 mDeviceProfile.layout(this, true /* notifyListeners */);
447 }
448
Sunny Goyal32554d12015-12-03 15:31:25 -0800449 /**
450 * Call this after onCreate to set or clear overlay.
451 */
452 public void setLauncherOverlay(LauncherOverlay overlay) {
453 if (overlay != null) {
454 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
455 }
456 mWorkspace.setLauncherOverlay(overlay);
457 }
458
Adam Cohen9211d422014-10-07 18:14:53 -0700459 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
460 mLauncherCallbacks = callbacks;
461 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700462 }
463
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700464 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700465 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700466 if (mLauncherCallbacks != null) {
467 mLauncherCallbacks.onLauncherProviderChange();
468 }
469 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700470
Hyunyoung Song3f471442015-04-08 19:01:34 -0700471 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700472 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
473 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700474 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700475 }
476
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000477 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700478 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
479 // This cast is safe as long as the id < 0x00FFFFFF
480 // Since we jail all the dynamically generated views, there should be no clashes
481 // with any other views.
482 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000483 }
484
Tony Wickham010d2552017-01-20 08:15:28 -0800485 public PopupDataProvider getPopupDataProvider() {
486 return mPopupDataProvider;
487 }
488
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000489 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700490 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
491 * a configuration step, this allows the proper animations to run after other transitions.
492 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700493 private long completeAdd(
494 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
495 long screenId = info.screenId;
496 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700497 // When the screen id represents an actual screen (as opposed to a rank) we make sure
498 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700499 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700500 }
Adam Cohendb364c32014-05-20 14:23:40 -0700501
Sunny Goyal2100c782016-08-22 16:00:03 -0700502 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700504 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700505 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700507 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700508 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700509 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700510 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700511 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700512 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700513 int widgetId = appWidgetId;
514 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700515 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700516 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700517 // Since the view was just bound, also launch the configure activity if needed
518 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
519 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800520 if (provider != null) {
521 new WidgetAddFlowHandler(provider)
522 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700523 }
524 }
525 break;
526 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800527 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700528
Adam Cohendb364c32014-05-20 14:23:40 -0700529 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 }
531
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800532 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700533 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700534 if (isWorkspaceLoading()) {
535 // process the result once the workspace has loaded.
536 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
537 return;
538 }
539 mPendingActivityResult = null;
540
Winson Chunge341d302013-08-16 14:31:00 -0700541 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700542 final PendingRequestArgs requestArgs = mPendingRequestArgs;
543 setWaitingForResult(null);
544 if (requestArgs == null) {
545 return;
546 }
547
548 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700549
Adam Cohenad4e15c2013-10-17 16:21:35 -0700550 Runnable exitSpringLoaded = new Runnable() {
551 @Override
552 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700553 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700554 }
555 };
556
Michael Jurka8b805b12012-04-18 14:23:14 -0700557 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700558 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700559 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
561 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700562 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700563 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700564 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700565 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700566 addAppWidgetImpl(
567 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800568 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700569 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700570 }
571 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200572 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700573 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700574 // User could have free-scrolled between pages before picking a wallpaper; make sure
575 // we move to the closest one now.
576 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700577 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200578 }
579 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200581
Adam Cohened66b2b2012-01-23 17:28:51 -0800582 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700583 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700584
Adam Cohened66b2b2012-01-23 17:28:51 -0800585 // We have special handling for widgets
586 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800587 final int appWidgetId;
588 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
589 : -1;
590 if (widgetId < 0) {
591 appWidgetId = pendingAddWidgetId;
592 } else {
593 appWidgetId = widgetId;
594 }
595
Adam Cohenad4e15c2013-10-17 16:21:35 -0700596 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800597 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700598 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
599 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700600 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700601 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700602 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700603 @Override
604 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700605 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700606 }
607 };
Adam Cohendb364c32014-05-20 14:23:40 -0700608
Sunny Goyal2100c782016-08-22 16:00:03 -0700609 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
610 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
611 } else {
612 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
613 // When the screen id represents an actual screen (as opposed to a rank)
614 // we make sure that the drop page actually exists.
615 requestArgs.screenId =
616 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700617 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700618 final CellLayout dropLayout =
619 mWorkspace.getScreenWithId(requestArgs.screenId);
620
621 dropLayout.setDropPending(true);
622 final Runnable onComplete = new Runnable() {
623 @Override
624 public void run() {
625 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
626 dropLayout.setDropPending(false);
627 }
628 };
629 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
630 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700631 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800632 return;
633 }
634
Sunny Goyald478c832016-04-01 12:04:16 -0700635 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
636 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700637 if (resultCode == RESULT_OK) {
638 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700639 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700640 }
641 // Leave the widget in the pending state if the user canceled the configure.
642 return;
643 }
644
Sunny Goyalaad90582015-07-09 14:22:50 -0700645 if (requestCode == REQUEST_CREATE_SHORTCUT) {
646 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700647 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
648 completeAdd(requestCode, data, -1, requestArgs);
649 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
650 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
651
Sunny Goyalaad90582015-07-09 14:22:50 -0700652 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700653 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
654 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800658 }
659
660 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700661 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800662 final int requestCode, final int resultCode, final Intent data) {
663 handleActivityResult(requestCode, resultCode, data);
664 if (mLauncherCallbacks != null) {
665 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
666 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800667 }
668
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700669 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700670 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600671 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 PendingRequestArgs pendingArgs = mPendingRequestArgs;
673 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
674 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
675 setWaitingForResult(null);
676
Sunny Goyal28c6b962015-10-12 11:42:05 -0700677 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700678 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700679 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700680 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700681 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 Intent intent = pendingArgs.getPendingIntent();
683
Sunny Goyal28c6b962015-10-12 11:42:05 -0700684 if (grantResults.length > 0
685 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700686 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700687 } else {
688 // TODO: Show a snack bar with link to settings
689 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700690 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700691 }
692 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600693 if (mLauncherCallbacks != null) {
694 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
695 grantResults);
696 }
697 }
698
Adam Cohendb364c32014-05-20 14:23:40 -0700699 /**
700 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
701 *
702 * @param screenId the screen id to check
703 * @return the new screen, or screenId if it exists
704 */
705 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800706 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700707 if (dropLayout == null) {
708 // it's possible that the add screen was removed because it was
709 // empty and a re-bind occurred
710 mWorkspace.addExtraEmptyScreen();
711 return mWorkspace.commitExtraEmptyScreen();
712 } else {
713 return screenId;
714 }
715 }
716
Sunny Goyal2100c782016-08-22 16:00:03 -0700717 @Thunk void completeTwoStageWidgetDrop(
718 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
719 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800720 Runnable onCompleteRunnable = null;
721 int animationType = 0;
722
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700723 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 if (resultCode == RESULT_OK) {
725 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
726 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800727 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700728 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 onCompleteRunnable = new Runnable() {
730 @Override
731 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700732 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700733 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 }
735 };
736 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800737 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700741 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700742 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
743 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700745 // The animated view may be null in the case of a rotation during widget configuration
746 onCompleteRunnable.run();
747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
749
750 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700751 protected void onStop() {
752 super.onStop();
753 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700754
755 if (mLauncherCallbacks != null) {
756 mLauncherCallbacks.onStop();
757 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800758 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800759
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700760 if (!mAppLaunchSuccess) {
761 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700762 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700763 }
Tony Wickham010d2552017-01-20 08:15:28 -0800764 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700765 }
766
767 @Override
768 protected void onStart() {
769 super.onStart();
770 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700771
772 if (mLauncherCallbacks != null) {
773 mLauncherCallbacks.onStart();
774 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800775 mAppWidgetHost.setListenIfResumed(true);
Sunny Goyalcac002d2018-01-04 09:51:45 -0800776 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Jon Miranda2d89ea82017-05-04 11:47:53 -0700777
Jon Miranda7fda2852017-07-19 16:07:01 -0700778 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700779 if (mScrimAnimator != null) {
780 mScrimAnimator.cancel();
781 }
782 mDragLayer.getBackground().setAlpha(0);
783 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
784 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
785 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
786 @Override
787 public void onAnimationEnd(Animator animation) {
788 mScrimAnimator = null;
789 }
790 });
791 mScrimAnimator.setDuration(600);
792 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
793 mScrimAnimator.start();
794 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700795 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700796 }
797
798 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700800 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700802 TraceHelper.partitionSection("ON_RESUME", "superCall");
803
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700804 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700805 getUserEventDispatcher().resetElapsedSessionMillis();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700806 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700807 // Process any items that were added while Launcher was away.
808 InstallShortcutReceiver.disableAndFlushInstallQueue(
809 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700810
Sunny Goyala474a9b2017-05-04 16:47:11 -0700811 // Refresh shortcuts if the permission changed.
812 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700813
Sunny Goyal7ede6112017-12-05 15:11:21 -0800814 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700815 if (mLauncherCallbacks != null) {
816 mLauncherCallbacks.onResume();
817 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800818
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700819 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700820 }
821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700823 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700824 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700825 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700826
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700827 super.onPause();
Joe Onorato24b6fd82009-11-12 13:47:09 -0800828 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700829 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700830
Adam Cohen9211d422014-10-07 18:14:53 -0700831 if (mLauncherCallbacks != null) {
832 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700833 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700834 }
835
Adam Cohenc2d6e892014-10-16 09:49:24 -0700836 public interface LauncherOverlay {
837
838 /**
839 * Touch interaction leading to overscroll has begun
840 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700841 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700842
843 /**
844 * Touch interaction related to overscroll has ended
845 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700846 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700847
848 /**
849 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
850 * screen (or in the case of RTL, the rightmost screen).
851 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700852 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700853
854 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800855 * Called when the launcher is ready to use the overlay
856 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700857 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700858 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859 }
860
861 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700862 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700863 }
864
865 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
866
Sunny Goyalc86df472016-02-25 09:19:38 -0800867 public void onScrollChanged(float progress) {
868 if (mWorkspace != null) {
869 mWorkspace.onOverlayScrollChanged(progress);
870 }
871 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700872 }
873
Sunny Goyal16764582017-11-06 16:00:34 -0800874 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700875 if (mLauncherCallbacks != null) {
876 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700877 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800878 // On O and above we there is always some setting present settings (add icon to
879 // home screen or icon badging). On earlier APIs we will have the allow rotation
880 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700881 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700882 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800883 }
884
Sunny Goyalf9403d92017-10-18 10:55:56 -0700885 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700886 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700887 }
888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 /**
890 * Restores the previous state, if it exists.
891 *
892 * @param savedState The previous state.
893 */
894 private void restoreState(Bundle savedState) {
895 if (savedState == null) {
896 return;
897 }
898
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700899 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700900 LauncherState[] stateValues = LauncherState.values();
901 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800902 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700903 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800904 }
905
Sunny Goyal2100c782016-08-22 16:00:03 -0700906 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
907 if (requestArgs != null) {
908 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700910
911 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700912
913 SparseArray<Parcelable> widgetsState =
914 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
915 if (widgetsState != null) {
916 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919
920 /**
921 * Finds all the views we need and configure them properly.
922 */
923 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -0700924 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700925 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700926 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700927 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800928 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700929
Sunny Goyal784f9c32016-03-23 16:56:54 -0700930 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
931 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
932 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933
Winson Chung4c98d922011-05-31 16:50:48 -0700934 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800935 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700936
Winson Chung3d503fb2011-07-13 17:25:49 -0700937 // Setup the hotseat
938 mHotseat = (Hotseat) findViewById(R.id.hotseat);
939 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700940 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 }
942
Winson Chung4c98d922011-05-31 16:50:48 -0700943 // Setup the workspace
944 mWorkspace.setHapticFeedbackEnabled(false);
945 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700946 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700947 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
948 // default state, otherwise we will update to the wrong offsets in RTL
949 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700950 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700951 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700952
Tony Wickham34d2c912015-09-11 09:27:58 -0700953 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700954 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700955
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700956 // Setup Apps
957 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700958
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700960 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700961 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400962
Hyunyoung Songd725f642017-08-17 22:26:35 -0700963 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 /**
967 * Creates a view representing a shortcut.
968 *
969 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800971 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700972 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974
975 /**
976 * Creates a view representing a shortcut inflated from the specified resource.
977 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 * @param parent The group the shortcut belongs to.
979 * @param info The data structure describing the shortcut.
980 *
981 * @return A View inflated from layoutResId.
982 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700983 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800984 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
985 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800986 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700988 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 return favorite;
990 }
991
992 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700993 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 *
995 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700998 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700999 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1000 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001001 return;
1002 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001003
Jon Mirandac476d6e2017-05-04 16:30:01 -07001004 int[] cellXY = mTmpAddItemCellCoordinates;
1005 CellLayout layout = getCellLayout(container, screenId);
1006
Sunny Goyal782f0c92017-01-19 10:27:54 -08001007 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001008 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001009 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1010 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001011 }
1012
1013 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001014 // Legacy shortcuts are only supported for primary profile.
1015 info = Process.myUserHandle().equals(args.user)
1016 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001017
Sunny Goyalb57645f2017-03-20 13:57:28 -07001018 if (info == null) {
1019 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1020 return;
1021 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001022 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001023 // The app is trying to add a shortcut without sufficient permissions
1024 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1025 return;
1026 }
1027 }
1028
Jon Mirandac476d6e2017-05-04 16:30:01 -07001029 if (container < 0) {
1030 // Adding a shortcut to the Workspace.
1031 final View view = createShortcut(info);
1032 boolean foundCellSpan = false;
1033 // First we check if we already know the exact location where we want to add this item.
1034 if (cellX >= 0 && cellY >= 0) {
1035 cellXY[0] = cellX;
1036 cellXY[1] = cellY;
1037 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001038
Jon Mirandac476d6e2017-05-04 16:30:01 -07001039 // If appropriate, either create a folder or add to an existing folder
1040 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001041 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001042 return;
1043 }
1044 DragObject dragObject = new DragObject();
1045 dragObject.dragInfo = info;
1046 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1047 true)) {
1048 return;
1049 }
1050 } else {
1051 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1052 }
1053
1054 if (!foundCellSpan) {
1055 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001056 return;
1057 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001058
1059 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1060 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001061 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001062 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001063 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001064 if (folderIcon != null) {
1065 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1066 folderInfo.add(info, args.rank, false);
1067 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001068 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001069 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
Sunny Goyale29897f2017-07-20 10:09:42 -07001073 public FolderIcon findFolderIcon(final long folderIconId) {
1074 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1075 @Override
1076 public boolean evaluate(ItemInfo info, View view) {
1077 return info != null && info.id == folderIconId;
1078 }
1079 });
1080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001085 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001087 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001088 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1089
Adam Cohened66b2b2012-01-23 17:28:51 -08001090 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001091 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Adam Cohen59400422014-03-05 18:07:04 -08001094 LauncherAppWidgetInfo launcherInfo;
1095 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001096 launcherInfo.spanX = itemInfo.spanX;
1097 launcherInfo.spanY = itemInfo.spanY;
1098 launcherInfo.minSpanX = itemInfo.minSpanX;
1099 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001100 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001102 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001103 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104
Sunny Goyal2100c782016-08-22 16:00:03 -07001105 if (hostView == null) {
1106 // Perform actual inflation because we're live
1107 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001109 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301110 prepareAppWidget(hostView, launcherInfo);
1111 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Sunny Goyald5462aa2016-11-22 16:52:39 +05301114 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001115 hostView.setTag(item);
1116 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001117 hostView.setFocusable(true);
1118 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001119 }
1120
Adam Cohended9f8d2010-11-03 13:25:16 -07001121 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1122 @Override
1123 public void onReceive(Context context, Intent intent) {
1124 final String action = intent.getAction();
1125 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001126 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001127 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001128 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001129 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001130 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001131 mShouldFadeInScrim = true;
1132 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1133 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1134 // the user unlocked and the Launcher is not in the foreground.
1135 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001136 }
1137 }
1138 };
1139
Tony Wickham010d2552017-01-20 08:15:28 -08001140 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001141 mWorkspace.updateIconBadges(updatedBadges);
1142 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001143
Sunny Goyal29947f02017-12-18 13:49:44 -08001144 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1145 if (popup != null) {
1146 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001147 }
1148 }
1149
Adam Cohended9f8d2010-11-03 13:25:16 -07001150 @Override
1151 public void onAttachedToWindow() {
1152 super.onAttachedToWindow();
1153
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001154 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001155 if (mLauncherCallbacks != null) {
1156 mLauncherCallbacks.onAttachedToWindow();
1157 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 }
1159
1160 @Override
1161 public void onDetachedFromWindow() {
1162 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001163
1164 if (mLauncherCallbacks != null) {
1165 mLauncherCallbacks.onDetachedFromWindow();
1166 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001167 }
1168
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001169 public AllAppsTransitionController getAllAppsController() {
1170 return mAllAppsController;
1171 }
1172
Winson Chunga6945242014-01-08 14:04:34 -08001173 public DragLayer getDragLayer() {
1174 return mDragLayer;
1175 }
1176
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001177 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001178 return mAppsView;
1179 }
1180
Winson Chunga6945242014-01-08 14:04:34 -08001181 public Workspace getWorkspace() {
1182 return mWorkspace;
1183 }
1184
1185 public Hotseat getHotseat() {
1186 return mHotseat;
1187 }
1188
Winson Chung8ae41982017-11-10 11:42:13 -08001189 public <T extends ViewGroup> T getOverviewPanel() {
1190 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001191 }
1192
Sunny Goyal47328fd2016-05-25 18:56:41 -07001193 public DropTargetBar getDropTargetBar() {
1194 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001195 }
1196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 public LauncherAppWidgetHost getAppWidgetHost() {
1198 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
Romain Guycbb89e42009-06-08 15:52:54 -07001200
Winson Chunga9abd0e2010-10-27 17:18:37 -07001201 public LauncherModel getModel() {
1202 return mModel;
1203 }
1204
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001205 public ModelWriter getModelWriter() {
1206 return mModelWriter;
1207 }
1208
Adam Cohen79d90c52016-04-22 13:29:20 -07001209 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001210 return mSharedPrefs;
1211 }
1212
Jon Miranda6bed3502017-09-19 14:43:17 -07001213 public int getOrientation() { return mOrientation; }
1214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 @Override
1216 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001217 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 super.onNewIntent(intent);
1219
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001220 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001221 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1222 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001223
1224 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001225 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001226 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001227 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001228 boolean internalStateHandled = InternalStateHandler
1229 .handleNewIntent(this, intent, alreadyOnHome);
1230
Winson15f8b172015-08-19 11:02:39 -07001231 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001232 if (!internalStateHandled) {
1233 // Note: There should be at most one log per method call. This is enforced
1234 // implicitly by using if-else statements.
1235 UserEventDispatcher ued = getUserEventDispatcher();
1236 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1237 if (topOpenView != null) {
1238 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1239 } else if (alreadyOnHome) {
1240 Target target = newContainerTarget(mStateManager.getState().containerType);
1241 target.pageIndex = mWorkspace.getCurrentPage();
1242 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1243 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001244
Sunny Goyald3864fa2017-11-14 15:06:36 -08001245 // In all these cases, only animate if we're already on home
1246 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001247
Sunny Goyald3864fa2017-11-14 15:06:36 -08001248 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1249
1250 // Reset the apps view
1251 if (!alreadyOnHome && mAppsView != null) {
1252 mAppsView.reset();
1253 }
1254
1255 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1256 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1257 }
1258 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001259
1260 final View v = getWindow().peekDecorView();
1261 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001262 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001263 }
1264
Adam Cohen9211d422014-10-07 18:14:53 -07001265 if (mLauncherCallbacks != null) {
1266 mLauncherCallbacks.onHomeIntent();
1267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
Winson15f8b172015-08-19 11:02:39 -07001269
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001270 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001271 }
1272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001274 public void onRestoreInstanceState(Bundle state) {
1275 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001276 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278
1279 @Override
1280 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001281 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001282 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001283
Adam Cohen21cd0022013-10-09 18:57:02 -07001284 }
Sunny Goyalea609262017-10-25 15:47:38 -07001285 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001286
1287
1288 AbstractFloatingView widgets = AbstractFloatingView
1289 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1290 if (widgets != null) {
1291 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1292 widgets.saveHierarchyState(widgetsState);
1293 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1294 } else {
1295 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1296 }
1297
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001298 // We close any open folders and shortcut containers since they will not be re-opened,
1299 // and we need to make sure this state is reflected.
1300 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001301
Sunny Goyal2100c782016-08-22 16:00:03 -07001302 if (mPendingRequestArgs != null) {
1303 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1304 }
1305 if (mPendingActivityResult != null) {
1306 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001309 super.onSaveInstanceState(outState);
1310
Adam Cohen9211d422014-10-07 18:14:53 -07001311 if (mLauncherCallbacks != null) {
1312 mLauncherCallbacks.onSaveInstanceState(outState);
1313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
1316 @Override
1317 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001319
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001320 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001321 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001322
Winson Chungcd2b0142011-06-08 16:02:26 -07001323 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001324 // It's possible to receive onDestroy after a new Launcher activity has
1325 // been created. In this case, don't interfere with the new Launcher.
1326 if (mModel.isCurrentCallbacks(this)) {
1327 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001328 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001329 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001330
Sunny Goyal745bad92016-05-02 10:54:12 -07001331 if (mRotationPrefChangeHandler != null) {
1332 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1333 }
1334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001336 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001338 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001342 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1343
Michael Jurka2ecf9952012-06-18 12:52:28 -07001344 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001345
Tony2917a8b2017-07-31 23:29:42 -07001346 clearPendingBinds();
1347
Adam Cohen9211d422014-10-07 18:14:53 -07001348 if (mLauncherCallbacks != null) {
1349 mLauncherCallbacks.onDestroy();
1350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
Sunny Goyalae502842016-06-17 08:43:56 -07001353 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1354 return mAccessibilityDelegate;
1355 }
1356
Adam Cohena9cf38f2011-05-02 15:36:58 -07001357 public DragController getDragController() {
1358 return mDragController;
1359 }
1360
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001362 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001363 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001364 }
1365
1366 @Override
1367 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1368 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001369 try {
1370 super.startIntentSenderForResult(intent, requestCode,
1371 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1372 } catch (IntentSender.SendIntentException e) {
1373 throw new ActivityNotFoundException();
1374 }
1375 }
1376
Winson Chung70d72102011-08-12 11:24:35 -07001377 /**
1378 * Indicates that we want global search for this activity by setting the globalSearch
1379 * argument for {@link #startSearch} to true.
1380 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001382 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 Bundle appSearchData, boolean globalSearch) {
1384 if (appSearchData == null) {
1385 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001386 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001388
Sunny Goyal6f28e712016-09-13 14:19:29 -07001389 if (mLauncherCallbacks == null ||
1390 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1391 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001392 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001393 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001394
1395 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001396 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001397 }
1398
Joe Onorato9c1289c2009-08-17 11:03:03 -04001399 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001400 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001401 }
1402
Adam Cohen517a7f52014-03-01 12:12:59 -08001403 public boolean isWorkspaceLoading() {
1404 return mWorkspaceLoading;
1405 }
1406
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001407 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001408 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001409 }
1410
Sunny Goyal04a324a2017-01-20 21:08:59 -08001411 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001412 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001413 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001414
Sunny Goyal2100c782016-08-22 16:00:03 -07001415 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001416 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001417 if (LOGD) {
1418 Log.d(TAG, "Adding widget from drop");
1419 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001420 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001421 }
1422
Sunny Goyal2100c782016-08-22 16:00:03 -07001423 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001424 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1425 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1426 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001427
Adam Cohenad4e15c2013-10-17 16:21:35 -07001428 Runnable onComplete = new Runnable() {
1429 @Override
1430 public void run() {
1431 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001432 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001433 }
1434 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001435 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001436 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 }
1438 }
Romain Guycbb89e42009-06-08 15:52:54 -07001439
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001440 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1441 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001442 info.container = container;
1443 info.screenId = screenId;
1444 if (cell != null) {
1445 info.cellX = cell[0];
1446 info.cellY = cell[1];
1447 }
1448 info.spanX = spanX;
1449 info.spanY = spanY;
1450
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001451 switch (info.itemType) {
1452 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1453 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001454 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001455 break;
1456 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001457 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001458 break;
1459 default:
1460 throw new IllegalStateException("Unknown item type: " + info.itemType);
1461 }
1462 }
1463
Adam Cohenfbba09b2011-07-18 21:43:05 -07001464 /**
1465 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001466 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001467 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001468 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1469 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001470 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1471 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1472 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001473 }
1474
Adam Cohenfbba09b2011-07-18 21:43:05 -07001475 /**
1476 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001477 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001478 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001479 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001480 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001481 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001482 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001483 // In the case where we've prebound the widget, we remove it from the DragLayer
1484 if (LOGD) {
1485 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1486 }
1487 getDragLayer().removeView(hostView);
1488
Adam Cohened66b2b2012-01-23 17:28:51 -08001489 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001490 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001491
1492 // Clear the boundWidget so that it doesn't get destroyed.
1493 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001494 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001495 // In this case, we either need to start an activity to get permission to bind
1496 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001497 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1498 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1499 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1500 this, info.componentName);
1501 } else {
1502 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1503 }
Adam Cohendd70d662012-10-04 16:53:44 -07001504 Bundle options = info.bindOptions;
1505
Sunny Goyalffe83f12014-08-14 17:39:34 -07001506 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1507 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001508 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001509 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001510 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001511 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001512 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001513 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001514 }
1515
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001518 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 folderInfo.title = getText(R.string.folder_name);
1520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001522 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523
1524 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001525 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301526 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001527 // Force measure the new folder icon
1528 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1529 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001530 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Winsonc0b52fe2015-09-09 16:38:15 -07001533 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001534 * Unbinds the view for the specified item, and removes the item and all its children.
1535 *
1536 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001537 * @param itemInfo the {@link ItemInfo} for this view.
1538 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001539 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001540 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001541 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001542 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001543 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1544 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001545 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001546 } else {
1547 mWorkspace.removeWorkspaceItem(v);
1548 }
Winsonc0b52fe2015-09-09 16:38:15 -07001549 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001550 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001551 }
1552 } else if (itemInfo instanceof FolderInfo) {
1553 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001554 if (v instanceof FolderIcon) {
1555 ((FolderIcon) v).removeListeners();
1556 }
Winsonc0b52fe2015-09-09 16:38:15 -07001557 mWorkspace.removeWorkspaceItem(v);
1558 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001559 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001560 }
1561 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1562 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001563 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001564 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001565 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001566 }
1567 } else {
1568 return false;
1569 }
1570 return true;
1571 }
1572
1573 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001574 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001575 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001576 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001577 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001578 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001579 // Deleting an app widget ID is a void call but writes to disk before returning
1580 // to the caller...
1581 new AsyncTask<Void, Void, Void>() {
1582 public Void doInBackground(Void ... args) {
1583 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1584 return null;
1585 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001586 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001587 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001588 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 @Override
1592 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001593 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
1595
Joe Onorato88ec0992009-11-19 13:16:06 -08001596 @Override
1597 public void onBackPressed() {
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08001598 if (finishAutoCancelActionMode()) {
1599 return;
1600 }
Adam Cohen9211d422014-10-07 18:14:53 -07001601 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1602 return;
1603 }
1604
Sunny Goyal45478022015-06-08 16:52:41 -07001605 if (mDragController.isDragging()) {
1606 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001607 return;
1608 }
1609
Jon Mirandafeba90f2016-10-06 10:53:29 -07001610 // Note: There should be at most one log per method call. This is enforced implicitly
1611 // by using if-else statements.
1612 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001613 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1614 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001615 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001616 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001617 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001618 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001619 } else {
1620 // Back button is a no-op here, but give at least some feedback for the button press
1621 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001622 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001623 }
1624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 * Launches the intent referred by the clicked shortcut.
1627 *
1628 * @param v The view representing the clicked shortcut.
1629 */
1630 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001631 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1632 // view has detached (it's possible for this to happen if the view is removed mid touch).
1633 if (v.getWindowToken() == null) {
1634 return;
1635 }
1636
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001637 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001638 return;
1639 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001640
Adam Cohen1697b792013-09-17 19:08:21 -07001641 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001642 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001643 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1644 LauncherLogProto.Action.Direction.NONE,
1645 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001646 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001647 }
1648 return;
1649 }
1650
1651 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001652 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001653 int page = mWorkspace.indexOfChild(v);
1654 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1655 LauncherLogProto.Action.Direction.NONE,
1656 LauncherLogProto.ContainerType.OVERVIEW, page);
1657 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001658 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001659 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001660 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001661 }
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001664 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001665 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001667 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001668 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001669 }
Sunny Goyal508da152014-08-14 10:53:27 -07001670 } else if (tag instanceof AppInfo) {
1671 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001672 } else if (tag instanceof LauncherAppWidgetInfo) {
1673 if (v instanceof PendingAppWidgetHostView) {
1674 onClickPendingWidget((PendingAppWidgetHostView) v);
1675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 }
1677 }
1678
Sunny Goyal70660032015-05-14 00:07:08 -07001679 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001680 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001681 return false;
1682 }
1683
Michael Jurkaaf442092010-06-10 17:01:57 -07001684 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001685 * Event handler for the app widget view which has not fully restored.
1686 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001687 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001688 if (mIsSafeModeEnabled) {
1689 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1690 return;
1691 }
1692
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001693 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001694 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001695 LauncherAppWidgetProviderInfo appWidgetInfo =
1696 mAppWidgetManager.findProvider(info.providerName, info.user);
1697 if (appWidgetInfo == null) {
1698 return;
1699 }
1700 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1701
Sunny Goyald478c832016-04-01 12:04:16 -07001702 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1703 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1704 // This should not happen, as we make sure that an Id is allocated during bind.
1705 return;
1706 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001707 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1708 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001709 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001710 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001711 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001712 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001713 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001714 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001715 }
1716 }
1717
Sunny Goyale6e72002017-01-12 16:55:36 -08001718 private void onClickPendingAppItem(final View v, final String packageName,
1719 boolean downloadStarted) {
1720 if (downloadStarted) {
1721 // If the download has started, simply direct to the market app.
1722 startMarketIntentForPackage(v, packageName);
1723 return;
1724 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001725 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001726 .setTitle(R.string.abandoned_promises_title)
1727 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001728 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1729 @Override
1730 public void onClick(DialogInterface dialogInterface, int i) {
1731 startMarketIntentForPackage(v, packageName);
1732 }
1733 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001734 .setNeutralButton(R.string.abandoned_clean_this,
1735 new DialogInterface.OnClickListener() {
1736 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001737 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001738 mWorkspace.removeAbandonedPromise(packageName, user);
1739 }
1740 })
1741 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001742 }
1743
1744 private void startMarketIntentForPackage(View v, String packageName) {
1745 ItemInfo item = (ItemInfo) v.getTag();
1746 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001747 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001748 }
1749
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001750 /**
1751 * Event handler for an app shortcut click.
1752 *
1753 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1754 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001755 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001756 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1757 Object tag = v.getTag();
1758 if (!(tag instanceof ShortcutInfo)) {
1759 throw new IllegalArgumentException("Input must be a Shortcut");
1760 }
1761
1762 // Open shortcut
1763 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001764
Sunny Goyal076839c2017-10-30 13:52:20 -07001765 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001766 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1767 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001768 ~FLAG_DISABLED_SUSPENDED &
1769 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001770 // If the app is only disabled because of the above flags, launch activity anyway.
1771 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001772 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001773 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1774 // Use a message specific to this shortcut, if it has one.
1775 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1776 return;
1777 }
1778 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001779 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001780 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001781 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001782 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1783 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001784 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001785 }
1786 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1787 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001788 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001789 }
1790
Chris Wren40c5ed32014-06-24 18:24:23 -04001791 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001792 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001793 String packageName = shortcut.intent.getComponent() != null ?
1794 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1795 if (!TextUtils.isEmpty(packageName)) {
1796 onClickPendingAppItem(v, packageName,
1797 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1798 return;
1799 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001800 }
1801
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001802 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001803 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001804 }
1805
Sunny Goyala7ce1662016-05-31 15:01:35 -07001806 private void startAppShortcutOrInfoActivity(View v) {
1807 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001808 Intent intent;
1809 if (item instanceof PromiseAppInfo) {
1810 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
1811 intent = promiseAppInfo.getMarketIntent();
1812 } else {
1813 intent = item.getIntent();
1814 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001815 if (intent == null) {
1816 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001817 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001818 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001819 }
1820
1821 /**
1822 * Event handler for a folder icon click.
1823 *
1824 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1825 */
1826 protected void onClickFolderIcon(View v) {
1827 if (LOGD) Log.d(TAG, "onClickFolder");
1828 if (!(v instanceof FolderIcon)){
1829 throw new IllegalArgumentException("Input must be a FolderIcon");
1830 }
1831
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001832 Folder folder = ((FolderIcon) v).getFolder();
1833 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001834 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001835 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001836 }
Michael Jurka5130e402011-10-13 04:55:35 -07001837 }
1838
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001839 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001840 * Event handler for the wallpaper picker button that appears after a long press
1841 * on the home screen.
1842 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001843 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001844 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001845 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1846 return;
1847 }
1848
Tony Wickham785f7a52015-08-31 17:28:32 -07001849 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1850 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001851 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001852 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001853 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001854
1855 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001856 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1857 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001858 intent.setPackage(pickerPackage);
1859 }
1860
Sunny Goyala5ace712017-11-15 17:12:51 -08001861 final Bundle launchOptions;
1862 if (v != null) {
1863 intent.setSourceBounds(getViewBounds(v));
1864 // If there is no target package, use the default intent chooser animation
1865 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
1866 } else {
1867 launchOptions = null;
1868 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001869 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001870 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001871 } catch (ActivityNotFoundException e) {
1872 setWaitingForResult(null);
1873 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1874 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001875 }
1876
Sunny Goyala7ce1662016-05-31 15:01:35 -07001877 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001879 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1880 try {
1881 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1882 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1883 // is enabled by default on NYC.
1884 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1885 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001886
1887 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1888 String id = ((ShortcutInfo) info).getDeepShortcutId();
1889 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301890 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001891 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001892 } else {
1893 // Could be launching some bookkeeping activity
1894 startActivity(intent, optsBundle);
1895 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001896 } finally {
1897 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001900 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1901 // corresponding permission. Show the appropriate permission prompt if that
1902 // is the case.
1903 if (intent.getComponent() == null
1904 && Intent.ACTION_CALL.equals(intent.getAction())
1905 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1906 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001907
1908 setWaitingForResult(PendingRequestArgs
1909 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001910 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1911 REQUEST_PERMISSION_CALL_PHONE);
1912 } else {
1913 // No idea why this was thrown.
1914 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001918
Sunny Goyalfe770c92016-09-27 14:38:58 -07001919 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07001920 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001921 if (Utilities.ATLEAST_MARSHMALLOW) {
1922 int left = 0, top = 0;
1923 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001924 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001925 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001926 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07001927 if (icon != null) {
1928 Rect bounds = icon.getBounds();
1929 left = (width - bounds.width()) / 2;
1930 top = v.getPaddingTop();
1931 width = bounds.width();
1932 height = bounds.height();
1933 }
1934 }
1935 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
1936 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
1937 // On L devices, we use the device default slide-up transition.
1938 // On L MR1 devices, we use a custom version of the slide-up transition which
1939 // doesn't have the delay present in the device default.
1940 return ActivityOptions.makeCustomAnimation(
1941 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
1942 }
1943 return null;
1944 }
1945
Tonye3c59252017-05-02 21:32:27 -07001946 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001947 int[] pos = new int[2];
1948 v.getLocationOnScreen(pos);
1949 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1950 }
1951
Sunny Goyala7ce1662016-05-31 15:01:35 -07001952 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001953 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001954 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1955 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001956 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001957 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001958 // Only launch using the new animation if the shortcut has not opted out (this is a
1959 // private contract between launcher and may be ignored in the future).
1960 boolean useLaunchAnimation = (v != null) &&
1961 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1962 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
1963
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001964 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001965
1966 // Prepare intent
1967 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1968 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001969 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001970 }
1971 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001972 if (Utilities.ATLEAST_MARSHMALLOW
1973 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001974 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001975 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08001976 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001977 // Shortcuts need some special checks due to legacy reasons.
1978 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001979 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001980 // Could be launching some bookkeeping activity
1981 startActivity(intent, optsBundle);
1982 } else {
1983 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1984 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1985 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001986
1987 if (v instanceof BubbleTextView) {
1988 // This is set to the view that launched the activity that navigated the user away
1989 // from launcher. Since there is no callback for when the activity has finished
1990 // launching, enable the press state and keep this reference to reset the press
1991 // state when we return to launcher.
1992 BubbleTextView btv = (BubbleTextView) v;
1993 btv.setStayPressed(true);
1994 setOnResumeCallback(btv);
1995 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001996 mAppLaunchSuccess = true;
1997 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07001998 } catch (ActivityNotFoundException|SecurityException e) {
1999 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2000 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2001 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002002 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002003 }
2004
Tony Wickhamdadb3042016-02-24 11:07:00 -08002005 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002006 public boolean dispatchTouchEvent(MotionEvent ev) {
2007 mLastDispatchTouchEventX = ev.getX();
2008 return super.dispatchTouchEvent(ev);
2009 }
2010
2011 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002013 if (!isDraggingEnabled()) return false;
2014 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002015 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016
Jon Miranda51f037d2016-11-07 08:37:20 -08002017 boolean ignoreLongPressToOverview =
2018 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002019
Adam Cohen1697b792013-09-17 19:08:21 -07002020 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002021 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002022 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302023 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2024 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002025 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002026 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002027 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2028 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2029 return true;
2030 } else {
2031 return false;
2032 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002033 } else {
2034 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002035 }
Adam Cohen1697b792013-09-17 19:08:21 -07002036 }
2037
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002038 CellLayout.CellInfo longClickCellInfo = null;
2039 View itemUnderLongClick = null;
2040 if (v.getTag() instanceof ItemInfo) {
2041 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002042 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002043 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002044 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
Winson Chung3d503fb2011-07-13 17:25:49 -07002047 // The hotseat touch handling does not go through Workspace, and we always allow long press
2048 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002049 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002050 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002051 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002052 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002053 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302054 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2055 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002056 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002057 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002058 return false;
2059 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302060 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2061 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002062 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002063 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002064 }
Jon Miranda379198e2016-09-23 08:54:40 -07002065 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2066 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002068 final boolean isAllAppsButton =
2069 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2070 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2071 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002072 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002074 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076 }
2077 }
2078 return true;
2079 }
2080
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002082 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 return mHotseat != null && layout != null &&
2084 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2085 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002086
Winson Chung3d503fb2011-07-13 17:25:49 -07002087 /**
2088 * Returns the CellLayout of the specified container at the specified screen.
2089 */
Sunny Goyal92820592015-03-02 11:31:35 -08002090 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002091 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2092 if (mHotseat != null) {
2093 return mHotseat.getLayout();
2094 } else {
2095 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002096 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002097 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002098 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002099 }
2100 }
2101
Michael Jurkae326f182011-11-21 14:05:46 -08002102 @Override
2103 public void onTrimMemory(int level) {
2104 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002105 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2106 // The widget preview db can result in holding onto over
2107 // 3MB of memory for caching which isn't necessary.
2108 SQLiteDatabase.releaseMemory();
2109
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002110 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002111 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002112 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002113 if (mLauncherCallbacks != null) {
2114 mLauncherCallbacks.onTrimMemory(level);
2115 }
Michael Jurkae326f182011-11-21 14:05:46 -08002116 }
2117
Michael Jurkad7c28052012-04-27 15:43:36 -07002118 @Override
2119 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002120 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002121 final List<CharSequence> text = event.getText();
2122 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002123 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002124 // TODO: When can workspace be null?
2125 text.add(mWorkspace == null
2126 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002127 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002128 return result;
2129 }
2130
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002131 public void setOnResumeCallback(OnResumeCallback callback) {
2132 if (mOnResumeCallback != null) {
2133 mOnResumeCallback.onLauncherResume();
2134 }
2135 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002136 }
2137
Michael Jurka7607c2f2013-04-03 14:33:19 -07002138 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002141 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002143 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002144 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002145 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002146 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002147 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002149
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002151 * Clear any pending bind callbacks. This is called when is loader is planning to
2152 * perform a full rebind from scratch.
2153 */
2154 @Override
2155 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002156 if (mPendingExecutor != null) {
2157 mPendingExecutor.markCompleted();
2158 mPendingExecutor = null;
2159 }
2160 }
2161
2162 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 * Refreshes the shortcuts shown on the workspace.
2164 *
2165 * Implementation of the method from LauncherModel.Callbacks.
2166 */
2167 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002168 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002169 // Floating panels (except the full widget sheet) are associated with individual icons. If
2170 // we are starting a fresh bind, close all such panels as all the icons are about
2171 // to go away.
2172 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002173 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002174
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002175 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002176
Winson Chungd64d1762013-08-20 14:37:16 -07002177 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002178 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002179 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002180
Winson Chung3d503fb2011-07-13 17:25:49 -07002181 if (mHotseat != null) {
2182 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002184 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 }
2186
Adam Cohendcd297f2013-06-18 13:13:40 -07002187 @Override
2188 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002189 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002190 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2191 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002192 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2193 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002194 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002195 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2196 // If there are no screens, we need to have an empty screen
2197 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002198 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002199 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002200
Winsonc7d2e832016-07-28 12:24:55 -07002201 // After we have added all the screens, if the wallpaper was locked to the default state,
2202 // then notify to indicate that it can be released and a proper wallpaper offset can be
2203 // computed before the next layout
2204 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002205 }
2206
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002207 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002208 int count = orderedScreenIds.size();
2209 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002210 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002211 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002212 // No need to bind the first screen, as its always bound.
2213 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2214 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 }
2217
Sunny Goyalb23980c2017-08-17 07:45:25 -07002218 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002219 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2220 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002221 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002222 if (newScreens != null) {
2223 bindAddScreens(newScreens);
2224 }
Winson Chungd64d1762013-08-20 14:37:16 -07002225
2226 // We add the items without animation on non-visible pages, and with
2227 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002228 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002229 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002230 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002231 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002232 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002233 }
Winson Chungc58497e2013-09-03 17:48:37 -07002234
Winson Chung87412982013-10-03 18:34:14 -07002235 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002236 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002237 }
2238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002240 * Bind the items start-end from the list.
2241 *
2242 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002244 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002245 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002246 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002247 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002248 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002249 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002250 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002251 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002252 int end = items.size();
2253 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002254 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002255
2256 // Short circuit if we are loading dock items for a configuration which has no dock
2257 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2258 mHotseat == null) {
2259 continue;
2260 }
2261
Sunny Goyal639e9062015-08-19 19:17:06 -07002262 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002263 switch (item.itemType) {
2264 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2265 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002266 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002267 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002268 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002269 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002270 }
2271 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002272 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002273 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002274 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002275 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002276 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002277 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2278 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002279 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002280 if (view == null) {
2281 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002282 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002283 break;
2284 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002285 default:
2286 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002287 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002288
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002289 /*
2290 * Remove colliding items.
2291 */
2292 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2293 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2294 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2295 View v = cl.getChildAt(item.cellX, item.cellY);
2296 Object tag = v.getTag();
2297 String desc = "Collision while binding workspace item: " + item
2298 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002299 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002300 throw (new RuntimeException(desc));
2301 } else {
2302 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002303 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002304 continue;
2305 }
2306 }
2307 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302308 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002309 if (animateIcons) {
2310 // Animate all the applications up now
2311 view.setAlpha(0f);
2312 view.setScaleX(0f);
2313 view.setScaleY(0f);
2314 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002315 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002318
Winson Chung997a9232013-07-24 15:33:46 -07002319 if (animateIcons) {
2320 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002321 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002322 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002323 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002324 final Runnable startBounceAnimRunnable = new Runnable() {
2325 public void run() {
2326 anim.playTogether(bounceAnims);
2327 anim.start();
2328 }
2329 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002330 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002331 // We post the animation slightly delayed to prevent slowdowns
2332 // when we are loading right after we return to launcher.
2333 mWorkspace.postDelayed(new Runnable() {
2334 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002335 if (mWorkspace != null) {
2336 mWorkspace.snapToPage(newScreenIndex);
2337 mWorkspace.postDelayed(startBounceAnimRunnable,
2338 NEW_APPS_ANIMATION_DELAY);
2339 }
Winson Chung94d67682013-09-25 16:29:40 -07002340 }
2341 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002342 } else {
2343 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002344 }
2345 }
Winson Chung64359a52013-07-08 17:17:08 -07002346 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002347 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 }
2349
Joe Onorato9c1289c2009-08-17 11:03:03 -04002350 /**
2351 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002352 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002353 public void bindAppWidget(LauncherAppWidgetInfo item) {
2354 View view = inflateAppWidget(item);
2355 if (view != null) {
2356 mWorkspace.addInScreen(view, item);
2357 mWorkspace.requestLayout();
2358 }
2359 }
2360
2361 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002362 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302363 PendingAppWidgetHostView view =
2364 new PendingAppWidgetHostView(this, item, mIconCache, true);
2365 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002366 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002367 }
2368
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002369 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002370
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002371 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002372
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002373 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2374 // If the provider is not ready, bind as a pending widget.
2375 appWidgetInfo = null;
2376 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2377 // The widget id is not valid. Try to find the widget based on the provider info.
2378 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2379 } else {
2380 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2381 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002382
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002383 // If the provider is ready, but the width is not yet restored, try to restore it.
2384 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2385 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002386 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002387 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2388 + " belongs to component " + item.providerName
2389 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002390 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002391 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002392 }
Sunny Goyalff572272014-07-23 13:58:07 -07002393
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002394 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002395 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002396 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2397 // Id has not been allocated yet. Allocate a new id.
2398 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2399 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002400
Sunny Goyald478c832016-04-01 12:04:16 -07002401 // Also try to bind the widget. If the bind fails, the user will be shown
2402 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002403 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002404 pendingInfo.spanX = item.spanX;
2405 pendingInfo.spanY = item.spanY;
2406 pendingInfo.minSpanX = item.minSpanX;
2407 pendingInfo.minSpanY = item.minSpanY;
2408 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002409
2410 boolean isDirectConfig =
2411 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2412 if (isDirectConfig && item.bindOptions != null) {
2413 Bundle newOptions = item.bindOptions.getExtras();
2414 if (options != null) {
2415 newOptions.putAll(options);
2416 }
2417 options = newOptions;
2418 }
Sunny Goyald478c832016-04-01 12:04:16 -07002419 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2420 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002421
Sunny Goyal86df1382016-08-10 15:03:22 -07002422 // We tried to bind once. If we were not able to bind, we would need to
2423 // go through the permission dialog, which means we cannot skip the config
2424 // activity.
2425 item.bindOptions = null;
2426 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2427
Sunny Goyald478c832016-04-01 12:04:16 -07002428 // Bind succeeded
2429 if (success) {
2430 // If the widget has a configure activity, it is still needs to set it up,
2431 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002432 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002433 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2434 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002435 }
Sunny Goyald478c832016-04-01 12:04:16 -07002436
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002437 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002438 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002439 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002440 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002441 // The widget was marked as UI not ready, but there is no configure activity to
2442 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002443 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002444 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002445 }
Sunny Goyalff572272014-07-23 13:58:07 -07002446 }
2447
Sunny Goyald5462aa2016-11-22 16:52:39 +05302448 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002449 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002450 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002451 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002452 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002453 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002454 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002455 }
2456
Sunny Goyal233ee962015-08-03 13:05:01 -07002457 item.minSpanX = appWidgetInfo.minSpanX;
2458 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302459 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002460 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302461 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002462 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302463 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002464
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002465 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002466 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002467 }
2468
Sunny Goyalff572272014-07-23 13:58:07 -07002469 /**
2470 * Restores a pending widget.
2471 *
2472 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002473 */
Sunny Goyald478c832016-04-01 12:04:16 -07002474 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002475 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2476 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2477 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002478 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002479 }
2480
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002481 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002482 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002483 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2484 info.pendingItemInfo = null;
2485 }
Sunny Goyalff572272014-07-23 13:58:07 -07002486
Sunny Goyalba47faa2017-10-04 16:50:57 -07002487 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002488 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002489 }
2490
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002491 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002492 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002493 }
2494
Adam Cohen1462de32012-07-24 22:34:36 -07002495 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002496 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002497 }
2498
Sunny Goyal527c7d32015-08-28 15:19:36 -07002499 @Override
2500 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2501 if (mPendingExecutor != null) {
2502 mPendingExecutor.markCompleted();
2503 }
2504 mPendingExecutor = executor;
2505 executor.attachTo(this);
2506 }
2507
2508 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2509 if (mPendingExecutor == executor) {
2510 mPendingExecutor = null;
2511 }
2512 }
2513
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002514 @Override
2515 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002516 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002517 mDragLayer.animate().alpha(1).withEndAction(
2518 executor == null ? null : executor::onLoadAnimationCompleted).start();
2519 } else if (executor != null) {
2520 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002521 }
2522 }
2523
Joe Onorato9c1289c2009-08-17 11:03:03 -04002524 /**
2525 * Callback saying that there aren't any more items to bind.
2526 *
2527 * Implementation of the method from LauncherModel.Callbacks.
2528 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002529 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002530 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002531 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002532
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002533 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002534
Sunny Goyal2100c782016-08-22 16:00:03 -07002535 if (mPendingActivityResult != null) {
2536 handleActivityResult(mPendingActivityResult.requestCode,
2537 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2538 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002539 }
2540
Sunny Goyala474a9b2017-05-04 16:47:11 -07002541 InstallShortcutReceiver.disableAndFlushInstallQueue(
2542 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002543
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002544 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002545 }
2546
Winson Chunga2413752012-04-03 14:22:34 -07002547 private boolean canRunNewAppsAnimation() {
2548 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002549 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002550 }
2551
Winson Chungc9168342013-06-26 14:54:55 -07002552 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002553 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002554 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2555 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002556 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002557 return bounceAnim;
2558 }
2559
Adam Cohen27772732013-11-11 14:00:56 +00002560 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002561 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002562 }
2563
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002564 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 * Add the icons for all apps.
2566 *
2567 * Implementation of the method from LauncherModel.Callbacks.
2568 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002569 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002570 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002571 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002572 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002573 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002574 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002575 return;
2576 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002577
Winson Chungb745afb2015-03-02 11:51:23 -08002578 mAppsView.setApps(apps);
2579 }
Adam Cohen9211d422014-10-07 18:14:53 -07002580 if (mLauncherCallbacks != null) {
2581 mLauncherCallbacks.bindAllApplications(apps);
2582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002583 }
2584
2585 /**
Tony2917a8b2017-07-31 23:29:42 -07002586 * Returns an Executor that will run after the launcher is first drawn (including after the
2587 * initial fade in animation). Returns null if the first draw has already occurred.
2588 */
2589 public @Nullable Executor getPendingExecutor() {
2590 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2591 }
2592
2593 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002594 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2595 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2596 */
2597 @Override
2598 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002599 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002600 }
2601
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002602 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002603 * A package was updated.
2604 *
2605 * Implementation of the method from LauncherModel.Callbacks.
2606 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002607 @Override
2608 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002609 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002610 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002611 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002612 }
2613
Sunny Goyal4390ace2014-10-13 11:33:11 -07002614 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002615 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002616 if (mAppsView != null) {
2617 mAppsView.updatePromiseAppProgress(app);
2618 }
2619 }
2620
2621 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002622 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002623 mWorkspace.widgetsRestored(widgets);
2624 }
2625
Joe Onorato9c1289c2009-08-17 11:03:03 -04002626 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002627 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002628 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002629 *
2630 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002631 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002632 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002633 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002634 if (!updated.isEmpty()) {
2635 mWorkspace.updateShortcuts(updated);
2636 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002637 }
2638
2639 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002640 * Update the state of a package, typically related to install state.
2641 *
2642 * Implementation of the method from LauncherModel.Callbacks.
2643 */
Sunny Goyale755d462014-07-22 13:48:29 -07002644 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002645 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002646 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002647 }
2648
2649 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002650 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002651 * in addition to specific applications to remove, the reason being that
2652 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002653 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002654 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002655 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002656 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002657 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002658 mWorkspace.removeItemsByMatcher(matcher);
2659 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002660 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002661
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002662 @Override
2663 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002664 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002665 if (mAppsView != null) {
2666 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002667 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002668 }
Joe Onoratobe386092009-11-17 17:32:16 -08002669
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002670 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002671 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2672 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002673 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2674 if (topView != null) {
2675 topView.onWidgetsBound();
2676 }
2677 }
2678
Tony Wickham86222d22017-03-29 15:30:43 -07002679 /**
2680 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2681 * refreshes the widgets and shortcuts associated with the given package/user
2682 */
2683 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002684 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002685 }
2686
Sunny Goyalc99cb172017-10-19 16:15:09 -07002687 public boolean isRotationEnabled () {
2688 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002689 }
2690
Winson Chung80baf5a2010-08-09 16:03:15 -07002691 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002692 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002693 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002694 @Override
2695 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2696 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002697
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002698 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2699 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002700 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002701 writer.println(prefix + " Homescreen " + i);
2702
2703 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2704 for (int j = 0; j < layout.getChildCount(); j++) {
2705 Object tag = layout.getChildAt(j).getTag();
2706 if (tag != null) {
2707 writer.println(prefix + " " + tag.toString());
2708 }
2709 }
2710 }
2711
2712 writer.println(prefix + " Hotseat");
2713 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002714 for (int j = 0; j < layout.getChildCount(); j++) {
2715 Object tag = layout.getChildAt(j).getTag();
2716 if (tag != null) {
2717 writer.println(prefix + " " + tag.toString());
2718 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002719 }
Sunny Goyala1365452015-10-01 15:46:24 -07002720
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002721 try {
2722 FileLog.flushAll(writer);
2723 } catch (Exception e) {
2724 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002725 }
2726 }
2727
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002728 writer.println(prefix + "Misc:");
2729 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2730 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2731 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2732
2733 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002734
Adam Cohen9211d422014-10-07 18:14:53 -07002735 if (mLauncherCallbacks != null) {
2736 mLauncherCallbacks.dump(prefix, fd, writer, args);
2737 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002738 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002739
Sunny Goyal66b24572016-09-21 15:57:55 -07002740 @Override
2741 @TargetApi(Build.VERSION_CODES.N)
2742 public void onProvideKeyboardShortcuts(
2743 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2744
2745 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002746 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002747 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2748 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2749 }
2750 View currentFocus = getCurrentFocus();
2751 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2752 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2753 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2754 }
Tony18c4aa42017-05-10 21:23:57 -05002755 if (currentFocus.getTag() instanceof ItemInfo
2756 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002757 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2758 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2759 }
2760 if (!shortcutInfos.isEmpty()) {
2761 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2762 }
2763
2764 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2765 }
2766
2767 @Override
2768 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2769 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2770 switch (keyCode) {
2771 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002772 if (isInState(NORMAL)) {
2773 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002774 return true;
2775 }
2776 break;
2777 case KeyEvent.KEYCODE_S: {
2778 View focusedView = getCurrentFocus();
2779 if (focusedView instanceof BubbleTextView
2780 && focusedView.getTag() instanceof ItemInfo
2781 && mAccessibilityDelegate.performAction(focusedView,
2782 (ItemInfo) focusedView.getTag(),
2783 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002784 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002785 return true;
2786 }
2787 break;
2788 }
2789 case KeyEvent.KEYCODE_O:
2790 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2791 return true;
2792 }
2793 break;
2794 }
2795 }
2796 return super.onKeyShortcut(keyCode, event);
2797 }
2798
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002799 public static Launcher getLauncher(Context context) {
2800 if (context instanceof Launcher) {
2801 return (Launcher) context;
2802 }
2803 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2804 }
2805
Sunny Goyal5a81c382017-03-20 15:08:06 -07002806 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002807
2808 @Override
2809 public void onSharedPreferenceChanged(
2810 SharedPreferences sharedPreferences, String key) {
2811 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002812 // Recreate the activity so that it initializes the rotation preference again.
2813 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002814 }
2815 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002816 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002817
Sunny Goyal7c8a65e2017-12-22 11:11:33 -08002818 @Override
2819 public void onActionModeStarted(ActionMode mode) {
2820 super.onActionModeStarted(mode);
2821 mCurrentActionMode = mode;
2822 }
2823
2824 @Override
2825 public void onActionModeFinished(ActionMode mode) {
2826 super.onActionModeFinished(mode);
2827 mCurrentActionMode = null;
2828 }
2829
2830 public boolean finishAutoCancelActionMode() {
2831 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
2832 mCurrentActionMode.finish();
2833 return true;
2834 }
2835 return false;
2836 }
2837
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002838 /**
2839 * Callback for listening for onResume
2840 */
2841 public interface OnResumeCallback {
2842
2843 void onLauncherResume();
2844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845}