blob: cb6351048405fb798dbccf657729417111dc3c2a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_APPS;
33import static com.android.launcher3.util.RunnableWithId.RUNNABLE_ID_BIND_WIDGETS;
34
Sunny Goyal28c6b962015-10-12 11:42:05 -070035import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070039import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070040import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070041import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000042import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070043import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040044import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080046import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080049import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080050import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070053import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040054import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070056import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070057import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070058import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070059import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070061import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070062import android.graphics.Point;
Sunny Goyala5ace712017-11-15 17:12:51 -080063import android.graphics.PointF;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020067import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070069import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080070import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070071import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070072import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070073import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070076import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070078import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070079import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.HapticFeedbackConstants;
82import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070083import android.view.KeyboardShortcutGroup;
84import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080085import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080089import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070092import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070097import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070098import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700100import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700101import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -0700104import com.android.launcher3.compat.LauncherAppsCompatVO;
Adam Cohen39933482017-09-29 16:43:51 -0700105import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800106import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700109import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700110import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700111import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700114import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700115import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700116import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800118import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800119import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700120import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal10a1bd02017-10-09 14:56:21 -0700121import com.android.launcher3.popup.BaseActionPopup;
Tony Wickham540913e2017-01-23 11:47:51 -0800122import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800123import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700124import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700125import com.android.launcher3.states.AllAppsState;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800126import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800127import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800128import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530129import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
130import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
131import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700132import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700133import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700134import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700135import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700136import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700137import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800138import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700139import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal326403e2017-10-02 12:45:10 -0700140import com.android.launcher3.util.RunnableWithId;
Sunny Goyal8392c822017-06-20 10:03:56 -0700141import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700142import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700143import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700144import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700145import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700146import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800147import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700148import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800149import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700150import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700151import com.android.launcher3.widget.WidgetListRowEntry;
152import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700153import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700154
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700155import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700156import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700157import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700158import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700159import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700160import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800161import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700162import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164/**
165 * Default launcher application.
166 */
Sunny Goyal27835952017-01-13 12:15:53 -0800167public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700168 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
169 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700170 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700171 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700172 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chunga2413752012-04-03 14:22:34 -0700174 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700178
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700180 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Michael Jurka8b805b12012-04-18 14:23:14 -0700182 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700183 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
184 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700185
Jon Mirandac476d6e2017-05-04 16:30:01 -0700186 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700187
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700188 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
189
Mathew Inwood876a8462013-06-14 14:12:41 +0100190 /**
191 * IntentStarter uses request codes starting with this. This must be greater than all activity
192 * request codes used internally.
193 */
194 protected static final int REQUEST_LAST = 100;
195
Winson Chung2672ff92012-05-04 16:22:30 -0700196 // The Intent extra that defines whether to ignore the launch animation
197 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400198 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 // Type: int
201 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700202 // Type: int
203 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700204 // Type: PendingRequestArgs
205 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
206 // Type: ActivityResultInfo
207 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700208 // Type: SparseArray<Parcelable>
209 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700211 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700218 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
220 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700223 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800225 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700226
Sunny Goyalffe83f12014-08-14 17:39:34 -0700227 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700229
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700230 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231
Sunny Goyal316490e2015-06-02 09:38:28 -0700232 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700233
Michael Jurka838a4ca2011-02-07 13:33:06 -0800234 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700235
Sunny Goyal47328fd2016-05-25 18:56:41 -0700236 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700237
238 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700239 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700240 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700241
Winson Chung8ae41982017-11-10 11:42:13 -0800242 // UI and state for the overview panel
243 private ViewGroup mOverviewPanel;
244
Jon Miranda6bed3502017-09-19 14:43:17 -0700245 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
246 // that results in widgets being inflated in the wrong orientation.
247 private int mOrientation;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private SpannableStringBuilder mDefaultKeySsb = null;
250
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800253 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700254 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700256 private final ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<>();
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700257 private OnResumeCallback mOnResumeCallback;
258
Sunny Goyal527c7d32015-08-28 15:19:36 -0700259 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700260
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800262 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800263 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700264 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700265 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266
Jon Miranda2d89ea82017-05-04 11:47:53 -0700267 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700268 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700269
Tony Wickham010d2552017-01-20 08:15:28 -0800270 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700271
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700272 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
Adam Cohen1462de32012-07-24 22:34:36 -0700273
Winson Chung46353de2012-02-16 14:05:10 -0800274 // We only want to get the SharedPreferences once since it does an FS stat each time we get
275 // it from the context.
276 private SharedPreferences mSharedPrefs;
277
Sunny Goyal2100c782016-08-22 16:00:03 -0700278 // Activity result which needs to be processed after workspace has loaded.
279 private ActivityResultInfo mPendingActivityResult;
280 /**
281 * Holds extra information required to handle a result from an external call, like
282 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
283 */
284 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800285
Jon Miranda379198e2016-09-23 08:54:40 -0700286 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700287
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700288 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700289 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700290 private boolean mAppLaunchSuccess;
291
Sunny Goyal745bad92016-05-02 10:54:12 -0700292 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 @Override
295 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700296 if (DEBUG_STRICT_MODE) {
297 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
298 .detectDiskReads()
299 .detectDiskWrites()
300 .detectNetwork() // or .detectAll() for all detectable problems
301 .penaltyLog()
302 .build());
303 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
304 .detectLeakedSqlLiteObjects()
305 .detectLeakedClosableObjects()
306 .penaltyLog()
307 .penaltyDeath()
308 .build());
309 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700310 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700311
Adam Cohen9211d422014-10-07 18:14:53 -0700312 if (mLauncherCallbacks != null) {
313 mLauncherCallbacks.preOnCreate();
314 }
315
Mario Bertschler27288382017-05-24 15:35:09 -0700316 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
317 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700318 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700321 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400322
Sunny Goyal87f784c2017-01-11 10:48:34 -0800323 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700324
Adam Cohen2e6da152015-05-06 11:42:25 -0700325 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800326 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700327 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700328 Display display = getWindowManager().getDefaultDisplay();
329 Point mwSize = new Point();
330 display.getSize(mwSize);
331 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
332 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700333
Jon Miranda6bed3502017-09-19 14:43:17 -0700334 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700335 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700336 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800337 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800338 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800339 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700340 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700341
Joe Onorato41a12d22009-10-31 18:30:00 -0400342 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700343 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800344 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700345
Sunny Goyalffe83f12014-08-14 17:39:34 -0700346 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700347
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700348 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700349 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700350
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700351 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
352 // this also ensures that any synchronous binding below doesn't re-trigger another
353 // LauncherModel load.
354 mPaused = false;
355
Sunny Goyal60820d72017-05-09 12:40:11 -0700356 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 setupViews();
Winson1f064272016-07-18 17:18:02 -0700359 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360
Tony Wickham010d2552017-01-20 08:15:28 -0800361 mPopupDataProvider = new PopupDataProvider(this);
362
Sunny Goyald3864fa2017-11-14 15:06:36 -0800363 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
364 // In case we are on a device with locked rotation, we should look at preferences to check
365 // if the user has specifically allowed rotation.
366 if (!mRotationEnabled) {
367 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
368 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
369 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371
Sunny Goyald3864fa2017-11-14 15:06:36 -0800372 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
373 if (internalStateHandled) {
374 // Temporarily enable the rotation
375 mRotationEnabled = true;
376
377 if (savedInstanceState != null) {
378 // InternalStateHandler has already set the appropriate state.
379 // We dont need to do anything.
380 savedInstanceState.remove(RUNTIME_STATE);
381 }
382 }
383 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800384
Sunny Goyal2100c782016-08-22 16:00:03 -0700385 // We only load the page synchronously if the user rotates (or triggers a
386 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700387 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
388 if (savedInstanceState != null) {
389 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
390 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800391
Sunny Goyalfe770c92016-09-27 14:38:58 -0700392 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800393 if (!internalStateHandled) {
394 // If we are not binding synchronously, show a fade in animation when
395 // the first page bind completes.
396 mDragLayer.setAlpha(0);
397 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700398 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700399 // Pages bound synchronously.
400 mWorkspace.setCurrentPage(currentScreen);
401
Sunny Goyal2100c782016-08-22 16:00:03 -0700402 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 }
404
405 // For handling default keys
406 mDefaultKeySsb = new SpannableStringBuilder();
407 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800408
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700409 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
410 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700411 setRequestedOrientation(mRotationEnabled
412 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700413
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800414 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700415
Jon Miranda7fda2852017-07-19 16:07:01 -0700416 // Listen for broadcasts
417 IntentFilter filter = new IntentFilter();
418 filter.addAction(Intent.ACTION_SCREEN_OFF);
419 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
420 registerReceiver(mReceiver, filter);
421 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700422
Sunny Goyal8392c822017-06-20 10:03:56 -0700423 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
424 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700425
426 if (mLauncherCallbacks != null) {
427 mLauncherCallbacks.onCreate(savedInstanceState);
428 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700429
430 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700431 }
432
Sunny Goyal7779d622015-06-11 16:18:39 -0700433 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700434 public void onThemeChanged() {
435 recreate();
436 }
437
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700438 public LauncherStateManager getStateManager() {
439 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700440 }
441
Mario Bertschlera6936942017-05-31 14:48:19 -0700442 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700443 if (isDark) {
444 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700445 } else if (supportsDarkText) {
446 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700447 }
448 }
449
450 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700451 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800452 return mLauncherView.findViewById(id);
453 }
454
455 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700456 public void onAppWidgetHostReset() {
457 if (mAppWidgetHost != null) {
458 mAppWidgetHost.startListening();
459 }
460 }
461
Adam Cohen9211d422014-10-07 18:14:53 -0700462 private LauncherCallbacks mLauncherCallbacks;
463
Winson1f064272016-07-18 17:18:02 -0700464 public void onInsetsChanged(Rect insets) {
465 mDeviceProfile.updateInsets(insets);
466 mDeviceProfile.layout(this, true /* notifyListeners */);
467 }
468
Sunny Goyal32554d12015-12-03 15:31:25 -0800469 /**
470 * Call this after onCreate to set or clear overlay.
471 */
472 public void setLauncherOverlay(LauncherOverlay overlay) {
473 if (overlay != null) {
474 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
475 }
476 mWorkspace.setLauncherOverlay(overlay);
477 }
478
Adam Cohen9211d422014-10-07 18:14:53 -0700479 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
480 mLauncherCallbacks = callbacks;
481 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700482 }
483
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700484 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700485 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700486 if (mLauncherCallbacks != null) {
487 mLauncherCallbacks.onLauncherProviderChange();
488 }
489 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700490
Hyunyoung Song3f471442015-04-08 19:01:34 -0700491 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700492 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
493 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700494 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700495 }
496
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000497 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700498 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
499 // This cast is safe as long as the id < 0x00FFFFFF
500 // Since we jail all the dynamically generated views, there should be no clashes
501 // with any other views.
502 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000503 }
504
Tony Wickham010d2552017-01-20 08:15:28 -0800505 public PopupDataProvider getPopupDataProvider() {
506 return mPopupDataProvider;
507 }
508
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000509 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700510 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
511 * a configuration step, this allows the proper animations to run after other transitions.
512 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700513 private long completeAdd(
514 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
515 long screenId = info.screenId;
516 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700517 // When the screen id represents an actual screen (as opposed to a rank) we make sure
518 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700519 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700520 }
Adam Cohendb364c32014-05-20 14:23:40 -0700521
Sunny Goyal2100c782016-08-22 16:00:03 -0700522 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800523 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700524 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700525 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800526 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700527 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700528 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700529 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700530 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700531 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700532 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700533 int widgetId = appWidgetId;
534 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700535 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700536 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700537 // Since the view was just bound, also launch the configure activity if needed
538 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
539 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800540 if (provider != null) {
541 new WidgetAddFlowHandler(provider)
542 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700543 }
544 }
545 break;
546 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800547 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700548
Adam Cohendb364c32014-05-20 14:23:40 -0700549 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800550 }
551
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800552 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700553 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700554 if (isWorkspaceLoading()) {
555 // process the result once the workspace has loaded.
556 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
557 return;
558 }
559 mPendingActivityResult = null;
560
Winson Chunge341d302013-08-16 14:31:00 -0700561 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700562 final PendingRequestArgs requestArgs = mPendingRequestArgs;
563 setWaitingForResult(null);
564 if (requestArgs == null) {
565 return;
566 }
567
568 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700569
Adam Cohenad4e15c2013-10-17 16:21:35 -0700570 Runnable exitSpringLoaded = new Runnable() {
571 @Override
572 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700573 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700574 }
575 };
576
Michael Jurka8b805b12012-04-18 14:23:14 -0700577 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700578 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700579 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
581 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700582 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700583 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700584 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700586 addAppWidgetImpl(
587 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800588 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700589 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700590 }
591 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200592 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700593 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700594 // User could have free-scrolled between pages before picking a wallpaper; make sure
595 // we move to the closest one now.
596 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700597 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200598 }
599 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700600 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200601
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700603 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700604
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 // We have special handling for widgets
606 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800607 final int appWidgetId;
608 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
609 : -1;
610 if (widgetId < 0) {
611 appWidgetId = pendingAddWidgetId;
612 } else {
613 appWidgetId = widgetId;
614 }
615
Adam Cohenad4e15c2013-10-17 16:21:35 -0700616 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800617 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700618 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
619 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700620 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700621 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700622 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700623 @Override
624 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700625 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700626 }
627 };
Adam Cohendb364c32014-05-20 14:23:40 -0700628
Sunny Goyal2100c782016-08-22 16:00:03 -0700629 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
630 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
631 } else {
632 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
633 // When the screen id represents an actual screen (as opposed to a rank)
634 // we make sure that the drop page actually exists.
635 requestArgs.screenId =
636 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700637 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700638 final CellLayout dropLayout =
639 mWorkspace.getScreenWithId(requestArgs.screenId);
640
641 dropLayout.setDropPending(true);
642 final Runnable onComplete = new Runnable() {
643 @Override
644 public void run() {
645 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
646 dropLayout.setDropPending(false);
647 }
648 };
649 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
650 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700651 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 return;
653 }
654
Sunny Goyald478c832016-04-01 12:04:16 -0700655 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
656 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700657 if (resultCode == RESULT_OK) {
658 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700660 }
661 // Leave the widget in the pending state if the user canceled the configure.
662 return;
663 }
664
Sunny Goyalaad90582015-07-09 14:22:50 -0700665 if (requestCode == REQUEST_CREATE_SHORTCUT) {
666 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
668 completeAdd(requestCode, data, -1, requestArgs);
669 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
670 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
671
Sunny Goyalaad90582015-07-09 14:22:50 -0700672 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700673 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
674 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800678 }
679
680 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700681 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800682 final int requestCode, final int resultCode, final Intent data) {
683 handleActivityResult(requestCode, resultCode, data);
684 if (mLauncherCallbacks != null) {
685 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
686 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 }
688
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700689 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700690 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600691 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 PendingRequestArgs pendingArgs = mPendingRequestArgs;
693 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
694 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
695 setWaitingForResult(null);
696
Sunny Goyal28c6b962015-10-12 11:42:05 -0700697 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700699 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700701 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700702 Intent intent = pendingArgs.getPendingIntent();
703
Sunny Goyal28c6b962015-10-12 11:42:05 -0700704 if (grantResults.length > 0
705 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700706 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700707 } else {
708 // TODO: Show a snack bar with link to settings
709 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700710 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700711 }
712 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600713 if (mLauncherCallbacks != null) {
714 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
715 grantResults);
716 }
717 }
718
Adam Cohendb364c32014-05-20 14:23:40 -0700719 /**
720 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
721 *
722 * @param screenId the screen id to check
723 * @return the new screen, or screenId if it exists
724 */
725 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800726 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700727 if (dropLayout == null) {
728 // it's possible that the add screen was removed because it was
729 // empty and a re-bind occurred
730 mWorkspace.addExtraEmptyScreen();
731 return mWorkspace.commitExtraEmptyScreen();
732 } else {
733 return screenId;
734 }
735 }
736
Sunny Goyal2100c782016-08-22 16:00:03 -0700737 @Thunk void completeTwoStageWidgetDrop(
738 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
739 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 Runnable onCompleteRunnable = null;
741 int animationType = 0;
742
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 if (resultCode == RESULT_OK) {
745 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
746 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800747 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700748 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800749 onCompleteRunnable = new Runnable() {
750 @Override
751 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700753 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 }
755 };
756 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800757 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700760 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700761 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700762 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
763 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700765 // The animated view may be null in the case of a rotation during widget configuration
766 onCompleteRunnable.run();
767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 }
769
770 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700771 protected void onStop() {
772 super.onStop();
773 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700774
775 if (mLauncherCallbacks != null) {
776 mLauncherCallbacks.onStop();
777 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800778 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800779
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700780 if (!mAppLaunchSuccess) {
781 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700782 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700783 }
Tony Wickham010d2552017-01-20 08:15:28 -0800784 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700785 }
786
787 @Override
788 protected void onStart() {
789 super.onStart();
790 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700791
792 if (mLauncherCallbacks != null) {
793 mLauncherCallbacks.onStart();
794 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800795 mAppWidgetHost.setListenIfResumed(true);
Tony Wickham010d2552017-01-20 08:15:28 -0800796
797 if (!isWorkspaceLoading()) {
798 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
799 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700800
Jon Miranda7fda2852017-07-19 16:07:01 -0700801 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700802 if (mScrimAnimator != null) {
803 mScrimAnimator.cancel();
804 }
805 mDragLayer.getBackground().setAlpha(0);
806 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
807 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
808 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
809 @Override
810 public void onAnimationEnd(Animator animation) {
811 mScrimAnimator = null;
812 }
813 });
814 mScrimAnimator.setDuration(600);
815 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
816 mScrimAnimator.start();
817 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700818 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700819 }
820
821 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700823 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700825 TraceHelper.partitionSection("ON_RESUME", "superCall");
826
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700827 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700828 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800829 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700830 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700831 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700832 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700833 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700835 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200836 // We might have postponed some bind calls until onResume (see waitUntilResume) --
837 // execute them here
Michael Jurka1e2f4652013-07-08 18:03:46 -0700838 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
839 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200840 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700841 mBindOnResumeCallbacks.clear();
Michael Jurka447bf842013-05-15 14:52:15 +0200842 }
Winson Chunge4e50662012-01-23 14:45:13 -0800843
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700844 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700845 // Process any items that were added while Launcher was away.
846 InstallShortcutReceiver.disableAndFlushInstallQueue(
847 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700848
Sunny Goyala474a9b2017-05-04 16:47:11 -0700849 // Refresh shortcuts if the permission changed.
850 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700851
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700852 if (shouldShowDiscoveryBounce()) {
853 mAllAppsController.showDiscoveryBounce();
854 }
Adam Cohen9211d422014-10-07 18:14:53 -0700855 if (mLauncherCallbacks != null) {
856 mLauncherCallbacks.onResume();
857 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800858
Sunny Goyala502aa32017-10-02 16:04:06 -0700859 clearTypedText();
860
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700861 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700862 }
863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700865 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700866 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700867 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700868
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700869 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700870 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800871 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700872 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700873
Adam Cohen9211d422014-10-07 18:14:53 -0700874 if (mLauncherCallbacks != null) {
875 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700876 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700877 }
878
Adam Cohenc2d6e892014-10-16 09:49:24 -0700879 public interface LauncherOverlay {
880
881 /**
882 * Touch interaction leading to overscroll has begun
883 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700884 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700885
886 /**
887 * Touch interaction related to overscroll has ended
888 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700889 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700890
891 /**
892 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
893 * screen (or in the case of RTL, the rightmost screen).
894 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700895 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700896
897 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800898 * Called when the launcher is ready to use the overlay
899 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700900 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700901 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700902 }
903
904 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700905 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700906 }
907
908 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
909
Sunny Goyalc86df472016-02-25 09:19:38 -0800910 public void onScrollChanged(float progress) {
911 if (mWorkspace != null) {
912 mWorkspace.onOverlayScrollChanged(progress);
913 }
914 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700915 }
916
Sunny Goyal16764582017-11-06 16:00:34 -0800917 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700918 if (mLauncherCallbacks != null) {
919 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700920 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800921 // On O and above we there is always some setting present settings (add icon to
922 // home screen or icon badging). On earlier APIs we will have the allow rotation
923 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700924 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700925 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800926 }
927
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700928 @Override
929 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -0700931 if (mModel.isCurrentCallbacks(this)) {
932 mModel.stopLoader();
933 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700934 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
935
Romain Guy13c2e7b2010-03-10 19:45:00 -0800936 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700937 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700938
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800939 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800940 @Override
941 public void onWindowFocusChanged(boolean hasFocus) {
942 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -0700943 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -0700944
945 if (mLauncherCallbacks != null) {
946 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
947 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800948 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800949
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 private boolean acceptFilter() {
951 final InputMethodManager inputManager = (InputMethodManager)
952 getSystemService(Context.INPUT_METHOD_SERVICE);
953 return !inputManager.isFullscreenMode();
954 }
955
956 @Override
957 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700958 final int uniChar = event.getUnicodeChar();
959 final boolean handled = super.onKeyDown(keyCode, event);
960 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
961 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
963 keyCode, event);
964 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700966 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 // showSearchDialog()
968 // If there are multiple keystrokes before the search dialog takes focus,
969 // onSearchRequested() will be called for every keystroke,
970 // but it is idempotent, so it's fine.
971 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 }
973 }
974
Joe Onorato8a9625e2010-01-28 15:55:35 -0800975 // Eat the long press event so the keyboard doesn't come up.
976 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
977 return true;
978 }
979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 return handled;
981 }
982
Karl Rosaen138a0412009-04-23 19:00:21 -0700983 private String getTypedText() {
984 return mDefaultKeySsb.toString();
985 }
986
Sunny Goyal6f28e712016-09-13 14:19:29 -0700987 @Override
988 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700989 mDefaultKeySsb.clear();
990 mDefaultKeySsb.clearSpans();
991 Selection.setSelection(mDefaultKeySsb, 0);
992 }
993
Sunny Goyalf9403d92017-10-18 10:55:56 -0700994 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700995 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700996 }
997
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 /**
999 * Restores the previous state, if it exists.
1000 *
1001 * @param savedState The previous state.
1002 */
1003 private void restoreState(Bundle savedState) {
1004 if (savedState == null) {
1005 return;
1006 }
1007
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001008 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -07001009 LauncherState[] stateValues = LauncherState.values();
1010 LauncherState state = stateValues[stateOrdinal];
1011 if (!state.doNotRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001012 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001013 }
1014
Sunny Goyal2100c782016-08-22 16:00:03 -07001015 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1016 if (requestArgs != null) {
1017 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001019
1020 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001021
1022 SparseArray<Parcelable> widgetsState =
1023 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
1024 if (widgetsState != null) {
1025 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
1026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 }
1028
1029 /**
1030 * Finds all the views we need and configure them properly.
1031 */
1032 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001033 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001034 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001035 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001036 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -08001037 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -07001038
Sunny Goyal784f9c32016-03-23 16:56:54 -07001039 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1040 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1041 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042
Winson Chung4c98d922011-05-31 16:50:48 -07001043 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001044 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001045
Winson Chung3d503fb2011-07-13 17:25:49 -07001046 // Setup the hotseat
1047 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1048 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001049 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 }
1051
Winson Chung4c98d922011-05-31 16:50:48 -07001052 // Setup the workspace
1053 mWorkspace.setHapticFeedbackEnabled(false);
1054 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001055 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001056 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1057 // default state, otherwise we will update to the wrong offsets in RTL
1058 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001059 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001060 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001061
Tony Wickham34d2c912015-09-11 09:27:58 -07001062 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -07001063 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001064
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001065 // Setup Apps
1066 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001067
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001069 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001070 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001071
Hyunyoung Songd725f642017-08-17 22:26:35 -07001072 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 }
1074
1075 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001076 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001077 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001078 */
1079 public void setAllAppsButton(View allAppsButton) {
1080 mAllAppsButton = allAppsButton;
1081 }
1082
Anjali Koppal5ad44842014-03-10 20:34:39 -07001083 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 * Creates a view representing a shortcut.
1085 *
1086 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001088 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001089 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
1092 /**
1093 * Creates a view representing a shortcut inflated from the specified resource.
1094 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 * @param parent The group the shortcut belongs to.
1096 * @param info The data structure describing the shortcut.
1097 *
1098 * @return A View inflated from layoutResId.
1099 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001100 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001101 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1102 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001103 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001105 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 return favorite;
1107 }
1108
1109 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001110 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 *
1112 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001115 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001116 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1117 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001118 return;
1119 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001120
Jon Mirandac476d6e2017-05-04 16:30:01 -07001121 int[] cellXY = mTmpAddItemCellCoordinates;
1122 CellLayout layout = getCellLayout(container, screenId);
1123
Sunny Goyal782f0c92017-01-19 10:27:54 -08001124 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001125 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001126 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1127 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001128 }
1129
1130 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001131 // Legacy shortcuts are only supported for primary profile.
1132 info = Process.myUserHandle().equals(args.user)
1133 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001134
Sunny Goyalb57645f2017-03-20 13:57:28 -07001135 if (info == null) {
1136 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1137 return;
1138 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001139 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001140 // The app is trying to add a shortcut without sufficient permissions
1141 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1142 return;
1143 }
1144 }
1145
Jon Mirandac476d6e2017-05-04 16:30:01 -07001146 if (container < 0) {
1147 // Adding a shortcut to the Workspace.
1148 final View view = createShortcut(info);
1149 boolean foundCellSpan = false;
1150 // First we check if we already know the exact location where we want to add this item.
1151 if (cellX >= 0 && cellY >= 0) {
1152 cellXY[0] = cellX;
1153 cellXY[1] = cellY;
1154 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001155
Jon Mirandac476d6e2017-05-04 16:30:01 -07001156 // If appropriate, either create a folder or add to an existing folder
1157 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001158 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001159 return;
1160 }
1161 DragObject dragObject = new DragObject();
1162 dragObject.dragInfo = info;
1163 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1164 true)) {
1165 return;
1166 }
1167 } else {
1168 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1169 }
1170
1171 if (!foundCellSpan) {
1172 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001173 return;
1174 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001175
1176 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1177 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001178 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001179 // Adding a shortcut to a Folder.
Sunny Goyale29897f2017-07-20 10:09:42 -07001180 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001181 if (folderIcon != null) {
1182 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1183 folderInfo.add(info, args.rank, false);
1184 } else {
Sunny Goyale29897f2017-07-20 10:09:42 -07001185 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001186 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
1189
Sunny Goyale29897f2017-07-20 10:09:42 -07001190 public FolderIcon findFolderIcon(final long folderIconId) {
1191 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1192 @Override
1193 public boolean evaluate(ItemInfo info, View view) {
1194 return info != null && info.id == folderIconId;
1195 }
1196 });
1197 }
1198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001200 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001202 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001204 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001205 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1206
Adam Cohened66b2b2012-01-23 17:28:51 -08001207 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001208 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001209 }
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Adam Cohen59400422014-03-05 18:07:04 -08001211 LauncherAppWidgetInfo launcherInfo;
1212 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001213 launcherInfo.spanX = itemInfo.spanX;
1214 launcherInfo.spanY = itemInfo.spanY;
1215 launcherInfo.minSpanX = itemInfo.minSpanX;
1216 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001217 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001218
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001219 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001220 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221
Sunny Goyal2100c782016-08-22 16:00:03 -07001222 if (hostView == null) {
1223 // Perform actual inflation because we're live
1224 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001226 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301227 prepareAppWidget(hostView, launcherInfo);
1228 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
Romain Guycbb89e42009-06-08 15:52:54 -07001230
Sunny Goyald5462aa2016-11-22 16:52:39 +05301231 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001232 hostView.setTag(item);
1233 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001234 hostView.setFocusable(true);
1235 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001236 }
1237
Adam Cohended9f8d2010-11-03 13:25:16 -07001238 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1239 @Override
1240 public void onReceive(Context context, Intent intent) {
1241 final String action = intent.getAction();
1242 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001243 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001244 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001245 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001246 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001247 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001248 mShouldFadeInScrim = true;
1249 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1250 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1251 // the user unlocked and the Launcher is not in the foreground.
1252 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001253 }
1254 }
1255 };
1256
Tony Wickham010d2552017-01-20 08:15:28 -08001257 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1258 Runnable r = new Runnable() {
1259 @Override
1260 public void run() {
1261 mWorkspace.updateIconBadges(updatedBadges);
1262 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001263
Sunny Goyal10a1bd02017-10-09 14:56:21 -07001264 BaseActionPopup popup = BaseActionPopup.getOpen(Launcher.this);
1265 if (popup instanceof PopupContainerWithArrow) {
1266 ((PopupContainerWithArrow) popup).updateNotificationHeader(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001267 }
Tony Wickham010d2552017-01-20 08:15:28 -08001268 }
1269 };
1270 if (!waitUntilResume(r)) {
1271 r.run();
1272 }
1273 }
1274
Adam Cohended9f8d2010-11-03 13:25:16 -07001275 @Override
1276 public void onAttachedToWindow() {
1277 super.onAttachedToWindow();
1278
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001279 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001280 if (mLauncherCallbacks != null) {
1281 mLauncherCallbacks.onAttachedToWindow();
1282 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001283 }
1284
1285 @Override
1286 public void onDetachedFromWindow() {
1287 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001288
1289 if (mLauncherCallbacks != null) {
1290 mLauncherCallbacks.onDetachedFromWindow();
1291 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001292 }
1293
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001294 public AllAppsTransitionController getAllAppsController() {
1295 return mAllAppsController;
1296 }
1297
Winson Chunga6945242014-01-08 14:04:34 -08001298 public DragLayer getDragLayer() {
1299 return mDragLayer;
1300 }
1301
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001302 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001303 return mAppsView;
1304 }
1305
Winson Chunga6945242014-01-08 14:04:34 -08001306 public Workspace getWorkspace() {
1307 return mWorkspace;
1308 }
1309
1310 public Hotseat getHotseat() {
1311 return mHotseat;
1312 }
1313
Winson Chung8ae41982017-11-10 11:42:13 -08001314 public <T extends ViewGroup> T getOverviewPanel() {
1315 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001316 }
1317
Sunny Goyal47328fd2016-05-25 18:56:41 -07001318 public DropTargetBar getDropTargetBar() {
1319 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001320 }
1321
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001322 public LauncherAppWidgetHost getAppWidgetHost() {
1323 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 }
Romain Guycbb89e42009-06-08 15:52:54 -07001325
Winson Chunga9abd0e2010-10-27 17:18:37 -07001326 public LauncherModel getModel() {
1327 return mModel;
1328 }
1329
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001330 public ModelWriter getModelWriter() {
1331 return mModelWriter;
1332 }
1333
Adam Cohen79d90c52016-04-22 13:29:20 -07001334 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001335 return mSharedPrefs;
1336 }
1337
Jon Miranda6bed3502017-09-19 14:43:17 -07001338 public int getOrientation() { return mOrientation; }
1339
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 @Override
1341 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001342 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 super.onNewIntent(intent);
1344
Winson15f8b172015-08-19 11:02:39 -07001345 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1346 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1347 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001348
1349 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001350 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001351 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001352 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001353 boolean internalStateHandled = InternalStateHandler
1354 .handleNewIntent(this, intent, alreadyOnHome);
1355
Winson15f8b172015-08-19 11:02:39 -07001356 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001357 if (!internalStateHandled) {
1358 // Note: There should be at most one log per method call. This is enforced
1359 // implicitly by using if-else statements.
1360 UserEventDispatcher ued = getUserEventDispatcher();
1361 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1362 if (topOpenView != null) {
1363 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1364 } else if (alreadyOnHome) {
1365 Target target = newContainerTarget(mStateManager.getState().containerType);
1366 target.pageIndex = mWorkspace.getCurrentPage();
1367 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1368 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001369
Sunny Goyald3864fa2017-11-14 15:06:36 -08001370 // In all these cases, only animate if we're already on home
1371 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001372
Sunny Goyald3864fa2017-11-14 15:06:36 -08001373 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1374
1375 // Reset the apps view
1376 if (!alreadyOnHome && mAppsView != null) {
1377 mAppsView.reset();
1378 }
1379
1380 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1381 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1382 }
1383 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001384
1385 final View v = getWindow().peekDecorView();
1386 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001387 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001388 }
1389
Adam Cohen9211d422014-10-07 18:14:53 -07001390 if (mLauncherCallbacks != null) {
1391 mLauncherCallbacks.onHomeIntent();
1392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
Winson15f8b172015-08-19 11:02:39 -07001394
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001395 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001399 public void onRestoreInstanceState(Bundle state) {
1400 super.onRestoreInstanceState(state);
1401 for (int page: mSynchronouslyBoundPages) {
1402 mWorkspace.restoreInstanceStateForChild(page);
1403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
1406 @Override
1407 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001408 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001409 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001410
Adam Cohen21cd0022013-10-09 18:57:02 -07001411 }
Sunny Goyalea609262017-10-25 15:47:38 -07001412 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001413
1414
1415 AbstractFloatingView widgets = AbstractFloatingView
1416 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1417 if (widgets != null) {
1418 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1419 widgets.saveHierarchyState(widgetsState);
1420 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1421 } else {
1422 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1423 }
1424
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001425 // We close any open folders and shortcut containers since they will not be re-opened,
1426 // and we need to make sure this state is reflected.
1427 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001428
Sunny Goyal2100c782016-08-22 16:00:03 -07001429 if (mPendingRequestArgs != null) {
1430 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1431 }
1432 if (mPendingActivityResult != null) {
1433 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001436 super.onSaveInstanceState(outState);
1437
Adam Cohen9211d422014-10-07 18:14:53 -07001438 if (mLauncherCallbacks != null) {
1439 mLauncherCallbacks.onSaveInstanceState(outState);
1440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442
1443 @Override
1444 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001446
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001447 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001448 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001449
Winson Chungcd2b0142011-06-08 16:02:26 -07001450 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001451 // It's possible to receive onDestroy after a new Launcher activity has
1452 // been created. In this case, don't interfere with the new Launcher.
1453 if (mModel.isCurrentCallbacks(this)) {
1454 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001455 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001456 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001457
Sunny Goyal745bad92016-05-02 10:54:12 -07001458 if (mRotationPrefChangeHandler != null) {
1459 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1460 }
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001463 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001465 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001467 mAppWidgetHost = null;
1468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001470 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1471
Michael Jurka2ecf9952012-06-18 12:52:28 -07001472 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001473
Tony2917a8b2017-07-31 23:29:42 -07001474 clearPendingBinds();
1475
Adam Cohen9211d422014-10-07 18:14:53 -07001476 if (mLauncherCallbacks != null) {
1477 mLauncherCallbacks.onDestroy();
1478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
Sunny Goyalae502842016-06-17 08:43:56 -07001481 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1482 return mAccessibilityDelegate;
1483 }
1484
Adam Cohena9cf38f2011-05-02 15:36:58 -07001485 public DragController getDragController() {
1486 return mDragController;
1487 }
1488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001490 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001491 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001492 }
1493
1494 @Override
1495 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1496 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001497 try {
1498 super.startIntentSenderForResult(intent, requestCode,
1499 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1500 } catch (IntentSender.SendIntentException e) {
1501 throw new ActivityNotFoundException();
1502 }
1503 }
1504
Winson Chung70d72102011-08-12 11:24:35 -07001505 /**
1506 * Indicates that we want global search for this activity by setting the globalSearch
1507 * argument for {@link #startSearch} to true.
1508 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001510 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001512
Karl Rosaen138a0412009-04-23 19:00:21 -07001513 if (initialQuery == null) {
1514 // Use any text typed in the launcher as the initial query
1515 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001516 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 if (appSearchData == null) {
1518 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001519 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001521
Sunny Goyal6f28e712016-09-13 14:19:29 -07001522 if (mLauncherCallbacks == null ||
1523 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1524 // Starting search from the callbacks failed. Start the default global search.
1525 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001526 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001527
1528 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001529 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001530 }
1531
Ian Parkinson047036e2014-09-01 15:40:53 +01001532 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001533 * Starts the global search activity. This code is a copied from SearchManager
1534 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001535 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001536 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001537 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001538 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1539 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1540 if (globalSearchActivity == null) {
1541 Log.w(TAG, "No global search activity found.");
1542 return;
1543 }
1544 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1545 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1546 intent.setComponent(globalSearchActivity);
1547 // Make sure that we have a Bundle to put source in
1548 if (appSearchData == null) {
1549 appSearchData = new Bundle();
1550 } else {
1551 appSearchData = new Bundle(appSearchData);
1552 }
Adam Cohen9211d422014-10-07 18:14:53 -07001553 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001554 if (!appSearchData.containsKey("source")) {
1555 appSearchData.putString("source", getPackageName());
1556 }
1557 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1558 if (!TextUtils.isEmpty(initialQuery)) {
1559 intent.putExtra(SearchManager.QUERY, initialQuery);
1560 }
1561 if (selectInitialQuery) {
1562 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1563 }
1564 intent.setSourceBounds(sourceBounds);
1565 try {
1566 startActivity(intent);
1567 } catch (ActivityNotFoundException ex) {
1568 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
Winson Chung70d72102011-08-12 11:24:35 -07001572 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001573 public boolean onPrepareOptionsMenu(Menu menu) {
1574 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001575 if (mLauncherCallbacks != null) {
1576 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1577 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001578 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001581 @Override
1582 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001583 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001584 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001585 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001586 }
1587
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001589 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590 }
1591
Adam Cohen517a7f52014-03-01 12:12:59 -08001592 public boolean isWorkspaceLoading() {
1593 return mWorkspaceLoading;
1594 }
1595
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001596 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001597 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001598 }
1599
Sunny Goyal04a324a2017-01-20 21:08:59 -08001600 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001601 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001602 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001603
Sunny Goyal2100c782016-08-22 16:00:03 -07001604 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001605 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001606 if (LOGD) {
1607 Log.d(TAG, "Adding widget from drop");
1608 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001609 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001610 }
1611
Sunny Goyal2100c782016-08-22 16:00:03 -07001612 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001613 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1614 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1615 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001616
Adam Cohenad4e15c2013-10-17 16:21:35 -07001617 Runnable onComplete = new Runnable() {
1618 @Override
1619 public void run() {
1620 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001621 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001622 }
1623 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001624 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001625 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 }
1627 }
Romain Guycbb89e42009-06-08 15:52:54 -07001628
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001629 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1630 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001631 info.container = container;
1632 info.screenId = screenId;
1633 if (cell != null) {
1634 info.cellX = cell[0];
1635 info.cellY = cell[1];
1636 }
1637 info.spanX = spanX;
1638 info.spanY = spanY;
1639
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001640 switch (info.itemType) {
1641 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1642 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001643 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001644 break;
1645 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001646 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001647 break;
1648 default:
1649 throw new IllegalStateException("Unknown item type: " + info.itemType);
1650 }
1651 }
1652
Adam Cohenfbba09b2011-07-18 21:43:05 -07001653 /**
1654 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001655 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001656 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001657 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1658 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001659 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1660 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1661 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001662 }
1663
Adam Cohenfbba09b2011-07-18 21:43:05 -07001664 /**
1665 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001666 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001667 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001668 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001669 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001670 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001671 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001672 // In the case where we've prebound the widget, we remove it from the DragLayer
1673 if (LOGD) {
1674 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1675 }
1676 getDragLayer().removeView(hostView);
1677
Adam Cohened66b2b2012-01-23 17:28:51 -08001678 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001679 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001680
1681 // Clear the boundWidget so that it doesn't get destroyed.
1682 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001683 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001684 // In this case, we either need to start an activity to get permission to bind
1685 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001686 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1687 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1688 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1689 this, info.componentName);
1690 } else {
1691 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1692 }
Adam Cohendd70d662012-10-04 16:53:44 -07001693 Bundle options = info.bindOptions;
1694
Sunny Goyalffe83f12014-08-14 17:39:34 -07001695 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1696 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001697 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001698 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001699 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001700 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001701 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001702 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001703 }
1704
Adam Cohendcd297f2013-06-18 13:13:40 -07001705 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001706 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001707 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 folderInfo.title = getText(R.string.folder_name);
1709
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001711 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712
1713 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001714 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301715 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001716 // Force measure the new folder icon
1717 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1718 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001719 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Winsonc0b52fe2015-09-09 16:38:15 -07001722 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001723 * Unbinds the view for the specified item, and removes the item and all its children.
1724 *
1725 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001726 * @param itemInfo the {@link ItemInfo} for this view.
1727 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001728 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001729 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001730 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001731 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001732 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1733 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001734 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001735 } else {
1736 mWorkspace.removeWorkspaceItem(v);
1737 }
Winsonc0b52fe2015-09-09 16:38:15 -07001738 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001739 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001740 }
1741 } else if (itemInfo instanceof FolderInfo) {
1742 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001743 if (v instanceof FolderIcon) {
1744 ((FolderIcon) v).removeListeners();
1745 }
Winsonc0b52fe2015-09-09 16:38:15 -07001746 mWorkspace.removeWorkspaceItem(v);
1747 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001748 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001749 }
1750 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1751 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001752 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001753 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001754 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001755 }
1756 } else {
1757 return false;
1758 }
1759 return true;
1760 }
1761
1762 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001763 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001764 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001765 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001766 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001767 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001768 // Deleting an app widget ID is a void call but writes to disk before returning
1769 // to the caller...
1770 new AsyncTask<Void, Void, Void>() {
1771 public Void doInBackground(Void ... args) {
1772 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1773 return null;
1774 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001775 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001776 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001777 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001778 }
1779
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 @Override
1781 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001782 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
1784
Joe Onorato88ec0992009-11-19 13:16:06 -08001785 @Override
1786 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001787 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1788 return;
1789 }
1790
Sunny Goyal45478022015-06-08 16:52:41 -07001791 if (mDragController.isDragging()) {
1792 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001793 return;
1794 }
1795
Jon Mirandafeba90f2016-10-06 10:53:29 -07001796 // Note: There should be at most one log per method call. This is enforced implicitly
1797 // by using if-else statements.
1798 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001799 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1800 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001801 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001802 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001803 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001804 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001805 } else {
1806 // Back button is a no-op here, but give at least some feedback for the button press
1807 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001808 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001809 }
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 * Launches the intent referred by the clicked shortcut.
1813 *
1814 * @param v The view representing the clicked shortcut.
1815 */
1816 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001817 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1818 // view has detached (it's possible for this to happen if the view is removed mid touch).
1819 if (v.getWindowToken() == null) {
1820 return;
1821 }
1822
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001823 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001824 return;
1825 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001826
Adam Cohen1697b792013-09-17 19:08:21 -07001827 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001828 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001829 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1830 LauncherLogProto.Action.Direction.NONE,
1831 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001832 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001833 }
1834 return;
1835 }
1836
1837 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001838 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001839 int page = mWorkspace.indexOfChild(v);
1840 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1841 LauncherLogProto.Action.Direction.NONE,
1842 LauncherLogProto.ContainerType.OVERVIEW, page);
1843 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001844 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001845 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001846 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001847 }
1848
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001850 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001851 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001853 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001854 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001855 }
Hyunyoung Songd725f642017-08-17 22:26:35 -07001856 } else if ((v instanceof PageIndicator) ||
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001857 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001858 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07001859 } else if (tag instanceof AppInfo) {
1860 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001861 } else if (tag instanceof LauncherAppWidgetInfo) {
1862 if (v instanceof PendingAppWidgetHostView) {
1863 onClickPendingWidget((PendingAppWidgetHostView) v);
1864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866 }
1867
Sunny Goyal70660032015-05-14 00:07:08 -07001868 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001869 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001870 return false;
1871 }
1872
Michael Jurkaaf442092010-06-10 17:01:57 -07001873 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001874 * Event handler for the app widget view which has not fully restored.
1875 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001876 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001877 if (mIsSafeModeEnabled) {
1878 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1879 return;
1880 }
1881
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001882 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001883 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001884 LauncherAppWidgetProviderInfo appWidgetInfo =
1885 mAppWidgetManager.findProvider(info.providerName, info.user);
1886 if (appWidgetInfo == null) {
1887 return;
1888 }
1889 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1890
Sunny Goyald478c832016-04-01 12:04:16 -07001891 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1892 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1893 // This should not happen, as we make sure that an Id is allocated during bind.
1894 return;
1895 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001896 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1897 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001898 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001899 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001900 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001901 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001902 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001903 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001904 }
1905 }
1906
1907 /**
Adam Cohenbf78f3c2017-07-14 15:58:48 -07001908 * Event handler for the "grid" button or "caret" that appears on the home screen, which
1909 * enters all apps mode. In verticalBarLayout the caret can be seen when all apps is open, and
1910 * so in that case reverses the action.
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001911 *
1912 * @param v The view that was clicked.
1913 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001914 protected void onClickAllAppsButton(View v) {
1915 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001916 if (!isInState(ALL_APPS)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301917 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
1918 ControlType.ALL_APPS_BUTTON);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001919 mStateManager.goToState(ALL_APPS);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001920 } else {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001921 mStateManager.goToState(NORMAL);
Adam Cohen5441a9d2017-07-14 14:22:05 -07001922 }
1923 }
1924
Sunny Goyale6e72002017-01-12 16:55:36 -08001925 private void onClickPendingAppItem(final View v, final String packageName,
1926 boolean downloadStarted) {
1927 if (downloadStarted) {
1928 // If the download has started, simply direct to the market app.
1929 startMarketIntentForPackage(v, packageName);
1930 return;
1931 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001932 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001933 .setTitle(R.string.abandoned_promises_title)
1934 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001935 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1936 @Override
1937 public void onClick(DialogInterface dialogInterface, int i) {
1938 startMarketIntentForPackage(v, packageName);
1939 }
1940 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001941 .setNeutralButton(R.string.abandoned_clean_this,
1942 new DialogInterface.OnClickListener() {
1943 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001944 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001945 mWorkspace.removeAbandonedPromise(packageName, user);
1946 }
1947 })
1948 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001949 }
1950
1951 private void startMarketIntentForPackage(View v, String packageName) {
1952 ItemInfo item = (ItemInfo) v.getTag();
1953 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001954 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001955 }
1956
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001957 /**
1958 * Event handler for an app shortcut click.
1959 *
1960 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1961 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001962 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001963 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1964 Object tag = v.getTag();
1965 if (!(tag instanceof ShortcutInfo)) {
1966 throw new IllegalArgumentException("Input must be a Shortcut");
1967 }
1968
1969 // Open shortcut
1970 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001971
Sunny Goyal076839c2017-10-30 13:52:20 -07001972 if (shortcut.isDisabled()) {
1973 if ((shortcut.runtimeStatusFlags &
1974 ~FLAG_DISABLED_SUSPENDED &
1975 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001976 // If the app is only disabled because of the above flags, launch activity anyway.
1977 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001978 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001979 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1980 // Use a message specific to this shortcut, if it has one.
1981 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1982 return;
1983 }
1984 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001985 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001986 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001987 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001988 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1989 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001990 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001991 }
1992 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1993 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001994 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001995 }
1996
Chris Wren40c5ed32014-06-24 18:24:23 -04001997 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001998 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001999 String packageName = shortcut.intent.getComponent() != null ?
2000 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2001 if (!TextUtils.isEmpty(packageName)) {
2002 onClickPendingAppItem(v, packageName,
2003 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2004 return;
2005 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002006 }
2007
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002008 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002009 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002010 }
2011
Sunny Goyala7ce1662016-05-31 15:01:35 -07002012 private void startAppShortcutOrInfoActivity(View v) {
2013 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002014 Intent intent;
2015 if (item instanceof PromiseAppInfo) {
2016 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2017 intent = promiseAppInfo.getMarketIntent();
2018 } else {
2019 intent = item.getIntent();
2020 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002021 if (intent == null) {
2022 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002023 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002024 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002025 }
2026
2027 /**
2028 * Event handler for a folder icon click.
2029 *
2030 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2031 */
2032 protected void onClickFolderIcon(View v) {
2033 if (LOGD) Log.d(TAG, "onClickFolder");
2034 if (!(v instanceof FolderIcon)){
2035 throw new IllegalArgumentException("Input must be a FolderIcon");
2036 }
2037
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002038 Folder folder = ((FolderIcon) v).getFolder();
2039 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002040 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002041 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002042 }
Michael Jurka5130e402011-10-13 04:55:35 -07002043 }
2044
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002045 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002046 * Event handler for the wallpaper picker button that appears after a long press
2047 * on the home screen.
2048 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002049 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002050 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002051 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2052 return;
2053 }
2054
Tony Wickham785f7a52015-08-31 17:28:32 -07002055 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2056 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002057 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002058 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002059 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002060
2061 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002062 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2063 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002064 intent.setPackage(pickerPackage);
2065 }
2066
Sunny Goyala5ace712017-11-15 17:12:51 -08002067 final Bundle launchOptions;
2068 if (v != null) {
2069 intent.setSourceBounds(getViewBounds(v));
2070 // If there is no target package, use the default intent chooser animation
2071 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
2072 } else {
2073 launchOptions = null;
2074 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002075 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08002076 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002077 } catch (ActivityNotFoundException e) {
2078 setWaitingForResult(null);
2079 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2080 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002081 }
2082
Sunny Goyala7ce1662016-05-31 15:01:35 -07002083 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002085 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2086 try {
2087 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2088 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2089 // is enabled by default on NYC.
2090 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2091 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002092
2093 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2094 String id = ((ShortcutInfo) info).getDeepShortcutId();
2095 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302096 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002097 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002098 } else {
2099 // Could be launching some bookkeeping activity
2100 startActivity(intent, optsBundle);
2101 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002102 } finally {
2103 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002106 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2107 // corresponding permission. Show the appropriate permission prompt if that
2108 // is the case.
2109 if (intent.getComponent() == null
2110 && Intent.ACTION_CALL.equals(intent.getAction())
2111 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2112 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002113
2114 setWaitingForResult(PendingRequestArgs
2115 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002116 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2117 REQUEST_PERMISSION_CALL_PHONE);
2118 } else {
2119 // No idea why this was thrown.
2120 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002124
Sunny Goyalfe770c92016-09-27 14:38:58 -07002125 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002126 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002127 if (Utilities.ATLEAST_MARSHMALLOW) {
2128 int left = 0, top = 0;
2129 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002130 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002131 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07002132 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07002133 if (icon != null) {
2134 Rect bounds = icon.getBounds();
2135 left = (width - bounds.width()) / 2;
2136 top = v.getPaddingTop();
2137 width = bounds.width();
2138 height = bounds.height();
2139 }
2140 }
2141 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2142 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2143 // On L devices, we use the device default slide-up transition.
2144 // On L MR1 devices, we use a custom version of the slide-up transition which
2145 // doesn't have the delay present in the device default.
2146 return ActivityOptions.makeCustomAnimation(
2147 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2148 }
2149 return null;
2150 }
2151
Tonye3c59252017-05-02 21:32:27 -07002152 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002153 int[] pos = new int[2];
2154 v.getLocationOnScreen(pos);
2155 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2156 }
2157
Sunny Goyala7ce1662016-05-31 15:01:35 -07002158 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002159 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002160 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2161 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002162 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002163 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002164 // Only launch using the new animation if the shortcut has not opted out (this is a
2165 // private contract between launcher and may be ignored in the future).
2166 boolean useLaunchAnimation = (v != null) &&
2167 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2168 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2169
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002170 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002171
2172 // Prepare intent
2173 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2174 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002175 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002176 }
2177 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002178 if (Utilities.ATLEAST_MARSHMALLOW
2179 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002180 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002181 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002182 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002183 // Shortcuts need some special checks due to legacy reasons.
2184 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002185 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002186 // Could be launching some bookkeeping activity
2187 startActivity(intent, optsBundle);
2188 } else {
2189 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2190 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2191 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002192
2193 if (v instanceof BubbleTextView) {
2194 // This is set to the view that launched the activity that navigated the user away
2195 // from launcher. Since there is no callback for when the activity has finished
2196 // launching, enable the press state and keep this reference to reset the press
2197 // state when we return to launcher.
2198 BubbleTextView btv = (BubbleTextView) v;
2199 btv.setStayPressed(true);
2200 setOnResumeCallback(btv);
2201 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002202 mAppLaunchSuccess = true;
2203 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002204 } catch (ActivityNotFoundException|SecurityException e) {
2205 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2206 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2207 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002208 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002209 }
2210
Tony Wickhamdadb3042016-02-24 11:07:00 -08002211 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002212 public boolean dispatchTouchEvent(MotionEvent ev) {
2213 mLastDispatchTouchEventX = ev.getX();
2214 return super.dispatchTouchEvent(ev);
2215 }
2216
2217 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002219 if (!isDraggingEnabled()) return false;
2220 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002221 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222
Jon Miranda51f037d2016-11-07 08:37:20 -08002223 boolean ignoreLongPressToOverview =
2224 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002225
Adam Cohen1697b792013-09-17 19:08:21 -07002226 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002227 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002228 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302229 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2230 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002231 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002232 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002233 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2234 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2235 return true;
2236 } else {
2237 return false;
2238 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002239 } else {
2240 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002241 }
Adam Cohen1697b792013-09-17 19:08:21 -07002242 }
2243
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002244 CellLayout.CellInfo longClickCellInfo = null;
2245 View itemUnderLongClick = null;
2246 if (v.getTag() instanceof ItemInfo) {
2247 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002248 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002249 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002250 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 }
2252
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 // The hotseat touch handling does not go through Workspace, and we always allow long press
2254 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002255 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002257 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002258 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002259 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302260 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2261 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002262 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002263 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002264 return false;
2265 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302266 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2267 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002268 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002269 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002270 }
Jon Miranda379198e2016-09-23 08:54:40 -07002271 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2272 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002274 final boolean isAllAppsButton =
2275 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2276 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2277 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002278 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002280 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 }
2282 }
2283 }
2284 return true;
2285 }
2286
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002288 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002289 return mHotseat != null && layout != null &&
2290 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2291 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002292
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 /**
2294 * Returns the CellLayout of the specified container at the specified screen.
2295 */
Sunny Goyal92820592015-03-02 11:31:35 -08002296 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2298 if (mHotseat != null) {
2299 return mHotseat.getLayout();
2300 } else {
2301 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002302 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002304 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002305 }
2306 }
2307
Michael Jurkae326f182011-11-21 14:05:46 -08002308 @Override
2309 public void onTrimMemory(int level) {
2310 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002311 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2312 // The widget preview db can result in holding onto over
2313 // 3MB of memory for caching which isn't necessary.
2314 SQLiteDatabase.releaseMemory();
2315
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002316 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002317 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002318 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002319 if (mLauncherCallbacks != null) {
2320 mLauncherCallbacks.onTrimMemory(level);
2321 }
Michael Jurkae326f182011-11-21 14:05:46 -08002322 }
2323
Michael Jurkad7c28052012-04-27 15:43:36 -07002324 @Override
2325 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002326 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002327 final List<CharSequence> text = event.getText();
2328 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002329 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002330 // TODO: When can workspace be null?
2331 text.add(mWorkspace == null
2332 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002333 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002334 return result;
2335 }
2336
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002337 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07002338 * If the activity is currently paused, signal that we need to run the passed Runnable
2339 * in onResume.
2340 *
2341 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002342 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
2343 * wrong when we're not running, and if the activity comes back to what the configuration was
2344 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07002345 *
2346 * Implementation of the method from LauncherModel.Callbacks.
2347 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002348 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07002349 */
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002350 @Thunk boolean waitUntilResume(Runnable run) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002351 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002352 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002353 if (run instanceof RunnableWithId) {
2354 // Remove any runnables which have the same id
2355 while (mBindOnResumeCallbacks.remove(run)) { }
Michael Jurkac402cd92013-05-20 15:49:32 +02002356 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07002357 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07002358 return true;
2359 } else {
2360 return false;
2361 }
2362 }
2363
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002364 public void setOnResumeCallback(OnResumeCallback callback) {
2365 if (mOnResumeCallback != null) {
2366 mOnResumeCallback.onLauncherResume();
2367 }
2368 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002369 }
2370
Michael Jurka7607c2f2013-04-03 14:33:19 -07002371 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002372 * If the activity is currently paused, signal that we need to re-run the loader
2373 * in onResume.
2374 *
2375 * This needs to be called from incoming places where resources might have been loaded
2376 * while we are paused. That is becaues the Configuration might be wrong
2377 * when we're not running, and if it comes back to what it was when we
2378 * were paused, we are not restarted.
2379 *
2380 * Implementation of the method from LauncherModel.Callbacks.
2381 *
2382 * @return true if we are currently paused. The caller might be able to
2383 * skip some work in that case since we will come back again.
2384 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002385 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002386 public boolean setLoadOnResume() {
2387 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002388 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002389 mOnResumeNeedsLoad = true;
2390 return true;
2391 } else {
2392 return false;
2393 }
2394 }
2395
2396 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002397 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002399 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002400 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002401 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002402 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002403 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002404 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002406 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002407
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002409 * Clear any pending bind callbacks. This is called when is loader is planning to
2410 * perform a full rebind from scratch.
2411 */
2412 @Override
2413 public void clearPendingBinds() {
2414 mBindOnResumeCallbacks.clear();
2415 if (mPendingExecutor != null) {
2416 mPendingExecutor.markCompleted();
2417 mPendingExecutor = null;
2418 }
2419 }
2420
2421 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 * Refreshes the shortcuts shown on the workspace.
2423 *
2424 * Implementation of the method from LauncherModel.Callbacks.
2425 */
2426 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002427 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002428 // Floating panels (except the full widget sheet) are associated with individual icons. If
2429 // we are starting a fresh bind, close all such panels as all the icons are about
2430 // to go away.
2431 AbstractFloatingView.closeOpenViews(this, true,
2432 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002433
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002434 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002435
Winson Chungd64d1762013-08-20 14:37:16 -07002436 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002437 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002438 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002439
Winson Chung3d503fb2011-07-13 17:25:49 -07002440 if (mHotseat != null) {
2441 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002443 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 }
2445
Adam Cohendcd297f2013-06-18 13:13:40 -07002446 @Override
2447 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002448 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002449 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2450 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002451 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2452 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002453 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002454 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2455 // If there are no screens, we need to have an empty screen
2456 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002457 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002458 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002459
Winsonc7d2e832016-07-28 12:24:55 -07002460 // After we have added all the screens, if the wallpaper was locked to the default state,
2461 // then notify to indicate that it can be released and a proper wallpaper offset can be
2462 // computed before the next layout
2463 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002464 }
2465
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002466 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002467 int count = orderedScreenIds.size();
2468 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002469 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002470 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002471 // No need to bind the first screen, as its always bound.
2472 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2473 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 }
2476
Sunny Goyalb23980c2017-08-17 07:45:25 -07002477 @Override
Winson Chungd64d1762013-08-20 14:37:16 -07002478 public void bindAppsAdded(final ArrayList<Long> newScreens,
2479 final ArrayList<ItemInfo> addNotAnimated,
Sunny Goyalb23980c2017-08-17 07:45:25 -07002480 final ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002481 Runnable r = new Runnable() {
2482 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002483 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
Winson Chungd64d1762013-08-20 14:37:16 -07002484 }
2485 };
2486 if (waitUntilResume(r)) {
2487 return;
2488 }
2489
Winson Chungd64d1762013-08-20 14:37:16 -07002490 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002491 if (newScreens != null) {
2492 bindAddScreens(newScreens);
2493 }
Winson Chungd64d1762013-08-20 14:37:16 -07002494
2495 // We add the items without animation on non-visible pages, and with
2496 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002497 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002498 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002499 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002500 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002501 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002502 }
Winson Chungc58497e2013-09-03 17:48:37 -07002503
Winson Chung87412982013-10-03 18:34:14 -07002504 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002505 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002509 * Bind the items start-end from the list.
2510 *
2511 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002513 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002514 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07002515 Runnable r = new Runnable() {
2516 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002517 bindItems(items, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07002518 }
2519 };
2520 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002521 return;
2522 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002523
Sunny Goyal3be633b2016-12-08 09:59:25 -08002524 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002525 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002526 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002527 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002528 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002529 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002530 int end = items.size();
2531 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002532 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002533
2534 // Short circuit if we are loading dock items for a configuration which has no dock
2535 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2536 mHotseat == null) {
2537 continue;
2538 }
2539
Sunny Goyal639e9062015-08-19 19:17:06 -07002540 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002541 switch (item.itemType) {
2542 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2543 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002544 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002545 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002546 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002548 }
2549 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002550 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002551 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002552 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002553 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002554 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002555 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2556 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002557 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002558 if (view == null) {
2559 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002560 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002561 break;
2562 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002563 default:
2564 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002566
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002567 /*
2568 * Remove colliding items.
2569 */
2570 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2571 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2572 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2573 View v = cl.getChildAt(item.cellX, item.cellY);
2574 Object tag = v.getTag();
2575 String desc = "Collision while binding workspace item: " + item
2576 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002577 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002578 throw (new RuntimeException(desc));
2579 } else {
2580 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002581 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002582 continue;
2583 }
2584 }
2585 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302586 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002587 if (animateIcons) {
2588 // Animate all the applications up now
2589 view.setAlpha(0f);
2590 view.setScaleX(0f);
2591 view.setScaleY(0f);
2592 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002593 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002596
Winson Chung997a9232013-07-24 15:33:46 -07002597 if (animateIcons) {
2598 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002599 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002600 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002601 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002602 final Runnable startBounceAnimRunnable = new Runnable() {
2603 public void run() {
2604 anim.playTogether(bounceAnims);
2605 anim.start();
2606 }
2607 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002608 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002609 // We post the animation slightly delayed to prevent slowdowns
2610 // when we are loading right after we return to launcher.
2611 mWorkspace.postDelayed(new Runnable() {
2612 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002613 if (mWorkspace != null) {
2614 mWorkspace.snapToPage(newScreenIndex);
2615 mWorkspace.postDelayed(startBounceAnimRunnable,
2616 NEW_APPS_ANIMATION_DELAY);
2617 }
Winson Chung94d67682013-09-25 16:29:40 -07002618 }
2619 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002620 } else {
2621 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002622 }
2623 }
Winson Chung64359a52013-07-08 17:17:08 -07002624 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002625 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 }
2627
Joe Onorato9c1289c2009-08-17 11:03:03 -04002628 /**
2629 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002631 public void bindAppWidget(LauncherAppWidgetInfo item) {
2632 View view = inflateAppWidget(item);
2633 if (view != null) {
2634 mWorkspace.addInScreen(view, item);
2635 mWorkspace.requestLayout();
2636 }
2637 }
2638
2639 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002640 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302641 PendingAppWidgetHostView view =
2642 new PendingAppWidgetHostView(this, item, mIconCache, true);
2643 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002644 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002645 }
2646
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002647 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002648
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002649 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002650
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002651 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2652 // If the provider is not ready, bind as a pending widget.
2653 appWidgetInfo = null;
2654 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2655 // The widget id is not valid. Try to find the widget based on the provider info.
2656 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2657 } else {
2658 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2659 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002660
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002661 // If the provider is ready, but the width is not yet restored, try to restore it.
2662 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2663 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002664 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002665 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2666 + " belongs to component " + item.providerName
2667 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002668 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002669 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002670 }
Sunny Goyalff572272014-07-23 13:58:07 -07002671
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002672 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002673 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002674 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2675 // Id has not been allocated yet. Allocate a new id.
2676 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2677 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002678
Sunny Goyald478c832016-04-01 12:04:16 -07002679 // Also try to bind the widget. If the bind fails, the user will be shown
2680 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002681 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002682 pendingInfo.spanX = item.spanX;
2683 pendingInfo.spanY = item.spanY;
2684 pendingInfo.minSpanX = item.minSpanX;
2685 pendingInfo.minSpanY = item.minSpanY;
2686 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002687
2688 boolean isDirectConfig =
2689 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2690 if (isDirectConfig && item.bindOptions != null) {
2691 Bundle newOptions = item.bindOptions.getExtras();
2692 if (options != null) {
2693 newOptions.putAll(options);
2694 }
2695 options = newOptions;
2696 }
Sunny Goyald478c832016-04-01 12:04:16 -07002697 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2698 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002699
Sunny Goyal86df1382016-08-10 15:03:22 -07002700 // We tried to bind once. If we were not able to bind, we would need to
2701 // go through the permission dialog, which means we cannot skip the config
2702 // activity.
2703 item.bindOptions = null;
2704 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2705
Sunny Goyald478c832016-04-01 12:04:16 -07002706 // Bind succeeded
2707 if (success) {
2708 // If the widget has a configure activity, it is still needs to set it up,
2709 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002710 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002711 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2712 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002713 }
Sunny Goyald478c832016-04-01 12:04:16 -07002714
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002715 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002716 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002717 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002718 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002719 // The widget was marked as UI not ready, but there is no configure activity to
2720 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002721 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002722 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002723 }
Sunny Goyalff572272014-07-23 13:58:07 -07002724 }
2725
Sunny Goyald5462aa2016-11-22 16:52:39 +05302726 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002727 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002728 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002729 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002730 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002731 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002732 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002733 }
2734
Sunny Goyal233ee962015-08-03 13:05:01 -07002735 item.minSpanX = appWidgetInfo.minSpanX;
2736 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302737 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002738 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302739 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002740 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302741 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002742
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002743 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002744 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002745 }
2746
Sunny Goyalff572272014-07-23 13:58:07 -07002747 /**
2748 * Restores a pending widget.
2749 *
2750 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002751 */
Sunny Goyald478c832016-04-01 12:04:16 -07002752 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002753 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2754 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2755 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002756 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002757 }
2758
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002759 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002760 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002761 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2762 info.pendingItemInfo = null;
2763 }
Sunny Goyalff572272014-07-23 13:58:07 -07002764
Sunny Goyalba47faa2017-10-04 16:50:57 -07002765 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
2766 view.reinflate();
2767 }
2768
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002769 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002770 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002771 }
2772
Adam Cohen1462de32012-07-24 22:34:36 -07002773 public void onPageBoundSynchronously(int page) {
2774 mSynchronouslyBoundPages.add(page);
2775 }
2776
Sunny Goyal527c7d32015-08-28 15:19:36 -07002777 @Override
2778 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2779 if (mPendingExecutor != null) {
2780 mPendingExecutor.markCompleted();
2781 }
2782 mPendingExecutor = executor;
2783 executor.attachTo(this);
2784 }
2785
2786 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2787 if (mPendingExecutor == executor) {
2788 mPendingExecutor = null;
2789 }
2790 }
2791
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002792 @Override
2793 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
2794 Runnable r = new Runnable() {
2795 public void run() {
2796 finishFirstPageBind(executor);
2797 }
2798 };
2799 if (waitUntilResume(r)) {
2800 return;
2801 }
2802
2803 Runnable onComplete = new Runnable() {
2804 @Override
2805 public void run() {
2806 if (executor != null) {
2807 executor.onLoadAnimationCompleted();
2808 }
2809 }
2810 };
2811 if (mDragLayer.getAlpha() < 1) {
2812 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
2813 } else {
2814 onComplete.run();
2815 }
2816 }
2817
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 /**
2819 * Callback saying that there aren't any more items to bind.
2820 *
2821 * Implementation of the method from LauncherModel.Callbacks.
2822 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002823 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07002824 Runnable r = new Runnable() {
2825 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002826 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07002827 }
2828 };
2829 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002830 return;
2831 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002832 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002833 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002835 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002836
Sunny Goyal2100c782016-08-22 16:00:03 -07002837 if (mPendingActivityResult != null) {
2838 handleActivityResult(mPendingActivityResult.requestCode,
2839 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2840 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002841 }
2842
Sunny Goyala474a9b2017-05-04 16:47:11 -07002843 InstallShortcutReceiver.disableAndFlushInstallQueue(
2844 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002845
Tony Wickham010d2552017-01-20 08:15:28 -08002846 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002847 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002848 }
2849
Winson Chunga2413752012-04-03 14:22:34 -07002850 private boolean canRunNewAppsAnimation() {
2851 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002852 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002853 }
2854
Winson Chungc9168342013-06-26 14:54:55 -07002855 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002856 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002857 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2858 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002859 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002860 return bounceAnim;
2861 }
2862
Adam Cohen27772732013-11-11 14:00:56 +00002863 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002864 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002865 }
2866
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002867 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002868 * Add the icons for all apps.
2869 *
2870 * Implementation of the method from LauncherModel.Callbacks.
2871 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002872 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002873 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_APPS) {
2874 public void run() {
2875 bindAllApplications(apps);
2876 }
2877 };
2878 if (waitUntilResume(r)) {
Winson Chungbb785c62015-05-05 10:05:08 -07002879 return;
2880 }
2881
Winson Chungb745afb2015-03-02 11:51:23 -08002882 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002883 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002884 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002885 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002886 pendingExecutor.execute(r);
Tony2917a8b2017-07-31 23:29:42 -07002887 return;
2888 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002889
Winson Chungb745afb2015-03-02 11:51:23 -08002890 mAppsView.setApps(apps);
2891 }
Adam Cohen9211d422014-10-07 18:14:53 -07002892 if (mLauncherCallbacks != null) {
2893 mLauncherCallbacks.bindAllApplications(apps);
2894 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002895 }
2896
2897 /**
Tony2917a8b2017-07-31 23:29:42 -07002898 * Returns an Executor that will run after the launcher is first drawn (including after the
2899 * initial fade in animation). Returns null if the first draw has already occurred.
2900 */
2901 public @Nullable Executor getPendingExecutor() {
2902 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2903 }
2904
2905 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002906 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2907 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2908 */
2909 @Override
2910 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002911 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002912 }
2913
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002914 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 * A package was updated.
2916 *
2917 * Implementation of the method from LauncherModel.Callbacks.
2918 */
Sunny Goyalb23980c2017-08-17 07:45:25 -07002919 public void bindAppsAddedOrUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07002920 Runnable r = new Runnable() {
2921 public void run() {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002922 bindAppsAddedOrUpdated(apps);
Winson Chungd64d1762013-08-20 14:37:16 -07002923 }
2924 };
2925 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07002926 return;
2927 }
2928
Winson Chungb745afb2015-03-02 11:51:23 -08002929 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002930 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 }
2933
Sunny Goyal4390ace2014-10-13 11:33:11 -07002934 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002935 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
2936 Runnable r = new Runnable() {
2937 public void run() {
2938 bindPromiseAppProgressUpdated(app);
2939 }
2940 };
2941 if (waitUntilResume(r)) {
2942 return;
2943 }
2944
2945 if (mAppsView != null) {
2946 mAppsView.updatePromiseAppProgress(app);
2947 }
2948 }
2949
2950 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07002951 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
2952 Runnable r = new Runnable() {
2953 public void run() {
2954 bindWidgetsRestored(widgets);
2955 }
2956 };
2957 if (waitUntilResume(r)) {
2958 return;
2959 }
2960 mWorkspace.widgetsRestored(widgets);
2961 }
2962
Joe Onorato9c1289c2009-08-17 11:03:03 -04002963 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002964 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002965 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002966 *
2967 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002968 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002969 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002970 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002971 Runnable r = new Runnable() {
2972 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002973 bindShortcutsChanged(updated, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002974 }
2975 };
2976 if (waitUntilResume(r)) {
2977 return;
2978 }
2979
Sunny Goyal4390ace2014-10-13 11:33:11 -07002980 if (!updated.isEmpty()) {
2981 mWorkspace.updateShortcuts(updated);
2982 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002983 }
2984
2985 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002986 * Update the state of a package, typically related to install state.
2987 *
2988 * Implementation of the method from LauncherModel.Callbacks.
2989 */
Sunny Goyale755d462014-07-22 13:48:29 -07002990 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07002991 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
2992 Runnable r = new Runnable() {
2993 public void run() {
2994 bindRestoreItemsChange(updates);
2995 }
2996 };
2997 if (waitUntilResume(r)) {
2998 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002999 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003000
Sunny Goyal756adbc2015-04-16 15:20:51 -07003001 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003002 }
3003
3004 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003005 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003006 * in addition to specific applications to remove, the reason being that
3007 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003008 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003009 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003010 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003011 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003012 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Winson Chungd64d1762013-08-20 14:37:16 -07003013 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003014 public void run() {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003015 bindWorkspaceComponentsRemoved(matcher);
Winson Chung83892cc2013-05-01 16:53:33 -07003016 }
Winson Chungd64d1762013-08-20 14:37:16 -07003017 };
3018 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003019 return;
3020 }
Sunny Goyal6e13dd32017-08-17 03:01:19 -07003021 mWorkspace.removeItemsByMatcher(matcher);
3022 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003023 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003024
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003025 @Override
3026 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3027 Runnable r = new Runnable() {
3028 public void run() {
3029 bindAppInfosRemoved(appInfos);
3030 }
3031 };
3032 if (waitUntilResume(r)) {
3033 return;
Joe Onorato36115782010-06-17 13:28:48 -04003034 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003035
Winson Chungdf95eb12013-10-16 14:57:07 -07003036 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003037 if (mAppsView != null) {
3038 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 }
Joe Onoratobe386092009-11-17 17:32:16 -08003041
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003042 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07003043 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
3044 mPopupDataProvider.setAllWidgets(allWidgets);
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003045 Runnable r = new RunnableWithId(RUNNABLE_ID_BIND_WIDGETS) {
3046 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07003047 public void run() {
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003048 bindAllWidgets(allWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003049 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003050 };
Sunny Goyal49fc3f72017-08-18 10:17:06 -07003051 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003052 return;
3053 }
3054
Tony Wickham26b17462017-03-20 17:12:24 -07003055 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3056 if (topView != null) {
3057 topView.onWidgetsBound();
3058 }
3059 }
3060
Tony Wickham86222d22017-03-29 15:30:43 -07003061 /**
3062 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3063 * refreshes the widgets and shortcuts associated with the given package/user
3064 */
3065 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003066 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003067 }
3068
Sunny Goyalc99cb172017-10-19 16:15:09 -07003069 public boolean isRotationEnabled () {
3070 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08003071 }
3072
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003073 private boolean shouldShowDiscoveryBounce() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003074 return isInState(NORMAL)
Sunny Goyalbe93f262017-10-20 17:05:27 -07003075 && !mSharedPrefs.getBoolean(AllAppsState.APPS_VIEW_SHOWN, false)
Sunny Goyalf9403d92017-10-18 10:55:56 -07003076 && !UserManagerCompat.getInstance(this).isDemoUser();
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003077 }
3078
Winson Chung80baf5a2010-08-09 16:03:15 -07003079 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003080 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003081 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003082 @Override
3083 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3084 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003085
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003086 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3087 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07003088 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003089 writer.println(prefix + " Homescreen " + i);
3090
3091 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3092 for (int j = 0; j < layout.getChildCount(); j++) {
3093 Object tag = layout.getChildAt(j).getTag();
3094 if (tag != null) {
3095 writer.println(prefix + " " + tag.toString());
3096 }
3097 }
3098 }
3099
3100 writer.println(prefix + " Hotseat");
3101 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003102 for (int j = 0; j < layout.getChildCount(); j++) {
3103 Object tag = layout.getChildAt(j).getTag();
3104 if (tag != null) {
3105 writer.println(prefix + " " + tag.toString());
3106 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003107 }
Sunny Goyala1365452015-10-01 15:46:24 -07003108
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003109 try {
3110 FileLog.flushAll(writer);
3111 } catch (Exception e) {
3112 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003113 }
3114 }
3115
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003116 writer.println(prefix + "Misc:");
3117 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3118 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3119 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3120
3121 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003122
Adam Cohen9211d422014-10-07 18:14:53 -07003123 if (mLauncherCallbacks != null) {
3124 mLauncherCallbacks.dump(prefix, fd, writer, args);
3125 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003126 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003127
Sunny Goyal66b24572016-09-21 15:57:55 -07003128 @Override
3129 @TargetApi(Build.VERSION_CODES.N)
3130 public void onProvideKeyboardShortcuts(
3131 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
3132
3133 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003134 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003135 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
3136 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
3137 }
3138 View currentFocus = getCurrentFocus();
3139 if (new CustomActionsPopup(this, currentFocus).canShow()) {
3140 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
3141 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
3142 }
Tony18c4aa42017-05-10 21:23:57 -05003143 if (currentFocus.getTag() instanceof ItemInfo
3144 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07003145 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
3146 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
3147 }
3148 if (!shortcutInfos.isEmpty()) {
3149 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
3150 }
3151
3152 super.onProvideKeyboardShortcuts(data, menu, deviceId);
3153 }
3154
3155 @Override
3156 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
3157 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
3158 switch (keyCode) {
3159 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003160 if (isInState(NORMAL)) {
3161 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07003162 return true;
3163 }
3164 break;
3165 case KeyEvent.KEYCODE_S: {
3166 View focusedView = getCurrentFocus();
3167 if (focusedView instanceof BubbleTextView
3168 && focusedView.getTag() instanceof ItemInfo
3169 && mAccessibilityDelegate.performAction(focusedView,
3170 (ItemInfo) focusedView.getTag(),
3171 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal10a1bd02017-10-09 14:56:21 -07003172 BaseActionPopup.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07003173 return true;
3174 }
3175 break;
3176 }
3177 case KeyEvent.KEYCODE_O:
3178 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
3179 return true;
3180 }
3181 break;
3182 }
3183 }
3184 return super.onKeyShortcut(keyCode, event);
3185 }
3186
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07003187 public static Launcher getLauncher(Context context) {
3188 if (context instanceof Launcher) {
3189 return (Launcher) context;
3190 }
3191 return ((Launcher) ((ContextWrapper) context).getBaseContext());
3192 }
3193
Sunny Goyal5a81c382017-03-20 15:08:06 -07003194 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07003195
3196 @Override
3197 public void onSharedPreferenceChanged(
3198 SharedPreferences sharedPreferences, String key) {
3199 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07003200 // Recreate the activity so that it initializes the rotation preference again.
3201 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07003202 }
3203 }
Sunny Goyal745bad92016-05-02 10:54:12 -07003204 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07003205
3206 /**
3207 * Callback for listening for onResume
3208 */
3209 public interface OnResumeCallback {
3210
3211 void onLauncherResume();
3212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213}