blob: 826f686d40bd171b42d7c838c6fcac6d222a2db1 [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 Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070023import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000024import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070025import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040026import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080028import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070029import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080031import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080032import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040033import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070035import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040036import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070038import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070039import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070040import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070041import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070044import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070045import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070046import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070048import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020049import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.os.Handler;
Sunny Goyalb38fab72017-01-20 19:32:31 -080052import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080053import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070054import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040055import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070056import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070057import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070060import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070063import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.HapticFeedbackConstants;
65import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070066import android.view.KeyboardShortcutGroup;
67import android.view.KeyboardShortcutInfo;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080071import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.ViewGroup;
73import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080074import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070075import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070076import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.inputmethod.InputMethodManager;
Chet Haasea8f996d2015-02-17 12:56:04 -080078import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070080
Sunny Goyal651077b2014-06-30 14:15:31 -070081import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070082import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070083import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070084import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070085import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080086import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalb5e65c82016-10-26 18:32:38 -070087import com.android.launcher3.anim.AnimationLayerSet;
Tony Wickham9438ed42017-01-20 09:38:25 -080088import com.android.launcher3.notification.NotificationListener;
Tony Wickham010d2552017-01-20 08:15:28 -080089import com.android.launcher3.popup.PopupDataProvider;
Sunny Goyalffe83f12014-08-14 17:39:34 -070090import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010091import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal782f0c92017-01-19 10:27:54 -080092import com.android.launcher3.compat.PinItemRequestCompat;
Kenny Guyed131872014-04-30 03:02:21 +010093import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080094import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070095import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -070096import com.android.launcher3.dragndrop.DragController;
97import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070098import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070099import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800100import com.android.launcher3.dragndrop.PinItemDragListener;
Tony Wickham827cef22016-03-17 15:39:39 -0700101import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000102import com.android.launcher3.folder.Folder;
103import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700104import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700105import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700106import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700107import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700108import com.android.launcher3.model.PackageItemInfo;
109import com.android.launcher3.model.WidgetItem;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700110import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800111import com.android.launcher3.popup.PopupContainerWithArrow;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700112import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700113import com.android.launcher3.shortcuts.ShortcutKey;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800114import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530115import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
116import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
117import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700118import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.util.ItemInfoMatcher;
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -0800121import com.android.launcher3.util.LogConfig;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700122import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700123import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800124import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700125import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700126import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700127import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700128import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800129import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700130import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800131import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700132import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700133import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700134
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700135import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700136import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700138import java.util.Collection;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700139import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700140import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700141import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800142import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144/**
145 * Default launcher application.
146 */
Sunny Goyal27835952017-01-13 12:15:53 -0800147public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700148 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
149 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700150 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700151 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700152 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700154 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700155 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400156 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700159 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700160 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700161 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Michael Jurka8b805b12012-04-18 14:23:14 -0700163 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700164 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700165 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700166
Sunny Goyal28c6b962015-10-12 11:42:05 -0700167 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
168
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700169 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
170
Mathew Inwood876a8462013-06-14 14:12:41 +0100171 /**
172 * IntentStarter uses request codes starting with this. This must be greater than all activity
173 * request codes used internally.
174 */
175 protected static final int REQUEST_LAST = 100;
176
Winson Chung2672ff92012-05-04 16:22:30 -0700177 // The Intent extra that defines whether to ignore the launch animation
178 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400179 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
182 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700183 // Type: int
184 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700185 // Type: PendingRequestArgs
186 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
187 // Type: ActivityResultInfo
188 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700190 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800191
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700192 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700193 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
194 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700195
Adam Cohen091440a2015-03-18 14:16:05 -0700196 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700197 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700199 private boolean mIsSafeModeEnabled;
200
Sunny Goyal27835952017-01-13 12:15:53 -0800201 public static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800202 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700203 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700204 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Winson Chunga2413752012-04-03 14:22:34 -0700206 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700207 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
208 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700210
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700212 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800214 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700215 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700216
217 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700218
Sunny Goyalffe83f12014-08-14 17:39:34 -0700219 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700220 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700221
Michael Jurka0280c3b2010-09-17 15:00:07 -0700222 private int[] mTmpAddItemCellCoordinates = new int[2];
223
Sunny Goyal316490e2015-06-02 09:38:28 -0700224 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800225 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700226
Michael Jurka838a4ca2011-02-07 13:33:06 -0800227 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700228 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700229
Sunny Goyal47328fd2016-05-25 18:56:41 -0700230 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700231
232 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700233 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700234 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700235
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700236 // Main container view and the model for the widget tray screen.
237 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700238 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700239
Winson Chung4a2afa32012-07-19 14:53:05 -0700240 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
241 // scroll issues (because the workspace may not have been measured yet) and extra work.
242 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
243 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244
245 private SpannableStringBuilder mDefaultKeySsb = null;
246
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800249 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700250 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
Michael Jurka1e2f4652013-07-08 18:03:46 -0700252 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700253 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700254 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700255
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800257 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700258 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700259 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700260 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700261 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700262 private boolean mHasFocus = false;
263 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264
Tony Wickham010d2552017-01-20 08:15:28 -0800265 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700266
Adam Cohen61f560d2013-09-30 15:58:20 -0700267 private View.OnTouchListener mHapticFeedbackTouchListener;
268
Winson Chung400438b2011-01-16 17:53:48 -0800269 // Determines how long to wait after a rotation before restoring the screen orientation to
270 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700271 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800272
Adam Cohen1462de32012-07-24 22:34:36 -0700273 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
274
Winson Chung46353de2012-02-16 14:05:10 -0800275 // We only want to get the SharedPreferences once since it does an FS stat each time we get
276 // it from the context.
277 private SharedPreferences mSharedPrefs;
278
Adam Cohen4b66bf32015-09-18 12:15:19 -0700279 private boolean mMoveToDefaultScreenFromNewIntent;
280
Winson Chung13eb5272015-05-11 16:30:13 -0700281 // This is set to the view that launched the activity that navigated the user away from
282 // launcher. Since there is no callback for when the activity has finished launching, enable
283 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800284 private BubbleTextView mWaitingForResume;
285
Adam Cohen59400422014-03-05 18:07:04 -0800286 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
287 new HashMap<String, CustomAppWidget>();
288
Adam Cohen59400422014-03-05 18:07:04 -0800289 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700290 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
291 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800292 }
293 }
294
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700295 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
296 // state transition. If another state transition happened during this delay,
297 // simply unregister this runnable.
298 private Runnable mExitSpringLoadedModeRunnable;
299
Adam Cohen091440a2015-03-18 14:16:05 -0700300 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700301 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700302 if (mWorkspace != null) {
303 mWorkspace.buildPageHardwareLayers();
304 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700305 }
306 };
307
Sunny Goyal2100c782016-08-22 16:00:03 -0700308 // Activity result which needs to be processed after workspace has loaded.
309 private ActivityResultInfo mPendingActivityResult;
310 /**
311 * Holds extra information required to handle a result from an external call, like
312 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
313 */
314 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800315
Hyunyoung Songaa953652016-04-19 18:30:24 -0700316 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800317
Jon Miranda379198e2016-09-23 08:54:40 -0700318 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700319
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700320 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700321 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400322
323 @Thunk void setOrientation() {
324 if (mRotationEnabled) {
325 unlockScreenOrientation(true);
326 } else {
327 setRequestedOrientation(
328 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700329 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400330 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700331
Sunny Goyal745bad92016-05-02 10:54:12 -0700332 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700336 if (DEBUG_STRICT_MODE) {
337 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
338 .detectDiskReads()
339 .detectDiskWrites()
340 .detectNetwork() // or .detectAll() for all detectable problems
341 .penaltyLog()
342 .build());
343 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
344 .detectLeakedSqlLiteObjects()
345 .detectLeakedClosableObjects()
346 .penaltyLog()
347 .penaltyDeath()
348 .build());
349 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700350 if (LauncherAppState.PROFILE_STARTUP) {
351 Trace.beginSection("Launcher-onCreate");
352 }
Winson Chunga2413752012-04-03 14:22:34 -0700353
Adam Cohen9211d422014-10-07 18:14:53 -0700354 if (mLauncherCallbacks != null) {
355 mLauncherCallbacks.preOnCreate();
356 }
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400359
Sunny Goyal87f784c2017-01-11 10:48:34 -0800360 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700361
Adam Cohen2e6da152015-05-06 11:42:25 -0700362 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800363 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Sunny Goyalf5e37442016-11-02 10:31:24 -0700364 if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700365 Display display = getWindowManager().getDefaultDisplay();
366 Point mwSize = new Point();
367 display.getSize(mwSize);
368 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
369 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700370
Sunny Goyalf7258242015-10-19 16:59:07 -0700371 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700372 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800373 mModel = app.setLauncher(this);
374 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700375 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700376
Joe Onorato41a12d22009-10-31 18:30:00 -0400377 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700378 mAllAppsController = new AllAppsTransitionController(this);
379 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700380
Sunny Goyalffe83f12014-08-14 17:39:34 -0700381 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700382
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
384 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700385
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700386 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
387 // this also ensures that any synchronous binding below doesn't re-trigger another
388 // LauncherModel load.
389 mPaused = false;
390
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800391 mLauncherView = getLayoutInflater().inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 setupViews();
Winson1f064272016-07-18 17:18:02 -0700394 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700395 mExtractedColors = new ExtractedColors();
396 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397
Tony Wickham010d2552017-01-20 08:15:28 -0800398 mPopupDataProvider = new PopupDataProvider(this);
399
Tony Wickhame2217252016-03-22 16:34:23 -0700400 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
401 .addAccessibilityStateChangeListener(this);
402
Joe Onorato7c312c12009-08-13 21:36:53 -0700403 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700404
Sunny Goyalfe770c92016-09-27 14:38:58 -0700405 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406
Sunny Goyale26d1002016-06-20 14:52:14 -0700407 if (LauncherAppState.PROFILE_STARTUP) {
408 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
Sunny Goyal2100c782016-08-22 16:00:03 -0700411 // We only load the page synchronously if the user rotates (or triggers a
412 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700413 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
414 if (savedInstanceState != null) {
415 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
416 }
417 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700418 // If we are not binding synchronously, show a fade in animation when
419 // the first page bind completes.
420 mDragLayer.setAlpha(0);
421 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700422 // Pages bound synchronously.
423 mWorkspace.setCurrentPage(currentScreen);
424
Sunny Goyal2100c782016-08-22 16:00:03 -0700425 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
428 // For handling default keys
429 mDefaultKeySsb = new SpannableStringBuilder();
430 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800431
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800432 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700433 // In case we are on a device with locked rotation, we should look at preferences to check
434 // if the user has specifically allowed rotation.
435 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700436 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700437 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
438 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700439 }
440
441 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
442 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400443 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700444
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800445 setContentView(mLauncherView);
Adam Cohen9211d422014-10-07 18:14:53 -0700446 if (mLauncherCallbacks != null) {
447 mLauncherCallbacks.onCreate(savedInstanceState);
448 }
449 }
450
Sunny Goyal7779d622015-06-11 16:18:39 -0700451 @Override
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800452 public View findViewById(int id) {
453 return mLauncherView.findViewById(id);
454 }
455
456 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700457 public void onExtractedColorsChanged() {
458 loadExtractedColorsAndColorItems();
459 }
460
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700461 @Override
462 public void onAppWidgetHostReset() {
463 if (mAppWidgetHost != null) {
464 mAppWidgetHost.startListening();
465 }
466 }
467
Tony Wickham827cef22016-03-17 15:39:39 -0700468 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700469 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700470 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700471 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700472 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700473 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800474 boolean lightStatusBar = (FeatureFlags.LIGHT_STATUS_BAR
475 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
476 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700477 // It's possible that All Apps is visible when this is run,
Hyunyoung Song3285da32016-12-19 14:46:21 -0800478 // so always use light status bar in that case. Only change nav bar color to status bar
479 // color when All Apps is visible.
480 activateLightStatusBar(lightStatusBar || isAllAppsVisible(), isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700481 }
482 }
483
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800484 // TODO: use platform flag on API >= 26
485 private static final int SYSTEM_UI_FLAG_LIGHT_NAV_BAR = 0x10;
486
Tony Wickham345bff32016-09-28 15:34:51 -0700487 /**
488 * Sets the status bar to be light or not. Light status bar means dark icons.
Hyunyoung Song3285da32016-12-19 14:46:21 -0800489 * @param lightStatusBar make sure the status bar is light
490 * @param changeNavBar if true, make the nav bar theme in sync with status bar.
Tony Wickham345bff32016-09-28 15:34:51 -0700491 */
Hyunyoung Song3285da32016-12-19 14:46:21 -0800492 public void activateLightStatusBar(boolean lightStatusBar, boolean changeNavBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700493 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
494 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700495 if (lightStatusBar) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800496 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR ;
Hyunyoung Song3285da32016-12-19 14:46:21 -0800497 if (changeNavBar && Utilities.isAtLeastO()) {
498 newSystemUiFlags |= SYSTEM_UI_FLAG_LIGHT_NAV_BAR;
499 }
Tony Wickham93113872016-09-14 16:46:32 -0700500 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700501 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800502 if (changeNavBar && Utilities.isAtLeastO()) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800503 newSystemUiFlags &= ~(SYSTEM_UI_FLAG_LIGHT_NAV_BAR);
504 }
505 }
Hyunyoung Song3285da32016-12-19 14:46:21 -0800506
Tony Wickham345bff32016-09-28 15:34:51 -0700507 if (newSystemUiFlags != oldSystemUiFlags) {
508 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
509 }
Tony Wickham93113872016-09-14 16:46:32 -0700510 }
511
Adam Cohen9211d422014-10-07 18:14:53 -0700512 private LauncherCallbacks mLauncherCallbacks;
513
514 public void onPostCreate(Bundle savedInstanceState) {
515 super.onPostCreate(savedInstanceState);
516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onPostCreate(savedInstanceState);
518 }
519 }
520
Winson1f064272016-07-18 17:18:02 -0700521 public void onInsetsChanged(Rect insets) {
522 mDeviceProfile.updateInsets(insets);
523 mDeviceProfile.layout(this, true /* notifyListeners */);
524 }
525
Sunny Goyal32554d12015-12-03 15:31:25 -0800526 /**
527 * Call this after onCreate to set or clear overlay.
528 */
529 public void setLauncherOverlay(LauncherOverlay overlay) {
530 if (overlay != null) {
531 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
532 }
533 mWorkspace.setLauncherOverlay(overlay);
534 }
535
Adam Cohen9211d422014-10-07 18:14:53 -0700536 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
537 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 private boolean mWorkspaceImportanceStored = false;
540 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800542 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
544
545 @Override
546 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700547 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 return;
549 }
550 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700551 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700552 if (mWorkspace != null) {
553 mWorkspaceImportanceForAccessibility =
554 mWorkspace.getImportantForAccessibility();
555 mWorkspace.setImportantForAccessibility(
556 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
557 mWorkspaceImportanceStored = true;
558 }
559 if (mHotseat != null) {
560 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
561 mHotseat.setImportantForAccessibility(
562 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
563 mHotseatImportanceStored = true;
564 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 }
566
567 @Override
568 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700569 if (mWorkspaceImportanceStored && mWorkspace != null) {
570 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
571 }
572 if (mHotseatImportanceStored && mHotseat != null) {
573 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
574 }
575 mWorkspaceImportanceStored = false;
576 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700577 }
578 });
Adam Cohen9211d422014-10-07 18:14:53 -0700579 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700580 }
581
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700582 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700583 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700584 if (mLauncherCallbacks != null) {
585 mLauncherCallbacks.onLauncherProviderChange();
586 }
587 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700588
Adam Cohen9211d422014-10-07 18:14:53 -0700589 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700591 if (mLauncherCallbacks != null) {
592 return mLauncherCallbacks.hasCustomContentToLeft();
593 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 return false;
595 }
596
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 * {@link #addToCustomContentPage}. This will only be invoked if
600 * {@link #hasCustomContentToLeft()} is {@code true}.
601 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700603 if (mLauncherCallbacks != null) {
604 mLauncherCallbacks.populateCustomContentContainer();
605 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 }
607
608 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700609 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 * ensure the custom content page is added or removed if necessary.
611 */
612 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600613 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 // Not bound yet, wait for bindScreens to be called.
615 return;
616 }
617
618 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
619 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500620 mWorkspace.createCustomContentContainer();
621 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
623 mWorkspace.removeCustomContentPage();
624 }
625 }
626
Hyunyoung Songaa953652016-04-19 18:30:24 -0700627 public UserEventDispatcher getUserEventDispatcher() {
628 if (mLauncherCallbacks != null) {
629 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
630 if (dispatcher != null) {
631 return dispatcher;
632 }
633 }
634
Sunny Goyal64976d52016-06-20 14:56:28 -0700635 // Logger object is a singleton and does not have to be coupled with the foreground
636 // activity. Since most user event logging is done on the UI, the object is retrieved
637 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700638 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700639 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700640 }
641 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800642 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100643
Hyunyoung Song3f471442015-04-08 19:01:34 -0700644 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700645 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
646 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700647 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700648 }
649
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000650 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700651 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
652 // This cast is safe as long as the id < 0x00FFFFFF
653 // Since we jail all the dynamically generated views, there should be no clashes
654 // with any other views.
655 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000656 }
657
Tony Wickham010d2552017-01-20 08:15:28 -0800658 public PopupDataProvider getPopupDataProvider() {
659 return mPopupDataProvider;
660 }
661
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700663 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
664 * a configuration step, this allows the proper animations to run after other transitions.
665 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 private long completeAdd(
667 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
668 long screenId = info.screenId;
669 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700670 // When the screen id represents an actual screen (as opposed to a rank) we make sure
671 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700673 }
Adam Cohendb364c32014-05-20 14:23:40 -0700674
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800676 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700677 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700678 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700680 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700681 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700682 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700684 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700685 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 int widgetId = appWidgetId;
687 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700688 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700690 // Since the view was just bound, also launch the configure activity if needed
691 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
692 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800693 if (provider != null) {
694 new WidgetAddFlowHandler(provider)
695 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700696 }
697 }
698 break;
699 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700701
Adam Cohendb364c32014-05-20 14:23:40 -0700702 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800705 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700707 if (isWorkspaceLoading()) {
708 // process the result once the workspace has loaded.
709 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
710 return;
711 }
712 mPendingActivityResult = null;
713
Winson Chunge341d302013-08-16 14:31:00 -0700714 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700715 final PendingRequestArgs requestArgs = mPendingRequestArgs;
716 setWaitingForResult(null);
717 if (requestArgs == null) {
718 return;
719 }
720
721 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700722
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 Runnable exitSpringLoaded = new Runnable() {
724 @Override
725 public void run() {
726 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
727 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
728 }
729 };
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700732 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
735 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700737 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700740 addAppWidgetImpl(
741 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800742 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700743 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 }
745 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
747 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700748 // User could have free-scrolled between pages before picking a wallpaper; make sure
749 // we move to the closest one now.
750 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700751 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752 }
753 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700757 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700758
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 // We have special handling for widgets
760 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800761 final int appWidgetId;
762 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
763 : -1;
764 if (widgetId < 0) {
765 appWidgetId = pendingAddWidgetId;
766 } else {
767 appWidgetId = widgetId;
768 }
769
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800771 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700772 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
773 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700776 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 @Override
778 public void run() {
779 exitSpringLoadedDragModeDelayed(false, 0, null);
780 }
781 };
Adam Cohendb364c32014-05-20 14:23:40 -0700782
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
784 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
785 } else {
786 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
787 // When the screen id represents an actual screen (as opposed to a rank)
788 // we make sure that the drop page actually exists.
789 requestArgs.screenId =
790 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700791 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700792 final CellLayout dropLayout =
793 mWorkspace.getScreenWithId(requestArgs.screenId);
794
795 dropLayout.setDropPending(true);
796 final Runnable onComplete = new Runnable() {
797 @Override
798 public void run() {
799 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
800 dropLayout.setDropPending(false);
801 }
802 };
803 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700805 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800806 return;
807 }
808
Sunny Goyald478c832016-04-01 12:04:16 -0700809 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
810 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700811 if (resultCode == RESULT_OK) {
812 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700813 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700814 }
815 // Leave the widget in the pending state if the user canceled the configure.
816 return;
817 }
818
Sunny Goyalaad90582015-07-09 14:22:50 -0700819 if (requestCode == REQUEST_CREATE_SHORTCUT) {
820 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700821 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
822 completeAdd(requestCode, data, -1, requestArgs);
823 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
824 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
825
Sunny Goyalaad90582015-07-09 14:22:50 -0700826 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700827 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800832 }
833
834 @Override
Sunny Goyaldec3a902017-01-25 18:23:36 -0800835 protected void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800836 final int requestCode, final int resultCode, final Intent data) {
837 handleActivityResult(requestCode, resultCode, data);
838 if (mLauncherCallbacks != null) {
839 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
840 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 }
842
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600843 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700844 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600845 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700846 PendingRequestArgs pendingArgs = mPendingRequestArgs;
847 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
848 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
849 setWaitingForResult(null);
850
Sunny Goyal28c6b962015-10-12 11:42:05 -0700851 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700852 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700854 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700855 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700856 Intent intent = pendingArgs.getPendingIntent();
857
Sunny Goyal28c6b962015-10-12 11:42:05 -0700858 if (grantResults.length > 0
859 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700860 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700861 } else {
862 // TODO: Show a snack bar with link to settings
863 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700864 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700865 }
866 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600867 if (mLauncherCallbacks != null) {
868 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
869 grantResults);
870 }
871 }
872
Adam Cohendb364c32014-05-20 14:23:40 -0700873 /**
874 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
875 *
876 * @param screenId the screen id to check
877 * @return the new screen, or screenId if it exists
878 */
879 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800880 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700881 if (dropLayout == null) {
882 // it's possible that the add screen was removed because it was
883 // empty and a re-bind occurred
884 mWorkspace.addExtraEmptyScreen();
885 return mWorkspace.commitExtraEmptyScreen();
886 } else {
887 return screenId;
888 }
889 }
890
Sunny Goyal2100c782016-08-22 16:00:03 -0700891 @Thunk void completeTwoStageWidgetDrop(
892 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
893 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800894 Runnable onCompleteRunnable = null;
895 int animationType = 0;
896
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 if (resultCode == RESULT_OK) {
899 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
900 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800901 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 onCompleteRunnable = new Runnable() {
904 @Override
905 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700906 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700907 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
908 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 }
910 };
911 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800912 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700916 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
918 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700919 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 // The animated view may be null in the case of a rotation during widget configuration
921 onCompleteRunnable.run();
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700926 protected void onStop() {
927 super.onStop();
928 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700929
930 if (mLauncherCallbacks != null) {
931 mLauncherCallbacks.onStop();
932 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700933
Sunny Goyalf5e37442016-11-02 10:31:24 -0700934 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700935 mAppWidgetHost.stopListening();
936 }
Tony Wickham010d2552017-01-20 08:15:28 -0800937
938 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700939 }
940
941 @Override
942 protected void onStart() {
943 super.onStart();
944 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700945
946 if (mLauncherCallbacks != null) {
947 mLauncherCallbacks.onStart();
948 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700949
Sunny Goyalf5e37442016-11-02 10:31:24 -0700950 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700951 mAppWidgetHost.startListening();
952 }
Tony Wickham010d2552017-01-20 08:15:28 -0800953
954 if (!isWorkspaceLoading()) {
955 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
956 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700957 }
958
959 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200961 long startTime = 0;
962 if (DEBUG_RESUME_TIME) {
963 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400964 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200965 }
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.preOnResume();
969 }
970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700972 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700973
Winson Chung4a2afa32012-07-19 14:53:05 -0700974 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700975 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700976 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800977 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700978 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700979 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700980 // view after launching an app, as they may be depending on the UI to be static to
981 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700982 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700983 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800984 } else if (mOnResumeState == State.WIDGETS) {
985 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700986 }
987 mOnResumeState = State.NONE;
988
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800989 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700990 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700991 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700992 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700993 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700995 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200996 // We might have postponed some bind calls until onResume (see waitUntilResume) --
997 // execute them here
998 long startTimeCallbacks = 0;
999 if (DEBUG_RESUME_TIME) {
1000 startTimeCallbacks = System.currentTimeMillis();
1001 }
1002
Michael Jurka1e2f4652013-07-08 18:03:46 -07001003 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1004 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 if (DEBUG_RESUME_TIME) {
1008 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1009 (System.currentTimeMillis() - startTimeCallbacks));
1010 }
Michael Jurka447bf842013-05-15 14:52:15 +02001011 }
Michael Jurka54554252013-08-01 12:52:23 +02001012 if (mOnResumeCallbacks.size() > 0) {
1013 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1014 mOnResumeCallbacks.get(i).run();
1015 }
1016 mOnResumeCallbacks.clear();
1017 }
Winson Chunge4e50662012-01-23 14:45:13 -08001018
1019 // Reset the pressed state of icons that were locked in the press state while activities
1020 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001021 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001022 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001023 mWaitingForResume.setStayPressed(false);
1024 }
Winson Chung82963d52013-10-09 11:20:57 -07001025
Adam Cohen06dff352012-06-01 17:17:08 -07001026 // It is possible that widgets can receive updates while launcher is not in the foreground.
1027 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1028 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1029 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001030 if (!isWorkspaceLoading()) {
1031 getWorkspace().reinflateWidgetsIfNecessary();
1032 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001033
Michael Jurka447bf842013-05-15 14:52:15 +02001034 if (DEBUG_RESUME_TIME) {
1035 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1036 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001037
Adam Cohen4b66bf32015-09-18 12:15:19 -07001038 // We want to suppress callbacks about CustomContent being shown if we have just received
1039 // onNewIntent while the user was present within launcher. In that case, we post a call
1040 // to move the user to the main screen (which will occur after onResume). We don't want to
1041 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1042 // suppress here.
1043 if (mWorkspace.getCustomContentCallbacks() != null
1044 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001045 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001046 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001047 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1048 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001049 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001050 }
1051 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001052 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001053 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001054 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001055
Sunny Goyal756adbc2015-04-16 15:20:51 -07001056 if (!isWorkspaceLoading()) {
1057 // Process any items that were added while Launcher was away.
1058 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001059
1060 // Refresh shortcuts if the permission changed.
1061 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001062 }
Adam Cohen9211d422014-10-07 18:14:53 -07001063
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001064 if (shouldShowDiscoveryBounce()) {
1065 mAllAppsController.showDiscoveryBounce();
1066 }
1067 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001068 if (mLauncherCallbacks != null) {
1069 mLauncherCallbacks.onResume();
1070 }
Adam Cohen06dff352012-06-01 17:17:08 -07001071 }
1072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001074 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001075 // Ensure that items added to Launcher are queued until Launcher returns
1076 InstallShortcutReceiver.enableInstallQueue();
1077
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001079 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001080 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001081 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001082
1083 // We call onHide() aggressively. The custom content callbacks should be able to
1084 // debounce excess onHide calls.
1085 if (mWorkspace.getCustomContentCallbacks() != null) {
1086 mWorkspace.getCustomContentCallbacks().onHide();
1087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Adam Cohen9211d422014-10-07 18:14:53 -07001089 if (mLauncherCallbacks != null) {
1090 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
1094 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001095 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1096 // by a onResume or by scrolling otherwise.
1097 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001098
1099 // Custom content is completely hidden
1100 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001101
1102 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1103 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001104
1105 // Indicates whether the user is allowed to scroll away from the custom content.
1106 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
1108
Adam Cohenc2d6e892014-10-16 09:49:24 -07001109 public interface LauncherOverlay {
1110
1111 /**
1112 * Touch interaction leading to overscroll has begun
1113 */
1114 public void onScrollInteractionBegin();
1115
1116 /**
1117 * Touch interaction related to overscroll has ended
1118 */
1119 public void onScrollInteractionEnd();
1120
1121 /**
1122 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1123 * screen (or in the case of RTL, the rightmost screen).
1124 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001125 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126
1127 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001128 * Called when the launcher is ready to use the overlay
1129 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001131 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 }
1133
Jun Mukai8af0cd82015-05-12 12:32:12 -07001134 public interface LauncherSearchCallbacks {
1135 /**
1136 * Called when the search overlay is shown.
1137 */
1138 public void onSearchOverlayOpened();
1139
1140 /**
1141 * Called when the search overlay is dismissed.
1142 */
1143 public void onSearchOverlayClosed();
1144 }
1145
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001147 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001148 }
1149
1150 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1151
Sunny Goyalc86df472016-02-25 09:19:38 -08001152 public void onScrollChanged(float progress) {
1153 if (mWorkspace != null) {
1154 mWorkspace.onOverlayScrollChanged(progress);
1155 }
1156 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001157 }
1158
Jorim Jaggid017f882014-01-14 17:08:48 -08001159 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001160 if (mLauncherCallbacks != null) {
1161 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001162 } else {
1163 // On devices with a locked orientation, we will at least have the allow rotation
1164 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001165 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001166 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001167 }
1168
Adam Cohen9211d422014-10-07 18:14:53 -07001169 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001170 CustomContentCallbacks callbacks, String description) {
1171 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001172 }
1173
Adam Cohenedb40762013-07-18 16:45:45 -07001174 // The custom content needs to offset its content to account for the QSB
1175 public int getTopOffsetForCustomContent() {
1176 return mWorkspace.getPaddingTop();
1177 }
1178
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001179 @Override
1180 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001182 if (mModel.isCurrentCallbacks(this)) {
1183 mModel.stopLoader();
1184 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001185 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1186
Romain Guy13c2e7b2010-03-10 19:45:00 -08001187 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001188 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001189
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001190 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001191 @Override
1192 public void onWindowFocusChanged(boolean hasFocus) {
1193 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001194 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001195
1196 if (mLauncherCallbacks != null) {
1197 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1198 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001199 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 private boolean acceptFilter() {
1202 final InputMethodManager inputManager = (InputMethodManager)
1203 getSystemService(Context.INPUT_METHOD_SERVICE);
1204 return !inputManager.isFullscreenMode();
1205 }
1206
1207 @Override
1208 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001209 final int uniChar = event.getUnicodeChar();
1210 final boolean handled = super.onKeyDown(keyCode, event);
1211 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1212 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1214 keyCode, event);
1215 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001216 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001217 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001218 // showSearchDialog()
1219 // If there are multiple keystrokes before the search dialog takes focus,
1220 // onSearchRequested() will be called for every keystroke,
1221 // but it is idempotent, so it's fine.
1222 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224 }
1225
Joe Onorato8a9625e2010-01-28 15:55:35 -08001226 // Eat the long press event so the keyboard doesn't come up.
1227 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1228 return true;
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 return handled;
1232 }
1233
Winson46163472015-09-22 17:31:56 -07001234 @Override
1235 public boolean onKeyUp(int keyCode, KeyEvent event) {
1236 if (keyCode == KeyEvent.KEYCODE_MENU) {
1237 // Ignore the menu key if we are currently dragging or are on the custom content screen
1238 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001239 // Close any open floating view
1240 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001241
Winson46163472015-09-22 17:31:56 -07001242 // Stop resizing any widgets
1243 mWorkspace.exitWidgetResizeMode();
1244
1245 // Show the overview mode if we are on the workspace
1246 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1247 !mWorkspace.isSwitchingState()) {
1248 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001249 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001250 }
1251 }
1252 return true;
1253 }
1254 return super.onKeyUp(keyCode, event);
1255 }
1256
Karl Rosaen138a0412009-04-23 19:00:21 -07001257 private String getTypedText() {
1258 return mDefaultKeySsb.toString();
1259 }
1260
Sunny Goyal6f28e712016-09-13 14:19:29 -07001261 @Override
1262 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001263 mDefaultKeySsb.clear();
1264 mDefaultKeySsb.clearSpans();
1265 Selection.setSelection(mDefaultKeySsb, 0);
1266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 /**
1269 * Restores the previous state, if it exists.
1270 *
1271 * @param savedState The previous state.
1272 */
1273 private void restoreState(Bundle savedState) {
1274 if (savedState == null) {
1275 return;
1276 }
1277
Sunny Goyalfe770c92016-09-27 14:38:58 -07001278 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1279 State[] stateValues = State.values();
1280 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1281 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001282 if (state == State.APPS || state == State.WIDGETS) {
1283 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001284 }
1285
Sunny Goyal2100c782016-08-22 16:00:03 -07001286 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1287 if (requestArgs != null) {
1288 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001290
1291 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
1293
1294 /**
1295 * Finds all the views we need and configure them properly.
1296 */
1297 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001298 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001299 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001300 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001301 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1302 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001303 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001304
Sunny Goyal784f9c32016-03-23 16:56:54 -07001305 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1306 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1307 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308
Winson Chung4c98d922011-05-31 16:50:48 -07001309 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001310 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001311
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 // Setup the hotseat
1313 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1314 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001315 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001316 }
1317
Winsone9f27272015-10-13 10:47:51 -07001318 // Setup the overview panel
1319 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001320
Winson Chung4c98d922011-05-31 16:50:48 -07001321 // Setup the workspace
1322 mWorkspace.setHapticFeedbackEnabled(false);
1323 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001324 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001325 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1326 // default state, otherwise we will update to the wrong offsets in RTL
1327 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001328 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001329 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001330
Tony Wickham34d2c912015-09-11 09:27:58 -07001331 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001332 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001333
Winson Chungef7f8742015-06-04 17:18:17 -07001334 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001335 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001336 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001337 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1338 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1339 } else {
1340 mAppsView.setSearchBarController(new DefaultAppSearchController());
1341 }
Craig Mautner360310b2012-10-26 15:13:08 -07001342
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001344 mDragController.setMoveTarget(mWorkspace);
1345 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001346 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001347
Peter Schiller310a9882016-06-30 13:52:36 -07001348 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1349 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1350 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001351
Sunny Goyal322d5562015-06-25 19:35:49 -07001352 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1353 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
Winsone9f27272015-10-13 10:47:51 -07001357 private void setupOverviewPanel() {
1358 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1359
Winsone9f27272015-10-13 10:47:51 -07001360 // Bind wallpaper button actions
1361 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301362 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001363 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001364 public void handleViewClick(View view) {
1365 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001366 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001367 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001368 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1369
1370 // Bind widget button actions
1371 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301372 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001373 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001374 public void handleViewClick(View view) {
1375 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001376 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001377 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001378 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1379
1380 // Bind settings actions
1381 View settingsButton = findViewById(R.id.settings_button);
1382 boolean hasSettings = hasSettings();
1383 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301384 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001385 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001386 public void handleViewClick(View view) {
1387 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001388 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001389 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001390 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1391 } else {
1392 settingsButton.setVisibility(View.GONE);
1393 }
1394
1395 mOverviewPanel.setAlpha(0f);
1396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001399 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001400 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001401 */
1402 public void setAllAppsButton(View allAppsButton) {
1403 mAllAppsButton = allAppsButton;
1404 }
1405
Sunny Goyalbb011da2016-06-15 15:42:29 -07001406 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001407 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001408 }
1409
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001410 public View getWidgetsButton() {
1411 return mWidgetsButton;
1412 }
1413
Anjali Koppal5ad44842014-03-10 20:34:39 -07001414 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 * Creates a view representing a shortcut.
1416 *
1417 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001419 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001420 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422
1423 /**
1424 * Creates a view representing a shortcut inflated from the specified resource.
1425 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 * @param parent The group the shortcut belongs to.
1427 * @param info The data structure describing the shortcut.
1428 *
1429 * @return A View inflated from layoutResId.
1430 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001431 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001432 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001433 parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001434 favorite.applyFromShortcutInfo(info);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001435 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001437 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 return favorite;
1439 }
1440
1441 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 * Add a shortcut to the workspace.
1443 *
1444 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001446 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001447 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001448 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001449 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001450
Sunny Goyal782f0c92017-01-19 10:27:54 -08001451 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001452 args.getPendingIntent().getComponent() == null) {
1453 return;
1454 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001455
Sunny Goyal782f0c92017-01-19 10:27:54 -08001456 ShortcutInfo info = null;
1457 if (Utilities.isAtLeastO()) {
Sunny Goyaldec3a902017-01-25 18:23:36 -08001458 info = LauncherAppsCompat.createShortcutInfoFromPinItemRequest(
1459 this, PinItemRequestCompat.getPinItemRequest(data));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001460 }
1461
1462 if (info == null) {
1463 info = InstallShortcutReceiver.fromShortcutIntent(this, data);
1464
1465 if (info == null || !PackageManagerHelper.hasPermissionForActivity(
1466 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1467 // The app is trying to add a shortcut without sufficient permissions
1468 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1469 return;
1470 }
1471 }
1472
1473 final View view = createShortcut(info);
Sunny Goyal5c97f512015-05-19 16:03:28 -07001474 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001475 // First we check if we already know the exact location where we want to add this item.
1476 if (cellX >= 0 && cellY >= 0) {
1477 cellXY[0] = cellX;
1478 cellXY[1] = cellY;
1479 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001480
1481 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001482 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001483 true, null,null)) {
1484 return;
1485 }
1486 DragObject dragObject = new DragObject();
1487 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001488 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1489 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001490 return;
1491 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001492 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001493 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001494 }
1495
1496 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001497 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001498 return;
1499 }
1500
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001501 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301502 mWorkspace.addInScreen(view, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 }
1504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001506 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001508 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001510 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001511 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1512
Adam Cohened66b2b2012-01-23 17:28:51 -08001513 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001514 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001515 }
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Adam Cohen59400422014-03-05 18:07:04 -08001517 if (appWidgetInfo.isCustomWidget) {
1518 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001519 }
1520
Adam Cohen59400422014-03-05 18:07:04 -08001521 LauncherAppWidgetInfo launcherInfo;
1522 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001523 launcherInfo.spanX = itemInfo.spanX;
1524 launcherInfo.spanY = itemInfo.spanY;
1525 launcherInfo.minSpanX = itemInfo.minSpanX;
1526 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyala52ecb02016-12-16 15:04:51 -08001527 launcherInfo.user = appWidgetInfo.getUser();
Romain Guycbb89e42009-06-08 15:52:54 -07001528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001530 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531
Sunny Goyal2100c782016-08-22 16:00:03 -07001532 if (hostView == null) {
1533 // Perform actual inflation because we're live
1534 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001536 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301537 prepareAppWidget(hostView, launcherInfo);
1538 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
Romain Guycbb89e42009-06-08 15:52:54 -07001540
Sunny Goyald5462aa2016-11-22 16:52:39 +05301541 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001542 hostView.setTag(item);
1543 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001544 hostView.setFocusable(true);
1545 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001546 }
1547
Adam Cohended9f8d2010-11-03 13:25:16 -07001548 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1549 @Override
1550 public void onReceive(Context context, Intent intent) {
1551 final String action = intent.getAction();
1552 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001553 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001554
Winson Chungc100e8e2011-08-09 16:02:43 -07001555 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001556 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001557 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001558 if (!showWorkspace(false)) {
1559 // If we are already on the workspace, then manually reset all apps
1560 mAppsView.reset();
1561 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001562 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001563 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001564 }
1565 }
1566 };
1567
Tony Wickham010d2552017-01-20 08:15:28 -08001568 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1569 Runnable r = new Runnable() {
1570 @Override
1571 public void run() {
1572 mWorkspace.updateIconBadges(updatedBadges);
1573 mAppsView.updateIconBadges(updatedBadges);
1574 }
1575 };
1576 if (!waitUntilResume(r)) {
1577 r.run();
1578 }
1579 }
1580
Adam Cohended9f8d2010-11-03 13:25:16 -07001581 @Override
1582 public void onAttachedToWindow() {
1583 super.onAttachedToWindow();
1584
1585 // Listen for broadcasts related to user-presence
1586 final IntentFilter filter = new IntentFilter();
1587 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001588 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001589 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001590 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001591
1592 if (mLauncherCallbacks != null) {
1593 mLauncherCallbacks.onAttachedToWindow();
1594 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001595 }
1596
1597 @Override
1598 public void onDetachedFromWindow() {
1599 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001600 if (mAttached) {
1601 unregisterReceiver(mReceiver);
1602 mAttached = false;
1603 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001604
1605 if (mLauncherCallbacks != null) {
1606 mLauncherCallbacks.onDetachedFromWindow();
1607 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 }
1609
1610 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001611 // The following code used to be in onResume, but it turns out onResume is called when
1612 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1613 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001614 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001615 if (!mWorkspaceLoading) {
1616 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001617 // We want to let Launcher draw itself at least once before we force it to build
1618 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001619 // apps is nice and speedy.
1620 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001621 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001622 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001623 if (mStarted) return;
1624 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001625 // We delay the layer building a bit in order to give
1626 // other message processing a time to run. In particular
1627 // this avoids a delay in hiding the IME if it was
1628 // currently shown, because doing that may involve
1629 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001630 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001631 final ViewTreeObserver.OnDrawListener listener = this;
1632 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001633 public void run() {
1634 if (mWorkspace != null &&
1635 mWorkspace.getViewTreeObserver() != null) {
1636 mWorkspace.getViewTreeObserver().
1637 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001638 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001639 }
1640 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001641 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001642 }
1643 });
1644 }
1645 clearTypedText();
1646 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 }
1648
Winson Chunga6945242014-01-08 14:04:34 -08001649 public DragLayer getDragLayer() {
1650 return mDragLayer;
1651 }
1652
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001653 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001654 return mAppsView;
1655 }
1656
Hyunyoung Song3f471442015-04-08 19:01:34 -07001657 public WidgetsContainerView getWidgetsView() {
1658 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001659 }
1660
Winson Chunga6945242014-01-08 14:04:34 -08001661 public Workspace getWorkspace() {
1662 return mWorkspace;
1663 }
1664
Sunny Goyal6178f132016-07-11 17:30:03 -07001665 public View getQsbContainer() {
1666 return mQsbContainer;
1667 }
1668
Winson Chunga6945242014-01-08 14:04:34 -08001669 public Hotseat getHotseat() {
1670 return mHotseat;
1671 }
1672
Jorim Jaggid017f882014-01-14 17:08:48 -08001673 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001674 return mOverviewPanel;
1675 }
1676
Sunny Goyal47328fd2016-05-25 18:56:41 -07001677 public DropTargetBar getDropTargetBar() {
1678 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001679 }
1680
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001681 public LauncherAppWidgetHost getAppWidgetHost() {
1682 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
Romain Guycbb89e42009-06-08 15:52:54 -07001684
Winson Chunga9abd0e2010-10-27 17:18:37 -07001685 public LauncherModel getModel() {
1686 return mModel;
1687 }
1688
Adam Cohen79d90c52016-04-22 13:29:20 -07001689 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001690 return mSharedPrefs;
1691 }
1692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 @Override
1694 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001695 long startTime = 0;
1696 if (DEBUG_RESUME_TIME) {
1697 startTime = System.currentTimeMillis();
1698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 super.onNewIntent(intent);
1700
Winson15f8b172015-08-19 11:02:39 -07001701 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1702 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1703 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001704
1705 // Check this condition before handling isActionMain, as this will get reset.
1706 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001707 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001708
Winson15f8b172015-08-19 11:02:39 -07001709 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1710 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001711 if (mWorkspace == null) {
1712 // Can be cases where mWorkspace is null, this prevents a NPE
1713 return;
1714 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001715
1716 // Note: There should be at most one log per method call. This is enforced implicitly
1717 // by using if-else statements.
1718 UserEventDispatcher ued = getUserEventDispatcher();
1719
1720 // TODO: Log this case.
Adam Cohen6fecd412013-10-02 17:41:50 -07001721 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001722
Jon Mirandafeba90f2016-10-06 10:53:29 -07001723 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham540913e2017-01-23 11:47:51 -08001724 if (topOpenView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301725 ued.logActionCommand(Action.Command.HOME_INTENT,
1726 topOpenView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001727 } else if (topOpenView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301728 ued.logActionCommand(Action.Command.HOME_INTENT,
1729 ((Folder) topOpenView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001730 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301731 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001732 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1733 }
1734
1735 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001736 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001737 exitSpringLoadedDragMode();
1738
1739 // If we are already on home, then just animate back to the workspace,
1740 // otherwise, just wait until onResume to set the state back to Workspace
1741 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001742 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001743 } else {
1744 mOnResumeState = State.WORKSPACE;
1745 }
1746
1747 final View v = getWindow().peekDecorView();
1748 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001749 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001750 INPUT_METHOD_SERVICE);
1751 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1752 }
1753
Winson Chungb745afb2015-03-02 11:51:23 -08001754 // Reset the apps view
1755 if (!alreadyOnHome && mAppsView != null) {
1756 mAppsView.scrollToTop();
1757 }
1758
Hyunyoung Song3f471442015-04-08 19:01:34 -07001759 // Reset the widgets view
1760 if (!alreadyOnHome && mWidgetsView != null) {
1761 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001762 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001763
Adam Cohen9211d422014-10-07 18:14:53 -07001764 if (mLauncherCallbacks != null) {
1765 mLauncherCallbacks.onHomeIntent();
1766 }
Sunny Goyalb38fab72017-01-20 19:32:31 -08001767
1768 Parcelable dragExtra = intent
1769 .getParcelableExtra(PinItemDragListener.EXTRA_PIN_ITEM_DRAG_LISTENER);
1770 if (dragExtra instanceof PinItemDragListener) {
1771 PinItemDragListener dragListener = (PinItemDragListener) dragExtra;
1772 dragListener.setLauncher(this);
1773 mDragLayer.setOnDragListener(dragListener);
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
Adam Cohened307df2013-10-02 09:37:31 -07001776
Adam Cohen9211d422014-10-07 18:14:53 -07001777 if (mLauncherCallbacks != null) {
1778 mLauncherCallbacks.onNewIntent(intent);
1779 }
Winson15f8b172015-08-19 11:02:39 -07001780
1781 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1782 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1783 // animation.
1784 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001785 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001786 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001787 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1788 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001789
1790 // We use this flag to suppress noisy callbacks above custom content state
1791 // from onResume.
1792 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001793 mWorkspace.post(new Runnable() {
1794 @Override
1795 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001796 if (mWorkspace != null) {
1797 mWorkspace.moveToDefaultScreen(true);
1798 }
Winson15f8b172015-08-19 11:02:39 -07001799 }
1800 });
1801 }
1802 }
1803
1804 if (DEBUG_RESUME_TIME) {
1805 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1806 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001807 }
1808
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001810 public void onRestoreInstanceState(Bundle state) {
1811 super.onRestoreInstanceState(state);
1812 for (int page: mSynchronouslyBoundPages) {
1813 mWorkspace.restoreInstanceStateForChild(page);
1814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
1816
1817 @Override
1818 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001819 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001820 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1821 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001822
Adam Cohen21cd0022013-10-09 18:57:02 -07001823 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001824 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825
Michael Jurka883f55b2010-10-21 15:47:14 -07001826 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001827 // We close any open folders and shortcut containers since they will not be re-opened,
1828 // and we need to make sure this state is reflected.
1829 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001830
Sunny Goyal2100c782016-08-22 16:00:03 -07001831 if (mPendingRequestArgs != null) {
1832 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1833 }
1834 if (mPendingActivityResult != null) {
1835 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837
Adam Cohen9211d422014-10-07 18:14:53 -07001838 if (mLauncherCallbacks != null) {
1839 mLauncherCallbacks.onSaveInstanceState(outState);
1840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 }
1842
1843 @Override
1844 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001846
Michael Jurka9d906c72011-10-14 06:25:36 -07001847 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001848 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001849
Winson Chungcd2b0142011-06-08 16:02:26 -07001850 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001851 // It's possible to receive onDestroy after a new Launcher activity has
1852 // been created. In this case, don't interfere with the new Launcher.
1853 if (mModel.isCurrentCallbacks(this)) {
1854 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001855 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001856 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001857
Sunny Goyal745bad92016-05-02 10:54:12 -07001858 if (mRotationPrefChangeHandler != null) {
1859 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1860 }
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001863 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001865 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001867 mAppWidgetHost = null;
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 TextKeyListener.getInstance().release();
1870
Tony Wickhame2217252016-03-22 16:34:23 -07001871 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1872 .removeAccessibilityStateChangeListener(this);
1873
Michael Jurka2ecf9952012-06-18 12:52:28 -07001874 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001875
1876 if (mLauncherCallbacks != null) {
1877 mLauncherCallbacks.onDestroy();
1878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880
Sunny Goyalae502842016-06-17 08:43:56 -07001881 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1882 return mAccessibilityDelegate;
1883 }
1884
Adam Cohena9cf38f2011-05-02 15:36:58 -07001885 public DragController getDragController() {
1886 return mDragController;
1887 }
1888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001890 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001891 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001892 }
1893
1894 @Override
1895 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1896 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001897 try {
1898 super.startIntentSenderForResult(intent, requestCode,
1899 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1900 } catch (IntentSender.SendIntentException e) {
1901 throw new ActivityNotFoundException();
1902 }
1903 }
1904
Winson Chung70d72102011-08-12 11:24:35 -07001905 /**
1906 * Indicates that we want global search for this activity by setting the globalSearch
1907 * argument for {@link #startSearch} to true.
1908 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001910 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001912
Karl Rosaen138a0412009-04-23 19:00:21 -07001913 if (initialQuery == null) {
1914 // Use any text typed in the launcher as the initial query
1915 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 if (appSearchData == null) {
1918 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001919 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001921
Sunny Goyal6f28e712016-09-13 14:19:29 -07001922 if (mLauncherCallbacks == null ||
1923 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1924 // Starting search from the callbacks failed. Start the default global search.
1925 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001926 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001927
1928 // We need to show the workspace after starting the search
1929 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001930 }
1931
Ian Parkinson047036e2014-09-01 15:40:53 +01001932 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001933 * Starts the global search activity. This code is a copied from SearchManager
1934 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001935 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001936 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001937 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001938 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1939 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1940 if (globalSearchActivity == null) {
1941 Log.w(TAG, "No global search activity found.");
1942 return;
1943 }
1944 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1945 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1946 intent.setComponent(globalSearchActivity);
1947 // Make sure that we have a Bundle to put source in
1948 if (appSearchData == null) {
1949 appSearchData = new Bundle();
1950 } else {
1951 appSearchData = new Bundle(appSearchData);
1952 }
Adam Cohen9211d422014-10-07 18:14:53 -07001953 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001954 if (!appSearchData.containsKey("source")) {
1955 appSearchData.putString("source", getPackageName());
1956 }
1957 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1958 if (!TextUtils.isEmpty(initialQuery)) {
1959 intent.putExtra(SearchManager.QUERY, initialQuery);
1960 }
1961 if (selectInitialQuery) {
1962 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1963 }
1964 intent.setSourceBounds(sourceBounds);
1965 try {
1966 startActivity(intent);
1967 } catch (ActivityNotFoundException ex) {
1968 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
Yura4f93ec62014-02-04 14:15:21 +00001972 public boolean isOnCustomContent() {
1973 return mWorkspace.isOnOrMovingToCustomContent();
1974 }
1975
Winson Chung70d72102011-08-12 11:24:35 -07001976 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001977 public boolean onPrepareOptionsMenu(Menu menu) {
1978 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001979 if (mLauncherCallbacks != null) {
1980 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1981 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001982 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001985 @Override
1986 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001987 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001988 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001989 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001990 }
1991
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001993 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994 }
1995
Adam Cohen517a7f52014-03-01 12:12:59 -08001996 public boolean isWorkspaceLoading() {
1997 return mWorkspaceLoading;
1998 }
1999
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002000 private void setWorkspaceLoading(boolean value) {
2001 boolean isLocked = isWorkspaceLocked();
2002 mWorkspaceLoading = value;
2003 if (isLocked != isWorkspaceLocked()) {
2004 onWorkspaceLockedChanged();
2005 }
2006 }
2007
Sunny Goyal04a324a2017-01-20 21:08:59 -08002008 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002009 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002010 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002011 if (isLocked != isWorkspaceLocked()) {
2012 onWorkspaceLockedChanged();
2013 }
2014 }
2015
Adam Cohen9211d422014-10-07 18:14:53 -07002016 protected void onWorkspaceLockedChanged() {
2017 if (mLauncherCallbacks != null) {
2018 mLauncherCallbacks.onWorkspaceLockedChanged();
2019 }
2020 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002021
Sunny Goyal2100c782016-08-22 16:00:03 -07002022 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002023 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002024 if (LOGD) {
2025 Log.d(TAG, "Adding widget from drop");
2026 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002027 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002028 }
2029
Sunny Goyal2100c782016-08-22 16:00:03 -07002030 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002031 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
2032 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
2033 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07002034
Adam Cohenad4e15c2013-10-17 16:21:35 -07002035 Runnable onComplete = new Runnable() {
2036 @Override
2037 public void run() {
2038 // Exit spring loaded mode if necessary after adding the widget
2039 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2040 null);
2041 }
2042 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08002043 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07002044 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046 }
Romain Guycbb89e42009-06-08 15:52:54 -07002047
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002048 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002049 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002050 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002051 mWorkspace.moveToCustomContentScreen(animate);
2052 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002053
2054 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2055 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002056 info.container = container;
2057 info.screenId = screenId;
2058 if (cell != null) {
2059 info.cellX = cell[0];
2060 info.cellY = cell[1];
2061 }
2062 info.spanX = spanX;
2063 info.spanY = spanY;
2064
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002065 switch (info.itemType) {
2066 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2067 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002068 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002069 break;
2070 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08002071 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002072 break;
2073 default:
2074 throw new IllegalStateException("Unknown item type: " + info.itemType);
2075 }
2076 }
2077
Adam Cohenfbba09b2011-07-18 21:43:05 -07002078 /**
2079 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002080 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08002081 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002082 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2083 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08002084 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
2085 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
2086 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002087 }
2088
Adam Cohenfbba09b2011-07-18 21:43:05 -07002089 /**
2090 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002091 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002092 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002093 AppWidgetHostView hostView = info.boundWidget;
2094 int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08002095 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08002096 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002097 // In the case where we've prebound the widget, we remove it from the DragLayer
2098 if (LOGD) {
2099 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2100 }
2101 getDragLayer().removeView(hostView);
2102
Adam Cohened66b2b2012-01-23 17:28:51 -08002103 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08002104 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002105
2106 // Clear the boundWidget so that it doesn't get destroyed.
2107 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002108 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002109 // In this case, we either need to start an activity to get permission to bind
2110 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002111 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002112 Bundle options = info.bindOptions;
2113
Sunny Goyalffe83f12014-08-14 17:39:34 -07002114 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2115 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002116 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002117 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07002118 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002119 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07002120 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002121 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002122 }
2123
Adam Cohendcd297f2013-06-18 13:13:40 -07002124 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002125 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002126 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 folderInfo.title = getText(R.string.folder_name);
2128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002130 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2131 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132
2133 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002134 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302135 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07002136 // Force measure the new folder icon
2137 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2138 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002139 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 }
Romain Guycbb89e42009-06-08 15:52:54 -07002141
Winsonc0b52fe2015-09-09 16:38:15 -07002142 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002143 * Unbinds the view for the specified item, and removes the item and all its children.
2144 *
2145 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002146 * @param itemInfo the {@link ItemInfo} for this view.
2147 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002148 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002149 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002150 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002151 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002152 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2153 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002154 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002155 } else {
2156 mWorkspace.removeWorkspaceItem(v);
2157 }
Winsonc0b52fe2015-09-09 16:38:15 -07002158 if (deleteFromDb) {
2159 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2160 }
2161 } else if (itemInfo instanceof FolderInfo) {
2162 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002163 if (v instanceof FolderIcon) {
2164 ((FolderIcon) v).removeListeners();
2165 }
Winsonc0b52fe2015-09-09 16:38:15 -07002166 mWorkspace.removeWorkspaceItem(v);
2167 if (deleteFromDb) {
2168 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2169 }
2170 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2171 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002172 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002173 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002174 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002175 }
2176 } else {
2177 return false;
2178 }
2179 return true;
2180 }
2181
2182 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002183 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002184 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002185 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002186 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002187 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002188 // Deleting an app widget ID is a void call but writes to disk before returning
2189 // to the caller...
2190 new AsyncTask<Void, Void, Void>() {
2191 public Void doInBackground(Void ... args) {
2192 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2193 return null;
2194 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002195 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002196 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002197 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002198 }
2199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 @Override
2201 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002202 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 }
2204
Joe Onorato88ec0992009-11-19 13:16:06 -08002205 @Override
2206 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002207 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2208 return;
2209 }
2210
Sunny Goyal45478022015-06-08 16:52:41 -07002211 if (mDragController.isDragging()) {
2212 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002213 return;
2214 }
2215
Jon Mirandafeba90f2016-10-06 10:53:29 -07002216 // Note: There should be at most one log per method call. This is enforced implicitly
2217 // by using if-else statements.
2218 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002219 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2220 if (topView != null) {
2221 if (topView.getActiveTextView() != null) {
2222 topView.getActiveTextView().dispatchBackKey();
2223 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002224 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302225 ued.logActionCommand(Action.Command.BACK,
2226 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002227 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302228 ued.logActionCommand(Action.Command.BACK,
2229 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002230 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002231 topView.close(true);
2232 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002233 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302234 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002235 showWorkspace(true);
2236 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302237 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002238 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002239 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302240 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002241 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002242 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002243 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002244 mWorkspace.exitWidgetResizeMode();
2245
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002246 // Back button is a no-op here, but give at least some feedback for the button press
2247 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002248 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002249 }
2250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 * Launches the intent referred by the clicked shortcut.
2253 *
2254 * @param v The view representing the clicked shortcut.
2255 */
2256 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002257 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2258 // view has detached (it's possible for this to happen if the view is removed mid touch).
2259 if (v.getWindowToken() == null) {
2260 return;
2261 }
2262
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002263 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002264 return;
2265 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002266
Adam Cohen1697b792013-09-17 19:08:21 -07002267 if (v instanceof Workspace) {
2268 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002269 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002270 }
2271 return;
2272 }
2273
2274 if (v instanceof CellLayout) {
2275 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002276 int page = mWorkspace.indexOfChild(v);
2277 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2278 LauncherLogProto.Action.Direction.NONE,
2279 LauncherLogProto.ContainerType.OVERVIEW, page);
2280 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002281 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002282 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002283 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002284 }
2285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002287 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002288 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002290 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002291 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002292 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002293 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2294 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002295 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002296 } else if (tag instanceof AppInfo) {
2297 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002298 } else if (tag instanceof LauncherAppWidgetInfo) {
2299 if (v instanceof PendingAppWidgetHostView) {
2300 onClickPendingWidget((PendingAppWidgetHostView) v);
2301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 }
2303 }
2304
Sunny Goyal70660032015-05-14 00:07:08 -07002305 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002306 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002307 return false;
2308 }
2309
Michael Jurkaaf442092010-06-10 17:01:57 -07002310 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002311 * Event handler for the app widget view which has not fully restored.
2312 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002313 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002314 if (mIsSafeModeEnabled) {
2315 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2316 return;
2317 }
2318
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002319 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002320 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002321 LauncherAppWidgetProviderInfo appWidgetInfo =
2322 mAppWidgetManager.findProvider(info.providerName, info.user);
2323 if (appWidgetInfo == null) {
2324 return;
2325 }
2326 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2327
Sunny Goyald478c832016-04-01 12:04:16 -07002328 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2329 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2330 // This should not happen, as we make sure that an Id is allocated during bind.
2331 return;
2332 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002333 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2334 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002335 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002336 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002337 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002338 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002339 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002340 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002341 }
2342 }
2343
2344 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002345 * Event handler for the "grid" button that appears on the home screen, which
2346 * enters all apps mode.
2347 *
2348 * @param v The view that was clicked.
2349 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002350 protected void onClickAllAppsButton(View v) {
2351 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002352 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302353 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2354 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002355 showAppsView(true /* animated */, true /* updatePredictedApps */,
2356 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002357 }
2358 }
2359
2360 protected void onLongClickAllAppsButton(View v) {
2361 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2362 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302363 getUserEventDispatcher().logActionOnControl(Action.Touch.LONGPRESS,
2364 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002365 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002366 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002367 }
2368 }
2369
Sunny Goyale6e72002017-01-12 16:55:36 -08002370 private void onClickPendingAppItem(final View v, final String packageName,
2371 boolean downloadStarted) {
2372 if (downloadStarted) {
2373 // If the download has started, simply direct to the market app.
2374 startMarketIntentForPackage(v, packageName);
2375 return;
2376 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002377 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002378 .setTitle(R.string.abandoned_promises_title)
2379 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002380 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2381 @Override
2382 public void onClick(DialogInterface dialogInterface, int i) {
2383 startMarketIntentForPackage(v, packageName);
2384 }
2385 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002386 .setNeutralButton(R.string.abandoned_clean_this,
2387 new DialogInterface.OnClickListener() {
2388 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002389 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002390 mWorkspace.removeAbandonedPromise(packageName, user);
2391 }
2392 })
2393 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002394 }
2395
2396 private void startMarketIntentForPackage(View v, String packageName) {
2397 ItemInfo item = (ItemInfo) v.getTag();
2398 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2399 boolean success = startActivitySafely(v, intent, item);
2400 if (success && v instanceof BubbleTextView) {
2401 mWaitingForResume = (BubbleTextView) v;
2402 mWaitingForResume.setStayPressed(true);
2403 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002404 }
2405
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002406 /**
2407 * Event handler for an app shortcut click.
2408 *
2409 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2410 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002411 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002412 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2413 Object tag = v.getTag();
2414 if (!(tag instanceof ShortcutInfo)) {
2415 throw new IllegalArgumentException("Input must be a Shortcut");
2416 }
2417
2418 // Open shortcut
2419 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002420
2421 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002422 if ((shortcut.isDisabled &
2423 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2424 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2425 // If the app is only disabled because of the above flags, launch activity anyway.
2426 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002427 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002428 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2429 // Use a message specific to this shortcut, if it has one.
2430 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2431 return;
2432 }
2433 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002434 int error = R.string.activity_not_available;
2435 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2436 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002437 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2438 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002439 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002440 }
2441 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2442 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002443 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002444 }
2445
Chris Wren40c5ed32014-06-24 18:24:23 -04002446 // Check for abandoned promise
Sunny Goyale6e72002017-01-12 16:55:36 -08002447 if ((v instanceof BubbleTextView) && shortcut.isPromise()) {
2448 String packageName = shortcut.intent.getComponent() != null ?
2449 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2450 if (!TextUtils.isEmpty(packageName)) {
2451 onClickPendingAppItem(v, packageName,
2452 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2453 return;
2454 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002455 }
2456
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002457 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002458 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002459 }
2460
Sunny Goyala7ce1662016-05-31 15:01:35 -07002461 private void startAppShortcutOrInfoActivity(View v) {
2462 ItemInfo item = (ItemInfo) v.getTag();
2463 Intent intent = item.getIntent();
2464 if (intent == null) {
2465 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002466 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002467 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002468 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002469
2470 if (success && v instanceof BubbleTextView) {
2471 mWaitingForResume = (BubbleTextView) v;
2472 mWaitingForResume.setStayPressed(true);
2473 }
2474 }
2475
2476 /**
2477 * Event handler for a folder icon click.
2478 *
2479 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2480 */
2481 protected void onClickFolderIcon(View v) {
2482 if (LOGD) Log.d(TAG, "onClickFolder");
2483 if (!(v instanceof FolderIcon)){
2484 throw new IllegalArgumentException("Input must be a FolderIcon");
2485 }
2486
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002487 Folder folder = ((FolderIcon) v).getFolder();
2488 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002489 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002490 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002491 }
Michael Jurka5130e402011-10-13 04:55:35 -07002492 }
2493
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002494 /**
2495 * Event handler for the (Add) Widgets button that appears after a long press
2496 * on the home screen.
2497 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002498 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002500 if (mIsSafeModeEnabled) {
2501 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2502 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002503 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002504 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002505 }
2506
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002507 /**
2508 * Event handler for the wallpaper picker button that appears after a long press
2509 * on the home screen.
2510 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002511 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002512 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002513 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2514 return;
2515 }
2516
Tony Wickham785f7a52015-08-31 17:28:32 -07002517 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2518 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002519 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002520 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002521 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002522
2523 String pickerPackage = getString(R.string.wallpaper_picker_package);
2524 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2525 if (!hasTargetPackage) {
2526 intent.setPackage(pickerPackage);
2527 }
2528
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002529 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002530 try {
2531 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2532 // If there is no target package, use the default intent chooser animation
2533 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2534 } catch (ActivityNotFoundException e) {
2535 setWaitingForResult(null);
2536 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2537 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 }
2539
2540 /**
2541 * Event handler for a click on the settings button that appears after a long press
2542 * on the home screen.
2543 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002544 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002546 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2547 .setPackage(getPackageName());
2548 intent.setSourceBounds(getViewBounds(v));
2549 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 }
2551
Adam Cohen61f560d2013-09-30 15:58:20 -07002552 public View.OnTouchListener getHapticFeedbackTouchListener() {
2553 if (mHapticFeedbackTouchListener == null) {
2554 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002555 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002556 @Override
2557 public boolean onTouch(View v, MotionEvent event) {
2558 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2559 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2560 }
2561 return false;
2562 }
2563 };
2564 }
2565 return mHapticFeedbackTouchListener;
2566 }
2567
Tony Wickhame2217252016-03-22 16:34:23 -07002568 @Override
2569 public void onAccessibilityStateChanged(boolean enabled) {
2570 mDragLayer.onAccessibilityStateChanged(enabled);
2571 }
2572
Sunny Goyal06e21a22016-08-11 16:02:02 -07002573 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002574 if (isOnCustomContent()) {
2575 // Custom content screen doesn't participate in drag and drop. If on custom
2576 // content screen, move to default.
2577 moveWorkspaceToDefaultScreen();
2578 }
Adam Cohen9211d422014-10-07 18:14:53 -07002579 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002580
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002581 /**
2582 * Called when the user stops interacting with the launcher.
2583 * This implies that the user is now on the homescreen and is not doing housekeeping.
2584 */
Adam Cohen9211d422014-10-07 18:14:53 -07002585 protected void onInteractionEnd() {
2586 if (mLauncherCallbacks != null) {
2587 mLauncherCallbacks.onInteractionEnd();
2588 }
2589 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002590
2591 /**
2592 * Called when the user starts interacting with the launcher.
2593 * The possible interactions are:
2594 * - open all apps
2595 * - reorder an app shortcut, or a widget
2596 * - open the overview mode.
2597 * This is a good time to stop doing things that only make sense
2598 * when the user is on the homescreen and not doing housekeeping.
2599 */
Adam Cohen9211d422014-10-07 18:14:53 -07002600 protected void onInteractionBegin() {
2601 if (mLauncherCallbacks != null) {
2602 mLauncherCallbacks.onInteractionBegin();
2603 }
2604 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002605
Winson Chungcd99cd32015-04-29 11:03:24 -07002606 /** Updates the interaction state. */
2607 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002608 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002609 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002610 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2611 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002612 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002613 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002614 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002615 onInteractionEnd();
2616 }
2617 }
2618
Sunny Goyala7ce1662016-05-31 15:01:35 -07002619 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002621 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2622 try {
2623 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2624 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2625 // is enabled by default on NYC.
2626 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2627 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002628
2629 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2630 String id = ((ShortcutInfo) info).getDeepShortcutId();
2631 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302632 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002633 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002634 } else {
2635 // Could be launching some bookkeeping activity
2636 startActivity(intent, optsBundle);
2637 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002638 } finally {
2639 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002641 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002642 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2643 // corresponding permission. Show the appropriate permission prompt if that
2644 // is the case.
2645 if (intent.getComponent() == null
2646 && Intent.ACTION_CALL.equals(intent.getAction())
2647 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2648 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002649
2650 setWaitingForResult(PendingRequestArgs
2651 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002652 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2653 REQUEST_PERMISSION_CALL_PHONE);
2654 } else {
2655 // No idea why this was thrown.
2656 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 }
2659 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002660
Sunny Goyalfe770c92016-09-27 14:38:58 -07002661 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002662 private Bundle getActivityLaunchOptions(View v) {
2663 if (Utilities.ATLEAST_MARSHMALLOW) {
2664 int left = 0, top = 0;
2665 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2666 if (v instanceof TextView) {
2667 // Launch from center of icon, not entire view
2668 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2669 if (icon != null) {
2670 Rect bounds = icon.getBounds();
2671 left = (width - bounds.width()) / 2;
2672 top = v.getPaddingTop();
2673 width = bounds.width();
2674 height = bounds.height();
2675 }
2676 }
2677 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2678 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2679 // On L devices, we use the device default slide-up transition.
2680 // On L MR1 devices, we use a custom version of the slide-up transition which
2681 // doesn't have the delay present in the device default.
2682 return ActivityOptions.makeCustomAnimation(
2683 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2684 }
2685 return null;
2686 }
2687
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002688 private Rect getViewBounds(View v) {
2689 int[] pos = new int[2];
2690 v.getLocationOnScreen(pos);
2691 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2692 }
2693
Sunny Goyala7ce1662016-05-31 15:01:35 -07002694 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002695 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2696 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2697 return false;
2698 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002699 // Only launch using the new animation if the shortcut has not opted out (this is a
2700 // private contract between launcher and may be ignored in the future).
2701 boolean useLaunchAnimation = (v != null) &&
2702 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2703 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2704
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002705 UserHandle user = null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002706 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2707 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2708 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002709 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002710
2711 // Prepare intent
2712 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2713 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002714 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002715 }
2716 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002717 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2718 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002719 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002720 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002721 // Shortcuts need some special checks due to legacy reasons.
2722 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002723 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002724 // Could be launching some bookkeeping activity
2725 startActivity(intent, optsBundle);
2726 } else {
2727 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2728 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2729 }
2730 return true;
2731 } catch (ActivityNotFoundException|SecurityException e) {
2732 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2733 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2734 }
2735 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002736 }
2737
Tony Wickhamdadb3042016-02-24 11:07:00 -08002738 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002739 public boolean dispatchTouchEvent(MotionEvent ev) {
2740 mLastDispatchTouchEventX = ev.getX();
2741 return super.dispatchTouchEvent(ev);
2742 }
2743
2744 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002746 if (!isDraggingEnabled()) return false;
2747 if (isWorkspaceLocked()) return false;
2748 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002750 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2751 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002752 onLongClickAllAppsButton(v);
2753 return true;
2754 }
2755
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002756
Jon Miranda51f037d2016-11-07 08:37:20 -08002757 boolean ignoreLongPressToOverview =
2758 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002759
Adam Cohen1697b792013-09-17 19:08:21 -07002760 if (v instanceof Workspace) {
2761 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002762 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302763 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2764 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002765 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002766 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002767 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2768 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2769 return true;
2770 } else {
2771 return false;
2772 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002773 } else {
2774 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002775 }
Adam Cohen1697b792013-09-17 19:08:21 -07002776 }
2777
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002778 CellLayout.CellInfo longClickCellInfo = null;
2779 View itemUnderLongClick = null;
2780 if (v.getTag() instanceof ItemInfo) {
2781 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002782 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002783 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002784 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 }
2786
Winson Chung3d503fb2011-07-13 17:25:49 -07002787 // The hotseat touch handling does not go through Workspace, and we always allow long press
2788 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002789 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002790 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002791 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002792 if (mWorkspace.isInOverviewMode()) {
2793 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302794 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2795 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002796 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002797 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002798 return false;
2799 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302800 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2801 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002802 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002803 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002804 }
Jon Miranda379198e2016-09-23 08:54:40 -07002805 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2806 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002808 final boolean isAllAppsButton =
2809 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2810 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2811 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002812 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002813 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002814 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815 }
2816 }
2817 }
2818 return true;
2819 }
2820
Winson Chung3d503fb2011-07-13 17:25:49 -07002821 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002822 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002823 return mHotseat != null && layout != null &&
2824 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2825 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002826
Winson Chung3d503fb2011-07-13 17:25:49 -07002827 /**
2828 * Returns the CellLayout of the specified container at the specified screen.
2829 */
Sunny Goyal92820592015-03-02 11:31:35 -08002830 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002831 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2832 if (mHotseat != null) {
2833 return mHotseat.getLayout();
2834 } else {
2835 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002836 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002837 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002838 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002839 }
2840 }
2841
Winson Chungb745afb2015-03-02 11:51:23 -08002842 /**
2843 * For overridden classes.
2844 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002845 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002846 return isAppsViewVisible();
2847 }
2848
2849 public boolean isAppsViewVisible() {
2850 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2851 }
2852
2853 public boolean isWidgetsViewVisible() {
2854 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002855 }
2856
Michael Jurkae326f182011-11-21 14:05:46 -08002857 @Override
2858 public void onTrimMemory(int level) {
2859 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002860 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2861 // The widget preview db can result in holding onto over
2862 // 3MB of memory for caching which isn't necessary.
2863 SQLiteDatabase.releaseMemory();
2864
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002865 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002866 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002867 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002868 if (mLauncherCallbacks != null) {
2869 mLauncherCallbacks.onTrimMemory(level);
2870 }
Michael Jurkae326f182011-11-21 14:05:46 -08002871 }
2872
Winson5c6bdbb2015-09-03 11:36:19 -07002873 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002874 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002875 }
2876
Winson5c6bdbb2015-09-03 11:36:19 -07002877 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002878 boolean changed = mState != State.WORKSPACE ||
2879 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002880 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002881 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002882 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002883 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002884
Michael Jurkab3e22d92011-10-31 15:58:33 -07002885 // Set focus to the AppsCustomize button
2886 if (mAllAppsButton != null) {
2887 mAllAppsButton.requestFocus();
2888 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002889 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002890
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002891 // Change the state *after* we've called all the transition code
2892 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002893
Winson Chung0f785722015-04-08 10:27:49 -07002894 if (changed) {
2895 // Send an accessibility event to announce the context change
2896 getWindow().getDecorView()
2897 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002898 }
Winson5c6bdbb2015-09-03 11:36:19 -07002899 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002900 }
2901
Winsone9f27272015-10-13 10:47:51 -07002902 /**
2903 * Shows the overview button.
2904 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002905 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002906 showOverviewMode(animated, false);
2907 }
2908
2909 /**
2910 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2911 * onto one of the overview panel buttons.
2912 */
2913 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2914 Runnable postAnimRunnable = null;
2915 if (requestButtonFocus) {
2916 postAnimRunnable = new Runnable() {
2917 @Override
2918 public void run() {
2919 // Hitting the menu button when in touch mode does not trigger touch mode to
2920 // be disabled, so if requested, force focus on one of the overview panel
2921 // buttons.
2922 mOverviewPanel.requestFocusFromTouch();
2923 }
2924 };
2925 }
Adam Cohened307df2013-10-02 09:37:31 -07002926 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002927 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002928 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002929 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002930 // If animated from long press, then don't allow any of the controller in the drag
2931 // layer to intercept any remaining touch.
2932 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002933 }
2934
Winson Chungb745afb2015-03-02 11:51:23 -08002935 /**
2936 * Shows the apps view.
2937 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002938 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002939 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002940 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002941 if (updatePredictedApps) {
2942 tryAndUpdatePredictedApps();
2943 }
Winson Chung76648c52015-07-10 14:33:23 -07002944 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002945 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002946
Winson Chungb745afb2015-03-02 11:51:23 -08002947 /**
2948 * Shows the widgets view.
2949 */
2950 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002951 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002952 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002953 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002954 }
Winson Chung76648c52015-07-10 14:33:23 -07002955 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002956
2957 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002958 @Override
2959 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002960 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002961 }
2962 });
Winson Chungb745afb2015-03-02 11:51:23 -08002963 }
2964
2965 /**
2966 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002967 *
2968 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002969 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002970 // TODO: calling method should use the return value so that when {@code false} is returned
2971 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002972 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002973 if (!(mState == State.WORKSPACE ||
2974 mState == State.APPS_SPRING_LOADED ||
2975 mState == State.WIDGETS_SPRING_LOADED ||
2976 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002977 return false;
2978 }
2979 if (toState != State.APPS && toState != State.WIDGETS) {
2980 return false;
2981 }
Winson Chungb745afb2015-03-02 11:51:23 -08002982
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002983 // This is a safe and supported transition to bypass spring_loaded mode.
2984 if (mExitSpringLoadedModeRunnable != null) {
2985 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2986 mExitSpringLoadedModeRunnable = null;
2987 }
2988
Winson Chungb745afb2015-03-02 11:51:23 -08002989 if (toState == State.APPS) {
Sunny Goyaldb364372016-10-26 19:12:47 -07002990 mStateTransitionAnimation.startAnimationToAllApps(animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002991 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002992 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002993 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002994
Michael Jurkab3e22d92011-10-31 15:58:33 -07002995 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002996 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002997 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998
2999 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003000 getWindow().getDecorView()
3001 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003002 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003003 }
3004
Winson Chungcd99cd32015-04-29 11:03:24 -07003005 /**
3006 * Updates the workspace and interaction state on state change, and return the animation to this
3007 * new state.
3008 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003009 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07003010 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003011 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003012 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003013 updateInteraction(fromState, toState);
3014 return anim;
3015 }
3016
Hyunyoung Song3f471442015-04-08 19:01:34 -07003017 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003018 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003019 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003020 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003021 }
Winson Chungb745afb2015-03-02 11:51:23 -08003022
Winson Chung006ee262015-08-03 14:40:11 -07003023 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003024 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003025 null /* onCompleteRunnable */);
Sunny Goyalf5440cb2016-12-14 15:13:00 -08003026 mState = State.WORKSPACE_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003027 }
Adam Cohen7777d962011-08-18 18:58:38 -07003028
Hyunyoung Song3f471442015-04-08 19:01:34 -07003029 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003030 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003031 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003032
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003033 if (mExitSpringLoadedModeRunnable != null) {
3034 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3035 }
3036 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003037 @Override
3038 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003039 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003040 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3041 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003042 // Before we show workspace, hide all apps again because
3043 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3044 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003045 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003046 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003047 } else {
3048 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003049 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003050 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003051 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003052 };
3053 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003054 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003055
Tony Wickhame0c33232016-02-08 11:37:04 -08003056 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003057 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3058 || mState == State.WIDGETS_SPRING_LOADED;
3059 }
3060
Sunny Goyal0f76b562016-12-13 19:37:10 -08003061 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003062 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003063 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003064 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003065 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003066 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003067 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3068 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003069 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003070 }
3071
Winson Chung4ac30062015-05-08 17:34:17 -07003072 /**
3073 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3074 * resumed.
3075 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003076 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003077 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003078 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003079 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003080 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003081 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003082 }
3083 }
3084 }
3085
Michael Jurkab3e22d92011-10-31 15:58:33 -07003086 void lockAllApps() {
3087 // TODO
3088 }
3089
3090 void unlockAllApps() {
3091 // TODO
3092 }
3093
Michael Jurkad7c28052012-04-27 15:43:36 -07003094 @Override
3095 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003096 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003097 final List<CharSequence> text = event.getText();
3098 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003099 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003100 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003101 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003102 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003103 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003104 } else if (mWorkspace != null) {
3105 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003106 } else {
3107 text.add(getString(R.string.all_apps_home_button_label));
3108 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003109 return result;
3110 }
3111
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003112 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003113 * If the activity is currently paused, signal that we need to run the passed Runnable
3114 * in onResume.
3115 *
3116 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003117 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3118 * wrong when we're not running, and if the activity comes back to what the configuration was
3119 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003120 *
3121 * Implementation of the method from LauncherModel.Callbacks.
3122 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003123 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003124 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003125 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003126 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003127 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003128 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003129 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003130 }
3131 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003132 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003133 return true;
3134 } else {
3135 return false;
3136 }
3137 }
3138
Michael Jurkac402cd92013-05-20 15:49:32 +02003139 private boolean waitUntilResume(Runnable run) {
3140 return waitUntilResume(run, false);
3141 }
3142
Michael Jurka1e2f4652013-07-08 18:03:46 -07003143 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003144 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003145 }
3146
Michael Jurka7607c2f2013-04-03 14:33:19 -07003147 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003148 * If the activity is currently paused, signal that we need to re-run the loader
3149 * in onResume.
3150 *
3151 * This needs to be called from incoming places where resources might have been loaded
3152 * while we are paused. That is becaues the Configuration might be wrong
3153 * when we're not running, and if it comes back to what it was when we
3154 * were paused, we are not restarted.
3155 *
3156 * Implementation of the method from LauncherModel.Callbacks.
3157 *
3158 * @return true if we are currently paused. The caller might be able to
3159 * skip some work in that case since we will come back again.
3160 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003161 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003162 public boolean setLoadOnResume() {
3163 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003164 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003165 mOnResumeNeedsLoad = true;
3166 return true;
3167 } else {
3168 return false;
3169 }
3170 }
3171
3172 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003173 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003175 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003177 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003178 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003179 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003180 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003183
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003185 * Clear any pending bind callbacks. This is called when is loader is planning to
3186 * perform a full rebind from scratch.
3187 */
3188 @Override
3189 public void clearPendingBinds() {
3190 mBindOnResumeCallbacks.clear();
3191 if (mPendingExecutor != null) {
3192 mPendingExecutor.markCompleted();
3193 mPendingExecutor = null;
3194 }
3195 }
3196
3197 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003198 * Refreshes the shortcuts shown on the workspace.
3199 *
3200 * Implementation of the method from LauncherModel.Callbacks.
3201 */
3202 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003203 if (LauncherAppState.PROFILE_STARTUP) {
3204 Trace.beginSection("Starting page bind");
3205 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003206 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003207
Winson Chungd64d1762013-08-20 14:37:16 -07003208 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003209 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003210 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003211
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 if (mHotseat != null) {
3213 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003215 if (LauncherAppState.PROFILE_STARTUP) {
3216 Trace.endSection();
3217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 }
3219
Adam Cohendcd297f2013-06-18 13:13:40 -07003220 @Override
3221 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003222 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003223 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3224 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003225 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3226 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3227 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003228 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3229 // If there are no screens, we need to have an empty screen
3230 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003231 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003232 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003233
3234 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003235 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003236 if (hasCustomContentToLeft()) {
3237 mWorkspace.createCustomContentContainer();
3238 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003239 }
Winsonc7d2e832016-07-28 12:24:55 -07003240
3241 // After we have added all the screens, if the wallpaper was locked to the default state,
3242 // then notify to indicate that it can be released and a proper wallpaper offset can be
3243 // computed before the next layout
3244 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003245 }
3246
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003247 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003248 int count = orderedScreenIds.size();
3249 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003250 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003251 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003252 // No need to bind the first screen, as its always bound.
3253 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3254 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003255 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003256 }
3257
Winson Chungd64d1762013-08-20 14:37:16 -07003258 public void bindAppsAdded(final ArrayList<Long> newScreens,
3259 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003260 final ArrayList<ItemInfo> addAnimated,
3261 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003262 Runnable r = new Runnable() {
3263 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003264 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003265 }
3266 };
3267 if (waitUntilResume(r)) {
3268 return;
3269 }
3270
Winson Chungd64d1762013-08-20 14:37:16 -07003271 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003272 if (newScreens != null) {
3273 bindAddScreens(newScreens);
3274 }
Winson Chungd64d1762013-08-20 14:37:16 -07003275
3276 // We add the items without animation on non-visible pages, and with
3277 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003278 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003279 bindItems(addNotAnimated, 0,
3280 addNotAnimated.size(), false);
3281 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003282 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003283 bindItems(addAnimated, 0,
3284 addAnimated.size(), true);
3285 }
Winson Chungc58497e2013-09-03 17:48:37 -07003286
Winson Chung87412982013-10-03 18:34:14 -07003287 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003288 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003289
Winson Chungb745afb2015-03-02 11:51:23 -08003290 if (addedApps != null && mAppsView != null) {
3291 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003292 }
Winson Chungd64d1762013-08-20 14:37:16 -07003293 }
3294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003295 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 * Bind the items start-end from the list.
3297 *
3298 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003299 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003300 @Override
Sunny Goyal3be633b2016-12-08 09:59:25 -08003301 public void bindItems(final ArrayList<ItemInfo> items, final int start, final int end,
Winson Chung64359a52013-07-08 17:17:08 -07003302 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003303 Runnable r = new Runnable() {
3304 public void run() {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003305 bindItems(items, start, end, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003306 }
3307 };
3308 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003309 return;
3310 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003311
Sunny Goyal3be633b2016-12-08 09:59:25 -08003312 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003313 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3314 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003315 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003316 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003317 long newItemsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003318 for (int i = start; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003319 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003320
3321 // Short circuit if we are loading dock items for a configuration which has no dock
3322 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3323 mHotseat == null) {
3324 continue;
3325 }
3326
Sunny Goyal639e9062015-08-19 19:17:06 -07003327 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 switch (item.itemType) {
3329 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3330 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003331 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003332 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003333 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003334 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003335 }
3336 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003337 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003338 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003339 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003340 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003341 }
3342 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
3343 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
3344 if (mIsSafeModeEnabled) {
3345 view = new PendingAppWidgetHostView(this, info, mIconCache, true);
3346 } else {
3347 LauncherAppWidgetProviderInfo providerInfo =
3348 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
3349 if (providerInfo == null) {
3350 deleteWidgetInfo(info);
3351 continue;
3352 }
3353 view = mAppWidgetHost.createView(this, info.appWidgetId, providerInfo);
3354 }
3355 prepareAppWidget((AppWidgetHostView) view, info);
3356 break;
3357 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003358 default:
3359 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003361
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003362 /*
3363 * Remove colliding items.
3364 */
3365 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3366 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3367 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3368 View v = cl.getChildAt(item.cellX, item.cellY);
3369 Object tag = v.getTag();
3370 String desc = "Collision while binding workspace item: " + item
3371 + ". Collides with " + tag;
3372 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3373 throw (new RuntimeException(desc));
3374 } else {
3375 Log.d(TAG, desc);
3376 LauncherModel.deleteItemFromDatabase(this, item);
3377 continue;
3378 }
3379 }
3380 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303381 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003382 if (animateIcons) {
3383 // Animate all the applications up now
3384 view.setAlpha(0f);
3385 view.setScaleX(0f);
3386 view.setScaleY(0f);
3387 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003388 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003390 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003391
Winson Chung997a9232013-07-24 15:33:46 -07003392 if (animateIcons) {
3393 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003394 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003395 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003396 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003397 final Runnable startBounceAnimRunnable = new Runnable() {
3398 public void run() {
3399 anim.playTogether(bounceAnims);
3400 anim.start();
3401 }
3402 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003403 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003404 // We post the animation slightly delayed to prevent slowdowns
3405 // when we are loading right after we return to launcher.
3406 mWorkspace.postDelayed(new Runnable() {
3407 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003408 if (mWorkspace != null) {
3409 mWorkspace.snapToPage(newScreenIndex);
3410 mWorkspace.postDelayed(startBounceAnimRunnable,
3411 NEW_APPS_ANIMATION_DELAY);
3412 }
Winson Chung94d67682013-09-25 16:29:40 -07003413 }
3414 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003415 } else {
3416 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003417 }
3418 }
Winson Chung64359a52013-07-08 17:17:08 -07003419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003421 }
3422
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 /**
3424 * Add the views for a widget to the workspace.
3425 *
3426 * Implementation of the method from LauncherModel.Callbacks.
3427 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003428 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003429 Runnable r = new Runnable() {
3430 public void run() {
3431 bindAppWidget(item);
3432 }
3433 };
3434 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003435 return;
3436 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003437
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003438 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303439 PendingAppWidgetHostView view =
3440 new PendingAppWidgetHostView(this, item, mIconCache, true);
3441 prepareAppWidget(view, item);
3442 mWorkspace.addInScreen(view, item);
3443 mWorkspace.requestLayout();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003444 return;
3445 }
3446
Daniel Sandler843e8602010-06-07 14:59:01 -04003447 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3448 if (DEBUG_WIDGETS) {
3449 Log.d(TAG, "bindAppWidget: " + item);
3450 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003451
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003452 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003453
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003454 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3455 // If the provider is not ready, bind as a pending widget.
3456 appWidgetInfo = null;
3457 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3458 // The widget id is not valid. Try to find the widget based on the provider info.
3459 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3460 } else {
3461 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3462 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003463
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003464 // If the provider is ready, but the width is not yet restored, try to restore it.
3465 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3466 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003467 if (appWidgetInfo == null) {
3468 if (DEBUG_WIDGETS) {
3469 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3470 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003471 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003472 }
3473 LauncherModel.deleteItemFromDatabase(this, item);
3474 return;
3475 }
Sunny Goyalff572272014-07-23 13:58:07 -07003476
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003477 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003478 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003479 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3480 // Id has not been allocated yet. Allocate a new id.
3481 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3482 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003483
Sunny Goyald478c832016-04-01 12:04:16 -07003484 // Also try to bind the widget. If the bind fails, the user will be shown
3485 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003486 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003487 pendingInfo.spanX = item.spanX;
3488 pendingInfo.spanY = item.spanY;
3489 pendingInfo.minSpanX = item.minSpanX;
3490 pendingInfo.minSpanY = item.minSpanY;
3491 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003492
3493 boolean isDirectConfig =
3494 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3495 if (isDirectConfig && item.bindOptions != null) {
3496 Bundle newOptions = item.bindOptions.getExtras();
3497 if (options != null) {
3498 newOptions.putAll(options);
3499 }
3500 options = newOptions;
3501 }
Sunny Goyald478c832016-04-01 12:04:16 -07003502 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3503 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003504
Sunny Goyal86df1382016-08-10 15:03:22 -07003505 // We tried to bind once. If we were not able to bind, we would need to
3506 // go through the permission dialog, which means we cannot skip the config
3507 // activity.
3508 item.bindOptions = null;
3509 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3510
Sunny Goyald478c832016-04-01 12:04:16 -07003511 // Bind succeeded
3512 if (success) {
3513 // If the widget has a configure activity, it is still needs to set it up,
3514 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003515 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003516 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3517 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003518 }
Sunny Goyald478c832016-04-01 12:04:16 -07003519
3520 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003521 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003522 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003523 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003524 // The widget was marked as UI not ready, but there is no configure activity to
3525 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003526 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3527 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003528 }
Sunny Goyalff572272014-07-23 13:58:07 -07003529 }
3530
Sunny Goyald5462aa2016-11-22 16:52:39 +05303531 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003532 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003533 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003534 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3535 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003536 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003537
Sunny Goyal56c73602015-09-25 12:55:01 -07003538 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003539 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003540 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003541 deleteWidgetInfo(item);
3542 return;
3543 }
3544
Sunny Goyal233ee962015-08-03 13:05:01 -07003545 item.minSpanX = appWidgetInfo.minSpanX;
3546 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303547 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003548 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303549 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003550 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303551 prepareAppWidget(view, item);
3552 mWorkspace.addInScreen(view, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003553 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554
Daniel Sandler843e8602010-06-07 14:59:01 -04003555 if (DEBUG_WIDGETS) {
3556 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3557 + (SystemClock.uptimeMillis()-start) + "ms");
3558 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 }
3560
Sunny Goyalff572272014-07-23 13:58:07 -07003561 /**
3562 * Restores a pending widget.
3563 *
3564 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003565 */
Sunny Goyald478c832016-04-01 12:04:16 -07003566 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003567 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3568 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3569 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003570 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003571 }
3572
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003573 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003574 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003575
3576 mWorkspace.reinflateWidgetsIfNecessary();
3577 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003578 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003579 }
3580
Adam Cohen1462de32012-07-24 22:34:36 -07003581 public void onPageBoundSynchronously(int page) {
3582 mSynchronouslyBoundPages.add(page);
3583 }
3584
Sunny Goyal527c7d32015-08-28 15:19:36 -07003585 @Override
3586 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3587 if (mPendingExecutor != null) {
3588 mPendingExecutor.markCompleted();
3589 }
3590 mPendingExecutor = executor;
3591 executor.attachTo(this);
3592 }
3593
3594 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3595 if (mPendingExecutor == executor) {
3596 mPendingExecutor = null;
3597 }
3598 }
3599
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003600 @Override
3601 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3602 Runnable r = new Runnable() {
3603 public void run() {
3604 finishFirstPageBind(executor);
3605 }
3606 };
3607 if (waitUntilResume(r)) {
3608 return;
3609 }
3610
3611 Runnable onComplete = new Runnable() {
3612 @Override
3613 public void run() {
3614 if (executor != null) {
3615 executor.onLoadAnimationCompleted();
3616 }
3617 }
3618 };
3619 if (mDragLayer.getAlpha() < 1) {
3620 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3621 } else {
3622 onComplete.run();
3623 }
3624 }
3625
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 /**
3627 * Callback saying that there aren't any more items to bind.
3628 *
3629 * Implementation of the method from LauncherModel.Callbacks.
3630 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003631 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003632 Runnable r = new Runnable() {
3633 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003634 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003635 }
3636 };
3637 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003638 return;
3639 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003640 if (LauncherAppState.PROFILE_STARTUP) {
3641 Trace.beginSection("Page bind completed");
3642 }
Adam Cohen1462de32012-07-24 22:34:36 -07003643 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003645 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003646
Sunny Goyal2100c782016-08-22 16:00:03 -07003647 if (mPendingActivityResult != null) {
3648 handleActivityResult(mPendingActivityResult.requestCode,
3649 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3650 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003651 }
3652
Sunny Goyal756adbc2015-04-16 15:20:51 -07003653 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003654
Tony Wickham010d2552017-01-20 08:15:28 -08003655 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3656
Adam Cohen9211d422014-10-07 18:14:53 -07003657 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003658 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003659 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003660 if (LauncherAppState.PROFILE_STARTUP) {
3661 Trace.endSection();
3662 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003663 }
3664
Winson Chunga2413752012-04-03 14:22:34 -07003665 private boolean canRunNewAppsAnimation() {
3666 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003667 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003668 }
3669
Winson Chungc9168342013-06-26 14:54:55 -07003670 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003671 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003672 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3673 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003674 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003675 return bounceAnim;
3676 }
3677
Adam Cohen27772732013-11-11 14:00:56 +00003678 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003679 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003680 }
3681
Tony Wickham55616cd2015-09-23 14:55:17 -07003682 public int getSearchBarHeight() {
3683 if (mLauncherCallbacks != null) {
3684 return mLauncherCallbacks.getSearchBarHeight();
3685 }
3686 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3687 }
3688
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003689 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003690 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3691 * multiple calls to bind the same list.)
3692 */
3693 @Thunk ArrayList<AppInfo> mTmpAppsList;
3694 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3695 public void run() {
3696 bindAllApplications(mTmpAppsList);
3697 mTmpAppsList = null;
3698 }
3699 };
3700
3701 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 * Add the icons for all apps.
3703 *
3704 * Implementation of the method from LauncherModel.Callbacks.
3705 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003706 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003707 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3708 mTmpAppsList = apps;
3709 return;
3710 }
3711
Winson Chungb745afb2015-03-02 11:51:23 -08003712 if (mAppsView != null) {
3713 mAppsView.setApps(apps);
3714 }
Adam Cohen9211d422014-10-07 18:14:53 -07003715 if (mLauncherCallbacks != null) {
3716 mLauncherCallbacks.bindAllApplications(apps);
3717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 }
3719
3720 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003721 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3722 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3723 */
3724 @Override
3725 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003726 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003727 }
3728
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003729 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 * A package was updated.
3731 *
3732 * Implementation of the method from LauncherModel.Callbacks.
3733 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003734 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003735 Runnable r = new Runnable() {
3736 public void run() {
3737 bindAppsUpdated(apps);
3738 }
3739 };
3740 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003741 return;
3742 }
3743
Winson Chungb745afb2015-03-02 11:51:23 -08003744 if (mAppsView != null) {
3745 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747 }
3748
Sunny Goyal4390ace2014-10-13 11:33:11 -07003749 @Override
3750 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3751 Runnable r = new Runnable() {
3752 public void run() {
3753 bindWidgetsRestored(widgets);
3754 }
3755 };
3756 if (waitUntilResume(r)) {
3757 return;
3758 }
3759 mWorkspace.widgetsRestored(widgets);
3760 }
3761
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003763 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003764 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003765 *
3766 * @param updated list of shortcuts which have changed.
3767 * @param removed list of shortcuts which were deleted in the background. This can happen when
3768 * an app gets removed from the system or some of its components are no longer
3769 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003770 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003771 @Override
3772 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003773 final ArrayList<ShortcutInfo> removed, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003774 Runnable r = new Runnable() {
3775 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003776 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003777 }
3778 };
3779 if (waitUntilResume(r)) {
3780 return;
3781 }
3782
Sunny Goyal4390ace2014-10-13 11:33:11 -07003783 if (!updated.isEmpty()) {
3784 mWorkspace.updateShortcuts(updated);
3785 }
3786
3787 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003788 HashSet<ComponentName> removedComponents = new HashSet<>();
3789 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3790
Sunny Goyal4390ace2014-10-13 11:33:11 -07003791 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003792 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08003793 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003794 } else {
3795 removedComponents.add(si.getTargetComponent());
3796 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003797 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003798
3799 if (!removedComponents.isEmpty()) {
3800 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3801 mWorkspace.removeItemsByMatcher(matcher);
3802 mDragController.onAppsRemoved(matcher);
3803 }
3804
3805 if (!removedDeepShortcuts.isEmpty()) {
3806 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3807 mWorkspace.removeItemsByMatcher(matcher);
3808 mDragController.onAppsRemoved(matcher);
3809 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003810 }
3811 }
3812
3813 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003814 * Update the state of a package, typically related to install state.
3815 *
3816 * Implementation of the method from LauncherModel.Callbacks.
3817 */
Sunny Goyale755d462014-07-22 13:48:29 -07003818 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003819 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3820 Runnable r = new Runnable() {
3821 public void run() {
3822 bindRestoreItemsChange(updates);
3823 }
3824 };
3825 if (waitUntilResume(r)) {
3826 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003827 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003828
Sunny Goyal756adbc2015-04-16 15:20:51 -07003829 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003830 }
3831
3832 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003833 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003834 * in addition to specific applications to remove, the reason being that
3835 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003836 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003837 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003839 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003840 public void bindWorkspaceComponentsRemoved(
3841 final HashSet<String> packageNames, final HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003842 final UserHandle user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003843 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003844 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003845 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003846 }
Winson Chungd64d1762013-08-20 14:37:16 -07003847 };
3848 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003849 return;
3850 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003851 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003852 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3853 mWorkspace.removeItemsByMatcher(matcher);
3854 mDragController.onAppsRemoved(matcher);
3855
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003856 }
3857 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003858 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3859 mWorkspace.removeItemsByMatcher(matcher);
3860 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003861 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003862 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003863
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003864 @Override
3865 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3866 Runnable r = new Runnable() {
3867 public void run() {
3868 bindAppInfosRemoved(appInfos);
3869 }
3870 };
3871 if (waitUntilResume(r)) {
3872 return;
Joe Onorato36115782010-06-17 13:28:48 -04003873 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003874
Winson Chungdf95eb12013-10-16 14:57:07 -07003875 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003876 if (mAppsView != null) {
3877 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 }
Joe Onoratobe386092009-11-17 17:32:16 -08003880
Sunny Goyald164b7f2016-10-12 20:49:31 -07003881 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003882 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003883 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003884 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003885 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003886
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003887 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003888 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3889 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3890 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003891 return;
3892 }
3893
Sunny Goyald164b7f2016-10-12 20:49:31 -07003894 if (mWidgetsView != null && allWidgets != null) {
3895 mWidgetsView.setWidgets(allWidgets);
3896 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003897 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003898 }
3899
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003900 @Override
3901 public void notifyWidgetProvidersChanged() {
3902 if (mWorkspace.getState().shouldUpdateWidget) {
3903 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3904 }
3905 }
3906
Winson Chung4b919f82012-05-01 10:44:08 -07003907 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003908 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003909 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003910 }
3911 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003912
Winson Chung4b919f82012-05-01 10:44:08 -07003913 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003914 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003915 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003916 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003917 } else {
3918 mHandler.postDelayed(new Runnable() {
3919 public void run() {
3920 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3921 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003922 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003923 }
Winson Chung4b919f82012-05-01 10:44:08 -07003924 }
Winson Chung400438b2011-01-16 17:53:48 -08003925 }
3926
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003927 private void markAppsViewShown() {
3928 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3929 return;
3930 }
3931 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3932 }
3933
3934 private boolean shouldShowDiscoveryBounce() {
3935 if (mState != mState.WORKSPACE) {
3936 return false;
3937 }
3938 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3939 return true;
3940 }
3941 if (!mIsResumeFromActionScreenOff) {
3942 return false;
3943 }
3944 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3945 return false;
3946 }
3947 return true;
3948 }
3949
Winson Chung5ffd51d2015-06-25 11:36:50 -07003950 protected void moveWorkspaceToDefaultScreen() {
3951 mWorkspace.moveToDefaultScreen(false);
3952 }
3953
Winson Chung80baf5a2010-08-09 16:03:15 -07003954 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003955 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003956 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003957 @Override
3958 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3959 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003960
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003961 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3962 writer.println(prefix + "Workspace Items");
3963 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
3964 writer.println(prefix + " Homescreen " + i);
3965
3966 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3967 for (int j = 0; j < layout.getChildCount(); j++) {
3968 Object tag = layout.getChildAt(j).getTag();
3969 if (tag != null) {
3970 writer.println(prefix + " " + tag.toString());
3971 }
3972 }
3973 }
3974
3975 writer.println(prefix + " Hotseat");
3976 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003977 for (int j = 0; j < layout.getChildCount(); j++) {
3978 Object tag = layout.getChildAt(j).getTag();
3979 if (tag != null) {
3980 writer.println(prefix + " " + tag.toString());
3981 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003982 }
Sunny Goyala1365452015-10-01 15:46:24 -07003983
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003984 try {
3985 FileLog.flushAll(writer);
3986 } catch (Exception e) {
3987 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003988 }
3989 }
3990
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003991 writer.println(prefix + "Misc:");
3992 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3993 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3994 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3995
3996 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003997
Adam Cohen9211d422014-10-07 18:14:53 -07003998 if (mLauncherCallbacks != null) {
3999 mLauncherCallbacks.dump(prefix, fd, writer, args);
4000 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004001 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004002
Sunny Goyal66b24572016-09-21 15:57:55 -07004003 @Override
4004 @TargetApi(Build.VERSION_CODES.N)
4005 public void onProvideKeyboardShortcuts(
4006 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4007
4008 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4009 if (mState == State.WORKSPACE) {
4010 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4011 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4012 }
4013 View currentFocus = getCurrentFocus();
4014 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4015 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4016 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4017 }
4018 if (currentFocus instanceof BubbleTextView &&
4019 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4020 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4021 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4022 }
4023 if (!shortcutInfos.isEmpty()) {
4024 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4025 }
4026
4027 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4028 }
4029
4030 @Override
4031 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4032 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4033 switch (keyCode) {
4034 case KeyEvent.KEYCODE_A:
4035 if (mState == State.WORKSPACE) {
4036 showAppsView(true, true, false);
4037 return true;
4038 }
4039 break;
4040 case KeyEvent.KEYCODE_S: {
4041 View focusedView = getCurrentFocus();
4042 if (focusedView instanceof BubbleTextView
4043 && focusedView.getTag() instanceof ItemInfo
4044 && mAccessibilityDelegate.performAction(focusedView,
4045 (ItemInfo) focusedView.getTag(),
4046 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08004047 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004048 return true;
4049 }
4050 break;
4051 }
4052 case KeyEvent.KEYCODE_O:
4053 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4054 return true;
4055 }
4056 break;
4057 }
4058 }
4059 return super.onKeyShortcut(keyCode, event);
4060 }
4061
Adam Cohen59400422014-03-05 18:07:04 -08004062 public static CustomAppWidget getCustomAppWidget(String name) {
4063 return sCustomAppWidgets.get(name);
4064 }
4065
4066 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4067 return sCustomAppWidgets;
4068 }
4069
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004070 public static Launcher getLauncher(Context context) {
4071 if (context instanceof Launcher) {
4072 return (Launcher) context;
4073 }
4074 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4075 }
4076
Sunny Goyal745bad92016-05-02 10:54:12 -07004077 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4078
4079 @Override
4080 public void onSharedPreferenceChanged(
4081 SharedPreferences sharedPreferences, String key) {
4082 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4083 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4084 if (!waitUntilResume(this, true)) {
4085 run();
4086 }
4087 }
4088 }
4089
4090 @Override
4091 public void run() {
4092 setOrientation();
4093 }
4094 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004095}