blob: 74e6bfc84d4aed2cd5348eb8c4c849c192338655 [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;
Sunny Goyalffe83f12014-08-14 17:39:34 -070088import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010089import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal782f0c92017-01-19 10:27:54 -080090import com.android.launcher3.compat.PinItemRequestCompat;
Kenny Guyed131872014-04-30 03:02:21 +010091import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080092import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070093import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -070094import com.android.launcher3.dragndrop.DragController;
95import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070096import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070097import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -080098import com.android.launcher3.dragndrop.PinItemDragListener;
Tony Wickham827cef22016-03-17 15:39:39 -070099import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000100import com.android.launcher3.folder.Folder;
101import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700102import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700103import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700104import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700105import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800106import com.android.launcher3.model.ModelWriter;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700107import com.android.launcher3.model.PackageItemInfo;
108import com.android.launcher3.model.WidgetItem;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.notification.NotificationListener;
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;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700113import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700114import com.android.launcher3.shortcuts.ShortcutKey;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800115import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530116import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
117import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
118import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700119import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700121import com.android.launcher3.util.ItemInfoMatcher;
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;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800257 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800258 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700259 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700260 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700261 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700262 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700263 private boolean mHasFocus = false;
264 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Tony Wickham010d2552017-01-20 08:15:28 -0800266 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700267
Adam Cohen61f560d2013-09-30 15:58:20 -0700268 private View.OnTouchListener mHapticFeedbackTouchListener;
269
Winson Chung400438b2011-01-16 17:53:48 -0800270 // Determines how long to wait after a rotation before restoring the screen orientation to
271 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700272 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800273
Adam Cohen1462de32012-07-24 22:34:36 -0700274 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
275
Winson Chung46353de2012-02-16 14:05:10 -0800276 // We only want to get the SharedPreferences once since it does an FS stat each time we get
277 // it from the context.
278 private SharedPreferences mSharedPrefs;
279
Adam Cohen4b66bf32015-09-18 12:15:19 -0700280 private boolean mMoveToDefaultScreenFromNewIntent;
281
Winson Chung13eb5272015-05-11 16:30:13 -0700282 // This is set to the view that launched the activity that navigated the user away from
283 // launcher. Since there is no callback for when the activity has finished launching, enable
284 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800285 private BubbleTextView mWaitingForResume;
286
Adam Cohen59400422014-03-05 18:07:04 -0800287 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
288 new HashMap<String, CustomAppWidget>();
289
Adam Cohen59400422014-03-05 18:07:04 -0800290 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700291 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
292 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800293 }
294 }
295
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700296 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
297 // state transition. If another state transition happened during this delay,
298 // simply unregister this runnable.
299 private Runnable mExitSpringLoadedModeRunnable;
300
Adam Cohen091440a2015-03-18 14:16:05 -0700301 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700302 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700303 if (mWorkspace != null) {
304 mWorkspace.buildPageHardwareLayers();
305 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700306 }
307 };
308
Sunny Goyal2100c782016-08-22 16:00:03 -0700309 // Activity result which needs to be processed after workspace has loaded.
310 private ActivityResultInfo mPendingActivityResult;
311 /**
312 * Holds extra information required to handle a result from an external call, like
313 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
314 */
315 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800316
Jon Miranda379198e2016-09-23 08:54:40 -0700317 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700318
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700319 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700320 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400321
322 @Thunk void setOrientation() {
323 if (mRotationEnabled) {
324 unlockScreenOrientation(true);
325 } else {
326 setRequestedOrientation(
327 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700328 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400329 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700330
Sunny Goyal745bad92016-05-02 10:54:12 -0700331 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 @Override
334 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700335 if (DEBUG_STRICT_MODE) {
336 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
337 .detectDiskReads()
338 .detectDiskWrites()
339 .detectNetwork() // or .detectAll() for all detectable problems
340 .penaltyLog()
341 .build());
342 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
343 .detectLeakedSqlLiteObjects()
344 .detectLeakedClosableObjects()
345 .penaltyLog()
346 .penaltyDeath()
347 .build());
348 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700349 if (LauncherAppState.PROFILE_STARTUP) {
350 Trace.beginSection("Launcher-onCreate");
351 }
Winson Chunga2413752012-04-03 14:22:34 -0700352
Adam Cohen9211d422014-10-07 18:14:53 -0700353 if (mLauncherCallbacks != null) {
354 mLauncherCallbacks.preOnCreate();
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400358
Sunny Goyal87f784c2017-01-11 10:48:34 -0800359 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700360
Adam Cohen2e6da152015-05-06 11:42:25 -0700361 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800362 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Sunny Goyalf5e37442016-11-02 10:31:24 -0700363 if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700364 Display display = getWindowManager().getDefaultDisplay();
365 Point mwSize = new Point();
366 display.getSize(mwSize);
367 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
368 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700369
Sunny Goyalf7258242015-10-19 16:59:07 -0700370 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700371 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800372 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800373 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800374 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 Song3f471442015-04-08 19:01:34 -0700627 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700628 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
629 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700630 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700631 }
632
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000633 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700634 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
635 // This cast is safe as long as the id < 0x00FFFFFF
636 // Since we jail all the dynamically generated views, there should be no clashes
637 // with any other views.
638 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000639 }
640
Tony Wickham010d2552017-01-20 08:15:28 -0800641 public PopupDataProvider getPopupDataProvider() {
642 return mPopupDataProvider;
643 }
644
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000645 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700646 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
647 * a configuration step, this allows the proper animations to run after other transitions.
648 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 private long completeAdd(
650 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
651 long screenId = info.screenId;
652 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700653 // When the screen id represents an actual screen (as opposed to a rank) we make sure
654 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700656 }
Adam Cohendb364c32014-05-20 14:23:40 -0700657
Sunny Goyal2100c782016-08-22 16:00:03 -0700658 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700660 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700664 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700665 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700667 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700668 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 int widgetId = appWidgetId;
670 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700671 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700673 // Since the view was just bound, also launch the configure activity if needed
674 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
675 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800676 if (provider != null) {
677 new WidgetAddFlowHandler(provider)
678 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700679 }
680 }
681 break;
682 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700684
Adam Cohendb364c32014-05-20 14:23:40 -0700685 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800686 }
687
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800688 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700689 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 if (isWorkspaceLoading()) {
691 // process the result once the workspace has loaded.
692 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
693 return;
694 }
695 mPendingActivityResult = null;
696
Winson Chunge341d302013-08-16 14:31:00 -0700697 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 final PendingRequestArgs requestArgs = mPendingRequestArgs;
699 setWaitingForResult(null);
700 if (requestArgs == null) {
701 return;
702 }
703
704 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700705
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 Runnable exitSpringLoaded = new Runnable() {
707 @Override
708 public void run() {
709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
710 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
711 }
712 };
713
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700715 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700716 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
718 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700720 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 addAppWidgetImpl(
724 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800725 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 }
728 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200729 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
730 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700731 // User could have free-scrolled between pages before picking a wallpaper; make sure
732 // we move to the closest one now.
733 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700734 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200735 }
736 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200738
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700740 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700741
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 // We have special handling for widgets
743 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800744 final int appWidgetId;
745 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
746 : -1;
747 if (widgetId < 0) {
748 appWidgetId = pendingAddWidgetId;
749 } else {
750 appWidgetId = widgetId;
751 }
752
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800754 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700755 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
756 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700758 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700759 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 @Override
761 public void run() {
762 exitSpringLoadedDragModeDelayed(false, 0, null);
763 }
764 };
Adam Cohendb364c32014-05-20 14:23:40 -0700765
Sunny Goyal2100c782016-08-22 16:00:03 -0700766 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
767 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
768 } else {
769 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
770 // When the screen id represents an actual screen (as opposed to a rank)
771 // we make sure that the drop page actually exists.
772 requestArgs.screenId =
773 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700774 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 final CellLayout dropLayout =
776 mWorkspace.getScreenWithId(requestArgs.screenId);
777
778 dropLayout.setDropPending(true);
779 final Runnable onComplete = new Runnable() {
780 @Override
781 public void run() {
782 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
783 dropLayout.setDropPending(false);
784 }
785 };
786 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
787 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700788 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800789 return;
790 }
791
Sunny Goyald478c832016-04-01 12:04:16 -0700792 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
793 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700794 if (resultCode == RESULT_OK) {
795 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700796 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700797 }
798 // Leave the widget in the pending state if the user canceled the configure.
799 return;
800 }
801
Sunny Goyalaad90582015-07-09 14:22:50 -0700802 if (requestCode == REQUEST_CREATE_SHORTCUT) {
803 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700804 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
805 completeAdd(requestCode, data, -1, requestArgs);
806 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
808
Sunny Goyalaad90582015-07-09 14:22:50 -0700809 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
811 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800814 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800815 }
816
817 @Override
Sunny Goyaldec3a902017-01-25 18:23:36 -0800818 protected void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800819 final int requestCode, final int resultCode, final Intent data) {
820 handleActivityResult(requestCode, resultCode, data);
821 if (mLauncherCallbacks != null) {
822 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
823 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800824 }
825
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600826 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700827 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600828 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700829 PendingRequestArgs pendingArgs = mPendingRequestArgs;
830 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
831 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
832 setWaitingForResult(null);
833
Sunny Goyal28c6b962015-10-12 11:42:05 -0700834 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700835 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700836 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700837 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700838 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700839 Intent intent = pendingArgs.getPendingIntent();
840
Sunny Goyal28c6b962015-10-12 11:42:05 -0700841 if (grantResults.length > 0
842 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700843 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700844 } else {
845 // TODO: Show a snack bar with link to settings
846 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700847 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700848 }
849 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
852 grantResults);
853 }
854 }
855
Adam Cohendb364c32014-05-20 14:23:40 -0700856 /**
857 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
858 *
859 * @param screenId the screen id to check
860 * @return the new screen, or screenId if it exists
861 */
862 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800863 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700864 if (dropLayout == null) {
865 // it's possible that the add screen was removed because it was
866 // empty and a re-bind occurred
867 mWorkspace.addExtraEmptyScreen();
868 return mWorkspace.commitExtraEmptyScreen();
869 } else {
870 return screenId;
871 }
872 }
873
Sunny Goyal2100c782016-08-22 16:00:03 -0700874 @Thunk void completeTwoStageWidgetDrop(
875 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
876 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 Runnable onCompleteRunnable = null;
878 int animationType = 0;
879
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700880 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 if (resultCode == RESULT_OK) {
882 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
883 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800884 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700885 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 onCompleteRunnable = new Runnable() {
887 @Override
888 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700889 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700890 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
891 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 }
893 };
894 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800895 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700899 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700900 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
901 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700902 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903 // The animated view may be null in the case of a rotation during widget configuration
904 onCompleteRunnable.run();
905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
908 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700909 protected void onStop() {
910 super.onStop();
911 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700912
913 if (mLauncherCallbacks != null) {
914 mLauncherCallbacks.onStop();
915 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700916
Sunny Goyalf5e37442016-11-02 10:31:24 -0700917 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700918 mAppWidgetHost.stopListening();
919 }
Tony Wickham010d2552017-01-20 08:15:28 -0800920
921 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700922 }
923
924 @Override
925 protected void onStart() {
926 super.onStart();
927 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700928
929 if (mLauncherCallbacks != null) {
930 mLauncherCallbacks.onStart();
931 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700932
Sunny Goyalf5e37442016-11-02 10:31:24 -0700933 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700934 mAppWidgetHost.startListening();
935 }
Tony Wickham010d2552017-01-20 08:15:28 -0800936
937 if (!isWorkspaceLoading()) {
938 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
939 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700940 }
941
942 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200944 long startTime = 0;
945 if (DEBUG_RESUME_TIME) {
946 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400947 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200948 }
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.preOnResume();
952 }
953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700955 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700956
Winson Chung4a2afa32012-07-19 14:53:05 -0700957 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700958 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700959 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800960 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700961 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700962 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700963 // view after launching an app, as they may be depending on the UI to be static to
964 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700965 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700966 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800967 } else if (mOnResumeState == State.WIDGETS) {
968 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700969 }
970 mOnResumeState = State.NONE;
971
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800972 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700973 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700974 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700975 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700976 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700978 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200979 // We might have postponed some bind calls until onResume (see waitUntilResume) --
980 // execute them here
981 long startTimeCallbacks = 0;
982 if (DEBUG_RESUME_TIME) {
983 startTimeCallbacks = System.currentTimeMillis();
984 }
985
Michael Jurka1e2f4652013-07-08 18:03:46 -0700986 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
987 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200988 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700989 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200990 if (DEBUG_RESUME_TIME) {
991 Log.d(TAG, "Time spent processing callbacks in onResume: " +
992 (System.currentTimeMillis() - startTimeCallbacks));
993 }
Michael Jurka447bf842013-05-15 14:52:15 +0200994 }
Michael Jurka54554252013-08-01 12:52:23 +0200995 if (mOnResumeCallbacks.size() > 0) {
996 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
997 mOnResumeCallbacks.get(i).run();
998 }
999 mOnResumeCallbacks.clear();
1000 }
Winson Chunge4e50662012-01-23 14:45:13 -08001001
1002 // Reset the pressed state of icons that were locked in the press state while activities
1003 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001004 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001005 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001006 mWaitingForResume.setStayPressed(false);
1007 }
Winson Chung82963d52013-10-09 11:20:57 -07001008
Adam Cohen06dff352012-06-01 17:17:08 -07001009 // It is possible that widgets can receive updates while launcher is not in the foreground.
1010 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1011 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1012 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001013 if (!isWorkspaceLoading()) {
1014 getWorkspace().reinflateWidgetsIfNecessary();
1015 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001016
Michael Jurka447bf842013-05-15 14:52:15 +02001017 if (DEBUG_RESUME_TIME) {
1018 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1019 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001020
Adam Cohen4b66bf32015-09-18 12:15:19 -07001021 // We want to suppress callbacks about CustomContent being shown if we have just received
1022 // onNewIntent while the user was present within launcher. In that case, we post a call
1023 // to move the user to the main screen (which will occur after onResume). We don't want to
1024 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1025 // suppress here.
1026 if (mWorkspace.getCustomContentCallbacks() != null
1027 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001028 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001029 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001030 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1031 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001032 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001033 }
1034 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001035 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001036 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001037 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001038
Sunny Goyal756adbc2015-04-16 15:20:51 -07001039 if (!isWorkspaceLoading()) {
1040 // Process any items that were added while Launcher was away.
1041 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001042
1043 // Refresh shortcuts if the permission changed.
1044 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001045 }
Adam Cohen9211d422014-10-07 18:14:53 -07001046
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001047 if (shouldShowDiscoveryBounce()) {
1048 mAllAppsController.showDiscoveryBounce();
1049 }
1050 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001051 if (mLauncherCallbacks != null) {
1052 mLauncherCallbacks.onResume();
1053 }
Adam Cohen06dff352012-06-01 17:17:08 -07001054 }
1055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001058 // Ensure that items added to Launcher are queued until Launcher returns
1059 InstallShortcutReceiver.enableInstallQueue();
1060
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001062 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001063 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001064 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001065
1066 // We call onHide() aggressively. The custom content callbacks should be able to
1067 // debounce excess onHide calls.
1068 if (mWorkspace.getCustomContentCallbacks() != null) {
1069 mWorkspace.getCustomContentCallbacks().onHide();
1070 }
Romain Guycbb89e42009-06-08 15:52:54 -07001071
Adam Cohen9211d422014-10-07 18:14:53 -07001072 if (mLauncherCallbacks != null) {
1073 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001074 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001075 }
1076
1077 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001078 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1079 // by a onResume or by scrolling otherwise.
1080 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001081
1082 // Custom content is completely hidden
1083 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001084
1085 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1086 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001087
1088 // Indicates whether the user is allowed to scroll away from the custom content.
1089 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001090 }
1091
Adam Cohenc2d6e892014-10-16 09:49:24 -07001092 public interface LauncherOverlay {
1093
1094 /**
1095 * Touch interaction leading to overscroll has begun
1096 */
1097 public void onScrollInteractionBegin();
1098
1099 /**
1100 * Touch interaction related to overscroll has ended
1101 */
1102 public void onScrollInteractionEnd();
1103
1104 /**
1105 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1106 * screen (or in the case of RTL, the rightmost screen).
1107 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001108 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001109
1110 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001111 * Called when the launcher is ready to use the overlay
1112 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001113 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001114 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001115 }
1116
Jun Mukai8af0cd82015-05-12 12:32:12 -07001117 public interface LauncherSearchCallbacks {
1118 /**
1119 * Called when the search overlay is shown.
1120 */
1121 public void onSearchOverlayOpened();
1122
1123 /**
1124 * Called when the search overlay is dismissed.
1125 */
1126 public void onSearchOverlayClosed();
1127 }
1128
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001130 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 }
1132
1133 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1134
Sunny Goyalc86df472016-02-25 09:19:38 -08001135 public void onScrollChanged(float progress) {
1136 if (mWorkspace != null) {
1137 mWorkspace.onOverlayScrollChanged(progress);
1138 }
1139 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001140 }
1141
Jorim Jaggid017f882014-01-14 17:08:48 -08001142 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001143 if (mLauncherCallbacks != null) {
1144 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001145 } else {
1146 // On devices with a locked orientation, we will at least have the allow rotation
1147 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001148 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001149 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001150 }
1151
Adam Cohen9211d422014-10-07 18:14:53 -07001152 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001153 CustomContentCallbacks callbacks, String description) {
1154 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001155 }
1156
Adam Cohenedb40762013-07-18 16:45:45 -07001157 // The custom content needs to offset its content to account for the QSB
1158 public int getTopOffsetForCustomContent() {
1159 return mWorkspace.getPaddingTop();
1160 }
1161
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001162 @Override
1163 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001165 if (mModel.isCurrentCallbacks(this)) {
1166 mModel.stopLoader();
1167 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001168 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1169
Romain Guy13c2e7b2010-03-10 19:45:00 -08001170 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001171 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001172
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001173 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001174 @Override
1175 public void onWindowFocusChanged(boolean hasFocus) {
1176 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001177 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001178
1179 if (mLauncherCallbacks != null) {
1180 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1181 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001182 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 private boolean acceptFilter() {
1185 final InputMethodManager inputManager = (InputMethodManager)
1186 getSystemService(Context.INPUT_METHOD_SERVICE);
1187 return !inputManager.isFullscreenMode();
1188 }
1189
1190 @Override
1191 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001192 final int uniChar = event.getUnicodeChar();
1193 final boolean handled = super.onKeyDown(keyCode, event);
1194 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1195 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1197 keyCode, event);
1198 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001199 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001200 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001201 // showSearchDialog()
1202 // If there are multiple keystrokes before the search dialog takes focus,
1203 // onSearchRequested() will be called for every keystroke,
1204 // but it is idempotent, so it's fine.
1205 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
1207 }
1208
Joe Onorato8a9625e2010-01-28 15:55:35 -08001209 // Eat the long press event so the keyboard doesn't come up.
1210 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1211 return true;
1212 }
1213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 return handled;
1215 }
1216
Winson46163472015-09-22 17:31:56 -07001217 @Override
1218 public boolean onKeyUp(int keyCode, KeyEvent event) {
1219 if (keyCode == KeyEvent.KEYCODE_MENU) {
1220 // Ignore the menu key if we are currently dragging or are on the custom content screen
1221 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001222 // Close any open floating view
1223 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001224
Winson46163472015-09-22 17:31:56 -07001225 // Stop resizing any widgets
1226 mWorkspace.exitWidgetResizeMode();
1227
1228 // Show the overview mode if we are on the workspace
1229 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1230 !mWorkspace.isSwitchingState()) {
1231 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001232 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001233 }
1234 }
1235 return true;
1236 }
1237 return super.onKeyUp(keyCode, event);
1238 }
1239
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 private String getTypedText() {
1241 return mDefaultKeySsb.toString();
1242 }
1243
Sunny Goyal6f28e712016-09-13 14:19:29 -07001244 @Override
1245 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001246 mDefaultKeySsb.clear();
1247 mDefaultKeySsb.clearSpans();
1248 Selection.setSelection(mDefaultKeySsb, 0);
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 /**
1252 * Restores the previous state, if it exists.
1253 *
1254 * @param savedState The previous state.
1255 */
1256 private void restoreState(Bundle savedState) {
1257 if (savedState == null) {
1258 return;
1259 }
1260
Sunny Goyalfe770c92016-09-27 14:38:58 -07001261 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1262 State[] stateValues = State.values();
1263 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1264 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001265 if (state == State.APPS || state == State.WIDGETS) {
1266 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001267 }
1268
Sunny Goyal2100c782016-08-22 16:00:03 -07001269 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1270 if (requestArgs != null) {
1271 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001273
1274 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
1276
1277 /**
1278 * Finds all the views we need and configure them properly.
1279 */
1280 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001281 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001282 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001283 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001284 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1285 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001286 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001287
Sunny Goyal784f9c32016-03-23 16:56:54 -07001288 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1289 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1290 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291
Winson Chung4c98d922011-05-31 16:50:48 -07001292 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001293 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001294
Winson Chung3d503fb2011-07-13 17:25:49 -07001295 // Setup the hotseat
1296 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1297 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001298 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001299 }
1300
Winsone9f27272015-10-13 10:47:51 -07001301 // Setup the overview panel
1302 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001303
Winson Chung4c98d922011-05-31 16:50:48 -07001304 // Setup the workspace
1305 mWorkspace.setHapticFeedbackEnabled(false);
1306 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001307 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001308 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1309 // default state, otherwise we will update to the wrong offsets in RTL
1310 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001311 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001312 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001313
Tony Wickham34d2c912015-09-11 09:27:58 -07001314 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001315 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001316
Winson Chungef7f8742015-06-04 17:18:17 -07001317 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001318 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001319 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001320 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1321 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1322 } else {
1323 mAppsView.setSearchBarController(new DefaultAppSearchController());
1324 }
Craig Mautner360310b2012-10-26 15:13:08 -07001325
Winson Chung3d503fb2011-07-13 17:25:49 -07001326 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001327 mDragController.setMoveTarget(mWorkspace);
1328 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001329 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001330
Peter Schiller310a9882016-06-30 13:52:36 -07001331 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1332 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1333 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001334
Sunny Goyal322d5562015-06-25 19:35:49 -07001335 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1336 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
Winsone9f27272015-10-13 10:47:51 -07001340 private void setupOverviewPanel() {
1341 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1342
Winsone9f27272015-10-13 10:47:51 -07001343 // Bind wallpaper button actions
1344 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301345 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001346 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001347 public void handleViewClick(View view) {
1348 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001349 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001350 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001351 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1352
1353 // Bind widget button actions
1354 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301355 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001356 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001357 public void handleViewClick(View view) {
1358 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001359 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001360 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001361 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1362
1363 // Bind settings actions
1364 View settingsButton = findViewById(R.id.settings_button);
1365 boolean hasSettings = hasSettings();
1366 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301367 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001368 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001369 public void handleViewClick(View view) {
1370 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001371 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001372 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001373 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1374 } else {
1375 settingsButton.setVisibility(View.GONE);
1376 }
1377
1378 mOverviewPanel.setAlpha(0f);
1379 }
1380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001382 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001383 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001384 */
1385 public void setAllAppsButton(View allAppsButton) {
1386 mAllAppsButton = allAppsButton;
1387 }
1388
Sunny Goyalbb011da2016-06-15 15:42:29 -07001389 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001390 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001391 }
1392
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001393 public View getWidgetsButton() {
1394 return mWidgetsButton;
1395 }
1396
Anjali Koppal5ad44842014-03-10 20:34:39 -07001397 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 * Creates a view representing a shortcut.
1399 *
1400 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001402 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001403 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
1406 /**
1407 * Creates a view representing a shortcut inflated from the specified resource.
1408 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 * @param parent The group the shortcut belongs to.
1410 * @param info The data structure describing the shortcut.
1411 *
1412 * @return A View inflated from layoutResId.
1413 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001414 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001415 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001416 parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001417 favorite.applyFromShortcutInfo(info);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001418 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001420 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 return favorite;
1422 }
1423
1424 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 * Add a shortcut to the workspace.
1426 *
1427 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001429 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001430 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001432 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001433
Sunny Goyal782f0c92017-01-19 10:27:54 -08001434 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001435 args.getPendingIntent().getComponent() == null) {
1436 return;
1437 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001438
Sunny Goyal782f0c92017-01-19 10:27:54 -08001439 ShortcutInfo info = null;
1440 if (Utilities.isAtLeastO()) {
Sunny Goyaldec3a902017-01-25 18:23:36 -08001441 info = LauncherAppsCompat.createShortcutInfoFromPinItemRequest(
1442 this, PinItemRequestCompat.getPinItemRequest(data));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001443 }
1444
1445 if (info == null) {
1446 info = InstallShortcutReceiver.fromShortcutIntent(this, data);
1447
Sunny Goyal342e4662017-02-02 15:21:08 -08001448 if (info == null || !new PackageManagerHelper(this).hasPermissionForActivity(
1449 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001450 // The app is trying to add a shortcut without sufficient permissions
1451 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1452 return;
1453 }
1454 }
1455
1456 final View view = createShortcut(info);
Sunny Goyal5c97f512015-05-19 16:03:28 -07001457 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001458 // First we check if we already know the exact location where we want to add this item.
1459 if (cellX >= 0 && cellY >= 0) {
1460 cellXY[0] = cellX;
1461 cellXY[1] = cellY;
1462 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001463
1464 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001465 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001466 true, null,null)) {
1467 return;
1468 }
1469 DragObject dragObject = new DragObject();
1470 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001471 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1472 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001473 return;
1474 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001475 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001476 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001477 }
1478
1479 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001480 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 return;
1482 }
1483
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001484 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301485 mWorkspace.addInScreen(view, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001489 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001491 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001493 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001494 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1495
Adam Cohened66b2b2012-01-23 17:28:51 -08001496 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001497 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001498 }
Romain Guycbb89e42009-06-08 15:52:54 -07001499
Adam Cohen59400422014-03-05 18:07:04 -08001500 if (appWidgetInfo.isCustomWidget) {
1501 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001502 }
1503
Adam Cohen59400422014-03-05 18:07:04 -08001504 LauncherAppWidgetInfo launcherInfo;
1505 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001506 launcherInfo.spanX = itemInfo.spanX;
1507 launcherInfo.spanY = itemInfo.spanY;
1508 launcherInfo.minSpanX = itemInfo.minSpanX;
1509 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyala52ecb02016-12-16 15:04:51 -08001510 launcherInfo.user = appWidgetInfo.getUser();
Romain Guycbb89e42009-06-08 15:52:54 -07001511
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001512 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001513 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514
Sunny Goyal2100c782016-08-22 16:00:03 -07001515 if (hostView == null) {
1516 // Perform actual inflation because we're live
1517 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001519 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301520 prepareAppWidget(hostView, launcherInfo);
1521 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
Romain Guycbb89e42009-06-08 15:52:54 -07001523
Sunny Goyald5462aa2016-11-22 16:52:39 +05301524 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001525 hostView.setTag(item);
1526 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001527 hostView.setFocusable(true);
1528 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001529 }
1530
Adam Cohended9f8d2010-11-03 13:25:16 -07001531 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1532 @Override
1533 public void onReceive(Context context, Intent intent) {
1534 final String action = intent.getAction();
1535 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001536 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001537
Winson Chungc100e8e2011-08-09 16:02:43 -07001538 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001539 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001540 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001541 if (!showWorkspace(false)) {
1542 // If we are already on the workspace, then manually reset all apps
1543 mAppsView.reset();
1544 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001545 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001546 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001547 }
1548 }
1549 };
1550
Tony Wickham010d2552017-01-20 08:15:28 -08001551 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1552 Runnable r = new Runnable() {
1553 @Override
1554 public void run() {
1555 mWorkspace.updateIconBadges(updatedBadges);
1556 mAppsView.updateIconBadges(updatedBadges);
1557 }
1558 };
1559 if (!waitUntilResume(r)) {
1560 r.run();
1561 }
1562 }
1563
Adam Cohended9f8d2010-11-03 13:25:16 -07001564 @Override
1565 public void onAttachedToWindow() {
1566 super.onAttachedToWindow();
1567
1568 // Listen for broadcasts related to user-presence
1569 final IntentFilter filter = new IntentFilter();
1570 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001572 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001573 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001574
1575 if (mLauncherCallbacks != null) {
1576 mLauncherCallbacks.onAttachedToWindow();
1577 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001578 }
1579
1580 @Override
1581 public void onDetachedFromWindow() {
1582 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001583 if (mAttached) {
1584 unregisterReceiver(mReceiver);
1585 mAttached = false;
1586 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001587
1588 if (mLauncherCallbacks != null) {
1589 mLauncherCallbacks.onDetachedFromWindow();
1590 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001591 }
1592
1593 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001594 // The following code used to be in onResume, but it turns out onResume is called when
1595 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1596 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001597 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001598 if (!mWorkspaceLoading) {
1599 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001600 // We want to let Launcher draw itself at least once before we force it to build
1601 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001602 // apps is nice and speedy.
1603 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001604 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001605 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001606 if (mStarted) return;
1607 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001608 // We delay the layer building a bit in order to give
1609 // other message processing a time to run. In particular
1610 // this avoids a delay in hiding the IME if it was
1611 // currently shown, because doing that may involve
1612 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001613 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001614 final ViewTreeObserver.OnDrawListener listener = this;
1615 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001616 public void run() {
1617 if (mWorkspace != null &&
1618 mWorkspace.getViewTreeObserver() != null) {
1619 mWorkspace.getViewTreeObserver().
1620 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001621 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001622 }
1623 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001624 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001625 }
1626 });
1627 }
1628 clearTypedText();
1629 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 }
1631
Winson Chunga6945242014-01-08 14:04:34 -08001632 public DragLayer getDragLayer() {
1633 return mDragLayer;
1634 }
1635
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001636 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001637 return mAppsView;
1638 }
1639
Hyunyoung Song3f471442015-04-08 19:01:34 -07001640 public WidgetsContainerView getWidgetsView() {
1641 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001642 }
1643
Winson Chunga6945242014-01-08 14:04:34 -08001644 public Workspace getWorkspace() {
1645 return mWorkspace;
1646 }
1647
Sunny Goyal6178f132016-07-11 17:30:03 -07001648 public View getQsbContainer() {
1649 return mQsbContainer;
1650 }
1651
Winson Chunga6945242014-01-08 14:04:34 -08001652 public Hotseat getHotseat() {
1653 return mHotseat;
1654 }
1655
Jorim Jaggid017f882014-01-14 17:08:48 -08001656 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001657 return mOverviewPanel;
1658 }
1659
Sunny Goyal47328fd2016-05-25 18:56:41 -07001660 public DropTargetBar getDropTargetBar() {
1661 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001662 }
1663
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001664 public LauncherAppWidgetHost getAppWidgetHost() {
1665 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
Romain Guycbb89e42009-06-08 15:52:54 -07001667
Winson Chunga9abd0e2010-10-27 17:18:37 -07001668 public LauncherModel getModel() {
1669 return mModel;
1670 }
1671
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001672 public ModelWriter getModelWriter() {
1673 return mModelWriter;
1674 }
1675
Adam Cohen79d90c52016-04-22 13:29:20 -07001676 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001677 return mSharedPrefs;
1678 }
1679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 @Override
1681 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001682 long startTime = 0;
1683 if (DEBUG_RESUME_TIME) {
1684 startTime = System.currentTimeMillis();
1685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 super.onNewIntent(intent);
1687
Winson15f8b172015-08-19 11:02:39 -07001688 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1689 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1690 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001691
1692 // Check this condition before handling isActionMain, as this will get reset.
1693 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001694 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001695
Winson15f8b172015-08-19 11:02:39 -07001696 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1697 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001698 if (mWorkspace == null) {
1699 // Can be cases where mWorkspace is null, this prevents a NPE
1700 return;
1701 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001702
1703 // Note: There should be at most one log per method call. This is enforced implicitly
1704 // by using if-else statements.
1705 UserEventDispatcher ued = getUserEventDispatcher();
1706
1707 // TODO: Log this case.
Adam Cohen6fecd412013-10-02 17:41:50 -07001708 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001709
Jon Mirandafeba90f2016-10-06 10:53:29 -07001710 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham540913e2017-01-23 11:47:51 -08001711 if (topOpenView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301712 ued.logActionCommand(Action.Command.HOME_INTENT,
1713 topOpenView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001714 } else if (topOpenView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301715 ued.logActionCommand(Action.Command.HOME_INTENT,
1716 ((Folder) topOpenView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001717 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301718 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001719 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1720 }
1721
1722 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001723 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001724 exitSpringLoadedDragMode();
1725
1726 // If we are already on home, then just animate back to the workspace,
1727 // otherwise, just wait until onResume to set the state back to Workspace
1728 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001729 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001730 } else {
1731 mOnResumeState = State.WORKSPACE;
1732 }
1733
1734 final View v = getWindow().peekDecorView();
1735 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001736 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001737 INPUT_METHOD_SERVICE);
1738 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1739 }
1740
Winson Chungb745afb2015-03-02 11:51:23 -08001741 // Reset the apps view
1742 if (!alreadyOnHome && mAppsView != null) {
1743 mAppsView.scrollToTop();
1744 }
1745
Hyunyoung Song3f471442015-04-08 19:01:34 -07001746 // Reset the widgets view
1747 if (!alreadyOnHome && mWidgetsView != null) {
1748 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001749 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001750
Adam Cohen9211d422014-10-07 18:14:53 -07001751 if (mLauncherCallbacks != null) {
1752 mLauncherCallbacks.onHomeIntent();
1753 }
Sunny Goyalb38fab72017-01-20 19:32:31 -08001754
1755 Parcelable dragExtra = intent
1756 .getParcelableExtra(PinItemDragListener.EXTRA_PIN_ITEM_DRAG_LISTENER);
1757 if (dragExtra instanceof PinItemDragListener) {
1758 PinItemDragListener dragListener = (PinItemDragListener) dragExtra;
1759 dragListener.setLauncher(this);
1760 mDragLayer.setOnDragListener(dragListener);
1761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 }
Adam Cohened307df2013-10-02 09:37:31 -07001763
Adam Cohen9211d422014-10-07 18:14:53 -07001764 if (mLauncherCallbacks != null) {
1765 mLauncherCallbacks.onNewIntent(intent);
1766 }
Winson15f8b172015-08-19 11:02:39 -07001767
1768 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1769 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1770 // animation.
1771 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001772 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001773 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001774 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1775 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001776
1777 // We use this flag to suppress noisy callbacks above custom content state
1778 // from onResume.
1779 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001780 mWorkspace.post(new Runnable() {
1781 @Override
1782 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001783 if (mWorkspace != null) {
1784 mWorkspace.moveToDefaultScreen(true);
1785 }
Winson15f8b172015-08-19 11:02:39 -07001786 }
1787 });
1788 }
1789 }
1790
1791 if (DEBUG_RESUME_TIME) {
1792 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1793 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001794 }
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001797 public void onRestoreInstanceState(Bundle state) {
1798 super.onRestoreInstanceState(state);
1799 for (int page: mSynchronouslyBoundPages) {
1800 mWorkspace.restoreInstanceStateForChild(page);
1801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
1803
1804 @Override
1805 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001806 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001807 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1808 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001809
Adam Cohen21cd0022013-10-09 18:57:02 -07001810 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001811 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
Michael Jurka883f55b2010-10-21 15:47:14 -07001813 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001814 // We close any open folders and shortcut containers since they will not be re-opened,
1815 // and we need to make sure this state is reflected.
1816 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001817
Sunny Goyal2100c782016-08-22 16:00:03 -07001818 if (mPendingRequestArgs != null) {
1819 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1820 }
1821 if (mPendingActivityResult != null) {
1822 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
1824
Adam Cohen9211d422014-10-07 18:14:53 -07001825 if (mLauncherCallbacks != null) {
1826 mLauncherCallbacks.onSaveInstanceState(outState);
1827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
1829
1830 @Override
1831 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001833
Michael Jurka9d906c72011-10-14 06:25:36 -07001834 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001835 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001836
Winson Chungcd2b0142011-06-08 16:02:26 -07001837 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001838 // It's possible to receive onDestroy after a new Launcher activity has
1839 // been created. In this case, don't interfere with the new Launcher.
1840 if (mModel.isCurrentCallbacks(this)) {
1841 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001842 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001843 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001844
Sunny Goyal745bad92016-05-02 10:54:12 -07001845 if (mRotationPrefChangeHandler != null) {
1846 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1847 }
1848
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001850 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001852 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001854 mAppWidgetHost = null;
1855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 TextKeyListener.getInstance().release();
1857
Tony Wickhame2217252016-03-22 16:34:23 -07001858 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1859 .removeAccessibilityStateChangeListener(this);
1860
Michael Jurka2ecf9952012-06-18 12:52:28 -07001861 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001862
1863 if (mLauncherCallbacks != null) {
1864 mLauncherCallbacks.onDestroy();
1865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
1867
Sunny Goyalae502842016-06-17 08:43:56 -07001868 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1869 return mAccessibilityDelegate;
1870 }
1871
Adam Cohena9cf38f2011-05-02 15:36:58 -07001872 public DragController getDragController() {
1873 return mDragController;
1874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001877 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001878 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001879 }
1880
1881 @Override
1882 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1883 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001884 try {
1885 super.startIntentSenderForResult(intent, requestCode,
1886 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1887 } catch (IntentSender.SendIntentException e) {
1888 throw new ActivityNotFoundException();
1889 }
1890 }
1891
Winson Chung70d72102011-08-12 11:24:35 -07001892 /**
1893 * Indicates that we want global search for this activity by setting the globalSearch
1894 * argument for {@link #startSearch} to true.
1895 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001897 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001899
Karl Rosaen138a0412009-04-23 19:00:21 -07001900 if (initialQuery == null) {
1901 // Use any text typed in the launcher as the initial query
1902 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 if (appSearchData == null) {
1905 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001906 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001908
Sunny Goyal6f28e712016-09-13 14:19:29 -07001909 if (mLauncherCallbacks == null ||
1910 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1911 // Starting search from the callbacks failed. Start the default global search.
1912 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001913 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001914
1915 // We need to show the workspace after starting the search
1916 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001917 }
1918
Ian Parkinson047036e2014-09-01 15:40:53 +01001919 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001920 * Starts the global search activity. This code is a copied from SearchManager
1921 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001922 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001923 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001924 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001925 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1926 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1927 if (globalSearchActivity == null) {
1928 Log.w(TAG, "No global search activity found.");
1929 return;
1930 }
1931 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1932 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1933 intent.setComponent(globalSearchActivity);
1934 // Make sure that we have a Bundle to put source in
1935 if (appSearchData == null) {
1936 appSearchData = new Bundle();
1937 } else {
1938 appSearchData = new Bundle(appSearchData);
1939 }
Adam Cohen9211d422014-10-07 18:14:53 -07001940 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001941 if (!appSearchData.containsKey("source")) {
1942 appSearchData.putString("source", getPackageName());
1943 }
1944 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1945 if (!TextUtils.isEmpty(initialQuery)) {
1946 intent.putExtra(SearchManager.QUERY, initialQuery);
1947 }
1948 if (selectInitialQuery) {
1949 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1950 }
1951 intent.setSourceBounds(sourceBounds);
1952 try {
1953 startActivity(intent);
1954 } catch (ActivityNotFoundException ex) {
1955 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Yura4f93ec62014-02-04 14:15:21 +00001959 public boolean isOnCustomContent() {
1960 return mWorkspace.isOnOrMovingToCustomContent();
1961 }
1962
Winson Chung70d72102011-08-12 11:24:35 -07001963 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001964 public boolean onPrepareOptionsMenu(Menu menu) {
1965 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001966 if (mLauncherCallbacks != null) {
1967 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1968 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001969 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001972 @Override
1973 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001974 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001975 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001976 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001977 }
1978
Joe Onorato9c1289c2009-08-17 11:03:03 -04001979 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001980 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981 }
1982
Adam Cohen517a7f52014-03-01 12:12:59 -08001983 public boolean isWorkspaceLoading() {
1984 return mWorkspaceLoading;
1985 }
1986
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001987 private void setWorkspaceLoading(boolean value) {
1988 boolean isLocked = isWorkspaceLocked();
1989 mWorkspaceLoading = value;
1990 if (isLocked != isWorkspaceLocked()) {
1991 onWorkspaceLockedChanged();
1992 }
1993 }
1994
Sunny Goyal04a324a2017-01-20 21:08:59 -08001995 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001996 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001997 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001998 if (isLocked != isWorkspaceLocked()) {
1999 onWorkspaceLockedChanged();
2000 }
2001 }
2002
Adam Cohen9211d422014-10-07 18:14:53 -07002003 protected void onWorkspaceLockedChanged() {
2004 if (mLauncherCallbacks != null) {
2005 mLauncherCallbacks.onWorkspaceLockedChanged();
2006 }
2007 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002008
Sunny Goyal2100c782016-08-22 16:00:03 -07002009 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002010 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002011 if (LOGD) {
2012 Log.d(TAG, "Adding widget from drop");
2013 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002014 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002015 }
2016
Sunny Goyal2100c782016-08-22 16:00:03 -07002017 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002018 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
2019 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
2020 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07002021
Adam Cohenad4e15c2013-10-17 16:21:35 -07002022 Runnable onComplete = new Runnable() {
2023 @Override
2024 public void run() {
2025 // Exit spring loaded mode if necessary after adding the widget
2026 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2027 null);
2028 }
2029 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08002030 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07002031 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033 }
Romain Guycbb89e42009-06-08 15:52:54 -07002034
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002035 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002036 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002037 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002038 mWorkspace.moveToCustomContentScreen(animate);
2039 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002040
2041 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2042 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002043 info.container = container;
2044 info.screenId = screenId;
2045 if (cell != null) {
2046 info.cellX = cell[0];
2047 info.cellY = cell[1];
2048 }
2049 info.spanX = spanX;
2050 info.spanY = spanY;
2051
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002052 switch (info.itemType) {
2053 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2054 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002055 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002056 break;
2057 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08002058 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002059 break;
2060 default:
2061 throw new IllegalStateException("Unknown item type: " + info.itemType);
2062 }
2063 }
2064
Adam Cohenfbba09b2011-07-18 21:43:05 -07002065 /**
2066 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002067 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08002068 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002069 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2070 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08002071 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
2072 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
2073 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002074 }
2075
Adam Cohenfbba09b2011-07-18 21:43:05 -07002076 /**
2077 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002078 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002079 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002080 AppWidgetHostView hostView = info.boundWidget;
2081 int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08002082 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08002083 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002084 // In the case where we've prebound the widget, we remove it from the DragLayer
2085 if (LOGD) {
2086 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2087 }
2088 getDragLayer().removeView(hostView);
2089
Adam Cohened66b2b2012-01-23 17:28:51 -08002090 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08002091 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002092
2093 // Clear the boundWidget so that it doesn't get destroyed.
2094 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002095 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002096 // In this case, we either need to start an activity to get permission to bind
2097 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002098 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002099 Bundle options = info.bindOptions;
2100
Sunny Goyalffe83f12014-08-14 17:39:34 -07002101 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2102 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002103 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002104 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07002105 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002106 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07002107 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002108 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002109 }
2110
Adam Cohendcd297f2013-06-18 13:13:40 -07002111 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002112 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002113 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 folderInfo.title = getText(R.string.folder_name);
2115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002117 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118
2119 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002120 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302121 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07002122 // Force measure the new folder icon
2123 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2124 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002125 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
Romain Guycbb89e42009-06-08 15:52:54 -07002127
Winsonc0b52fe2015-09-09 16:38:15 -07002128 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002129 * Unbinds the view for the specified item, and removes the item and all its children.
2130 *
2131 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002132 * @param itemInfo the {@link ItemInfo} for this view.
2133 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002134 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002135 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002136 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002137 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002138 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2139 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002140 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002141 } else {
2142 mWorkspace.removeWorkspaceItem(v);
2143 }
Winsonc0b52fe2015-09-09 16:38:15 -07002144 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002145 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002146 }
2147 } else if (itemInfo instanceof FolderInfo) {
2148 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002149 if (v instanceof FolderIcon) {
2150 ((FolderIcon) v).removeListeners();
2151 }
Winsonc0b52fe2015-09-09 16:38:15 -07002152 mWorkspace.removeWorkspaceItem(v);
2153 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002154 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002155 }
2156 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2157 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002158 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002159 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002160 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002161 }
2162 } else {
2163 return false;
2164 }
2165 return true;
2166 }
2167
2168 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002169 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002170 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002171 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002172 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002173 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002174 // Deleting an app widget ID is a void call but writes to disk before returning
2175 // to the caller...
2176 new AsyncTask<Void, Void, Void>() {
2177 public Void doInBackground(Void ... args) {
2178 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2179 return null;
2180 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002181 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002182 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002183 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002184 }
2185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 @Override
2187 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002188 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
2190
Joe Onorato88ec0992009-11-19 13:16:06 -08002191 @Override
2192 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002193 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2194 return;
2195 }
2196
Sunny Goyal45478022015-06-08 16:52:41 -07002197 if (mDragController.isDragging()) {
2198 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002199 return;
2200 }
2201
Jon Mirandafeba90f2016-10-06 10:53:29 -07002202 // Note: There should be at most one log per method call. This is enforced implicitly
2203 // by using if-else statements.
2204 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002205 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2206 if (topView != null) {
2207 if (topView.getActiveTextView() != null) {
2208 topView.getActiveTextView().dispatchBackKey();
2209 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002210 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302211 ued.logActionCommand(Action.Command.BACK,
2212 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002213 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302214 ued.logActionCommand(Action.Command.BACK,
2215 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002216 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002217 topView.close(true);
2218 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002219 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302220 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002221 showWorkspace(true);
2222 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302223 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002224 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002225 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302226 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002227 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002228 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002229 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002230 mWorkspace.exitWidgetResizeMode();
2231
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002232 // Back button is a no-op here, but give at least some feedback for the button press
2233 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002234 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002235 }
2236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 * Launches the intent referred by the clicked shortcut.
2239 *
2240 * @param v The view representing the clicked shortcut.
2241 */
2242 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002243 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2244 // view has detached (it's possible for this to happen if the view is removed mid touch).
2245 if (v.getWindowToken() == null) {
2246 return;
2247 }
2248
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002249 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002250 return;
2251 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002252
Adam Cohen1697b792013-09-17 19:08:21 -07002253 if (v instanceof Workspace) {
2254 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002255 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002256 }
2257 return;
2258 }
2259
2260 if (v instanceof CellLayout) {
2261 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002262 int page = mWorkspace.indexOfChild(v);
2263 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2264 LauncherLogProto.Action.Direction.NONE,
2265 LauncherLogProto.ContainerType.OVERVIEW, page);
2266 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002267 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002268 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002269 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002270 }
2271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002273 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002274 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002276 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002277 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002278 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002279 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2280 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002281 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002282 } else if (tag instanceof AppInfo) {
2283 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002284 } else if (tag instanceof LauncherAppWidgetInfo) {
2285 if (v instanceof PendingAppWidgetHostView) {
2286 onClickPendingWidget((PendingAppWidgetHostView) v);
2287 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 }
2289 }
2290
Sunny Goyal70660032015-05-14 00:07:08 -07002291 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002292 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002293 return false;
2294 }
2295
Michael Jurkaaf442092010-06-10 17:01:57 -07002296 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002297 * Event handler for the app widget view which has not fully restored.
2298 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002299 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002300 if (mIsSafeModeEnabled) {
2301 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2302 return;
2303 }
2304
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002305 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002306 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002307 LauncherAppWidgetProviderInfo appWidgetInfo =
2308 mAppWidgetManager.findProvider(info.providerName, info.user);
2309 if (appWidgetInfo == null) {
2310 return;
2311 }
2312 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2313
Sunny Goyald478c832016-04-01 12:04:16 -07002314 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2315 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2316 // This should not happen, as we make sure that an Id is allocated during bind.
2317 return;
2318 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002319 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2320 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002321 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002322 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002323 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002324 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002325 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002326 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002327 }
2328 }
2329
2330 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002331 * Event handler for the "grid" button that appears on the home screen, which
2332 * enters all apps mode.
2333 *
2334 * @param v The view that was clicked.
2335 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002336 protected void onClickAllAppsButton(View v) {
2337 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002338 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302339 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2340 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002341 showAppsView(true /* animated */, true /* updatePredictedApps */,
2342 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002343 }
2344 }
2345
2346 protected void onLongClickAllAppsButton(View v) {
2347 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2348 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302349 getUserEventDispatcher().logActionOnControl(Action.Touch.LONGPRESS,
2350 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002351 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002352 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002353 }
2354 }
2355
Sunny Goyale6e72002017-01-12 16:55:36 -08002356 private void onClickPendingAppItem(final View v, final String packageName,
2357 boolean downloadStarted) {
2358 if (downloadStarted) {
2359 // If the download has started, simply direct to the market app.
2360 startMarketIntentForPackage(v, packageName);
2361 return;
2362 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002363 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002364 .setTitle(R.string.abandoned_promises_title)
2365 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002366 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2367 @Override
2368 public void onClick(DialogInterface dialogInterface, int i) {
2369 startMarketIntentForPackage(v, packageName);
2370 }
2371 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002372 .setNeutralButton(R.string.abandoned_clean_this,
2373 new DialogInterface.OnClickListener() {
2374 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002375 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002376 mWorkspace.removeAbandonedPromise(packageName, user);
2377 }
2378 })
2379 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002380 }
2381
2382 private void startMarketIntentForPackage(View v, String packageName) {
2383 ItemInfo item = (ItemInfo) v.getTag();
2384 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2385 boolean success = startActivitySafely(v, intent, item);
2386 if (success && v instanceof BubbleTextView) {
2387 mWaitingForResume = (BubbleTextView) v;
2388 mWaitingForResume.setStayPressed(true);
2389 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002390 }
2391
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002392 /**
2393 * Event handler for an app shortcut click.
2394 *
2395 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2396 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002397 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002398 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2399 Object tag = v.getTag();
2400 if (!(tag instanceof ShortcutInfo)) {
2401 throw new IllegalArgumentException("Input must be a Shortcut");
2402 }
2403
2404 // Open shortcut
2405 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002406
2407 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002408 if ((shortcut.isDisabled &
2409 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2410 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2411 // If the app is only disabled because of the above flags, launch activity anyway.
2412 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002413 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002414 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2415 // Use a message specific to this shortcut, if it has one.
2416 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2417 return;
2418 }
2419 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002420 int error = R.string.activity_not_available;
2421 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2422 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002423 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2424 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002425 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002426 }
2427 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2428 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002429 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002430 }
2431
Chris Wren40c5ed32014-06-24 18:24:23 -04002432 // Check for abandoned promise
Sunny Goyale6e72002017-01-12 16:55:36 -08002433 if ((v instanceof BubbleTextView) && shortcut.isPromise()) {
2434 String packageName = shortcut.intent.getComponent() != null ?
2435 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2436 if (!TextUtils.isEmpty(packageName)) {
2437 onClickPendingAppItem(v, packageName,
2438 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2439 return;
2440 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002441 }
2442
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002443 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002444 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002445 }
2446
Sunny Goyala7ce1662016-05-31 15:01:35 -07002447 private void startAppShortcutOrInfoActivity(View v) {
2448 ItemInfo item = (ItemInfo) v.getTag();
2449 Intent intent = item.getIntent();
2450 if (intent == null) {
2451 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002452 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002453 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002454 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002455
2456 if (success && v instanceof BubbleTextView) {
2457 mWaitingForResume = (BubbleTextView) v;
2458 mWaitingForResume.setStayPressed(true);
2459 }
2460 }
2461
2462 /**
2463 * Event handler for a folder icon click.
2464 *
2465 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2466 */
2467 protected void onClickFolderIcon(View v) {
2468 if (LOGD) Log.d(TAG, "onClickFolder");
2469 if (!(v instanceof FolderIcon)){
2470 throw new IllegalArgumentException("Input must be a FolderIcon");
2471 }
2472
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002473 Folder folder = ((FolderIcon) v).getFolder();
2474 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002475 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002476 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002477 }
Michael Jurka5130e402011-10-13 04:55:35 -07002478 }
2479
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002480 /**
2481 * Event handler for the (Add) Widgets button that appears after a long press
2482 * on the home screen.
2483 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002484 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002485 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002486 if (mIsSafeModeEnabled) {
2487 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2488 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002489 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002490 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002491 }
2492
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002493 /**
2494 * Event handler for the wallpaper picker button that appears after a long press
2495 * on the home screen.
2496 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002497 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002498 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002499 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2500 return;
2501 }
2502
Tony Wickham785f7a52015-08-31 17:28:32 -07002503 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2504 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002505 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002506 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002507 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002508
2509 String pickerPackage = getString(R.string.wallpaper_picker_package);
2510 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2511 if (!hasTargetPackage) {
2512 intent.setPackage(pickerPackage);
2513 }
2514
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002515 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002516 try {
2517 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2518 // If there is no target package, use the default intent chooser animation
2519 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2520 } catch (ActivityNotFoundException e) {
2521 setWaitingForResult(null);
2522 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2523 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 }
2525
2526 /**
2527 * Event handler for a click on the settings button that appears after a long press
2528 * on the home screen.
2529 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002530 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002532 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2533 .setPackage(getPackageName());
2534 intent.setSourceBounds(getViewBounds(v));
2535 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002536 }
2537
Adam Cohen61f560d2013-09-30 15:58:20 -07002538 public View.OnTouchListener getHapticFeedbackTouchListener() {
2539 if (mHapticFeedbackTouchListener == null) {
2540 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002541 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002542 @Override
2543 public boolean onTouch(View v, MotionEvent event) {
2544 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2545 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2546 }
2547 return false;
2548 }
2549 };
2550 }
2551 return mHapticFeedbackTouchListener;
2552 }
2553
Tony Wickhame2217252016-03-22 16:34:23 -07002554 @Override
2555 public void onAccessibilityStateChanged(boolean enabled) {
2556 mDragLayer.onAccessibilityStateChanged(enabled);
2557 }
2558
Sunny Goyal06e21a22016-08-11 16:02:02 -07002559 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002560 if (isOnCustomContent()) {
2561 // Custom content screen doesn't participate in drag and drop. If on custom
2562 // content screen, move to default.
2563 moveWorkspaceToDefaultScreen();
2564 }
Adam Cohen9211d422014-10-07 18:14:53 -07002565 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002566
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002567 /**
2568 * Called when the user stops interacting with the launcher.
2569 * This implies that the user is now on the homescreen and is not doing housekeeping.
2570 */
Adam Cohen9211d422014-10-07 18:14:53 -07002571 protected void onInteractionEnd() {
2572 if (mLauncherCallbacks != null) {
2573 mLauncherCallbacks.onInteractionEnd();
2574 }
2575 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002576
2577 /**
2578 * Called when the user starts interacting with the launcher.
2579 * The possible interactions are:
2580 * - open all apps
2581 * - reorder an app shortcut, or a widget
2582 * - open the overview mode.
2583 * This is a good time to stop doing things that only make sense
2584 * when the user is on the homescreen and not doing housekeeping.
2585 */
Adam Cohen9211d422014-10-07 18:14:53 -07002586 protected void onInteractionBegin() {
2587 if (mLauncherCallbacks != null) {
2588 mLauncherCallbacks.onInteractionBegin();
2589 }
2590 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002591
Winson Chungcd99cd32015-04-29 11:03:24 -07002592 /** Updates the interaction state. */
2593 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002594 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002595 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002596 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2597 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002598 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002599 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002600 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002601 onInteractionEnd();
2602 }
2603 }
2604
Sunny Goyala7ce1662016-05-31 15:01:35 -07002605 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002607 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2608 try {
2609 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2610 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2611 // is enabled by default on NYC.
2612 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2613 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002614
2615 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2616 String id = ((ShortcutInfo) info).getDeepShortcutId();
2617 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302618 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002619 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002620 } else {
2621 // Could be launching some bookkeeping activity
2622 startActivity(intent, optsBundle);
2623 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002624 } finally {
2625 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002628 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2629 // corresponding permission. Show the appropriate permission prompt if that
2630 // is the case.
2631 if (intent.getComponent() == null
2632 && Intent.ACTION_CALL.equals(intent.getAction())
2633 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2634 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002635
2636 setWaitingForResult(PendingRequestArgs
2637 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002638 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2639 REQUEST_PERMISSION_CALL_PHONE);
2640 } else {
2641 // No idea why this was thrown.
2642 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002643 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644 }
2645 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002646
Sunny Goyalfe770c92016-09-27 14:38:58 -07002647 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002648 private Bundle getActivityLaunchOptions(View v) {
2649 if (Utilities.ATLEAST_MARSHMALLOW) {
2650 int left = 0, top = 0;
2651 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2652 if (v instanceof TextView) {
2653 // Launch from center of icon, not entire view
2654 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2655 if (icon != null) {
2656 Rect bounds = icon.getBounds();
2657 left = (width - bounds.width()) / 2;
2658 top = v.getPaddingTop();
2659 width = bounds.width();
2660 height = bounds.height();
2661 }
2662 }
2663 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2664 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2665 // On L devices, we use the device default slide-up transition.
2666 // On L MR1 devices, we use a custom version of the slide-up transition which
2667 // doesn't have the delay present in the device default.
2668 return ActivityOptions.makeCustomAnimation(
2669 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2670 }
2671 return null;
2672 }
2673
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002674 private Rect getViewBounds(View v) {
2675 int[] pos = new int[2];
2676 v.getLocationOnScreen(pos);
2677 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2678 }
2679
Sunny Goyala7ce1662016-05-31 15:01:35 -07002680 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002681 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2682 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2683 return false;
2684 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002685 // Only launch using the new animation if the shortcut has not opted out (this is a
2686 // private contract between launcher and may be ignored in the future).
2687 boolean useLaunchAnimation = (v != null) &&
2688 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2689 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2690
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002691 UserHandle user = null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002692 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2693 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2694 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002695 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002696
2697 // Prepare intent
2698 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2699 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002700 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002701 }
2702 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002703 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2704 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002705 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002706 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002707 // Shortcuts need some special checks due to legacy reasons.
2708 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002709 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002710 // Could be launching some bookkeeping activity
2711 startActivity(intent, optsBundle);
2712 } else {
2713 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2714 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2715 }
2716 return true;
2717 } catch (ActivityNotFoundException|SecurityException e) {
2718 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2719 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2720 }
2721 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002722 }
2723
Tony Wickhamdadb3042016-02-24 11:07:00 -08002724 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002725 public boolean dispatchTouchEvent(MotionEvent ev) {
2726 mLastDispatchTouchEventX = ev.getX();
2727 return super.dispatchTouchEvent(ev);
2728 }
2729
2730 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002732 if (!isDraggingEnabled()) return false;
2733 if (isWorkspaceLocked()) return false;
2734 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002736 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2737 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002738 onLongClickAllAppsButton(v);
2739 return true;
2740 }
2741
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002742
Jon Miranda51f037d2016-11-07 08:37:20 -08002743 boolean ignoreLongPressToOverview =
2744 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002745
Adam Cohen1697b792013-09-17 19:08:21 -07002746 if (v instanceof Workspace) {
2747 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002748 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302749 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2750 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002751 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002752 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002753 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2754 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2755 return true;
2756 } else {
2757 return false;
2758 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002759 } else {
2760 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002761 }
Adam Cohen1697b792013-09-17 19:08:21 -07002762 }
2763
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002764 CellLayout.CellInfo longClickCellInfo = null;
2765 View itemUnderLongClick = null;
2766 if (v.getTag() instanceof ItemInfo) {
2767 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002768 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002769 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002770 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 }
2772
Winson Chung3d503fb2011-07-13 17:25:49 -07002773 // The hotseat touch handling does not go through Workspace, and we always allow long press
2774 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002775 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002776 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002777 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002778 if (mWorkspace.isInOverviewMode()) {
2779 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302780 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2781 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002782 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002783 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002784 return false;
2785 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302786 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2787 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002788 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002789 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002790 }
Jon Miranda379198e2016-09-23 08:54:40 -07002791 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2792 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002793 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002794 final boolean isAllAppsButton =
2795 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2796 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2797 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002798 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002799 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002800 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801 }
2802 }
2803 }
2804 return true;
2805 }
2806
Winson Chung3d503fb2011-07-13 17:25:49 -07002807 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002808 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002809 return mHotseat != null && layout != null &&
2810 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2811 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002812
Winson Chung3d503fb2011-07-13 17:25:49 -07002813 /**
2814 * Returns the CellLayout of the specified container at the specified screen.
2815 */
Sunny Goyal92820592015-03-02 11:31:35 -08002816 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002817 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2818 if (mHotseat != null) {
2819 return mHotseat.getLayout();
2820 } else {
2821 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002822 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002823 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002824 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002825 }
2826 }
2827
Winson Chungb745afb2015-03-02 11:51:23 -08002828 /**
2829 * For overridden classes.
2830 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002831 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002832 return isAppsViewVisible();
2833 }
2834
2835 public boolean isAppsViewVisible() {
2836 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2837 }
2838
2839 public boolean isWidgetsViewVisible() {
2840 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002841 }
2842
Michael Jurkae326f182011-11-21 14:05:46 -08002843 @Override
2844 public void onTrimMemory(int level) {
2845 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002846 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2847 // The widget preview db can result in holding onto over
2848 // 3MB of memory for caching which isn't necessary.
2849 SQLiteDatabase.releaseMemory();
2850
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002851 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002852 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002853 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002854 if (mLauncherCallbacks != null) {
2855 mLauncherCallbacks.onTrimMemory(level);
2856 }
Michael Jurkae326f182011-11-21 14:05:46 -08002857 }
2858
Winson5c6bdbb2015-09-03 11:36:19 -07002859 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002860 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002861 }
2862
Winson5c6bdbb2015-09-03 11:36:19 -07002863 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002864 boolean changed = mState != State.WORKSPACE ||
2865 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002866 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002867 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002868 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002869 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002870
Michael Jurkab3e22d92011-10-31 15:58:33 -07002871 // Set focus to the AppsCustomize button
2872 if (mAllAppsButton != null) {
2873 mAllAppsButton.requestFocus();
2874 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002875 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002876
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002877 // Change the state *after* we've called all the transition code
2878 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002879
Winson Chung0f785722015-04-08 10:27:49 -07002880 if (changed) {
2881 // Send an accessibility event to announce the context change
2882 getWindow().getDecorView()
2883 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002884 }
Winson5c6bdbb2015-09-03 11:36:19 -07002885 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002886 }
2887
Winsone9f27272015-10-13 10:47:51 -07002888 /**
2889 * Shows the overview button.
2890 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002891 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002892 showOverviewMode(animated, false);
2893 }
2894
2895 /**
2896 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2897 * onto one of the overview panel buttons.
2898 */
2899 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2900 Runnable postAnimRunnable = null;
2901 if (requestButtonFocus) {
2902 postAnimRunnable = new Runnable() {
2903 @Override
2904 public void run() {
2905 // Hitting the menu button when in touch mode does not trigger touch mode to
2906 // be disabled, so if requested, force focus on one of the overview panel
2907 // buttons.
2908 mOverviewPanel.requestFocusFromTouch();
2909 }
2910 };
2911 }
Adam Cohened307df2013-10-02 09:37:31 -07002912 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002913 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002914 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002915 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002916 // If animated from long press, then don't allow any of the controller in the drag
2917 // layer to intercept any remaining touch.
2918 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002919 }
2920
Winson Chungb745afb2015-03-02 11:51:23 -08002921 /**
2922 * Shows the apps view.
2923 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002924 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002925 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002926 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002927 if (updatePredictedApps) {
2928 tryAndUpdatePredictedApps();
2929 }
Winson Chung76648c52015-07-10 14:33:23 -07002930 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002931 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002932
Winson Chungb745afb2015-03-02 11:51:23 -08002933 /**
2934 * Shows the widgets view.
2935 */
2936 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002937 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002938 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002939 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002940 }
Winson Chung76648c52015-07-10 14:33:23 -07002941 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002942
2943 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002944 @Override
2945 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002946 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002947 }
2948 });
Winson Chungb745afb2015-03-02 11:51:23 -08002949 }
2950
2951 /**
2952 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002953 *
2954 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002955 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002956 // TODO: calling method should use the return value so that when {@code false} is returned
2957 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002958 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002959 if (!(mState == State.WORKSPACE ||
2960 mState == State.APPS_SPRING_LOADED ||
2961 mState == State.WIDGETS_SPRING_LOADED ||
2962 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002963 return false;
2964 }
2965 if (toState != State.APPS && toState != State.WIDGETS) {
2966 return false;
2967 }
Winson Chungb745afb2015-03-02 11:51:23 -08002968
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002969 // This is a safe and supported transition to bypass spring_loaded mode.
2970 if (mExitSpringLoadedModeRunnable != null) {
2971 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2972 mExitSpringLoadedModeRunnable = null;
2973 }
2974
Winson Chungb745afb2015-03-02 11:51:23 -08002975 if (toState == State.APPS) {
Sunny Goyaldb364372016-10-26 19:12:47 -07002976 mStateTransitionAnimation.startAnimationToAllApps(animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002977 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002978 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002979 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002980
Michael Jurkab3e22d92011-10-31 15:58:33 -07002981 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002982 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002983 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002984
2985 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002986 getWindow().getDecorView()
2987 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002988 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002989 }
2990
Winson Chungcd99cd32015-04-29 11:03:24 -07002991 /**
2992 * Updates the workspace and interaction state on state change, and return the animation to this
2993 * new state.
2994 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002995 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002996 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002997 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002998 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002999 updateInteraction(fromState, toState);
3000 return anim;
3001 }
3002
Hyunyoung Song3f471442015-04-08 19:01:34 -07003003 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003004 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003005 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003006 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003007 }
Winson Chungb745afb2015-03-02 11:51:23 -08003008
Winson Chung006ee262015-08-03 14:40:11 -07003009 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003010 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003011 null /* onCompleteRunnable */);
Sunny Goyalf5440cb2016-12-14 15:13:00 -08003012 mState = State.WORKSPACE_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003013 }
Adam Cohen7777d962011-08-18 18:58:38 -07003014
Hyunyoung Song3f471442015-04-08 19:01:34 -07003015 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003016 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003017 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003018
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003019 if (mExitSpringLoadedModeRunnable != null) {
3020 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3021 }
3022 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003023 @Override
3024 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003025 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003026 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3027 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003028 // Before we show workspace, hide all apps again because
3029 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3030 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003031 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003032 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003033 } else {
3034 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003035 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003036 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003037 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003038 };
3039 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003040 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003041
Tony Wickhame0c33232016-02-08 11:37:04 -08003042 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003043 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3044 || mState == State.WIDGETS_SPRING_LOADED;
3045 }
3046
Sunny Goyal0f76b562016-12-13 19:37:10 -08003047 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003048 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003049 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003050 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003051 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003052 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003053 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3054 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003056 }
3057
Winson Chung4ac30062015-05-08 17:34:17 -07003058 /**
3059 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3060 * resumed.
3061 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003062 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003063 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003064 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003065 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003066 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003067 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003068 }
3069 }
3070 }
3071
Michael Jurkab3e22d92011-10-31 15:58:33 -07003072 void lockAllApps() {
3073 // TODO
3074 }
3075
3076 void unlockAllApps() {
3077 // TODO
3078 }
3079
Michael Jurkad7c28052012-04-27 15:43:36 -07003080 @Override
3081 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003082 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003083 final List<CharSequence> text = event.getText();
3084 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003085 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003086 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003087 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003088 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003089 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003090 } else if (mWorkspace != null) {
3091 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003092 } else {
3093 text.add(getString(R.string.all_apps_home_button_label));
3094 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003095 return result;
3096 }
3097
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003098 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003099 * If the activity is currently paused, signal that we need to run the passed Runnable
3100 * in onResume.
3101 *
3102 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003103 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3104 * wrong when we're not running, and if the activity comes back to what the configuration was
3105 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003106 *
3107 * Implementation of the method from LauncherModel.Callbacks.
3108 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003109 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003110 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003111 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003112 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003113 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003114 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003115 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003116 }
3117 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003118 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003119 return true;
3120 } else {
3121 return false;
3122 }
3123 }
3124
Michael Jurkac402cd92013-05-20 15:49:32 +02003125 private boolean waitUntilResume(Runnable run) {
3126 return waitUntilResume(run, false);
3127 }
3128
Michael Jurka1e2f4652013-07-08 18:03:46 -07003129 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003130 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003131 }
3132
Michael Jurka7607c2f2013-04-03 14:33:19 -07003133 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003134 * If the activity is currently paused, signal that we need to re-run the loader
3135 * in onResume.
3136 *
3137 * This needs to be called from incoming places where resources might have been loaded
3138 * while we are paused. That is becaues the Configuration might be wrong
3139 * when we're not running, and if it comes back to what it was when we
3140 * were paused, we are not restarted.
3141 *
3142 * Implementation of the method from LauncherModel.Callbacks.
3143 *
3144 * @return true if we are currently paused. The caller might be able to
3145 * skip some work in that case since we will come back again.
3146 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003147 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003148 public boolean setLoadOnResume() {
3149 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003150 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003151 mOnResumeNeedsLoad = true;
3152 return true;
3153 } else {
3154 return false;
3155 }
3156 }
3157
3158 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003159 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003160 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003161 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003162 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003163 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003164 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003165 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003166 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003167 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003169
Joe Onorato9c1289c2009-08-17 11:03:03 -04003170 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003171 * Clear any pending bind callbacks. This is called when is loader is planning to
3172 * perform a full rebind from scratch.
3173 */
3174 @Override
3175 public void clearPendingBinds() {
3176 mBindOnResumeCallbacks.clear();
3177 if (mPendingExecutor != null) {
3178 mPendingExecutor.markCompleted();
3179 mPendingExecutor = null;
3180 }
3181 }
3182
3183 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 * Refreshes the shortcuts shown on the workspace.
3185 *
3186 * Implementation of the method from LauncherModel.Callbacks.
3187 */
3188 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003189 if (LauncherAppState.PROFILE_STARTUP) {
3190 Trace.beginSection("Starting page bind");
3191 }
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08003192
3193 AbstractFloatingView.closeAllOpenViews(this);
3194
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003195 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003196
Winson Chungd64d1762013-08-20 14:37:16 -07003197 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003198 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003199 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003200
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 if (mHotseat != null) {
3202 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003204 if (LauncherAppState.PROFILE_STARTUP) {
3205 Trace.endSection();
3206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 }
3208
Adam Cohendcd297f2013-06-18 13:13:40 -07003209 @Override
3210 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003211 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003212 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3213 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003214 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3215 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3216 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003217 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3218 // If there are no screens, we need to have an empty screen
3219 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003220 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003221 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003222
3223 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003224 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003225 if (hasCustomContentToLeft()) {
3226 mWorkspace.createCustomContentContainer();
3227 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003228 }
Winsonc7d2e832016-07-28 12:24:55 -07003229
3230 // After we have added all the screens, if the wallpaper was locked to the default state,
3231 // then notify to indicate that it can be released and a proper wallpaper offset can be
3232 // computed before the next layout
3233 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003234 }
3235
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003236 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003237 int count = orderedScreenIds.size();
3238 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003239 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003240 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003241 // No need to bind the first screen, as its always bound.
3242 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3243 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003244 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003245 }
3246
Winson Chungd64d1762013-08-20 14:37:16 -07003247 public void bindAppsAdded(final ArrayList<Long> newScreens,
3248 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003249 final ArrayList<ItemInfo> addAnimated,
3250 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003251 Runnable r = new Runnable() {
3252 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003253 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003254 }
3255 };
3256 if (waitUntilResume(r)) {
3257 return;
3258 }
3259
Winson Chungd64d1762013-08-20 14:37:16 -07003260 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003261 if (newScreens != null) {
3262 bindAddScreens(newScreens);
3263 }
Winson Chungd64d1762013-08-20 14:37:16 -07003264
3265 // We add the items without animation on non-visible pages, and with
3266 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003267 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003268 bindItems(addNotAnimated, 0,
3269 addNotAnimated.size(), false);
3270 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003271 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003272 bindItems(addAnimated, 0,
3273 addAnimated.size(), true);
3274 }
Winson Chungc58497e2013-09-03 17:48:37 -07003275
Winson Chung87412982013-10-03 18:34:14 -07003276 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003277 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003278
Winson Chungb745afb2015-03-02 11:51:23 -08003279 if (addedApps != null && mAppsView != null) {
3280 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003281 }
Winson Chungd64d1762013-08-20 14:37:16 -07003282 }
3283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003284 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003285 * Bind the items start-end from the list.
3286 *
3287 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003288 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003289 @Override
Sunny Goyal3be633b2016-12-08 09:59:25 -08003290 public void bindItems(final ArrayList<ItemInfo> items, final int start, final int end,
Winson Chung64359a52013-07-08 17:17:08 -07003291 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003292 Runnable r = new Runnable() {
3293 public void run() {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003294 bindItems(items, start, end, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003295 }
3296 };
3297 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003298 return;
3299 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003300
Sunny Goyal3be633b2016-12-08 09:59:25 -08003301 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003302 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3303 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003304 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003305 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003306 long newItemsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003307 for (int i = start; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003308 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003309
3310 // Short circuit if we are loading dock items for a configuration which has no dock
3311 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3312 mHotseat == null) {
3313 continue;
3314 }
3315
Sunny Goyal639e9062015-08-19 19:17:06 -07003316 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 switch (item.itemType) {
3318 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3319 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003320 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003321 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003322 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003324 }
3325 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003326 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003327 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003328 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003330 }
3331 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
3332 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
3333 if (mIsSafeModeEnabled) {
3334 view = new PendingAppWidgetHostView(this, info, mIconCache, true);
3335 } else {
3336 LauncherAppWidgetProviderInfo providerInfo =
3337 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
3338 if (providerInfo == null) {
3339 deleteWidgetInfo(info);
3340 continue;
3341 }
3342 view = mAppWidgetHost.createView(this, info.appWidgetId, providerInfo);
3343 }
3344 prepareAppWidget((AppWidgetHostView) view, info);
3345 break;
3346 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003347 default:
3348 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003350
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003351 /*
3352 * Remove colliding items.
3353 */
3354 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3355 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3356 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3357 View v = cl.getChildAt(item.cellX, item.cellY);
3358 Object tag = v.getTag();
3359 String desc = "Collision while binding workspace item: " + item
3360 + ". Collides with " + tag;
3361 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3362 throw (new RuntimeException(desc));
3363 } else {
3364 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003365 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003366 continue;
3367 }
3368 }
3369 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303370 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003371 if (animateIcons) {
3372 // Animate all the applications up now
3373 view.setAlpha(0f);
3374 view.setScaleX(0f);
3375 view.setScaleY(0f);
3376 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003377 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003379 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003380
Winson Chung997a9232013-07-24 15:33:46 -07003381 if (animateIcons) {
3382 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003383 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003384 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003385 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003386 final Runnable startBounceAnimRunnable = new Runnable() {
3387 public void run() {
3388 anim.playTogether(bounceAnims);
3389 anim.start();
3390 }
3391 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003392 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003393 // We post the animation slightly delayed to prevent slowdowns
3394 // when we are loading right after we return to launcher.
3395 mWorkspace.postDelayed(new Runnable() {
3396 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003397 if (mWorkspace != null) {
3398 mWorkspace.snapToPage(newScreenIndex);
3399 mWorkspace.postDelayed(startBounceAnimRunnable,
3400 NEW_APPS_ANIMATION_DELAY);
3401 }
Winson Chung94d67682013-09-25 16:29:40 -07003402 }
3403 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003404 } else {
3405 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003406 }
3407 }
Winson Chung64359a52013-07-08 17:17:08 -07003408 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003410 }
3411
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 /**
3413 * Add the views for a widget to the workspace.
3414 *
3415 * Implementation of the method from LauncherModel.Callbacks.
3416 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003417 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003418 Runnable r = new Runnable() {
3419 public void run() {
3420 bindAppWidget(item);
3421 }
3422 };
3423 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003424 return;
3425 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003426
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003427 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303428 PendingAppWidgetHostView view =
3429 new PendingAppWidgetHostView(this, item, mIconCache, true);
3430 prepareAppWidget(view, item);
3431 mWorkspace.addInScreen(view, item);
3432 mWorkspace.requestLayout();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003433 return;
3434 }
3435
Daniel Sandler843e8602010-06-07 14:59:01 -04003436 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3437 if (DEBUG_WIDGETS) {
3438 Log.d(TAG, "bindAppWidget: " + item);
3439 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003441 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003442
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003443 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3444 // If the provider is not ready, bind as a pending widget.
3445 appWidgetInfo = null;
3446 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3447 // The widget id is not valid. Try to find the widget based on the provider info.
3448 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3449 } else {
3450 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3451 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003452
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003453 // If the provider is ready, but the width is not yet restored, try to restore it.
3454 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3455 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003456 if (appWidgetInfo == null) {
3457 if (DEBUG_WIDGETS) {
3458 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3459 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003460 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003461 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003462 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003463 return;
3464 }
Sunny Goyalff572272014-07-23 13:58:07 -07003465
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003466 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003467 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003468 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3469 // Id has not been allocated yet. Allocate a new id.
3470 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3471 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003472
Sunny Goyald478c832016-04-01 12:04:16 -07003473 // Also try to bind the widget. If the bind fails, the user will be shown
3474 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003475 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003476 pendingInfo.spanX = item.spanX;
3477 pendingInfo.spanY = item.spanY;
3478 pendingInfo.minSpanX = item.minSpanX;
3479 pendingInfo.minSpanY = item.minSpanY;
3480 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003481
3482 boolean isDirectConfig =
3483 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3484 if (isDirectConfig && item.bindOptions != null) {
3485 Bundle newOptions = item.bindOptions.getExtras();
3486 if (options != null) {
3487 newOptions.putAll(options);
3488 }
3489 options = newOptions;
3490 }
Sunny Goyald478c832016-04-01 12:04:16 -07003491 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3492 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003493
Sunny Goyal86df1382016-08-10 15:03:22 -07003494 // We tried to bind once. If we were not able to bind, we would need to
3495 // go through the permission dialog, which means we cannot skip the config
3496 // activity.
3497 item.bindOptions = null;
3498 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3499
Sunny Goyald478c832016-04-01 12:04:16 -07003500 // Bind succeeded
3501 if (success) {
3502 // If the widget has a configure activity, it is still needs to set it up,
3503 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003504 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003505 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3506 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003507 }
Sunny Goyald478c832016-04-01 12:04:16 -07003508
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003509 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003510 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003511 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003512 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003513 // The widget was marked as UI not ready, but there is no configure activity to
3514 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003515 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003516 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003517 }
Sunny Goyalff572272014-07-23 13:58:07 -07003518 }
3519
Sunny Goyald5462aa2016-11-22 16:52:39 +05303520 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003521 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003522 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003523 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3524 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003525 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003526
Sunny Goyal56c73602015-09-25 12:55:01 -07003527 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003528 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003529 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003530 deleteWidgetInfo(item);
3531 return;
3532 }
3533
Sunny Goyal233ee962015-08-03 13:05:01 -07003534 item.minSpanX = appWidgetInfo.minSpanX;
3535 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303536 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003537 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303538 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003539 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303540 prepareAppWidget(view, item);
3541 mWorkspace.addInScreen(view, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003542 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543
Daniel Sandler843e8602010-06-07 14:59:01 -04003544 if (DEBUG_WIDGETS) {
3545 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3546 + (SystemClock.uptimeMillis()-start) + "ms");
3547 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 }
3549
Sunny Goyalff572272014-07-23 13:58:07 -07003550 /**
3551 * Restores a pending widget.
3552 *
3553 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003554 */
Sunny Goyald478c832016-04-01 12:04:16 -07003555 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003556 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3557 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3558 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003559 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003560 }
3561
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003562 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003563 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003564
3565 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003566 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003567 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003568 }
3569
Adam Cohen1462de32012-07-24 22:34:36 -07003570 public void onPageBoundSynchronously(int page) {
3571 mSynchronouslyBoundPages.add(page);
3572 }
3573
Sunny Goyal527c7d32015-08-28 15:19:36 -07003574 @Override
3575 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3576 if (mPendingExecutor != null) {
3577 mPendingExecutor.markCompleted();
3578 }
3579 mPendingExecutor = executor;
3580 executor.attachTo(this);
3581 }
3582
3583 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3584 if (mPendingExecutor == executor) {
3585 mPendingExecutor = null;
3586 }
3587 }
3588
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003589 @Override
3590 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3591 Runnable r = new Runnable() {
3592 public void run() {
3593 finishFirstPageBind(executor);
3594 }
3595 };
3596 if (waitUntilResume(r)) {
3597 return;
3598 }
3599
3600 Runnable onComplete = new Runnable() {
3601 @Override
3602 public void run() {
3603 if (executor != null) {
3604 executor.onLoadAnimationCompleted();
3605 }
3606 }
3607 };
3608 if (mDragLayer.getAlpha() < 1) {
3609 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3610 } else {
3611 onComplete.run();
3612 }
3613 }
3614
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 /**
3616 * Callback saying that there aren't any more items to bind.
3617 *
3618 * Implementation of the method from LauncherModel.Callbacks.
3619 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003620 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003621 Runnable r = new Runnable() {
3622 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003623 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003624 }
3625 };
3626 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003627 return;
3628 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003629 if (LauncherAppState.PROFILE_STARTUP) {
3630 Trace.beginSection("Page bind completed");
3631 }
Adam Cohen1462de32012-07-24 22:34:36 -07003632 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003634 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003635
Sunny Goyal2100c782016-08-22 16:00:03 -07003636 if (mPendingActivityResult != null) {
3637 handleActivityResult(mPendingActivityResult.requestCode,
3638 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3639 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003640 }
3641
Sunny Goyal756adbc2015-04-16 15:20:51 -07003642 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003643
Tony Wickham010d2552017-01-20 08:15:28 -08003644 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3645
Adam Cohen9211d422014-10-07 18:14:53 -07003646 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003647 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003648 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003649 if (LauncherAppState.PROFILE_STARTUP) {
3650 Trace.endSection();
3651 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003652 }
3653
Winson Chunga2413752012-04-03 14:22:34 -07003654 private boolean canRunNewAppsAnimation() {
3655 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003656 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003657 }
3658
Winson Chungc9168342013-06-26 14:54:55 -07003659 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003660 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003661 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3662 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003663 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003664 return bounceAnim;
3665 }
3666
Adam Cohen27772732013-11-11 14:00:56 +00003667 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003668 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003669 }
3670
Tony Wickham55616cd2015-09-23 14:55:17 -07003671 public int getSearchBarHeight() {
3672 if (mLauncherCallbacks != null) {
3673 return mLauncherCallbacks.getSearchBarHeight();
3674 }
3675 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3676 }
3677
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003678 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003679 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3680 * multiple calls to bind the same list.)
3681 */
3682 @Thunk ArrayList<AppInfo> mTmpAppsList;
3683 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3684 public void run() {
3685 bindAllApplications(mTmpAppsList);
3686 mTmpAppsList = null;
3687 }
3688 };
3689
3690 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 * Add the icons for all apps.
3692 *
3693 * Implementation of the method from LauncherModel.Callbacks.
3694 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003695 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003696 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3697 mTmpAppsList = apps;
3698 return;
3699 }
3700
Winson Chungb745afb2015-03-02 11:51:23 -08003701 if (mAppsView != null) {
3702 mAppsView.setApps(apps);
3703 }
Adam Cohen9211d422014-10-07 18:14:53 -07003704 if (mLauncherCallbacks != null) {
3705 mLauncherCallbacks.bindAllApplications(apps);
3706 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 }
3708
3709 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003710 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3711 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3712 */
3713 @Override
3714 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003715 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003716 }
3717
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003718 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 * A package was updated.
3720 *
3721 * Implementation of the method from LauncherModel.Callbacks.
3722 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003723 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003724 Runnable r = new Runnable() {
3725 public void run() {
3726 bindAppsUpdated(apps);
3727 }
3728 };
3729 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003730 return;
3731 }
3732
Winson Chungb745afb2015-03-02 11:51:23 -08003733 if (mAppsView != null) {
3734 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 }
3737
Sunny Goyal4390ace2014-10-13 11:33:11 -07003738 @Override
3739 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3740 Runnable r = new Runnable() {
3741 public void run() {
3742 bindWidgetsRestored(widgets);
3743 }
3744 };
3745 if (waitUntilResume(r)) {
3746 return;
3747 }
3748 mWorkspace.widgetsRestored(widgets);
3749 }
3750
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003752 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003753 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003754 *
3755 * @param updated list of shortcuts which have changed.
3756 * @param removed list of shortcuts which were deleted in the background. This can happen when
3757 * an app gets removed from the system or some of its components are no longer
3758 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003759 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003760 @Override
3761 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003762 final ArrayList<ShortcutInfo> removed, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003763 Runnable r = new Runnable() {
3764 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003765 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003766 }
3767 };
3768 if (waitUntilResume(r)) {
3769 return;
3770 }
3771
Sunny Goyal4390ace2014-10-13 11:33:11 -07003772 if (!updated.isEmpty()) {
3773 mWorkspace.updateShortcuts(updated);
3774 }
3775
3776 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003777 HashSet<ComponentName> removedComponents = new HashSet<>();
3778 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3779
Sunny Goyal4390ace2014-10-13 11:33:11 -07003780 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003781 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08003782 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003783 } else {
3784 removedComponents.add(si.getTargetComponent());
3785 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003786 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003787
3788 if (!removedComponents.isEmpty()) {
3789 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3790 mWorkspace.removeItemsByMatcher(matcher);
3791 mDragController.onAppsRemoved(matcher);
3792 }
3793
3794 if (!removedDeepShortcuts.isEmpty()) {
3795 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3796 mWorkspace.removeItemsByMatcher(matcher);
3797 mDragController.onAppsRemoved(matcher);
3798 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003799 }
3800 }
3801
3802 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003803 * Update the state of a package, typically related to install state.
3804 *
3805 * Implementation of the method from LauncherModel.Callbacks.
3806 */
Sunny Goyale755d462014-07-22 13:48:29 -07003807 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003808 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3809 Runnable r = new Runnable() {
3810 public void run() {
3811 bindRestoreItemsChange(updates);
3812 }
3813 };
3814 if (waitUntilResume(r)) {
3815 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003816 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003817
Sunny Goyal756adbc2015-04-16 15:20:51 -07003818 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003819 }
3820
3821 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003822 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003823 * in addition to specific applications to remove, the reason being that
3824 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003825 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003826 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003827 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003828 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003829 public void bindWorkspaceComponentsRemoved(
3830 final HashSet<String> packageNames, final HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003831 final UserHandle user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003832 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003833 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003834 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003835 }
Winson Chungd64d1762013-08-20 14:37:16 -07003836 };
3837 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003838 return;
3839 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003840 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003841 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3842 mWorkspace.removeItemsByMatcher(matcher);
3843 mDragController.onAppsRemoved(matcher);
3844
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003845 }
3846 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003847 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3848 mWorkspace.removeItemsByMatcher(matcher);
3849 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003850 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003851 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003852
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003853 @Override
3854 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3855 Runnable r = new Runnable() {
3856 public void run() {
3857 bindAppInfosRemoved(appInfos);
3858 }
3859 };
3860 if (waitUntilResume(r)) {
3861 return;
Joe Onorato36115782010-06-17 13:28:48 -04003862 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003863
Winson Chungdf95eb12013-10-16 14:57:07 -07003864 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003865 if (mAppsView != null) {
3866 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003867 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003868 }
Joe Onoratobe386092009-11-17 17:32:16 -08003869
Sunny Goyald164b7f2016-10-12 20:49:31 -07003870 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003871 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003872 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003873 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003874 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003875
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003876 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003877 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3878 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3879 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003880 return;
3881 }
3882
Sunny Goyald164b7f2016-10-12 20:49:31 -07003883 if (mWidgetsView != null && allWidgets != null) {
3884 mWidgetsView.setWidgets(allWidgets);
3885 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003886 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003887 }
3888
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003889 @Override
3890 public void notifyWidgetProvidersChanged() {
3891 if (mWorkspace.getState().shouldUpdateWidget) {
3892 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3893 }
3894 }
3895
Winson Chung4b919f82012-05-01 10:44:08 -07003896 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003897 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003898 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003899 }
3900 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003901
Winson Chung4b919f82012-05-01 10:44:08 -07003902 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003903 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003904 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003905 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003906 } else {
3907 mHandler.postDelayed(new Runnable() {
3908 public void run() {
3909 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3910 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003911 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003912 }
Winson Chung4b919f82012-05-01 10:44:08 -07003913 }
Winson Chung400438b2011-01-16 17:53:48 -08003914 }
3915
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003916 private void markAppsViewShown() {
3917 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3918 return;
3919 }
3920 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3921 }
3922
3923 private boolean shouldShowDiscoveryBounce() {
3924 if (mState != mState.WORKSPACE) {
3925 return false;
3926 }
3927 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3928 return true;
3929 }
3930 if (!mIsResumeFromActionScreenOff) {
3931 return false;
3932 }
3933 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3934 return false;
3935 }
3936 return true;
3937 }
3938
Winson Chung5ffd51d2015-06-25 11:36:50 -07003939 protected void moveWorkspaceToDefaultScreen() {
3940 mWorkspace.moveToDefaultScreen(false);
3941 }
3942
Winson Chung80baf5a2010-08-09 16:03:15 -07003943 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003944 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003945 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003946 @Override
3947 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3948 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003949
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003950 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3951 writer.println(prefix + "Workspace Items");
3952 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
3953 writer.println(prefix + " Homescreen " + i);
3954
3955 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3956 for (int j = 0; j < layout.getChildCount(); j++) {
3957 Object tag = layout.getChildAt(j).getTag();
3958 if (tag != null) {
3959 writer.println(prefix + " " + tag.toString());
3960 }
3961 }
3962 }
3963
3964 writer.println(prefix + " Hotseat");
3965 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003966 for (int j = 0; j < layout.getChildCount(); j++) {
3967 Object tag = layout.getChildAt(j).getTag();
3968 if (tag != null) {
3969 writer.println(prefix + " " + tag.toString());
3970 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003971 }
Sunny Goyala1365452015-10-01 15:46:24 -07003972
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003973 try {
3974 FileLog.flushAll(writer);
3975 } catch (Exception e) {
3976 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003977 }
3978 }
3979
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003980 writer.println(prefix + "Misc:");
3981 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3982 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3983 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3984
3985 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003986
Adam Cohen9211d422014-10-07 18:14:53 -07003987 if (mLauncherCallbacks != null) {
3988 mLauncherCallbacks.dump(prefix, fd, writer, args);
3989 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003990 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003991
Sunny Goyal66b24572016-09-21 15:57:55 -07003992 @Override
3993 @TargetApi(Build.VERSION_CODES.N)
3994 public void onProvideKeyboardShortcuts(
3995 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3996
3997 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3998 if (mState == State.WORKSPACE) {
3999 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4000 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4001 }
4002 View currentFocus = getCurrentFocus();
4003 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4004 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4005 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4006 }
4007 if (currentFocus instanceof BubbleTextView &&
4008 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4009 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4010 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4011 }
4012 if (!shortcutInfos.isEmpty()) {
4013 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4014 }
4015
4016 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4017 }
4018
4019 @Override
4020 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4021 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4022 switch (keyCode) {
4023 case KeyEvent.KEYCODE_A:
4024 if (mState == State.WORKSPACE) {
4025 showAppsView(true, true, false);
4026 return true;
4027 }
4028 break;
4029 case KeyEvent.KEYCODE_S: {
4030 View focusedView = getCurrentFocus();
4031 if (focusedView instanceof BubbleTextView
4032 && focusedView.getTag() instanceof ItemInfo
4033 && mAccessibilityDelegate.performAction(focusedView,
4034 (ItemInfo) focusedView.getTag(),
4035 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08004036 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004037 return true;
4038 }
4039 break;
4040 }
4041 case KeyEvent.KEYCODE_O:
4042 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4043 return true;
4044 }
4045 break;
4046 }
4047 }
4048 return super.onKeyShortcut(keyCode, event);
4049 }
4050
Adam Cohen59400422014-03-05 18:07:04 -08004051 public static CustomAppWidget getCustomAppWidget(String name) {
4052 return sCustomAppWidgets.get(name);
4053 }
4054
4055 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4056 return sCustomAppWidgets;
4057 }
4058
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004059 public static Launcher getLauncher(Context context) {
4060 if (context instanceof Launcher) {
4061 return (Launcher) context;
4062 }
4063 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4064 }
4065
Sunny Goyal745bad92016-05-02 10:54:12 -07004066 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4067
4068 @Override
4069 public void onSharedPreferenceChanged(
4070 SharedPreferences sharedPreferences, String key) {
4071 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4072 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4073 if (!waitUntilResume(this, true)) {
4074 run();
4075 }
4076 }
4077 }
4078
4079 @Override
4080 public void run() {
4081 setOrientation();
4082 }
4083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004084}