blob: 8492a7985cd29dc0329b6b969b99cd67c819fe60 [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;
Jon Miranda2d89ea82017-05-04 11:47:53 -070021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070027import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080030import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080033import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080034import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040035import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070037import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040038import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070040import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070041import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070043import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070046import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070047import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070050import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020051import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080054import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070055import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040056import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070057import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070058import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070059import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.text.Selection;
61import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070062import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070064import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.HapticFeedbackConstants;
67import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070068import android.view.KeyboardShortcutGroup;
69import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080070import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080074import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.ViewGroup;
76import android.view.ViewTreeObserver;
Mario Bertschleracbf5702017-03-03 10:58:06 -080077import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080078import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070079import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070080import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070083
Sunny Goyal651077b2014-06-30 14:15:31 -070084import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070085import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070086import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070087import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070088import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070089import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalb5e65c82016-10-26 18:32:38 -070090import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyalffe83f12014-08-14 17:39:34 -070091import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010092import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070093import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080094import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070095import com.android.launcher3.dragndrop.DragController;
96import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070097import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070098import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -080099import com.android.launcher3.dragndrop.PinItemDragListener;
Tony Wickham827cef22016-03-17 15:39:39 -0700100import com.android.launcher3.dynamicui.ExtractedColors;
Mario Bertschler27288382017-05-24 15:35:09 -0700101import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000102import com.android.launcher3.folder.Folder;
103import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700104import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700105import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700106import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700107import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800108import com.android.launcher3.model.ModelWriter;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700109import com.android.launcher3.model.PackageItemInfo;
110import com.android.launcher3.model.WidgetItem;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700112import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800114import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700116import com.android.launcher3.shortcuts.ShortcutKey;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800117import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530118import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
119import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700121import com.android.launcher3.util.ActivityResultInfo;
Sunny Goyal49fc3f72017-08-18 10:17:06 -0700122import com.android.launcher3.util.RunnableWithId;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700124import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700125import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700129import com.android.launcher3.util.SystemUiController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700130import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700131import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700132import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700133import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800134import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700135import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800136import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700137import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700139
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700140import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700141import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700142import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700143import java.util.Collection;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700144import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700145import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700146import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800147import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700148import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700149
Sunny Goyal49fc3f72017-08-18 10:17:06 -0700150import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
151import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153/**
154 * Default launcher application.
155 */
Sunny Goyal27835952017-01-13 12:15:53 -0800156public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700157 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
158 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700159 AccessibilityManager.AccessibilityStateChangeListener,
160 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700161 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700162 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700164 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700165 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400166 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700170
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700171 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700172 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Michael Jurka8b805b12012-04-18 14:23:14 -0700174 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700175 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
176 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700177
Jon Mirandac476d6e2017-05-04 16:30:01 -0700178 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700179
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700180 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
181
Mathew Inwood876a8462013-06-14 14:12:41 +0100182 /**
183 * IntentStarter uses request codes starting with this. This must be greater than all activity
184 * request codes used internally.
185 */
186 protected static final int REQUEST_LAST = 100;
187
Mario Bertschleracbf5702017-03-03 10:58:06 -0800188 private static final int SOFT_INPUT_MODE_DEFAULT =
Jon Mirandafd516322017-03-09 13:14:50 -0800189 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800190 private static final int SOFT_INPUT_MODE_ALL_APPS =
Jon Mirandafd516322017-03-09 13:14:50 -0800191 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800192
Winson Chung2672ff92012-05-04 16:22:30 -0700193 // The Intent extra that defines whether to ignore the launch animation
194 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400195 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 // Type: int
198 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700199 // Type: int
200 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700201 // Type: PendingRequestArgs
202 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
203 // Type: ActivityResultInfo
204 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700206 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800207
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700209 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
210 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700211
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700215 private boolean mIsSafeModeEnabled;
216
Adam Cohen4f8071b2016-02-13 16:06:05 -0800217 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700218 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Winson Chunga2413752012-04-03 14:22:34 -0700220 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700221 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
222 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
223 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700224
Sunny Goyal60820d72017-05-09 12:40:11 -0700225 private final ExtractedColors mExtractedColors = new ExtractedColors();
226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700229 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800230 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700231
232 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Sunny Goyalffe83f12014-08-14 17:39:34 -0700234 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700235 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700237 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700238
Sunny Goyal316490e2015-06-02 09:38:28 -0700239 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800240 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700241
Michael Jurka838a4ca2011-02-07 13:33:06 -0800242 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700243 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700244
Sunny Goyal47328fd2016-05-25 18:56:41 -0700245 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700246
247 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700248 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700249 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700250
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700251 // Main container view and the model for the widget tray screen.
252 @Thunk WidgetsContainerView mWidgetsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Winson Chung4a2afa32012-07-19 14:53:05 -0700254 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
255 // scroll issues (because the workspace may not have been measured yet) and extra work.
256 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
257 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
259 private SpannableStringBuilder mDefaultKeySsb = null;
260
Adam Cohen091440a2015-03-18 14:16:05 -0700261 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800263 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700264 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700266 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
267 private final ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700268 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700269
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800271 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800272 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700273 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700274 private final Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700275 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700276 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Jon Miranda2d89ea82017-05-04 11:47:53 -0700278 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700279 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700280
Tony Wickham010d2552017-01-20 08:15:28 -0800281 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700282
Adam Cohen61f560d2013-09-30 15:58:20 -0700283 private View.OnTouchListener mHapticFeedbackTouchListener;
284
Winson Chung400438b2011-01-16 17:53:48 -0800285 // Determines how long to wait after a rotation before restoring the screen orientation to
286 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700287 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800288
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700289 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700290
Winson Chung46353de2012-02-16 14:05:10 -0800291 // We only want to get the SharedPreferences once since it does an FS stat each time we get
292 // it from the context.
293 private SharedPreferences mSharedPrefs;
294
Adam Cohen4b66bf32015-09-18 12:15:19 -0700295 private boolean mMoveToDefaultScreenFromNewIntent;
296
Winson Chung13eb5272015-05-11 16:30:13 -0700297 // This is set to the view that launched the activity that navigated the user away from
298 // launcher. Since there is no callback for when the activity has finished launching, enable
299 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800300 private BubbleTextView mWaitingForResume;
301
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700302 protected static final HashMap<String, CustomAppWidget> sCustomAppWidgets =
303 new HashMap<>();
Adam Cohen59400422014-03-05 18:07:04 -0800304
Adam Cohen59400422014-03-05 18:07:04 -0800305 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700306 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
307 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800308 }
309 }
310
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700311 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
312 // state transition. If another state transition happened during this delay,
313 // simply unregister this runnable.
314 private Runnable mExitSpringLoadedModeRunnable;
315
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700316 @Thunk final Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700317 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700318 if (mWorkspace != null) {
319 mWorkspace.buildPageHardwareLayers();
320 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700321 }
322 };
323
Sunny Goyal2100c782016-08-22 16:00:03 -0700324 // Activity result which needs to be processed after workspace has loaded.
325 private ActivityResultInfo mPendingActivityResult;
326 /**
327 * Holds extra information required to handle a result from an external call, like
328 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
329 */
330 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800331
Jon Miranda379198e2016-09-23 08:54:40 -0700332 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700333
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700334 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700335 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400336
337 @Thunk void setOrientation() {
338 if (mRotationEnabled) {
339 unlockScreenOrientation(true);
340 } else {
341 setRequestedOrientation(
342 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700343 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400344 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700345
Sunny Goyal745bad92016-05-02 10:54:12 -0700346 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 @Override
349 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700350 if (DEBUG_STRICT_MODE) {
351 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
352 .detectDiskReads()
353 .detectDiskWrites()
354 .detectNetwork() // or .detectAll() for all detectable problems
355 .penaltyLog()
356 .build());
357 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
358 .detectLeakedSqlLiteObjects()
359 .detectLeakedClosableObjects()
360 .penaltyLog()
361 .penaltyDeath()
362 .build());
363 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700364 if (LauncherAppState.PROFILE_STARTUP) {
365 Trace.beginSection("Launcher-onCreate");
366 }
Winson Chunga2413752012-04-03 14:22:34 -0700367
Adam Cohen9211d422014-10-07 18:14:53 -0700368 if (mLauncherCallbacks != null) {
369 mLauncherCallbacks.preOnCreate();
370 }
371
Mario Bertschler27288382017-05-24 15:35:09 -0700372 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
373 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700374 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400377
Sunny Goyal87f784c2017-01-11 10:48:34 -0800378 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700379
Adam Cohen2e6da152015-05-06 11:42:25 -0700380 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800381 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700382 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700383 Display display = getWindowManager().getDefaultDisplay();
384 Point mwSize = new Point();
385 display.getSize(mwSize);
386 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
387 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700388
Sunny Goyalf7258242015-10-19 16:59:07 -0700389 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700390 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800391 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800392 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800393 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700394 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700395
Joe Onorato41a12d22009-10-31 18:30:00 -0400396 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700397 mAllAppsController = new AllAppsTransitionController(this);
398 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700399
Sunny Goyalffe83f12014-08-14 17:39:34 -0700400 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700401
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700402 mAppWidgetHost = new LauncherAppWidgetHost(this);
403 if (Utilities.ATLEAST_MARSHMALLOW) {
404 mAppWidgetHost.addProviderChangeListener(this);
405 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700406 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700407
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700408 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
409 // this also ensures that any synchronous binding below doesn't re-trigger another
410 // LauncherModel load.
411 mPaused = false;
412
Sunny Goyal60820d72017-05-09 12:40:11 -0700413 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 setupViews();
Winson1f064272016-07-18 17:18:02 -0700416 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700417 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418
Tony Wickham010d2552017-01-20 08:15:28 -0800419 mPopupDataProvider = new PopupDataProvider(this);
420
Tony Wickhame2217252016-03-22 16:34:23 -0700421 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
422 .addAccessibilityStateChangeListener(this);
423
Joe Onorato7c312c12009-08-13 21:36:53 -0700424 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700425
Sunny Goyalfe770c92016-09-27 14:38:58 -0700426 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427
Sunny Goyale26d1002016-06-20 14:52:14 -0700428 if (LauncherAppState.PROFILE_STARTUP) {
429 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
431
Sunny Goyal2100c782016-08-22 16:00:03 -0700432 // We only load the page synchronously if the user rotates (or triggers a
433 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700434 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
435 if (savedInstanceState != null) {
436 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
437 }
438 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700439 // If we are not binding synchronously, show a fade in animation when
440 // the first page bind completes.
441 mDragLayer.setAlpha(0);
442 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700443 // Pages bound synchronously.
444 mWorkspace.setCurrentPage(currentScreen);
445
Sunny Goyal2100c782016-08-22 16:00:03 -0700446 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448
449 // For handling default keys
450 mDefaultKeySsb = new SpannableStringBuilder();
451 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800452
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800453 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700454 // In case we are on a device with locked rotation, we should look at preferences to check
455 // if the user has specifically allowed rotation.
456 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700457 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700458 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
459 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700460 }
461
Sunny Goyal5a81c382017-03-20 15:08:06 -0700462 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
463 // Temporarily enable the rotation
464 mRotationEnabled = true;
465 }
466
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700467 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
468 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400469 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700470
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800471 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700472
Jon Miranda7fda2852017-07-19 16:07:01 -0700473 // Listen for broadcasts
474 IntentFilter filter = new IntentFilter();
475 filter.addAction(Intent.ACTION_SCREEN_OFF);
476 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
477 registerReceiver(mReceiver, filter);
478 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700479
Sunny Goyal8392c822017-06-20 10:03:56 -0700480 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
481 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700482
483 if (mLauncherCallbacks != null) {
484 mLauncherCallbacks.onCreate(savedInstanceState);
485 }
Adam Cohen9211d422014-10-07 18:14:53 -0700486 }
487
Sunny Goyal7779d622015-06-11 16:18:39 -0700488 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700489 public void onThemeChanged() {
490 recreate();
491 }
492
Mario Bertschlera6936942017-05-31 14:48:19 -0700493 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700494 if (isDark) {
495 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700496 } else if (supportsDarkText) {
497 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700498 }
499 }
500
501 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700502 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800503 return mLauncherView.findViewById(id);
504 }
505
506 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700507 public void onExtractedColorsChanged() {
508 loadExtractedColorsAndColorItems();
Sunny Goyal60820d72017-05-09 12:40:11 -0700509 mExtractedColors.notifyChange();
Sunny Goyal5e7b2382017-04-25 23:09:06 -0700510 }
511
512 public ExtractedColors getExtractedColors() {
513 return mExtractedColors;
Tony Wickham827cef22016-03-17 15:39:39 -0700514 }
515
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700516 @Override
517 public void onAppWidgetHostReset() {
518 if (mAppWidgetHost != null) {
519 mAppWidgetHost.startListening();
520 }
521 }
522
Tony Wickham827cef22016-03-17 15:39:39 -0700523 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700524 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700525 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700526 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700527 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700528 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700529 }
530 }
531
Adam Cohen9211d422014-10-07 18:14:53 -0700532 private LauncherCallbacks mLauncherCallbacks;
533
534 public void onPostCreate(Bundle savedInstanceState) {
535 super.onPostCreate(savedInstanceState);
536 if (mLauncherCallbacks != null) {
537 mLauncherCallbacks.onPostCreate(savedInstanceState);
538 }
539 }
540
Winson1f064272016-07-18 17:18:02 -0700541 public void onInsetsChanged(Rect insets) {
542 mDeviceProfile.updateInsets(insets);
543 mDeviceProfile.layout(this, true /* notifyListeners */);
544 }
545
Sunny Goyal32554d12015-12-03 15:31:25 -0800546 /**
547 * Call this after onCreate to set or clear overlay.
548 */
549 public void setLauncherOverlay(LauncherOverlay overlay) {
550 if (overlay != null) {
551 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
552 }
553 mWorkspace.setLauncherOverlay(overlay);
554 }
555
Adam Cohen9211d422014-10-07 18:14:53 -0700556 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
557 mLauncherCallbacks = callbacks;
558 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700559 }
560
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700561 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700562 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700563 if (mLauncherCallbacks != null) {
564 mLauncherCallbacks.onLauncherProviderChange();
565 }
566 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700567
Adam Cohen9211d422014-10-07 18:14:53 -0700568 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700569 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700570 if (mLauncherCallbacks != null) {
571 return mLauncherCallbacks.hasCustomContentToLeft();
572 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700573 return false;
574 }
575
Dave Hawkeya8881582013-09-17 15:55:33 -0600576 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500577 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600578 * {@link #addToCustomContentPage}. This will only be invoked if
579 * {@link #hasCustomContentToLeft()} is {@code true}.
580 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500581 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700582 if (mLauncherCallbacks != null) {
583 mLauncherCallbacks.populateCustomContentContainer();
584 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600585 }
586
587 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700588 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600589 * ensure the custom content page is added or removed if necessary.
590 */
591 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600592 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600593 // Not bound yet, wait for bindScreens to be called.
594 return;
595 }
596
597 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
598 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 mWorkspace.createCustomContentContainer();
600 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
602 mWorkspace.removeCustomContentPage();
603 }
604 }
605
Hyunyoung Song3f471442015-04-08 19:01:34 -0700606 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700607 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
608 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700609 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700610 }
611
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000612 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700613 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
614 // This cast is safe as long as the id < 0x00FFFFFF
615 // Since we jail all the dynamically generated views, there should be no clashes
616 // with any other views.
617 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000618 }
619
Tony Wickham010d2552017-01-20 08:15:28 -0800620 public PopupDataProvider getPopupDataProvider() {
621 return mPopupDataProvider;
622 }
623
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000624 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700625 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
626 * a configuration step, this allows the proper animations to run after other transitions.
627 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700628 private long completeAdd(
629 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
630 long screenId = info.screenId;
631 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700632 // When the screen id represents an actual screen (as opposed to a rank) we make sure
633 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700634 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700635 }
Adam Cohendb364c32014-05-20 14:23:40 -0700636
Sunny Goyal2100c782016-08-22 16:00:03 -0700637 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800638 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700639 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700640 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800641 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700643 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700644 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700645 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700646 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700647 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 int widgetId = appWidgetId;
649 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700650 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700652 // Since the view was just bound, also launch the configure activity if needed
653 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
654 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800655 if (provider != null) {
656 new WidgetAddFlowHandler(provider)
657 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700658 }
659 }
660 break;
661 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700663
Adam Cohendb364c32014-05-20 14:23:40 -0700664 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800665 }
666
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800667 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700668 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 if (isWorkspaceLoading()) {
670 // process the result once the workspace has loaded.
671 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
672 return;
673 }
674 mPendingActivityResult = null;
675
Winson Chunge341d302013-08-16 14:31:00 -0700676 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 final PendingRequestArgs requestArgs = mPendingRequestArgs;
678 setWaitingForResult(null);
679 if (requestArgs == null) {
680 return;
681 }
682
683 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700684
Adam Cohenad4e15c2013-10-17 16:21:35 -0700685 Runnable exitSpringLoaded = new Runnable() {
686 @Override
687 public void run() {
688 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
689 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
690 }
691 };
692
Michael Jurka8b805b12012-04-18 14:23:14 -0700693 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700694 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700695 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700696 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
697 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700699 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700700 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700701 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700702 addAppWidgetImpl(
703 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800704 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700705 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 }
707 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200708 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
709 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700710 // User could have free-scrolled between pages before picking a wallpaper; make sure
711 // we move to the closest one now.
712 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700713 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200714 }
715 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200717
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700719 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700720
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 // We have special handling for widgets
722 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800723 final int appWidgetId;
724 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
725 : -1;
726 if (widgetId < 0) {
727 appWidgetId = pendingAddWidgetId;
728 } else {
729 appWidgetId = widgetId;
730 }
731
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800733 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700734 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
735 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700736 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700737 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700738 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 @Override
740 public void run() {
741 exitSpringLoadedDragModeDelayed(false, 0, null);
742 }
743 };
Adam Cohendb364c32014-05-20 14:23:40 -0700744
Sunny Goyal2100c782016-08-22 16:00:03 -0700745 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
746 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
747 } else {
748 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
749 // When the screen id represents an actual screen (as opposed to a rank)
750 // we make sure that the drop page actually exists.
751 requestArgs.screenId =
752 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700753 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700754 final CellLayout dropLayout =
755 mWorkspace.getScreenWithId(requestArgs.screenId);
756
757 dropLayout.setDropPending(true);
758 final Runnable onComplete = new Runnable() {
759 @Override
760 public void run() {
761 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
762 dropLayout.setDropPending(false);
763 }
764 };
765 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
766 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700767 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 return;
769 }
770
Sunny Goyald478c832016-04-01 12:04:16 -0700771 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
772 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700773 if (resultCode == RESULT_OK) {
774 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700776 }
777 // Leave the widget in the pending state if the user canceled the configure.
778 return;
779 }
780
Sunny Goyalaad90582015-07-09 14:22:50 -0700781 if (requestCode == REQUEST_CREATE_SHORTCUT) {
782 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
784 completeAdd(requestCode, data, -1, requestArgs);
785 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
786 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
787
Sunny Goyalaad90582015-07-09 14:22:50 -0700788 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700789 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
790 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800793 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800794 }
795
796 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700797 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800798 final int requestCode, final int resultCode, final Intent data) {
799 handleActivityResult(requestCode, resultCode, data);
800 if (mLauncherCallbacks != null) {
801 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
802 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 }
804
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600805 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700806 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600807 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700808 PendingRequestArgs pendingArgs = mPendingRequestArgs;
809 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
810 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
811 setWaitingForResult(null);
812
Sunny Goyal28c6b962015-10-12 11:42:05 -0700813 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700814 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700815 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700816 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700817 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700818 Intent intent = pendingArgs.getPendingIntent();
819
Sunny Goyal28c6b962015-10-12 11:42:05 -0700820 if (grantResults.length > 0
821 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700822 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700823 } else {
824 // TODO: Show a snack bar with link to settings
825 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700826 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700827 }
828 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600829 if (mLauncherCallbacks != null) {
830 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
831 grantResults);
832 }
833 }
834
Adam Cohendb364c32014-05-20 14:23:40 -0700835 /**
836 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
837 *
838 * @param screenId the screen id to check
839 * @return the new screen, or screenId if it exists
840 */
841 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800842 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700843 if (dropLayout == null) {
844 // it's possible that the add screen was removed because it was
845 // empty and a re-bind occurred
846 mWorkspace.addExtraEmptyScreen();
847 return mWorkspace.commitExtraEmptyScreen();
848 } else {
849 return screenId;
850 }
851 }
852
Sunny Goyal2100c782016-08-22 16:00:03 -0700853 @Thunk void completeTwoStageWidgetDrop(
854 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
855 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800856 Runnable onCompleteRunnable = null;
857 int animationType = 0;
858
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700859 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800860 if (resultCode == RESULT_OK) {
861 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
862 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800863 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700864 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 onCompleteRunnable = new Runnable() {
866 @Override
867 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700868 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700869 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
870 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 }
872 };
873 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800874 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700877 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700878 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700879 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
880 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700881 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700882 // The animated view may be null in the case of a rotation during widget configuration
883 onCompleteRunnable.run();
884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
887 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700888 protected void onStop() {
889 super.onStop();
890 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700891
892 if (mLauncherCallbacks != null) {
893 mLauncherCallbacks.onStop();
894 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700895
Sunny Goyalf5e37442016-11-02 10:31:24 -0700896 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700897 mAppWidgetHost.stopListening();
898 }
Tony Wickham010d2552017-01-20 08:15:28 -0800899
900 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700901 }
902
903 @Override
904 protected void onStart() {
905 super.onStart();
906 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700907
908 if (mLauncherCallbacks != null) {
909 mLauncherCallbacks.onStart();
910 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700911
Sunny Goyalf5e37442016-11-02 10:31:24 -0700912 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700913 mAppWidgetHost.startListening();
914 }
Tony Wickham010d2552017-01-20 08:15:28 -0800915
916 if (!isWorkspaceLoading()) {
917 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
918 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700919
Jon Miranda7fda2852017-07-19 16:07:01 -0700920 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700921 if (mScrimAnimator != null) {
922 mScrimAnimator.cancel();
923 }
924 mDragLayer.getBackground().setAlpha(0);
925 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
926 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
927 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
928 @Override
929 public void onAnimationEnd(Animator animation) {
930 mScrimAnimator = null;
931 }
932 });
933 mScrimAnimator.setDuration(600);
934 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
935 mScrimAnimator.start();
936 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700937 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700938 }
939
940 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200942 long startTime = 0;
943 if (DEBUG_RESUME_TIME) {
944 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400945 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200946 }
Adam Cohen9211d422014-10-07 18:14:53 -0700947
948 if (mLauncherCallbacks != null) {
949 mLauncherCallbacks.preOnResume();
950 }
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700953 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700954
Winson Chung4a2afa32012-07-19 14:53:05 -0700955 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700956 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700957 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800958 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700959 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700960 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700961 // view after launching an app, as they may be depending on the UI to be static to
962 // switch to another app, otherwise, if it was
Sunny Goyal5606e072017-06-15 14:24:21 -0700963 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800964 } else if (mOnResumeState == State.WIDGETS) {
965 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700966 }
Jon Miranda7653a602017-07-28 10:09:34 -0700967 if (mOnResumeState != State.APPS) {
968 tryAndUpdatePredictedApps();
969 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700970 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 Goyala474a9b2017-05-04 16:47:11 -07001039 // Process any items that were added while Launcher was away.
1040 InstallShortcutReceiver.disableAndFlushInstallQueue(
1041 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001042
Sunny Goyala474a9b2017-05-04 16:47:11 -07001043 // Refresh shortcuts if the permission changed.
1044 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -07001045
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001046 if (shouldShowDiscoveryBounce()) {
1047 mAllAppsController.showDiscoveryBounce();
1048 }
1049 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001050 if (mLauncherCallbacks != null) {
1051 mLauncherCallbacks.onResume();
1052 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001053
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
Sunny Goyala474a9b2017-05-04 16:47:11 -07001059 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -07001060
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.
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001080 void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001081
1082 // Custom content is completely hidden
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001083 void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001084
1085 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001086 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 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001097 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -07001098
1099 /**
1100 * Touch interaction related to overscroll has ended
1101 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001102 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103
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 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001108 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 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001114 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001115 }
1116
1117 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001118 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001119 }
1120
1121 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1122
Sunny Goyalc86df472016-02-25 09:19:38 -08001123 public void onScrollChanged(float progress) {
1124 if (mWorkspace != null) {
1125 mWorkspace.onOverlayScrollChanged(progress);
1126 }
1127 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128 }
1129
Jorim Jaggid017f882014-01-14 17:08:48 -08001130 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001131 if (mLauncherCallbacks != null) {
1132 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001133 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -08001134 // On O and above we there is always some setting present settings (add icon to
1135 // home screen or icon badging). On earlier APIs we will have the allow rotation
1136 // setting, on devices with a locked orientation,
1137 return Utilities.isAtLeastO() || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001138 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001139 }
1140
Adam Cohen9211d422014-10-07 18:14:53 -07001141 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001142 CustomContentCallbacks callbacks, String description) {
1143 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001144 }
1145
Adam Cohenedb40762013-07-18 16:45:45 -07001146 // The custom content needs to offset its content to account for the QSB
1147 public int getTopOffsetForCustomContent() {
1148 return mWorkspace.getPaddingTop();
1149 }
1150
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001151 @Override
1152 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001154 if (mModel.isCurrentCallbacks(this)) {
1155 mModel.stopLoader();
1156 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001157 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1158
Romain Guy13c2e7b2010-03-10 19:45:00 -08001159 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001160 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001161
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001162 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001163 @Override
1164 public void onWindowFocusChanged(boolean hasFocus) {
1165 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001166 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001167
1168 if (mLauncherCallbacks != null) {
1169 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1170 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001171 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 private boolean acceptFilter() {
1174 final InputMethodManager inputManager = (InputMethodManager)
1175 getSystemService(Context.INPUT_METHOD_SERVICE);
1176 return !inputManager.isFullscreenMode();
1177 }
1178
1179 @Override
1180 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001181 final int uniChar = event.getUnicodeChar();
1182 final boolean handled = super.onKeyDown(keyCode, event);
1183 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1184 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1186 keyCode, event);
1187 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001188 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001189 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001190 // showSearchDialog()
1191 // If there are multiple keystrokes before the search dialog takes focus,
1192 // onSearchRequested() will be called for every keystroke,
1193 // but it is idempotent, so it's fine.
1194 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 }
1196 }
1197
Joe Onorato8a9625e2010-01-28 15:55:35 -08001198 // Eat the long press event so the keyboard doesn't come up.
1199 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1200 return true;
1201 }
1202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 return handled;
1204 }
1205
Winson46163472015-09-22 17:31:56 -07001206 @Override
1207 public boolean onKeyUp(int keyCode, KeyEvent event) {
1208 if (keyCode == KeyEvent.KEYCODE_MENU) {
1209 // Ignore the menu key if we are currently dragging or are on the custom content screen
1210 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001211 // Close any open floating view
1212 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001213
Winson46163472015-09-22 17:31:56 -07001214 // Stop resizing any widgets
1215 mWorkspace.exitWidgetResizeMode();
1216
1217 // Show the overview mode if we are on the workspace
1218 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1219 !mWorkspace.isSwitchingState()) {
1220 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001221 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001222 }
1223 }
1224 return true;
1225 }
1226 return super.onKeyUp(keyCode, event);
1227 }
1228
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 private String getTypedText() {
1230 return mDefaultKeySsb.toString();
1231 }
1232
Sunny Goyal6f28e712016-09-13 14:19:29 -07001233 @Override
1234 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001235 mDefaultKeySsb.clear();
1236 mDefaultKeySsb.clearSpans();
1237 Selection.setSelection(mDefaultKeySsb, 0);
1238 }
1239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 /**
1241 * Restores the previous state, if it exists.
1242 *
1243 * @param savedState The previous state.
1244 */
1245 private void restoreState(Bundle savedState) {
1246 if (savedState == null) {
1247 return;
1248 }
1249
Sunny Goyalfe770c92016-09-27 14:38:58 -07001250 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1251 State[] stateValues = State.values();
1252 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1253 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001254 if (state == State.APPS || state == State.WIDGETS) {
1255 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001256 }
1257
Sunny Goyal2100c782016-08-22 16:00:03 -07001258 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1259 if (requestArgs != null) {
1260 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001262
1263 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265
1266 /**
1267 * Finds all the views we need and configure them properly.
1268 */
1269 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001270 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001271 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001272 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001273 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001274
Sunny Goyal784f9c32016-03-23 16:56:54 -07001275 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1276 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1277 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278
Winson Chung4c98d922011-05-31 16:50:48 -07001279 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001280 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001281
Winson Chung3d503fb2011-07-13 17:25:49 -07001282 // Setup the hotseat
1283 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1284 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001285 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001286 }
1287
Winsone9f27272015-10-13 10:47:51 -07001288 // Setup the overview panel
1289 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001290
Winson Chung4c98d922011-05-31 16:50:48 -07001291 // Setup the workspace
1292 mWorkspace.setHapticFeedbackEnabled(false);
1293 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001294 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001295 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1296 // default state, otherwise we will update to the wrong offsets in RTL
1297 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001298 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001299 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001300
Tony Wickham34d2c912015-09-11 09:27:58 -07001301 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001302 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001303
Winson Chungef7f8742015-06-04 17:18:17 -07001304 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001305 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001306 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001307
Winson Chung3d503fb2011-07-13 17:25:49 -07001308 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001309 mDragController.setMoveTarget(mWorkspace);
1310 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001311 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001312
Hyunyoung Songd725f642017-08-17 22:26:35 -07001313 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001314
Sunny Goyal322d5562015-06-25 19:35:49 -07001315 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1316 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
Winsone9f27272015-10-13 10:47:51 -07001320 private void setupOverviewPanel() {
1321 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1322
Winsone9f27272015-10-13 10:47:51 -07001323 // Bind wallpaper button actions
1324 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301325 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001326 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001327 public void handleViewClick(View view) {
1328 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001329 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001330 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001331 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1332
1333 // Bind widget button actions
1334 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301335 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001336 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001337 public void handleViewClick(View view) {
1338 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001339 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001340 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001341 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1342
1343 // Bind settings actions
1344 View settingsButton = findViewById(R.id.settings_button);
1345 boolean hasSettings = hasSettings();
1346 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301347 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001348 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001349 public void handleViewClick(View view) {
1350 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001351 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001352 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001353 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1354 } else {
1355 settingsButton.setVisibility(View.GONE);
1356 }
1357
1358 mOverviewPanel.setAlpha(0f);
1359 }
1360
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001362 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001363 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001364 */
1365 public void setAllAppsButton(View allAppsButton) {
1366 mAllAppsButton = allAppsButton;
1367 }
1368
Sunny Goyalbb011da2016-06-15 15:42:29 -07001369 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001370 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001371 }
1372
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001373 public View getWidgetsButton() {
1374 return mWidgetsButton;
1375 }
1376
Anjali Koppal5ad44842014-03-10 20:34:39 -07001377 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 * Creates a view representing a shortcut.
1379 *
1380 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001382 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001383 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
1386 /**
1387 * Creates a view representing a shortcut inflated from the specified resource.
1388 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 * @param parent The group the shortcut belongs to.
1390 * @param info The data structure describing the shortcut.
1391 *
1392 * @return A View inflated from layoutResId.
1393 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001394 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001395 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1396 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001397 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001399 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 return favorite;
1401 }
1402
1403 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001404 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 *
1406 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001408 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001409 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001410 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1411 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001412 return;
1413 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001414
Jon Mirandac476d6e2017-05-04 16:30:01 -07001415 int[] cellXY = mTmpAddItemCellCoordinates;
1416 CellLayout layout = getCellLayout(container, screenId);
1417
Sunny Goyal782f0c92017-01-19 10:27:54 -08001418 ShortcutInfo info = null;
1419 if (Utilities.isAtLeastO()) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001420 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1421 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001422 }
1423
1424 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001425 // Legacy shortcuts are only supported for primary profile.
1426 info = Process.myUserHandle().equals(args.user)
1427 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001428
Sunny Goyalb57645f2017-03-20 13:57:28 -07001429 if (info == null) {
1430 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1431 return;
1432 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001433 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001434 // The app is trying to add a shortcut without sufficient permissions
1435 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1436 return;
1437 }
1438 }
1439
Jon Mirandac476d6e2017-05-04 16:30:01 -07001440 if (container < 0) {
1441 // Adding a shortcut to the Workspace.
1442 final View view = createShortcut(info);
1443 boolean foundCellSpan = false;
1444 // First we check if we already know the exact location where we want to add this item.
1445 if (cellX >= 0 && cellY >= 0) {
1446 cellXY[0] = cellX;
1447 cellXY[1] = cellY;
1448 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001449
Jon Mirandac476d6e2017-05-04 16:30:01 -07001450 // If appropriate, either create a folder or add to an existing folder
1451 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1452 true, null, null)) {
1453 return;
1454 }
1455 DragObject dragObject = new DragObject();
1456 dragObject.dragInfo = info;
1457 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1458 true)) {
1459 return;
1460 }
1461 } else {
1462 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1463 }
1464
1465 if (!foundCellSpan) {
1466 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001467 return;
1468 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001469
1470 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1471 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001472 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001473 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001474 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001475 if (folderIcon != null) {
1476 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1477 folderInfo.add(info, args.rank, false);
1478 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001479 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001480 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
Sunny Goyale29897f2017-07-20 10:09:42 -07001484 public FolderIcon findFolderIcon(final long folderIconId) {
1485 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1486 @Override
1487 public boolean evaluate(ItemInfo info, View view) {
1488 return info != null && info.id == folderIconId;
1489 }
1490 });
1491 }
1492
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001494 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001496 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001498 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001499 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1500
Adam Cohened66b2b2012-01-23 17:28:51 -08001501 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001502 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001503 }
Romain Guycbb89e42009-06-08 15:52:54 -07001504
Adam Cohen59400422014-03-05 18:07:04 -08001505 if (appWidgetInfo.isCustomWidget) {
1506 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001507 }
1508
Adam Cohen59400422014-03-05 18:07:04 -08001509 LauncherAppWidgetInfo launcherInfo;
1510 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001511 launcherInfo.spanX = itemInfo.spanX;
1512 launcherInfo.spanY = itemInfo.spanY;
1513 launcherInfo.minSpanX = itemInfo.minSpanX;
1514 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyala52ecb02016-12-16 15:04:51 -08001515 launcherInfo.user = appWidgetInfo.getUser();
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001517 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001518 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519
Sunny Goyal2100c782016-08-22 16:00:03 -07001520 if (hostView == null) {
1521 // Perform actual inflation because we're live
1522 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001524 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301525 prepareAppWidget(hostView, launcherInfo);
1526 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 }
Romain Guycbb89e42009-06-08 15:52:54 -07001528
Sunny Goyald5462aa2016-11-22 16:52:39 +05301529 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001530 hostView.setTag(item);
1531 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001532 hostView.setFocusable(true);
1533 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001534 }
1535
Adam Cohended9f8d2010-11-03 13:25:16 -07001536 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1537 @Override
1538 public void onReceive(Context context, Intent intent) {
1539 final String action = intent.getAction();
1540 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001541 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001542
Winson Chungc100e8e2011-08-09 16:02:43 -07001543 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001544 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001545 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001546 if (!showWorkspace(false)) {
1547 // If we are already on the workspace, then manually reset all apps
1548 mAppsView.reset();
1549 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001550 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001551 mIsResumeFromActionScreenOff = true;
Jon Miranda7fda2852017-07-19 16:07:01 -07001552 mShouldFadeInScrim = true;
1553 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1554 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1555 // the user unlocked and the Launcher is not in the foreground.
1556 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001557 }
1558 }
1559 };
1560
Tony Wickham010d2552017-01-20 08:15:28 -08001561 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1562 Runnable r = new Runnable() {
1563 @Override
1564 public void run() {
1565 mWorkspace.updateIconBadges(updatedBadges);
1566 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001567
1568 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1569 if (popup != null) {
1570 popup.updateNotificationHeader(updatedBadges);
1571 }
Tony Wickham010d2552017-01-20 08:15:28 -08001572 }
1573 };
1574 if (!waitUntilResume(r)) {
1575 r.run();
1576 }
1577 }
1578
Adam Cohended9f8d2010-11-03 13:25:16 -07001579 @Override
1580 public void onAttachedToWindow() {
1581 super.onAttachedToWindow();
1582
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001583 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001584 if (mLauncherCallbacks != null) {
1585 mLauncherCallbacks.onAttachedToWindow();
1586 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001587 }
1588
1589 @Override
1590 public void onDetachedFromWindow() {
1591 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001592
1593 if (mLauncherCallbacks != null) {
1594 mLauncherCallbacks.onDetachedFromWindow();
1595 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001596 }
1597
1598 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001599 // The following code used to be in onResume, but it turns out onResume is called when
1600 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1601 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001602 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001603 if (!mWorkspaceLoading) {
1604 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001605 // We want to let Launcher draw itself at least once before we force it to build
1606 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001607 // apps is nice and speedy.
1608 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001609 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001610 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001611 if (mStarted) return;
1612 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001613 // We delay the layer building a bit in order to give
1614 // other message processing a time to run. In particular
1615 // this avoids a delay in hiding the IME if it was
1616 // currently shown, because doing that may involve
1617 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001618 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001619 final ViewTreeObserver.OnDrawListener listener = this;
1620 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001621 public void run() {
1622 if (mWorkspace != null &&
1623 mWorkspace.getViewTreeObserver() != null) {
1624 mWorkspace.getViewTreeObserver().
1625 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001626 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001627 }
1628 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001629 }
1630 });
1631 }
1632 clearTypedText();
1633 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 }
1635
Winson Chunga6945242014-01-08 14:04:34 -08001636 public DragLayer getDragLayer() {
1637 return mDragLayer;
1638 }
1639
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001640 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001641 return mAppsView;
1642 }
1643
Hyunyoung Song3f471442015-04-08 19:01:34 -07001644 public WidgetsContainerView getWidgetsView() {
1645 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001646 }
1647
Winson Chunga6945242014-01-08 14:04:34 -08001648 public Workspace getWorkspace() {
1649 return mWorkspace;
1650 }
1651
1652 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) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001743 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001744 }
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 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001755 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001756
Adam Cohen9211d422014-10-07 18:14:53 -07001757 if (mLauncherCallbacks != null) {
1758 mLauncherCallbacks.onNewIntent(intent);
1759 }
Winson15f8b172015-08-19 11:02:39 -07001760
1761 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1762 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1763 // animation.
1764 if (isActionMain) {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001765 boolean callbackAllowsMoveToDefaultScreen =
1766 mLauncherCallbacks == null || mLauncherCallbacks
1767 .shouldMoveToDefaultScreenOnHomeIntent();
Sunny Goyal85313732016-09-28 12:00:07 -07001768 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1769 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001770
1771 // We use this flag to suppress noisy callbacks above custom content state
1772 // from onResume.
1773 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001774 mWorkspace.post(new Runnable() {
1775 @Override
1776 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001777 if (mWorkspace != null) {
1778 mWorkspace.moveToDefaultScreen(true);
1779 }
Winson15f8b172015-08-19 11:02:39 -07001780 }
1781 });
1782 }
1783 }
1784
1785 if (DEBUG_RESUME_TIME) {
1786 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1787 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001788 }
1789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001791 public void onRestoreInstanceState(Bundle state) {
1792 super.onRestoreInstanceState(state);
1793 for (int page: mSynchronouslyBoundPages) {
1794 mWorkspace.restoreInstanceStateForChild(page);
1795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797
1798 @Override
1799 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001800 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001801 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1802 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001803
Adam Cohen21cd0022013-10-09 18:57:02 -07001804 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001805 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806
Michael Jurka883f55b2010-10-21 15:47:14 -07001807 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001808 // We close any open folders and shortcut containers since they will not be re-opened,
1809 // and we need to make sure this state is reflected.
1810 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001811
Sunny Goyal2100c782016-08-22 16:00:03 -07001812 if (mPendingRequestArgs != null) {
1813 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1814 }
1815 if (mPendingActivityResult != null) {
1816 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818
Adam Cohen9211d422014-10-07 18:14:53 -07001819 if (mLauncherCallbacks != null) {
1820 mLauncherCallbacks.onSaveInstanceState(outState);
1821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 }
1823
1824 @Override
1825 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001827
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001828 unregisterReceiver(mReceiver);
Michael Jurka9d906c72011-10-14 06:25:36 -07001829 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001830 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001831
Winson Chungcd2b0142011-06-08 16:02:26 -07001832 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001833 // It's possible to receive onDestroy after a new Launcher activity has
1834 // been created. In this case, don't interfere with the new Launcher.
1835 if (mModel.isCurrentCallbacks(this)) {
1836 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001837 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001838 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001839
Sunny Goyal745bad92016-05-02 10:54:12 -07001840 if (mRotationPrefChangeHandler != null) {
1841 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1842 }
1843
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001845 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001847 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001849 mAppWidgetHost = null;
1850
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 TextKeyListener.getInstance().release();
1852
Tony Wickhame2217252016-03-22 16:34:23 -07001853 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1854 .removeAccessibilityStateChangeListener(this);
1855
Mario Bertschler27288382017-05-24 15:35:09 -07001856 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1857
Michael Jurka2ecf9952012-06-18 12:52:28 -07001858 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001859
Tony2917a8b2017-07-31 23:29:42 -07001860 clearPendingBinds();
1861
Adam Cohen9211d422014-10-07 18:14:53 -07001862 if (mLauncherCallbacks != null) {
1863 mLauncherCallbacks.onDestroy();
1864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866
Sunny Goyalae502842016-06-17 08:43:56 -07001867 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1868 return mAccessibilityDelegate;
1869 }
1870
Adam Cohena9cf38f2011-05-02 15:36:58 -07001871 public DragController getDragController() {
1872 return mDragController;
1873 }
1874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001876 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001877 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001878 }
1879
1880 @Override
1881 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1882 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001883 try {
1884 super.startIntentSenderForResult(intent, requestCode,
1885 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1886 } catch (IntentSender.SendIntentException e) {
1887 throw new ActivityNotFoundException();
1888 }
1889 }
1890
Winson Chung70d72102011-08-12 11:24:35 -07001891 /**
1892 * Indicates that we want global search for this activity by setting the globalSearch
1893 * argument for {@link #startSearch} to true.
1894 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001896 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001898
Karl Rosaen138a0412009-04-23 19:00:21 -07001899 if (initialQuery == null) {
1900 // Use any text typed in the launcher as the initial query
1901 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 if (appSearchData == null) {
1904 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001905 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001907
Sunny Goyal6f28e712016-09-13 14:19:29 -07001908 if (mLauncherCallbacks == null ||
1909 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1910 // Starting search from the callbacks failed. Start the default global search.
1911 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001912 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001913
1914 // We need to show the workspace after starting the search
1915 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001916 }
1917
Ian Parkinson047036e2014-09-01 15:40:53 +01001918 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001919 * Starts the global search activity. This code is a copied from SearchManager
1920 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001921 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001922 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001923 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001924 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1925 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1926 if (globalSearchActivity == null) {
1927 Log.w(TAG, "No global search activity found.");
1928 return;
1929 }
1930 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1931 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1932 intent.setComponent(globalSearchActivity);
1933 // Make sure that we have a Bundle to put source in
1934 if (appSearchData == null) {
1935 appSearchData = new Bundle();
1936 } else {
1937 appSearchData = new Bundle(appSearchData);
1938 }
Adam Cohen9211d422014-10-07 18:14:53 -07001939 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001940 if (!appSearchData.containsKey("source")) {
1941 appSearchData.putString("source", getPackageName());
1942 }
1943 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1944 if (!TextUtils.isEmpty(initialQuery)) {
1945 intent.putExtra(SearchManager.QUERY, initialQuery);
1946 }
1947 if (selectInitialQuery) {
1948 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1949 }
1950 intent.setSourceBounds(sourceBounds);
1951 try {
1952 startActivity(intent);
1953 } catch (ActivityNotFoundException ex) {
1954 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957
Yura4f93ec62014-02-04 14:15:21 +00001958 public boolean isOnCustomContent() {
1959 return mWorkspace.isOnOrMovingToCustomContent();
1960 }
1961
Winson Chung70d72102011-08-12 11:24:35 -07001962 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001963 public boolean onPrepareOptionsMenu(Menu menu) {
1964 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001965 if (mLauncherCallbacks != null) {
1966 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1967 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001968 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001971 @Override
1972 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001973 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001974 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001975 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001976 }
1977
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001979 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001980 }
1981
Adam Cohen517a7f52014-03-01 12:12:59 -08001982 public boolean isWorkspaceLoading() {
1983 return mWorkspaceLoading;
1984 }
1985
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001986 private void setWorkspaceLoading(boolean value) {
1987 boolean isLocked = isWorkspaceLocked();
1988 mWorkspaceLoading = value;
1989 if (isLocked != isWorkspaceLocked()) {
1990 onWorkspaceLockedChanged();
1991 }
1992 }
1993
Sunny Goyal04a324a2017-01-20 21:08:59 -08001994 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001995 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001996 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001997 if (isLocked != isWorkspaceLocked()) {
1998 onWorkspaceLockedChanged();
1999 }
2000 }
2001
Adam Cohen9211d422014-10-07 18:14:53 -07002002 protected void onWorkspaceLockedChanged() {
2003 if (mLauncherCallbacks != null) {
2004 mLauncherCallbacks.onWorkspaceLockedChanged();
2005 }
2006 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002007
Sunny Goyal2100c782016-08-22 16:00:03 -07002008 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002009 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002010 if (LOGD) {
2011 Log.d(TAG, "Adding widget from drop");
2012 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002013 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002014 }
2015
Sunny Goyal2100c782016-08-22 16:00:03 -07002016 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002017 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
2018 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
2019 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07002020
Adam Cohenad4e15c2013-10-17 16:21:35 -07002021 Runnable onComplete = new Runnable() {
2022 @Override
2023 public void run() {
2024 // Exit spring loaded mode if necessary after adding the widget
2025 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2026 null);
2027 }
2028 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08002029 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07002030 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 }
2032 }
Romain Guycbb89e42009-06-08 15:52:54 -07002033
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002034 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002035 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002036 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002037 mWorkspace.moveToCustomContentScreen(animate);
2038 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002039
2040 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2041 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002042 info.container = container;
2043 info.screenId = screenId;
2044 if (cell != null) {
2045 info.cellX = cell[0];
2046 info.cellY = cell[1];
2047 }
2048 info.spanX = spanX;
2049 info.spanY = spanY;
2050
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002051 switch (info.itemType) {
2052 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2053 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002054 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002055 break;
2056 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08002057 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002058 break;
2059 default:
2060 throw new IllegalStateException("Unknown item type: " + info.itemType);
2061 }
2062 }
2063
Adam Cohenfbba09b2011-07-18 21:43:05 -07002064 /**
2065 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002066 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08002067 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002068 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2069 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08002070 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
2071 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
2072 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002073 }
2074
Adam Cohenfbba09b2011-07-18 21:43:05 -07002075 /**
2076 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002077 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002078 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002079 AppWidgetHostView hostView = info.boundWidget;
2080 int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08002081 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08002082 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002083 // In the case where we've prebound the widget, we remove it from the DragLayer
2084 if (LOGD) {
2085 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2086 }
2087 getDragLayer().removeView(hostView);
2088
Adam Cohened66b2b2012-01-23 17:28:51 -08002089 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08002090 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002091
2092 // Clear the boundWidget so that it doesn't get destroyed.
2093 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002094 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002095 // In this case, we either need to start an activity to get permission to bind
2096 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002097 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002098 Bundle options = info.bindOptions;
2099
Sunny Goyalffe83f12014-08-14 17:39:34 -07002100 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2101 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002102 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002103 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07002104 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002105 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07002106 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002107 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002108 }
2109
Adam Cohendcd297f2013-06-18 13:13:40 -07002110 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002111 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002112 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 folderInfo.title = getText(R.string.folder_name);
2114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002116 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117
2118 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002119 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302120 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07002121 // Force measure the new folder icon
2122 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2123 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002124 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
Romain Guycbb89e42009-06-08 15:52:54 -07002126
Winsonc0b52fe2015-09-09 16:38:15 -07002127 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002128 * Unbinds the view for the specified item, and removes the item and all its children.
2129 *
2130 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002131 * @param itemInfo the {@link ItemInfo} for this view.
2132 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002133 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002134 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002135 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002136 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002137 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2138 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002139 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002140 } else {
2141 mWorkspace.removeWorkspaceItem(v);
2142 }
Winsonc0b52fe2015-09-09 16:38:15 -07002143 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002144 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002145 }
2146 } else if (itemInfo instanceof FolderInfo) {
2147 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002148 if (v instanceof FolderIcon) {
2149 ((FolderIcon) v).removeListeners();
2150 }
Winsonc0b52fe2015-09-09 16:38:15 -07002151 mWorkspace.removeWorkspaceItem(v);
2152 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002153 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002154 }
2155 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2156 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002157 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002158 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002159 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002160 }
2161 } else {
2162 return false;
2163 }
2164 return true;
2165 }
2166
2167 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002168 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002169 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002170 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002171 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002172 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002173 // Deleting an app widget ID is a void call but writes to disk before returning
2174 // to the caller...
2175 new AsyncTask<Void, Void, Void>() {
2176 public Void doInBackground(Void ... args) {
2177 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2178 return null;
2179 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002180 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002181 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002182 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002183 }
2184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 @Override
2186 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002187 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
2189
Joe Onorato88ec0992009-11-19 13:16:06 -08002190 @Override
2191 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002192 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2193 return;
2194 }
2195
Sunny Goyal45478022015-06-08 16:52:41 -07002196 if (mDragController.isDragging()) {
2197 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002198 return;
2199 }
2200
Jon Mirandafeba90f2016-10-06 10:53:29 -07002201 // Note: There should be at most one log per method call. This is enforced implicitly
2202 // by using if-else statements.
2203 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002204 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2205 if (topView != null) {
2206 if (topView.getActiveTextView() != null) {
2207 topView.getActiveTextView().dispatchBackKey();
2208 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002209 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302210 ued.logActionCommand(Action.Command.BACK,
2211 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002212 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302213 ued.logActionCommand(Action.Command.BACK,
2214 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002215 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002216 topView.close(true);
2217 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002218 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302219 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002220 showWorkspace(true);
2221 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302222 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002223 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002224 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302225 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002226 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002227 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002228 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002229 mWorkspace.exitWidgetResizeMode();
2230
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002231 // Back button is a no-op here, but give at least some feedback for the button press
2232 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002233 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002234 }
2235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 * Launches the intent referred by the clicked shortcut.
2238 *
2239 * @param v The view representing the clicked shortcut.
2240 */
2241 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002242 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2243 // view has detached (it's possible for this to happen if the view is removed mid touch).
2244 if (v.getWindowToken() == null) {
2245 return;
2246 }
2247
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002248 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002249 return;
2250 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002251
Adam Cohen1697b792013-09-17 19:08:21 -07002252 if (v instanceof Workspace) {
2253 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08002254 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2255 LauncherLogProto.Action.Direction.NONE,
2256 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002257 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002258 }
2259 return;
2260 }
2261
2262 if (v instanceof CellLayout) {
2263 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002264 int page = mWorkspace.indexOfChild(v);
2265 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2266 LauncherLogProto.Action.Direction.NONE,
2267 LauncherLogProto.ContainerType.OVERVIEW, page);
2268 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002269 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002270 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002271 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002272 }
2273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002275 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002276 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002278 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002279 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002280 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07002281 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002282 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002283 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002284 } else if (tag instanceof AppInfo) {
2285 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002286 } else if (tag instanceof LauncherAppWidgetInfo) {
2287 if (v instanceof PendingAppWidgetHostView) {
2288 onClickPendingWidget((PendingAppWidgetHostView) v);
2289 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 }
2291 }
2292
Sunny Goyal70660032015-05-14 00:07:08 -07002293 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002294 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002295 return false;
2296 }
2297
Michael Jurkaaf442092010-06-10 17:01:57 -07002298 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002299 * Event handler for the app widget view which has not fully restored.
2300 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002301 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002302 if (mIsSafeModeEnabled) {
2303 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2304 return;
2305 }
2306
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002307 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002308 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002309 LauncherAppWidgetProviderInfo appWidgetInfo =
2310 mAppWidgetManager.findProvider(info.providerName, info.user);
2311 if (appWidgetInfo == null) {
2312 return;
2313 }
2314 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2315
Sunny Goyald478c832016-04-01 12:04:16 -07002316 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2317 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2318 // This should not happen, as we make sure that an Id is allocated during bind.
2319 return;
2320 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002321 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2322 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002323 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002324 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002325 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002326 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002327 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002328 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002329 }
2330 }
2331
2332 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07002333 * Event handler for the "grid" button or "caret" that appears on the home screen, which
2334 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
2335 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002336 *
2337 * @param v The view that was clicked.
2338 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002339 protected void onClickAllAppsButton(View v) {
2340 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002341 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302342 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2343 ControlType.ALL_APPS_BUTTON);
Sunny Goyal5606e072017-06-15 14:24:21 -07002344 showAppsView(true /* animated */, true /* updatePredictedApps */);
Adam Cohen5441a9d2017-07-14 14:22:05 -07002345 } else {
2346 showWorkspace(true);
2347 }
2348 }
2349
Sunny Goyale6e72002017-01-12 16:55:36 -08002350 private void onClickPendingAppItem(final View v, final String packageName,
2351 boolean downloadStarted) {
2352 if (downloadStarted) {
2353 // If the download has started, simply direct to the market app.
2354 startMarketIntentForPackage(v, packageName);
2355 return;
2356 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002357 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002358 .setTitle(R.string.abandoned_promises_title)
2359 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002360 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2361 @Override
2362 public void onClick(DialogInterface dialogInterface, int i) {
2363 startMarketIntentForPackage(v, packageName);
2364 }
2365 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002366 .setNeutralButton(R.string.abandoned_clean_this,
2367 new DialogInterface.OnClickListener() {
2368 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002369 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002370 mWorkspace.removeAbandonedPromise(packageName, user);
2371 }
2372 })
2373 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002374 }
2375
2376 private void startMarketIntentForPackage(View v, String packageName) {
2377 ItemInfo item = (ItemInfo) v.getTag();
2378 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2379 boolean success = startActivitySafely(v, intent, item);
2380 if (success && v instanceof BubbleTextView) {
2381 mWaitingForResume = (BubbleTextView) v;
2382 mWaitingForResume.setStayPressed(true);
2383 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002384 }
2385
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002386 /**
2387 * Event handler for an app shortcut click.
2388 *
2389 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2390 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002391 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002392 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2393 Object tag = v.getTag();
2394 if (!(tag instanceof ShortcutInfo)) {
2395 throw new IllegalArgumentException("Input must be a Shortcut");
2396 }
2397
2398 // Open shortcut
2399 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002400
2401 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002402 if ((shortcut.isDisabled &
2403 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2404 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2405 // If the app is only disabled because of the above flags, launch activity anyway.
2406 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002407 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002408 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2409 // Use a message specific to this shortcut, if it has one.
2410 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2411 return;
2412 }
2413 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002414 int error = R.string.activity_not_available;
2415 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2416 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002417 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2418 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002419 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002420 }
2421 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2422 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002423 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002424 }
2425
Chris Wren40c5ed32014-06-24 18:24:23 -04002426 // Check for abandoned promise
Sunny Goyale6e72002017-01-12 16:55:36 -08002427 if ((v instanceof BubbleTextView) && shortcut.isPromise()) {
2428 String packageName = shortcut.intent.getComponent() != null ?
2429 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2430 if (!TextUtils.isEmpty(packageName)) {
2431 onClickPendingAppItem(v, packageName,
2432 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2433 return;
2434 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002435 }
2436
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002437 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002438 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002439 }
2440
Sunny Goyala7ce1662016-05-31 15:01:35 -07002441 private void startAppShortcutOrInfoActivity(View v) {
2442 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002443 Intent intent;
2444 if (item instanceof PromiseAppInfo) {
2445 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2446 intent = promiseAppInfo.getMarketIntent();
2447 } else {
2448 intent = item.getIntent();
2449 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002450 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);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002454 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
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);
Tony85efb4b2017-06-02 13:06:10 -07002510 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2511 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002512 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));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002535 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002536 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002537 }
2538
Adam Cohen61f560d2013-09-30 15:58:20 -07002539 public View.OnTouchListener getHapticFeedbackTouchListener() {
2540 if (mHapticFeedbackTouchListener == null) {
2541 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002542 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002543 @Override
2544 public boolean onTouch(View v, MotionEvent event) {
2545 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2546 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2547 }
2548 return false;
2549 }
2550 };
2551 }
2552 return mHapticFeedbackTouchListener;
2553 }
2554
Tony Wickhame2217252016-03-22 16:34:23 -07002555 @Override
2556 public void onAccessibilityStateChanged(boolean enabled) {
2557 mDragLayer.onAccessibilityStateChanged(enabled);
2558 }
2559
Sunny Goyal06e21a22016-08-11 16:02:02 -07002560 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002561 if (isOnCustomContent()) {
2562 // Custom content screen doesn't participate in drag and drop. If on custom
2563 // content screen, move to default.
2564 moveWorkspaceToDefaultScreen();
2565 }
Adam Cohen9211d422014-10-07 18:14:53 -07002566 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002567
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002568 /**
2569 * Called when the user stops interacting with the launcher.
2570 * This implies that the user is now on the homescreen and is not doing housekeeping.
2571 */
Adam Cohen9211d422014-10-07 18:14:53 -07002572 protected void onInteractionEnd() {
2573 if (mLauncherCallbacks != null) {
2574 mLauncherCallbacks.onInteractionEnd();
2575 }
2576 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002577
2578 /**
2579 * Called when the user starts interacting with the launcher.
2580 * The possible interactions are:
2581 * - open all apps
2582 * - reorder an app shortcut, or a widget
2583 * - open the overview mode.
2584 * This is a good time to stop doing things that only make sense
2585 * when the user is on the homescreen and not doing housekeeping.
2586 */
Adam Cohen9211d422014-10-07 18:14:53 -07002587 protected void onInteractionBegin() {
2588 if (mLauncherCallbacks != null) {
2589 mLauncherCallbacks.onInteractionBegin();
2590 }
2591 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002592
Winson Chungcd99cd32015-04-29 11:03:24 -07002593 /** Updates the interaction state. */
2594 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002595 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002596 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002597 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2598 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002599 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002600 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002601 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002602 onInteractionEnd();
2603 }
2604 }
2605
Sunny Goyala7ce1662016-05-31 15:01:35 -07002606 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002608 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2609 try {
2610 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2611 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2612 // is enabled by default on NYC.
2613 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2614 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002615
2616 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2617 String id = ((ShortcutInfo) info).getDeepShortcutId();
2618 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302619 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002620 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002621 } else {
2622 // Could be launching some bookkeeping activity
2623 startActivity(intent, optsBundle);
2624 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002625 } finally {
2626 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002629 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2630 // corresponding permission. Show the appropriate permission prompt if that
2631 // is the case.
2632 if (intent.getComponent() == null
2633 && Intent.ACTION_CALL.equals(intent.getAction())
2634 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2635 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002636
2637 setWaitingForResult(PendingRequestArgs
2638 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002639 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2640 REQUEST_PERMISSION_CALL_PHONE);
2641 } else {
2642 // No idea why this was thrown.
2643 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645 }
2646 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002647
Sunny Goyalfe770c92016-09-27 14:38:58 -07002648 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002649 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002650 if (Utilities.ATLEAST_MARSHMALLOW) {
2651 int left = 0, top = 0;
2652 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002653 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002654 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002655 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002656 if (icon != null) {
2657 Rect bounds = icon.getBounds();
2658 left = (width - bounds.width()) / 2;
2659 top = v.getPaddingTop();
2660 width = bounds.width();
2661 height = bounds.height();
2662 }
2663 }
2664 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2665 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2666 // On L devices, we use the device default slide-up transition.
2667 // On L MR1 devices, we use a custom version of the slide-up transition which
2668 // doesn't have the delay present in the device default.
2669 return ActivityOptions.makeCustomAnimation(
2670 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2671 }
2672 return null;
2673 }
2674
Tonye3c59252017-05-02 21:32:27 -07002675 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002676 int[] pos = new int[2];
2677 v.getLocationOnScreen(pos);
2678 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2679 }
2680
Sunny Goyala7ce1662016-05-31 15:01:35 -07002681 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002682 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2683 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2684 return false;
2685 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002686 // Only launch using the new animation if the shortcut has not opted out (this is a
2687 // private contract between launcher and may be ignored in the future).
2688 boolean useLaunchAnimation = (v != null) &&
2689 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2690 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2691
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002692 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002693
2694 // Prepare intent
2695 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2696 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002697 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002698 }
2699 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002700 if (Utilities.ATLEAST_MARSHMALLOW
2701 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002702 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002703 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002704 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002705 // Shortcuts need some special checks due to legacy reasons.
2706 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002707 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002708 // Could be launching some bookkeeping activity
2709 startActivity(intent, optsBundle);
2710 } else {
2711 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2712 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2713 }
2714 return true;
2715 } catch (ActivityNotFoundException|SecurityException e) {
2716 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2717 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2718 }
2719 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002720 }
2721
Tony Wickhamdadb3042016-02-24 11:07:00 -08002722 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002723 public boolean dispatchTouchEvent(MotionEvent ev) {
2724 mLastDispatchTouchEventX = ev.getX();
2725 return super.dispatchTouchEvent(ev);
2726 }
2727
2728 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002729 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002730 if (!isDraggingEnabled()) return false;
2731 if (isWorkspaceLocked()) return false;
2732 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002733
Jon Miranda51f037d2016-11-07 08:37:20 -08002734 boolean ignoreLongPressToOverview =
2735 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002736
Adam Cohen1697b792013-09-17 19:08:21 -07002737 if (v instanceof Workspace) {
2738 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002739 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302740 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2741 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002742 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002743 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002744 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2745 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2746 return true;
2747 } else {
2748 return false;
2749 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002750 } else {
2751 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002752 }
Adam Cohen1697b792013-09-17 19:08:21 -07002753 }
2754
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002755 CellLayout.CellInfo longClickCellInfo = null;
2756 View itemUnderLongClick = null;
2757 if (v.getTag() instanceof ItemInfo) {
2758 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002759 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002760 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002761 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 }
2763
Winson Chung3d503fb2011-07-13 17:25:49 -07002764 // The hotseat touch handling does not go through Workspace, and we always allow long press
2765 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002766 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002767 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002768 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002769 if (mWorkspace.isInOverviewMode()) {
2770 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302771 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2772 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002773 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002774 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002775 return false;
2776 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302777 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2778 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002779 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002780 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002781 }
Jon Miranda379198e2016-09-23 08:54:40 -07002782 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2783 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002785 final boolean isAllAppsButton =
2786 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2787 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2788 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002789 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002791 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 }
2793 }
2794 }
2795 return true;
2796 }
2797
Winson Chung3d503fb2011-07-13 17:25:49 -07002798 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002799 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002800 return mHotseat != null && layout != null &&
2801 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2802 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002803
Winson Chung3d503fb2011-07-13 17:25:49 -07002804 /**
2805 * Returns the CellLayout of the specified container at the specified screen.
2806 */
Sunny Goyal92820592015-03-02 11:31:35 -08002807 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002808 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2809 if (mHotseat != null) {
2810 return mHotseat.getLayout();
2811 } else {
2812 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002813 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002814 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002815 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002816 }
2817 }
2818
Winson Chungb745afb2015-03-02 11:51:23 -08002819 /**
2820 * For overridden classes.
2821 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002822 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002823 return isAppsViewVisible();
2824 }
2825
2826 public boolean isAppsViewVisible() {
2827 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2828 }
2829
2830 public boolean isWidgetsViewVisible() {
2831 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002832 }
2833
Michael Jurkae326f182011-11-21 14:05:46 -08002834 @Override
2835 public void onTrimMemory(int level) {
2836 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002837 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2838 // The widget preview db can result in holding onto over
2839 // 3MB of memory for caching which isn't necessary.
2840 SQLiteDatabase.releaseMemory();
2841
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002842 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002843 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002844 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002845 if (mLauncherCallbacks != null) {
2846 mLauncherCallbacks.onTrimMemory(level);
2847 }
Michael Jurkae326f182011-11-21 14:05:46 -08002848 }
2849
Winson5c6bdbb2015-09-03 11:36:19 -07002850 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002851 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002852 }
2853
Winson5c6bdbb2015-09-03 11:36:19 -07002854 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002855 boolean changed = mState != State.WORKSPACE ||
2856 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002857 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002858 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002859 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002860 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002861
Michael Jurkab3e22d92011-10-31 15:58:33 -07002862 // Set focus to the AppsCustomize button
2863 if (mAllAppsButton != null) {
2864 mAllAppsButton.requestFocus();
2865 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002866 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002867
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002868 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002869 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002870
Winson Chung0f785722015-04-08 10:27:49 -07002871 if (changed) {
2872 // Send an accessibility event to announce the context change
2873 getWindow().getDecorView()
2874 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002875 }
Winson5c6bdbb2015-09-03 11:36:19 -07002876 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002877 }
2878
Winsone9f27272015-10-13 10:47:51 -07002879 /**
2880 * Shows the overview button.
2881 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002882 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002883 showOverviewMode(animated, false);
2884 }
2885
2886 /**
2887 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2888 * onto one of the overview panel buttons.
2889 */
2890 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2891 Runnable postAnimRunnable = null;
2892 if (requestButtonFocus) {
2893 postAnimRunnable = new Runnable() {
2894 @Override
2895 public void run() {
2896 // Hitting the menu button when in touch mode does not trigger touch mode to
2897 // be disabled, so if requested, force focus on one of the overview panel
2898 // buttons.
2899 mOverviewPanel.requestFocusFromTouch();
2900 }
2901 };
2902 }
Adam Cohened307df2013-10-02 09:37:31 -07002903 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002904 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002905 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002906 setState(State.WORKSPACE);
2907
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002908 // If animated from long press, then don't allow any of the controller in the drag
2909 // layer to intercept any remaining touch.
2910 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002911 }
2912
Mario Bertschleracbf5702017-03-03 10:58:06 -08002913 private void setState(State state) {
2914 this.mState = state;
2915 updateSoftInputMode();
2916 }
2917
2918 private void updateSoftInputMode() {
2919 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2920 final int mode;
2921 if (isAppsViewVisible()) {
2922 mode = SOFT_INPUT_MODE_ALL_APPS;
2923 } else {
2924 mode = SOFT_INPUT_MODE_DEFAULT;
2925 }
2926 getWindow().setSoftInputMode(mode);
2927 }
2928 }
2929
Winson Chungb745afb2015-03-02 11:51:23 -08002930 /**
2931 * Shows the apps view.
2932 */
Sunny Goyal5606e072017-06-15 14:24:21 -07002933 public void showAppsView(boolean animated, boolean updatePredictedApps) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002934 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002935 if (updatePredictedApps) {
2936 tryAndUpdatePredictedApps();
2937 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002938 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002939 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002940
Winson Chungb745afb2015-03-02 11:51:23 -08002941 /**
2942 * Shows the widgets view.
2943 */
2944 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002945 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002946 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002947 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002948 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002949 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002950
2951 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002952 @Override
2953 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002954 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002955 }
2956 });
Winson Chungb745afb2015-03-02 11:51:23 -08002957 }
2958
2959 /**
2960 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002961 *
2962 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002963 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002964 // TODO: calling method should use the return value so that when {@code false} is returned
2965 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002966 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002967 if (!(mState == State.WORKSPACE ||
2968 mState == State.APPS_SPRING_LOADED ||
2969 mState == State.WIDGETS_SPRING_LOADED ||
2970 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002971 return false;
2972 }
2973 if (toState != State.APPS && toState != State.WIDGETS) {
2974 return false;
2975 }
Winson Chungb745afb2015-03-02 11:51:23 -08002976
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002977 // This is a safe and supported transition to bypass spring_loaded mode.
2978 if (mExitSpringLoadedModeRunnable != null) {
2979 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2980 mExitSpringLoadedModeRunnable = null;
2981 }
2982
Winson Chungb745afb2015-03-02 11:51:23 -08002983 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002984 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002985 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002986 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002987 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002988
Michael Jurkab3e22d92011-10-31 15:58:33 -07002989 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002990 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002991 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002992
2993 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002994 getWindow().getDecorView()
2995 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002996 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002997 }
2998
Winson Chungcd99cd32015-04-29 11:03:24 -07002999 /**
3000 * Updates the workspace and interaction state on state change, and return the animation to this
3001 * new state.
3002 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003003 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07003004 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003005 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003006 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003007 updateInteraction(fromState, toState);
3008 return anim;
3009 }
3010
Hyunyoung Song3f471442015-04-08 19:01:34 -07003011 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003012 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003013 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003014 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003015 }
Winson Chungb745afb2015-03-02 11:51:23 -08003016
Winson Chung006ee262015-08-03 14:40:11 -07003017 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003018 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003019 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08003020 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08003021 }
Adam Cohen7777d962011-08-18 18:58:38 -07003022
Hyunyoung Song3f471442015-04-08 19:01:34 -07003023 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003024 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003025 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003026
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003027 if (mExitSpringLoadedModeRunnable != null) {
3028 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3029 }
3030 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003031 @Override
3032 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003033 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003034 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3035 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003036 // Before we show workspace, hide all apps again because
3037 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3038 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003039 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003040 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003041 } else {
3042 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003043 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003044 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003045 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003046 };
3047 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003048 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003049
Tony Wickhame0c33232016-02-08 11:37:04 -08003050 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003051 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3052 || mState == State.WIDGETS_SPRING_LOADED;
3053 }
3054
Sunny Goyal0f76b562016-12-13 19:37:10 -08003055 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003056 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyal5606e072017-06-15 14:24:21 -07003057 showAppsView(true /* animated */, false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003058 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003059 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003060 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3061 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003062 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003063 }
3064
Winson Chung4ac30062015-05-08 17:34:17 -07003065 /**
3066 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3067 * resumed.
3068 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003069 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003070 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003071 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003072 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003073 mAppsView.setPredictedApps(apps);
3074 }
3075 }
3076 }
3077
Michael Jurkab3e22d92011-10-31 15:58:33 -07003078 void lockAllApps() {
3079 // TODO
3080 }
3081
3082 void unlockAllApps() {
3083 // TODO
3084 }
3085
Michael Jurkad7c28052012-04-27 15:43:36 -07003086 @Override
3087 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003088 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003089 final List<CharSequence> text = event.getText();
3090 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003091 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003092 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003093 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003094 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003095 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003096 } else if (mWorkspace != null) {
3097 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003098 } else {
3099 text.add(getString(R.string.all_apps_home_button_label));
3100 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003101 return result;
3102 }
3103
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003104 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003105 * If the activity is currently paused, signal that we need to run the passed Runnable
3106 * in onResume.
3107 *
3108 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003109 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3110 * wrong when we're not running, and if the activity comes back to what the configuration was
3111 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003112 *
3113 * Implementation of the method from LauncherModel.Callbacks.
3114 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003115 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003116 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003117 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003118 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003119 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003120 if (run instanceof RunnableWithId) {
3121 // Remove any runnables which have the same id
3122 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02003123 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003124 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003125 return true;
3126 } else {
3127 return false;
3128 }
3129 }
3130
Michael Jurka1e2f4652013-07-08 18:03:46 -07003131 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003132 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003133 }
3134
Michael Jurka7607c2f2013-04-03 14:33:19 -07003135 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003136 * If the activity is currently paused, signal that we need to re-run the loader
3137 * in onResume.
3138 *
3139 * This needs to be called from incoming places where resources might have been loaded
3140 * while we are paused. That is becaues the Configuration might be wrong
3141 * when we're not running, and if it comes back to what it was when we
3142 * were paused, we are not restarted.
3143 *
3144 * Implementation of the method from LauncherModel.Callbacks.
3145 *
3146 * @return true if we are currently paused. The caller might be able to
3147 * skip some work in that case since we will come back again.
3148 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003149 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003150 public boolean setLoadOnResume() {
3151 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003152 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003153 mOnResumeNeedsLoad = true;
3154 return true;
3155 } else {
3156 return false;
3157 }
3158 }
3159
3160 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003163 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003164 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003165 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003166 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003167 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003168 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003169 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003170 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003171
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003173 * Clear any pending bind callbacks. This is called when is loader is planning to
3174 * perform a full rebind from scratch.
3175 */
3176 @Override
3177 public void clearPendingBinds() {
3178 mBindOnResumeCallbacks.clear();
3179 if (mPendingExecutor != null) {
3180 mPendingExecutor.markCompleted();
3181 mPendingExecutor = null;
3182 }
3183 }
3184
3185 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186 * Refreshes the shortcuts shown on the workspace.
3187 *
3188 * Implementation of the method from LauncherModel.Callbacks.
3189 */
3190 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003191 if (LauncherAppState.PROFILE_STARTUP) {
3192 Trace.beginSection("Starting page bind");
3193 }
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08003194
3195 AbstractFloatingView.closeAllOpenViews(this);
3196
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003197 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003198
Winson Chungd64d1762013-08-20 14:37:16 -07003199 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003200 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003201 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003202
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 if (mHotseat != null) {
3204 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003206 if (LauncherAppState.PROFILE_STARTUP) {
3207 Trace.endSection();
3208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 }
3210
Adam Cohendcd297f2013-06-18 13:13:40 -07003211 @Override
3212 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003213 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003214 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3215 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003216 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3217 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003218 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003219 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3220 // If there are no screens, we need to have an empty screen
3221 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003222 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003223 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003224
3225 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003226 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003227 if (hasCustomContentToLeft()) {
3228 mWorkspace.createCustomContentContainer();
3229 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003230 }
Winsonc7d2e832016-07-28 12:24:55 -07003231
3232 // After we have added all the screens, if the wallpaper was locked to the default state,
3233 // then notify to indicate that it can be released and a proper wallpaper offset can be
3234 // computed before the next layout
3235 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003236 }
3237
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003238 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003239 int count = orderedScreenIds.size();
3240 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003241 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003242 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003243 // No need to bind the first screen, as its always bound.
3244 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3245 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003246 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003247 }
3248
Sunny Goyalb23980c2017-08-17 07:45:25 -07003249 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07003250 public void bindAppsAdded(final ArrayList<Long> newScreens,
3251 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07003252 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07003253 Runnable r = new Runnable() {
3254 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003255 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07003256 }
3257 };
3258 if (waitUntilResume(r)) {
3259 return;
3260 }
3261
Winson Chungd64d1762013-08-20 14:37:16 -07003262 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003263 if (newScreens != null) {
3264 bindAddScreens(newScreens);
3265 }
Winson Chungd64d1762013-08-20 14:37:16 -07003266
3267 // We add the items without animation on non-visible pages, and with
3268 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003269 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003270 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07003271 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003272 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003273 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07003274 }
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 Chungd64d1762013-08-20 14:37:16 -07003278 }
3279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003280 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 * Bind the items start-end from the list.
3282 *
3283 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003284 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003285 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07003286 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003287 Runnable r = new Runnable() {
3288 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003289 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003290 }
3291 };
3292 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003293 return;
3294 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003295
Sunny Goyal3be633b2016-12-08 09:59:25 -08003296 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003297 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003298 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07003299 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003300 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003301 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07003302 int end = items.size();
3303 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003304 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003305
3306 // Short circuit if we are loading dock items for a configuration which has no dock
3307 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3308 mHotseat == null) {
3309 continue;
3310 }
3311
Sunny Goyal639e9062015-08-19 19:17:06 -07003312 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 switch (item.itemType) {
3314 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3315 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003316 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003317 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003318 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003320 }
3321 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003322 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003323 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003324 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003326 }
3327 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07003328 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003329 if (view == null) {
3330 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003331 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08003332 break;
3333 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003334 default:
3335 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003337
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003338 /*
3339 * Remove colliding items.
3340 */
3341 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3342 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3343 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3344 View v = cl.getChildAt(item.cellX, item.cellY);
3345 Object tag = v.getTag();
3346 String desc = "Collision while binding workspace item: " + item
3347 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003348 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003349 throw (new RuntimeException(desc));
3350 } else {
3351 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003352 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003353 continue;
3354 }
3355 }
3356 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303357 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003358 if (animateIcons) {
3359 // Animate all the applications up now
3360 view.setAlpha(0f);
3361 view.setScaleX(0f);
3362 view.setScaleY(0f);
3363 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003364 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003366 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003367
Winson Chung997a9232013-07-24 15:33:46 -07003368 if (animateIcons) {
3369 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003370 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003371 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003372 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003373 final Runnable startBounceAnimRunnable = new Runnable() {
3374 public void run() {
3375 anim.playTogether(bounceAnims);
3376 anim.start();
3377 }
3378 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003379 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003380 // We post the animation slightly delayed to prevent slowdowns
3381 // when we are loading right after we return to launcher.
3382 mWorkspace.postDelayed(new Runnable() {
3383 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003384 if (mWorkspace != null) {
3385 mWorkspace.snapToPage(newScreenIndex);
3386 mWorkspace.postDelayed(startBounceAnimRunnable,
3387 NEW_APPS_ANIMATION_DELAY);
3388 }
Winson Chung94d67682013-09-25 16:29:40 -07003389 }
3390 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003391 } else {
3392 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003393 }
3394 }
Winson Chung64359a52013-07-08 17:17:08 -07003395 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003397 }
3398
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 /**
3400 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07003402 public void bindAppWidget(LauncherAppWidgetInfo item) {
3403 View view = inflateAppWidget(item);
3404 if (view != null) {
3405 mWorkspace.addInScreen(view, item);
3406 mWorkspace.requestLayout();
3407 }
3408 }
3409
3410 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003411 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303412 PendingAppWidgetHostView view =
3413 new PendingAppWidgetHostView(this, item, mIconCache, true);
3414 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003415 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003416 }
3417
Daniel Sandler843e8602010-06-07 14:59:01 -04003418 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3419 if (DEBUG_WIDGETS) {
3420 Log.d(TAG, "bindAppWidget: " + item);
3421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003423 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003424
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003425 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3426 // If the provider is not ready, bind as a pending widget.
3427 appWidgetInfo = null;
3428 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3429 // The widget id is not valid. Try to find the widget based on the provider info.
3430 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3431 } else {
3432 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3433 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003434
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003435 // If the provider is ready, but the width is not yet restored, try to restore it.
3436 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3437 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003438 if (appWidgetInfo == null) {
3439 if (DEBUG_WIDGETS) {
3440 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3441 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003442 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003443 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003444 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003445 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003446 }
Sunny Goyalff572272014-07-23 13:58:07 -07003447
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003448 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003449 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003450 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3451 // Id has not been allocated yet. Allocate a new id.
3452 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3453 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003454
Sunny Goyald478c832016-04-01 12:04:16 -07003455 // Also try to bind the widget. If the bind fails, the user will be shown
3456 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003457 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003458 pendingInfo.spanX = item.spanX;
3459 pendingInfo.spanY = item.spanY;
3460 pendingInfo.minSpanX = item.minSpanX;
3461 pendingInfo.minSpanY = item.minSpanY;
3462 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003463
3464 boolean isDirectConfig =
3465 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3466 if (isDirectConfig && item.bindOptions != null) {
3467 Bundle newOptions = item.bindOptions.getExtras();
3468 if (options != null) {
3469 newOptions.putAll(options);
3470 }
3471 options = newOptions;
3472 }
Sunny Goyald478c832016-04-01 12:04:16 -07003473 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3474 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003475
Sunny Goyal86df1382016-08-10 15:03:22 -07003476 // We tried to bind once. If we were not able to bind, we would need to
3477 // go through the permission dialog, which means we cannot skip the config
3478 // activity.
3479 item.bindOptions = null;
3480 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3481
Sunny Goyald478c832016-04-01 12:04:16 -07003482 // Bind succeeded
3483 if (success) {
3484 // If the widget has a configure activity, it is still needs to set it up,
3485 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003486 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003487 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3488 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003489 }
Sunny Goyald478c832016-04-01 12:04:16 -07003490
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003491 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003492 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003493 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003494 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003495 // The widget was marked as UI not ready, but there is no configure activity to
3496 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003497 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003498 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003499 }
Sunny Goyalff572272014-07-23 13:58:07 -07003500 }
3501
Sunny Goyald5462aa2016-11-22 16:52:39 +05303502 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003503 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003504 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003505 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3506 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003507 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003508
Sunny Goyal56c73602015-09-25 12:55:01 -07003509 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003510 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003511 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003512 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07003513 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07003514 }
3515
Sunny Goyal233ee962015-08-03 13:05:01 -07003516 item.minSpanX = appWidgetInfo.minSpanX;
3517 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303518 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003519 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303520 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003521 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303522 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523
Daniel Sandler843e8602010-06-07 14:59:01 -04003524 if (DEBUG_WIDGETS) {
3525 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3526 + (SystemClock.uptimeMillis()-start) + "ms");
3527 }
Sunny Goyalb23980c2017-08-17 07:45:25 -07003528 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529 }
3530
Sunny Goyalff572272014-07-23 13:58:07 -07003531 /**
3532 * Restores a pending widget.
3533 *
3534 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003535 */
Sunny Goyald478c832016-04-01 12:04:16 -07003536 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003537 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3538 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3539 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003540 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003541 }
3542
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003543 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003544 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003545 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3546 info.pendingItemInfo = null;
3547 }
Sunny Goyalff572272014-07-23 13:58:07 -07003548
3549 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003550 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003551 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003552 }
3553
Adam Cohen1462de32012-07-24 22:34:36 -07003554 public void onPageBoundSynchronously(int page) {
3555 mSynchronouslyBoundPages.add(page);
3556 }
3557
Sunny Goyal527c7d32015-08-28 15:19:36 -07003558 @Override
3559 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3560 if (mPendingExecutor != null) {
3561 mPendingExecutor.markCompleted();
3562 }
3563 mPendingExecutor = executor;
3564 executor.attachTo(this);
3565 }
3566
3567 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3568 if (mPendingExecutor == executor) {
3569 mPendingExecutor = null;
3570 }
3571 }
3572
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003573 @Override
3574 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3575 Runnable r = new Runnable() {
3576 public void run() {
3577 finishFirstPageBind(executor);
3578 }
3579 };
3580 if (waitUntilResume(r)) {
3581 return;
3582 }
3583
3584 Runnable onComplete = new Runnable() {
3585 @Override
3586 public void run() {
3587 if (executor != null) {
3588 executor.onLoadAnimationCompleted();
3589 }
3590 }
3591 };
3592 if (mDragLayer.getAlpha() < 1) {
3593 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3594 } else {
3595 onComplete.run();
3596 }
3597 }
3598
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 /**
3600 * Callback saying that there aren't any more items to bind.
3601 *
3602 * Implementation of the method from LauncherModel.Callbacks.
3603 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003604 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003605 Runnable r = new Runnable() {
3606 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003607 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003608 }
3609 };
3610 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003611 return;
3612 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003613 if (LauncherAppState.PROFILE_STARTUP) {
3614 Trace.beginSection("Page bind completed");
3615 }
Adam Cohen1462de32012-07-24 22:34:36 -07003616 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003618 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003619
Sunny Goyal2100c782016-08-22 16:00:03 -07003620 if (mPendingActivityResult != null) {
3621 handleActivityResult(mPendingActivityResult.requestCode,
3622 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3623 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003624 }
3625
Sunny Goyala474a9b2017-05-04 16:47:11 -07003626 InstallShortcutReceiver.disableAndFlushInstallQueue(
3627 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003628
Tony Wickham010d2552017-01-20 08:15:28 -08003629 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3630
Adam Cohen9211d422014-10-07 18:14:53 -07003631 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003632 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003633 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003634 if (LauncherAppState.PROFILE_STARTUP) {
3635 Trace.endSection();
3636 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003637 }
3638
Winson Chunga2413752012-04-03 14:22:34 -07003639 private boolean canRunNewAppsAnimation() {
3640 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003641 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003642 }
3643
Winson Chungc9168342013-06-26 14:54:55 -07003644 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003645 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003646 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3647 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003648 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003649 return bounceAnim;
3650 }
3651
Adam Cohen27772732013-11-11 14:00:56 +00003652 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003653 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003654 }
3655
Tony Wickham55616cd2015-09-23 14:55:17 -07003656 public int getSearchBarHeight() {
3657 if (mLauncherCallbacks != null) {
3658 return mLauncherCallbacks.getSearchBarHeight();
3659 }
3660 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3661 }
3662
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003663 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 * Add the icons for all apps.
3665 *
3666 * Implementation of the method from LauncherModel.Callbacks.
3667 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003668 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003669 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
3670 public void run() {
3671 bindAllApplications(apps);
3672 }
3673 };
3674 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07003675 return;
3676 }
3677
Winson Chungb745afb2015-03-02 11:51:23 -08003678 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07003679 Executor pendingExecutor = getPendingExecutor();
3680 if (pendingExecutor != null && mState != State.APPS) {
3681 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003682 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003683 return;
3684 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003685
Winson Chungb745afb2015-03-02 11:51:23 -08003686 mAppsView.setApps(apps);
3687 }
Adam Cohen9211d422014-10-07 18:14:53 -07003688 if (mLauncherCallbacks != null) {
3689 mLauncherCallbacks.bindAllApplications(apps);
3690 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 }
3692
3693 /**
Tony2917a8b2017-07-31 23:29:42 -07003694 * Returns an Executor that will run after the launcher is first drawn (including after the
3695 * initial fade in animation). Returns null if the first draw has already occurred.
3696 */
3697 public @Nullable Executor getPendingExecutor() {
3698 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
3699 }
3700
3701 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003702 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3703 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3704 */
3705 @Override
3706 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003707 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003708 }
3709
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003710 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 * A package was updated.
3712 *
3713 * Implementation of the method from LauncherModel.Callbacks.
3714 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07003715 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003716 Runnable r = new Runnable() {
3717 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003718 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07003719 }
3720 };
3721 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003722 return;
3723 }
3724
Winson Chungb745afb2015-03-02 11:51:23 -08003725 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07003726 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 }
3729
Sunny Goyal4390ace2014-10-13 11:33:11 -07003730 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003731 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3732 Runnable r = new Runnable() {
3733 public void run() {
3734 bindPromiseAppProgressUpdated(app);
3735 }
3736 };
3737 if (waitUntilResume(r)) {
3738 return;
3739 }
3740
3741 if (mAppsView != null) {
3742 mAppsView.updatePromiseAppProgress(app);
3743 }
3744 }
3745
3746 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003747 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3748 Runnable r = new Runnable() {
3749 public void run() {
3750 bindWidgetsRestored(widgets);
3751 }
3752 };
3753 if (waitUntilResume(r)) {
3754 return;
3755 }
3756 mWorkspace.widgetsRestored(widgets);
3757 }
3758
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003760 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003761 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003762 *
3763 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003764 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003765 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003766 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003767 Runnable r = new Runnable() {
3768 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003769 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003770 }
3771 };
3772 if (waitUntilResume(r)) {
3773 return;
3774 }
3775
Sunny Goyal4390ace2014-10-13 11:33:11 -07003776 if (!updated.isEmpty()) {
3777 mWorkspace.updateShortcuts(updated);
3778 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003779 }
3780
3781 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003782 * Update the state of a package, typically related to install state.
3783 *
3784 * Implementation of the method from LauncherModel.Callbacks.
3785 */
Sunny Goyale755d462014-07-22 13:48:29 -07003786 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003787 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3788 Runnable r = new Runnable() {
3789 public void run() {
3790 bindRestoreItemsChange(updates);
3791 }
3792 };
3793 if (waitUntilResume(r)) {
3794 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003795 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003796
Sunny Goyal756adbc2015-04-16 15:20:51 -07003797 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003798 }
3799
3800 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003801 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003802 * in addition to specific applications to remove, the reason being that
3803 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003804 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003805 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003807 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003808 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003809 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003810 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003811 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003812 }
Winson Chungd64d1762013-08-20 14:37:16 -07003813 };
3814 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003815 return;
3816 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003817 mWorkspace.removeItemsByMatcher(matcher);
3818 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003819 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003820
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003821 @Override
3822 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3823 Runnable r = new Runnable() {
3824 public void run() {
3825 bindAppInfosRemoved(appInfos);
3826 }
3827 };
3828 if (waitUntilResume(r)) {
3829 return;
Joe Onorato36115782010-06-17 13:28:48 -04003830 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003831
Winson Chungdf95eb12013-10-16 14:57:07 -07003832 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003833 if (mAppsView != null) {
3834 mAppsView.removeApps(appInfos);
Hyunyoung Song51114f22017-05-02 16:06:28 -07003835 tryAndUpdatePredictedApps();
Winson Chungc58497e2013-09-03 17:48:37 -07003836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 }
Joe Onoratobe386092009-11-17 17:32:16 -08003838
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003839 @Override
3840 public void bindAllWidgets(final MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3841 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3842 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003843 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003844 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003845 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003846 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003847 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003848 return;
3849 }
3850
Sunny Goyald164b7f2016-10-12 20:49:31 -07003851 if (mWidgetsView != null && allWidgets != null) {
Tony2917a8b2017-07-31 23:29:42 -07003852 Executor pendingExecutor = getPendingExecutor();
3853 if (pendingExecutor != null && mState != State.WIDGETS) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003854 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07003855 return;
3856 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003857 mWidgetsView.setWidgets(allWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07003858 }
Tony Wickham26b17462017-03-20 17:12:24 -07003859
3860 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3861 if (topView != null) {
3862 topView.onWidgetsBound();
3863 }
3864 }
3865
3866 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3867 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003868 }
3869
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003870 @Override
3871 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003872 if (mWorkspace.getState().shouldUpdateWidget) {
3873 refreshAndBindWidgetsForPackageUser(null);
3874 }
Tony Wickham26b17462017-03-20 17:12:24 -07003875 }
3876
Tony Wickham86222d22017-03-29 15:30:43 -07003877 /**
3878 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3879 * refreshes the widgets and shortcuts associated with the given package/user
3880 */
3881 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003882 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003883 }
3884
Winson Chung4b919f82012-05-01 10:44:08 -07003885 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003886 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003887 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003888 }
3889 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003890
Winson Chung4b919f82012-05-01 10:44:08 -07003891 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003892 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003893 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003894 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003895 } else {
3896 mHandler.postDelayed(new Runnable() {
3897 public void run() {
3898 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3899 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003900 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003901 }
Winson Chung4b919f82012-05-01 10:44:08 -07003902 }
Winson Chung400438b2011-01-16 17:53:48 -08003903 }
3904
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003905 private void markAppsViewShown() {
3906 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3907 return;
3908 }
3909 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3910 }
3911
3912 private boolean shouldShowDiscoveryBounce() {
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003913 if (mState != State.WORKSPACE) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003914 return false;
3915 }
3916 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3917 return true;
3918 }
3919 if (!mIsResumeFromActionScreenOff) {
3920 return false;
3921 }
Rajeev Kumar43c0f582017-06-23 15:34:55 -07003922 return !mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false);
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003923 }
3924
Winson Chung5ffd51d2015-06-25 11:36:50 -07003925 protected void moveWorkspaceToDefaultScreen() {
3926 mWorkspace.moveToDefaultScreen(false);
3927 }
3928
Winson Chung80baf5a2010-08-09 16:03:15 -07003929 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003930 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003931 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003932 @Override
3933 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3934 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003935
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003936 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3937 writer.println(prefix + "Workspace Items");
3938 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
3939 writer.println(prefix + " Homescreen " + i);
3940
3941 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3942 for (int j = 0; j < layout.getChildCount(); j++) {
3943 Object tag = layout.getChildAt(j).getTag();
3944 if (tag != null) {
3945 writer.println(prefix + " " + tag.toString());
3946 }
3947 }
3948 }
3949
3950 writer.println(prefix + " Hotseat");
3951 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003952 for (int j = 0; j < layout.getChildCount(); j++) {
3953 Object tag = layout.getChildAt(j).getTag();
3954 if (tag != null) {
3955 writer.println(prefix + " " + tag.toString());
3956 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003957 }
Sunny Goyala1365452015-10-01 15:46:24 -07003958
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003959 try {
3960 FileLog.flushAll(writer);
3961 } catch (Exception e) {
3962 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003963 }
3964 }
3965
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003966 writer.println(prefix + "Misc:");
3967 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3968 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3969 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3970
3971 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003972
Adam Cohen9211d422014-10-07 18:14:53 -07003973 if (mLauncherCallbacks != null) {
3974 mLauncherCallbacks.dump(prefix, fd, writer, args);
3975 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003976 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003977
Sunny Goyal66b24572016-09-21 15:57:55 -07003978 @Override
3979 @TargetApi(Build.VERSION_CODES.N)
3980 public void onProvideKeyboardShortcuts(
3981 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3982
3983 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
3984 if (mState == State.WORKSPACE) {
3985 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3986 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3987 }
3988 View currentFocus = getCurrentFocus();
3989 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3990 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3991 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3992 }
Tony18c4aa42017-05-10 21:23:57 -05003993 if (currentFocus.getTag() instanceof ItemInfo
3994 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003995 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3996 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3997 }
3998 if (!shortcutInfos.isEmpty()) {
3999 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4000 }
4001
4002 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4003 }
4004
4005 @Override
4006 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4007 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4008 switch (keyCode) {
4009 case KeyEvent.KEYCODE_A:
4010 if (mState == State.WORKSPACE) {
Sunny Goyal5606e072017-06-15 14:24:21 -07004011 showAppsView(true, true);
Sunny Goyal66b24572016-09-21 15:57:55 -07004012 return true;
4013 }
4014 break;
4015 case KeyEvent.KEYCODE_S: {
4016 View focusedView = getCurrentFocus();
4017 if (focusedView instanceof BubbleTextView
4018 && focusedView.getTag() instanceof ItemInfo
4019 && mAccessibilityDelegate.performAction(focusedView,
4020 (ItemInfo) focusedView.getTag(),
4021 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08004022 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004023 return true;
4024 }
4025 break;
4026 }
4027 case KeyEvent.KEYCODE_O:
4028 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4029 return true;
4030 }
4031 break;
4032 }
4033 }
4034 return super.onKeyShortcut(keyCode, event);
4035 }
4036
Adam Cohen59400422014-03-05 18:07:04 -08004037 public static CustomAppWidget getCustomAppWidget(String name) {
4038 return sCustomAppWidgets.get(name);
4039 }
4040
4041 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4042 return sCustomAppWidgets;
4043 }
4044
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004045 public static Launcher getLauncher(Context context) {
4046 if (context instanceof Launcher) {
4047 return (Launcher) context;
4048 }
4049 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4050 }
4051
Sunny Goyal5a81c382017-03-20 15:08:06 -07004052 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07004053
4054 @Override
4055 public void onSharedPreferenceChanged(
4056 SharedPreferences sharedPreferences, String key) {
4057 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07004058 // Recreate the activity so that it initializes the rotation preference again.
4059 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07004060 }
4061 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004063}