blob: 9eaa6c73a4512c40f56231c3bd96da6386d468b7 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalc99cb172017-10-19 16:15:09 -070019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
21
Sunny Goyal076839c2017-10-30 13:52:20 -070022import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_BY_PUBLISHER;
23import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER;
24import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_QUIET_USER;
25import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE;
26import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070028import static com.android.launcher3.LauncherState.ALL_APPS;
29import static com.android.launcher3.LauncherState.NORMAL;
30import static com.android.launcher3.LauncherState.OVERVIEW;
Sunny Goyalaeb16432017-10-16 11:46:41 -070031import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
Sunny Goyal326403e2017-10-02 12:45:10 -070032
Sunny Goyal28c6b962015-10-12 11:42:05 -070033import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070034import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070035import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070036import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070037import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070038import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070039import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000040import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070041import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.app.AlertDialog;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080043import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070044import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080046import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080047import android.content.ComponentCallbacks2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070049import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040050import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070052import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070053import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070054import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070055import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070057import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070058import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070068import android.support.annotation.Nullable;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070072import android.util.SparseArray;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070076import android.view.KeyboardShortcutGroup;
77import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080078import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070085import android.view.animation.OvershootInterpolator;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070087
Sunny Goyal651077b2014-06-30 14:15:31 -070088import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070089import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070090import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070091import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070092import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070093import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal7ede6112017-12-05 15:11:21 -080094import com.android.launcher3.allapps.DiscoveryBounce;
Sunny Goyalffe83f12014-08-14 17:39:34 -070095import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010096import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070097import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080098import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070099import com.android.launcher3.dragndrop.DragController;
100import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700101import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700102import com.android.launcher3.dragndrop.DragView;
Mario Bertschler27288382017-05-24 15:35:09 -0700103import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000104import com.android.launcher3.folder.Folder;
105import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700106import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700107import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700108import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700109import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800110import com.android.launcher3.model.ModelWriter;
Sunny Goyala535ae42017-02-27 10:07:13 -0800111import com.android.launcher3.notification.NotificationListener;
Tony Wickham540913e2017-01-23 11:47:51 -0800112import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800113import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700114import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800115import com.android.launcher3.states.InternalStateHandler;
Sunny Goyala5ace712017-11-15 17:12:51 -0800116import com.android.launcher3.uioverrides.UiFactory;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800117import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530118import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
119import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyalaeb16432017-10-16 11:46:41 -0700120import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Sunny Goyal2100c782016-08-22 16:00:03 -0700121import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700122import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700123import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700124import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700125import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800126import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal8392c822017-06-20 10:03:56 -0700128import com.android.launcher3.util.SystemUiController;
Mario Bertschlera6936942017-05-31 14:48:19 -0700129import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700130import com.android.launcher3.util.Thunk;
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700131import com.android.launcher3.util.TraceHelper;
Sunny Goyal326403e2017-10-02 12:45:10 -0700132import com.android.launcher3.util.UiThreadHelper;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700133import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal29947f02017-12-18 13:49:44 -0800134import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800135import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal29947f02017-12-18 13:49:44 -0800137import com.android.launcher3.widget.PendingAppWidgetHostView;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800138import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700139import com.android.launcher3.widget.WidgetHostViewLoader;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700140import com.android.launcher3.widget.WidgetListRowEntry;
141import com.android.launcher3.widget.WidgetsFullSheet;
Sunny Goyal952e63d2017-08-16 04:59:08 -0700142import com.android.launcher3.widget.custom.CustomWidgetParser;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700143
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700144import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700145import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700146import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700147import java.util.Collection;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700148import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700149import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800150import java.util.Set;
Tony2917a8b2017-07-31 23:29:42 -0700151import java.util.concurrent.Executor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153/**
154 * Default launcher application.
155 */
Sunny Goyal27835952017-01-13 12:15:53 -0800156public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700157 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
158 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700159 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700160 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700161 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Winson Chunga2413752012-04-03 14:22:34 -0700163 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700167
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700169 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Michael Jurka8b805b12012-04-18 14:23:14 -0700171 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700172 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
173 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700174
Jon Mirandac476d6e2017-05-04 16:30:01 -0700175 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700176
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700177 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
178
Mathew Inwood876a8462013-06-14 14:12:41 +0100179 /**
180 * IntentStarter uses request codes starting with this. This must be greater than all activity
181 * request codes used internally.
182 */
183 protected static final int REQUEST_LAST = 100;
184
Winson Chung2672ff92012-05-04 16:22:30 -0700185 // The Intent extra that defines whether to ignore the launch animation
186 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400187 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
190 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700191 // Type: int
192 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700193 // Type: PendingRequestArgs
194 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
195 // Type: ActivityResultInfo
196 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700197 // Type: SparseArray<Parcelable>
198 private static final String RUNTIME_STATE_WIDGET_PANEL = "launcher.widget_panel";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700200 private LauncherStateManager mStateManager;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700202 private boolean mIsSafeModeEnabled;
203
Adam Cohenad4e15c2013-10-17 16:21:35 -0700204 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Winson Chunga2413752012-04-03 14:22:34 -0700206 // How long to wait before the new-shortcut animation automatically pans the workspace
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700207 private static final int NEW_APPS_PAGE_MOVE_DELAY = 500;
208 private static final int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
209 @Thunk static final int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700210
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700212 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800214 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700215
Sunny Goyalffe83f12014-08-14 17:39:34 -0700216 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700217 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700218
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700219 private final int[] mTmpAddItemCellCoordinates = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700220
Sunny Goyal316490e2015-06-02 09:38:28 -0700221 @Thunk Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700222
Sunny Goyal47328fd2016-05-25 18:56:41 -0700223 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700224
225 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700226 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700227 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700228
Winson Chung8ae41982017-11-10 11:42:13 -0800229 // UI and state for the overview panel
230 private ViewGroup mOverviewPanel;
231
Jon Miranda6bed3502017-09-19 14:43:17 -0700232 // We need to store the orientation Launcher was created with, due to a bug (b/64916689)
233 // that results in widgets being inflated in the wrong orientation.
234 private int mOrientation;
235
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800238 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700240 private OnResumeCallback mOnResumeCallback;
241
Sunny Goyal527c7d32015-08-28 15:19:36 -0700242 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700243
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800245 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700247 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248
Jon Miranda2d89ea82017-05-04 11:47:53 -0700249 private ObjectAnimator mScrimAnimator;
Jon Miranda7fda2852017-07-19 16:07:01 -0700250 private boolean mShouldFadeInScrim;
Jon Miranda2d89ea82017-05-04 11:47:53 -0700251
Tony Wickham010d2552017-01-20 08:15:28 -0800252 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700253
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800254 private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
Adam Cohen1462de32012-07-24 22:34:36 -0700255
Winson Chung46353de2012-02-16 14:05:10 -0800256 // We only want to get the SharedPreferences once since it does an FS stat each time we get
257 // it from the context.
258 private SharedPreferences mSharedPrefs;
259
Sunny Goyal2100c782016-08-22 16:00:03 -0700260 // Activity result which needs to be processed after workspace has loaded.
261 private ActivityResultInfo mPendingActivityResult;
262 /**
263 * Holds extra information required to handle a result from an external call, like
264 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
265 */
266 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800267
Jon Miranda379198e2016-09-23 08:54:40 -0700268 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700269
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700270 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700271 private boolean mRotationEnabled = false;
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700272 private boolean mAppLaunchSuccess;
273
Sunny Goyal745bad92016-05-02 10:54:12 -0700274 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 @Override
277 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700278 if (DEBUG_STRICT_MODE) {
279 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
280 .detectDiskReads()
281 .detectDiskWrites()
282 .detectNetwork() // or .detectAll() for all detectable problems
283 .penaltyLog()
284 .build());
285 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
286 .detectLeakedSqlLiteObjects()
287 .detectLeakedClosableObjects()
288 .penaltyLog()
289 .penaltyDeath()
290 .build());
291 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700292 TraceHelper.beginSection("Launcher-onCreate");
Winson Chunga2413752012-04-03 14:22:34 -0700293
Adam Cohen9211d422014-10-07 18:14:53 -0700294 if (mLauncherCallbacks != null) {
295 mLauncherCallbacks.preOnCreate();
296 }
297
Mario Bertschler27288382017-05-24 15:35:09 -0700298 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
299 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700300 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 super.onCreate(savedInstanceState);
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700303 TraceHelper.partitionSection("Launcher-onCreate", "super call");
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400304
Sunny Goyal87f784c2017-01-11 10:48:34 -0800305 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700306
Adam Cohen2e6da152015-05-06 11:42:25 -0700307 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800308 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700309 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700310 Display display = getWindowManager().getDefaultDisplay();
311 Point mwSize = new Point();
312 display.getSize(mwSize);
313 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
314 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700315
Jon Miranda6bed3502017-09-19 14:43:17 -0700316 mOrientation = getResources().getConfiguration().orientation;
Sunny Goyalf7258242015-10-19 16:59:07 -0700317 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700318 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800319 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800320 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800321 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700322 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700323
Joe Onorato41a12d22009-10-31 18:30:00 -0400324 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700325 mAllAppsController = new AllAppsTransitionController(this);
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800326 mStateManager = new LauncherStateManager(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700327
Sunny Goyalffe83f12014-08-14 17:39:34 -0700328 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700329
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700330 mAppWidgetHost = new LauncherAppWidgetHost(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700331 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700332
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700333 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
334 // this also ensures that any synchronous binding below doesn't re-trigger another
335 // LauncherModel load.
336 mPaused = false;
337
Sunny Goyal60820d72017-05-09 12:40:11 -0700338 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 setupViews();
Winson1f064272016-07-18 17:18:02 -0700341 mDeviceProfile.layout(this, false /* notifyListeners */);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342
Tony Wickham010d2552017-01-20 08:15:28 -0800343 mPopupDataProvider = new PopupDataProvider(this);
344
Sunny Goyald3864fa2017-11-14 15:06:36 -0800345 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
346 // In case we are on a device with locked rotation, we should look at preferences to check
347 // if the user has specifically allowed rotation.
348 if (!mRotationEnabled) {
349 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
350 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
351 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353
Sunny Goyald3864fa2017-11-14 15:06:36 -0800354 boolean internalStateHandled = InternalStateHandler.handleCreate(this, getIntent());
355 if (internalStateHandled) {
356 // Temporarily enable the rotation
357 mRotationEnabled = true;
358
359 if (savedInstanceState != null) {
360 // InternalStateHandler has already set the appropriate state.
361 // We dont need to do anything.
362 savedInstanceState.remove(RUNTIME_STATE);
363 }
364 }
365 restoreState(savedInstanceState);
Winson Chungb63b44c2017-11-10 17:54:44 -0800366
Sunny Goyal2100c782016-08-22 16:00:03 -0700367 // We only load the page synchronously if the user rotates (or triggers a
368 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700369 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
370 if (savedInstanceState != null) {
371 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
372 }
Sunny Goyald3864fa2017-11-14 15:06:36 -0800373
Sunny Goyalfe770c92016-09-27 14:38:58 -0700374 if (!mModel.startLoader(currentScreen)) {
Sunny Goyald3864fa2017-11-14 15:06:36 -0800375 if (!internalStateHandled) {
376 // If we are not binding synchronously, show a fade in animation when
377 // the first page bind completes.
378 mDragLayer.setAlpha(0);
379 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700380 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700381 // Pages bound synchronously.
382 mWorkspace.setCurrentPage(currentScreen);
383
Sunny Goyal2100c782016-08-22 16:00:03 -0700384 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386
387 // For handling default keys
Sunny Goyalf4aceab2017-12-06 16:16:31 -0800388 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800389
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700390 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
391 // we want the screen to auto-rotate based on the current orientation
Sunny Goyalc99cb172017-10-19 16:15:09 -0700392 setRequestedOrientation(mRotationEnabled
393 ? SCREEN_ORIENTATION_UNSPECIFIED : SCREEN_ORIENTATION_NOSENSOR);
Winson Chungaf40f202013-09-18 18:26:31 -0700394
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800395 setContentView(mLauncherView);
Mario Bertschlera6936942017-05-31 14:48:19 -0700396
Jon Miranda7fda2852017-07-19 16:07:01 -0700397 // Listen for broadcasts
398 IntentFilter filter = new IntentFilter();
399 filter.addAction(Intent.ACTION_SCREEN_OFF);
400 filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
401 registerReceiver(mReceiver, filter);
402 mShouldFadeInScrim = true;
Sunny Goyala616d2b2017-06-12 13:54:01 -0700403
Sunny Goyal8392c822017-06-20 10:03:56 -0700404 getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
405 Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText));
Sunny Goyal14b32402017-07-31 10:03:28 -0700406
407 if (mLauncherCallbacks != null) {
408 mLauncherCallbacks.onCreate(savedInstanceState);
409 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700410
411 TraceHelper.endSection("Launcher-onCreate");
Adam Cohen9211d422014-10-07 18:14:53 -0700412 }
413
Sunny Goyal7779d622015-06-11 16:18:39 -0700414 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700415 public void onThemeChanged() {
416 recreate();
417 }
418
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700419 public LauncherStateManager getStateManager() {
420 return mStateManager;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700421 }
422
Mario Bertschlera6936942017-05-31 14:48:19 -0700423 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700424 if (isDark) {
425 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700426 } else if (supportsDarkText) {
427 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700428 }
429 }
430
431 @Override
Sunny Goyalc7b8df82017-06-27 11:11:03 -0700432 public <T extends View> T findViewById(int id) {
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800433 return mLauncherView.findViewById(id);
434 }
435
436 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700437 public void onAppWidgetHostReset() {
438 if (mAppWidgetHost != null) {
439 mAppWidgetHost.startListening();
440 }
441 }
442
Adam Cohen9211d422014-10-07 18:14:53 -0700443 private LauncherCallbacks mLauncherCallbacks;
444
Winson1f064272016-07-18 17:18:02 -0700445 public void onInsetsChanged(Rect insets) {
446 mDeviceProfile.updateInsets(insets);
447 mDeviceProfile.layout(this, true /* notifyListeners */);
448 }
449
Sunny Goyal32554d12015-12-03 15:31:25 -0800450 /**
451 * Call this after onCreate to set or clear overlay.
452 */
453 public void setLauncherOverlay(LauncherOverlay overlay) {
454 if (overlay != null) {
455 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
456 }
457 mWorkspace.setLauncherOverlay(overlay);
458 }
459
Adam Cohen9211d422014-10-07 18:14:53 -0700460 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
461 mLauncherCallbacks = callbacks;
462 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700463 }
464
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700465 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700466 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700467 if (mLauncherCallbacks != null) {
468 mLauncherCallbacks.onLauncherProviderChange();
469 }
470 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700471
Hyunyoung Song3f471442015-04-08 19:01:34 -0700472 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700473 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
474 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700475 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700476 }
477
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000478 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700479 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
480 // This cast is safe as long as the id < 0x00FFFFFF
481 // Since we jail all the dynamically generated views, there should be no clashes
482 // with any other views.
483 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000484 }
485
Tony Wickham010d2552017-01-20 08:15:28 -0800486 public PopupDataProvider getPopupDataProvider() {
487 return mPopupDataProvider;
488 }
489
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000490 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700491 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
492 * a configuration step, this allows the proper animations to run after other transitions.
493 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700494 private long completeAdd(
495 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
496 long screenId = info.screenId;
497 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700498 // When the screen id represents an actual screen (as opposed to a rank) we make sure
499 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700500 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700501 }
Adam Cohendb364c32014-05-20 14:23:40 -0700502
Sunny Goyal2100c782016-08-22 16:00:03 -0700503 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800504 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700505 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700506 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700508 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700510 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700511 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700512 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700513 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700514 int widgetId = appWidgetId;
515 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700516 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700517 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700518 // Since the view was just bound, also launch the configure activity if needed
519 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
520 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800521 if (provider != null) {
522 new WidgetAddFlowHandler(provider)
523 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700524 }
525 }
526 break;
527 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700529
Adam Cohendb364c32014-05-20 14:23:40 -0700530 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 }
532
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800533 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700534 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700535 if (isWorkspaceLoading()) {
536 // process the result once the workspace has loaded.
537 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
538 return;
539 }
540 mPendingActivityResult = null;
541
Winson Chunge341d302013-08-16 14:31:00 -0700542 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700543 final PendingRequestArgs requestArgs = mPendingRequestArgs;
544 setWaitingForResult(null);
545 if (requestArgs == null) {
546 return;
547 }
548
549 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700550
Adam Cohenad4e15c2013-10-17 16:21:35 -0700551 Runnable exitSpringLoaded = new Runnable() {
552 @Override
553 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700554 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700555 }
556 };
557
Michael Jurka8b805b12012-04-18 14:23:14 -0700558 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700559 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700560 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
562 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700563 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700564 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700565 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700567 addAppWidgetImpl(
568 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800569 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700570 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700571 }
572 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200573 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700574 if (resultCode == RESULT_OK && isInState(OVERVIEW)) {
Tony Wickhame3054412015-09-09 09:05:25 -0700575 // User could have free-scrolled between pages before picking a wallpaper; make sure
576 // we move to the closest one now.
577 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700578 mStateManager.goToState(NORMAL, false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200579 }
580 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700581 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200582
Adam Cohened66b2b2012-01-23 17:28:51 -0800583 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700584 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 // We have special handling for widgets
587 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800588 final int appWidgetId;
589 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
590 : -1;
591 if (widgetId < 0) {
592 appWidgetId = pendingAddWidgetId;
593 } else {
594 appWidgetId = widgetId;
595 }
596
Adam Cohenad4e15c2013-10-17 16:21:35 -0700597 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800598 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700599 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
600 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700601 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700602 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700603 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700604 @Override
605 public void run() {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700606 getStateManager().goToState(NORMAL);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700607 }
608 };
Adam Cohendb364c32014-05-20 14:23:40 -0700609
Sunny Goyal2100c782016-08-22 16:00:03 -0700610 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
611 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
612 } else {
613 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
614 // When the screen id represents an actual screen (as opposed to a rank)
615 // we make sure that the drop page actually exists.
616 requestArgs.screenId =
617 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700618 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700619 final CellLayout dropLayout =
620 mWorkspace.getScreenWithId(requestArgs.screenId);
621
622 dropLayout.setDropPending(true);
623 final Runnable onComplete = new Runnable() {
624 @Override
625 public void run() {
626 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
627 dropLayout.setDropPending(false);
628 }
629 };
630 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
631 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700632 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 return;
634 }
635
Sunny Goyald478c832016-04-01 12:04:16 -0700636 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
637 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700638 if (resultCode == RESULT_OK) {
639 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700640 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700641 }
642 // Leave the widget in the pending state if the user canceled the configure.
643 return;
644 }
645
Sunny Goyalaad90582015-07-09 14:22:50 -0700646 if (requestCode == REQUEST_CREATE_SHORTCUT) {
647 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
649 completeAdd(requestCode, data, -1, requestArgs);
650 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
651 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
652
Sunny Goyalaad90582015-07-09 14:22:50 -0700653 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700654 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
655 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800659 }
660
661 @Override
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700662 public void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800663 final int requestCode, final int resultCode, final Intent data) {
664 handleActivityResult(requestCode, resultCode, data);
665 if (mLauncherCallbacks != null) {
666 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
667 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 }
669
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700670 @Override
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700671 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600672 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 PendingRequestArgs pendingArgs = mPendingRequestArgs;
674 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
675 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
676 setWaitingForResult(null);
677
Sunny Goyal28c6b962015-10-12 11:42:05 -0700678 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700680 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700682 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 Intent intent = pendingArgs.getPendingIntent();
684
Sunny Goyal28c6b962015-10-12 11:42:05 -0700685 if (grantResults.length > 0
686 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700687 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700688 } else {
689 // TODO: Show a snack bar with link to settings
690 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700691 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700692 }
693 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600694 if (mLauncherCallbacks != null) {
695 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
696 grantResults);
697 }
698 }
699
Adam Cohendb364c32014-05-20 14:23:40 -0700700 /**
701 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
702 *
703 * @param screenId the screen id to check
704 * @return the new screen, or screenId if it exists
705 */
706 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800707 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700708 if (dropLayout == null) {
709 // it's possible that the add screen was removed because it was
710 // empty and a re-bind occurred
711 mWorkspace.addExtraEmptyScreen();
712 return mWorkspace.commitExtraEmptyScreen();
713 } else {
714 return screenId;
715 }
716 }
717
Sunny Goyal2100c782016-08-22 16:00:03 -0700718 @Thunk void completeTwoStageWidgetDrop(
719 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
720 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 Runnable onCompleteRunnable = null;
722 int animationType = 0;
723
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700724 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800725 if (resultCode == RESULT_OK) {
726 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
727 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800728 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700729 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 onCompleteRunnable = new Runnable() {
731 @Override
732 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700733 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700734 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 }
736 };
737 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800738 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700741 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
744 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700745 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 // The animated view may be null in the case of a rotation during widget configuration
747 onCompleteRunnable.run();
748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
750
751 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700752 protected void onStop() {
753 super.onStop();
754 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700755
756 if (mLauncherCallbacks != null) {
757 mLauncherCallbacks.onStop();
758 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800759 mAppWidgetHost.setListenIfResumed(false);
Tony Wickham010d2552017-01-20 08:15:28 -0800760
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700761 if (!mAppLaunchSuccess) {
762 getUserEventDispatcher().logActionCommand(Action.Command.STOP,
Sunny Goyalea609262017-10-25 15:47:38 -0700763 mStateManager.getState().containerType);
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700764 }
Tony Wickham010d2552017-01-20 08:15:28 -0800765 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700766 }
767
768 @Override
769 protected void onStart() {
770 super.onStart();
771 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700772
773 if (mLauncherCallbacks != null) {
774 mLauncherCallbacks.onStart();
775 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800776 mAppWidgetHost.setListenIfResumed(true);
Tony Wickham010d2552017-01-20 08:15:28 -0800777
778 if (!isWorkspaceLoading()) {
779 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
780 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700781
Jon Miranda7fda2852017-07-19 16:07:01 -0700782 if (mShouldFadeInScrim && mDragLayer.getBackground() != null) {
Jon Miranda2d89ea82017-05-04 11:47:53 -0700783 if (mScrimAnimator != null) {
784 mScrimAnimator.cancel();
785 }
786 mDragLayer.getBackground().setAlpha(0);
787 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
788 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
789 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
790 @Override
791 public void onAnimationEnd(Animator animation) {
792 mScrimAnimator = null;
793 }
794 });
795 mScrimAnimator.setDuration(600);
796 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
797 mScrimAnimator.start();
798 }
Jon Miranda7fda2852017-07-19 16:07:01 -0700799 mShouldFadeInScrim = false;
Michael Jurkacd496d72013-04-11 11:32:45 -0700800 }
801
802 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 protected void onResume() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700804 TraceHelper.beginSection("ON_RESUME");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 super.onResume();
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700806 TraceHelper.partitionSection("ON_RESUME", "superCall");
807
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -0700808 mAppLaunchSuccess = false;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700809 getUserEventDispatcher().resetElapsedSessionMillis();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800810 mPaused = false;
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700811 setOnResumeCallback(null);
Sunny Goyala474a9b2017-05-04 16:47:11 -0700812 // Process any items that were added while Launcher was away.
813 InstallShortcutReceiver.disableAndFlushInstallQueue(
814 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700815
Sunny Goyala474a9b2017-05-04 16:47:11 -0700816 // Refresh shortcuts if the permission changed.
817 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -0700818
Sunny Goyal7ede6112017-12-05 15:11:21 -0800819 DiscoveryBounce.showIfNeeded(this);
Adam Cohen9211d422014-10-07 18:14:53 -0700820 if (mLauncherCallbacks != null) {
821 mLauncherCallbacks.onResume();
822 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -0800823
Sunny Goyalfca6bc92017-09-28 16:28:34 -0700824 TraceHelper.endSection("ON_RESUME");
Adam Cohen06dff352012-06-01 17:17:08 -0700825 }
826
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700828 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700829 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -0700830 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -0700831
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700832 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700833 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800834 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700835 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700836
Adam Cohen9211d422014-10-07 18:14:53 -0700837 if (mLauncherCallbacks != null) {
838 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -0700839 }
Adam Cohenbffe7452013-07-22 18:21:45 -0700840 }
841
Adam Cohenc2d6e892014-10-16 09:49:24 -0700842 public interface LauncherOverlay {
843
844 /**
845 * Touch interaction leading to overscroll has begun
846 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700847 void onScrollInteractionBegin();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700848
849 /**
850 * Touch interaction related to overscroll has ended
851 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700852 void onScrollInteractionEnd();
Adam Cohenc2d6e892014-10-16 09:49:24 -0700853
854 /**
855 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
856 * screen (or in the case of RTL, the rightmost screen).
857 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700858 void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700859
860 /**
Sunny Goyal32554d12015-12-03 15:31:25 -0800861 * Called when the launcher is ready to use the overlay
862 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -0700863 */
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700864 void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700865 }
866
867 public interface LauncherOverlayCallbacks {
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700868 void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700869 }
870
871 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
872
Sunny Goyalc86df472016-02-25 09:19:38 -0800873 public void onScrollChanged(float progress) {
874 if (mWorkspace != null) {
875 mWorkspace.onOverlayScrollChanged(progress);
876 }
877 }
Adam Cohenc2d6e892014-10-16 09:49:24 -0700878 }
879
Sunny Goyal16764582017-11-06 16:00:34 -0800880 public boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -0700881 if (mLauncherCallbacks != null) {
882 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700883 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800884 // On O and above we there is always some setting present settings (add icon to
885 // home screen or icon badging). On earlier APIs we will have the allow rotation
886 // setting, on devices with a locked orientation,
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700887 return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -0700888 }
Jorim Jaggid017f882014-01-14 17:08:48 -0800889 }
890
Sunny Goyalf9403d92017-10-18 10:55:56 -0700891 public boolean isInState(LauncherState state) {
Sunny Goyalea609262017-10-25 15:47:38 -0700892 return mStateManager.getState() == state;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700893 }
894
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 /**
896 * Restores the previous state, if it exists.
897 *
898 * @param savedState The previous state.
899 */
900 private void restoreState(Bundle savedState) {
901 if (savedState == null) {
902 return;
903 }
904
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700905 int stateOrdinal = savedState.getInt(RUNTIME_STATE, NORMAL.ordinal);
Sunny Goyalf9403d92017-10-18 10:55:56 -0700906 LauncherState[] stateValues = LauncherState.values();
907 LauncherState state = stateValues[stateOrdinal];
Sunny Goyalbc683e92017-12-06 10:25:07 -0800908 if (!state.disableRestore) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700909 mStateManager.goToState(state, false /* animated */);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800910 }
911
Sunny Goyal2100c782016-08-22 16:00:03 -0700912 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
913 if (requestArgs != null) {
914 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700916
917 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700918
919 SparseArray<Parcelable> widgetsState =
920 savedState.getSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL);
921 if (widgetsState != null) {
922 WidgetsFullSheet.show(this, false).restoreHierarchyState(widgetsState);
923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 /**
927 * Finds all the views we need and configure them properly.
928 */
929 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -0700930 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700931 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -0700932 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700933 mWorkspace.initParentViews(mDragLayer);
Sunny Goyal16764582017-11-06 16:00:34 -0800934 mOverviewPanel = findViewById(R.id.overview_panel);
Craig Mautner360310b2012-10-26 15:13:08 -0700935
Sunny Goyal784f9c32016-03-23 16:56:54 -0700936 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
937 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
938 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939
Winson Chung4c98d922011-05-31 16:50:48 -0700940 // Setup the drag layer
Sunny Goyal7ede6112017-12-05 15:11:21 -0800941 mDragLayer.setup(this, mDragController);
Winson Chung4c98d922011-05-31 16:50:48 -0700942
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 // Setup the hotseat
944 mHotseat = (Hotseat) findViewById(R.id.hotseat);
945 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -0700946 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 }
948
Winson Chung4c98d922011-05-31 16:50:48 -0700949 // Setup the workspace
950 mWorkspace.setHapticFeedbackEnabled(false);
951 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700952 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -0700953 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
954 // default state, otherwise we will update to the wrong offsets in RTL
955 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700956 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700957 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700958
Tony Wickham34d2c912015-09-11 09:27:58 -0700959 // Get the search/delete/uninstall bar
Rajeev Kumar43c0f582017-06-23 15:34:55 -0700960 mDropTargetBar = mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700961
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700962 // Setup Apps
963 mAppsView = findViewById(R.id.apps_view);
Craig Mautner360310b2012-10-26 15:13:08 -0700964
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -0700966 mDragController.setMoveTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700967 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -0400968
Hyunyoung Songd725f642017-08-17 22:26:35 -0700969 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971
972 /**
973 * Creates a view representing a shortcut.
974 *
975 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800977 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700978 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 }
980
981 /**
982 * Creates a view representing a shortcut inflated from the specified resource.
983 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 * @param parent The group the shortcut belongs to.
985 * @param info The data structure describing the shortcut.
986 *
987 * @return A View inflated from layoutResId.
988 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700989 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -0800990 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
991 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800992 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700994 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 return favorite;
996 }
997
998 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -0700999 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 *
1001 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001004 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001005 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1006 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001007 return;
1008 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001009
Jon Mirandac476d6e2017-05-04 16:30:01 -07001010 int[] cellXY = mTmpAddItemCellCoordinates;
1011 CellLayout layout = getCellLayout(container, screenId);
1012
Sunny Goyal782f0c92017-01-19 10:27:54 -08001013 ShortcutInfo info = null;
Hyunyoung Songe24cb632017-09-11 11:18:03 -07001014 if (Utilities.ATLEAST_OREO) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001015 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1016 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001017 }
1018
1019 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001020 // Legacy shortcuts are only supported for primary profile.
1021 info = Process.myUserHandle().equals(args.user)
1022 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001023
Sunny Goyalb57645f2017-03-20 13:57:28 -07001024 if (info == null) {
1025 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1026 return;
1027 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001028 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001029 // The app is trying to add a shortcut without sufficient permissions
1030 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1031 return;
1032 }
1033 }
1034
Jon Mirandac476d6e2017-05-04 16:30:01 -07001035 if (container < 0) {
1036 // Adding a shortcut to the Workspace.
1037 final View view = createShortcut(info);
1038 boolean foundCellSpan = false;
1039 // First we check if we already know the exact location where we want to add this item.
1040 if (cellX >= 0 && cellY >= 0) {
1041 cellXY[0] = cellX;
1042 cellXY[1] = cellY;
1043 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001044
Jon Mirandac476d6e2017-05-04 16:30:01 -07001045 // If appropriate, either create a folder or add to an existing folder
1046 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Sunny Goyal1797af42017-10-06 13:29:57 -07001047 true, null)) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001048 return;
1049 }
1050 DragObject dragObject = new DragObject();
1051 dragObject.dragInfo = info;
1052 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1053 true)) {
1054 return;
1055 }
1056 } else {
1057 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1058 }
1059
1060 if (!foundCellSpan) {
1061 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001062 return;
1063 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001064
1065 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1066 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001067 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001068 // Adding a shortcut to a Folder.
Sunny Goyale29897f52017-07-20 10:09:42 -07001069 FolderIcon folderIcon = findFolderIcon(container);
Jon Mirandac476d6e2017-05-04 16:30:01 -07001070 if (folderIcon != null) {
1071 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1072 folderInfo.add(info, args.rank, false);
1073 } else {
Sunny Goyale29897f52017-07-20 10:09:42 -07001074 Log.e(TAG, "Could not find folder with id " + container + " to add shortcut.");
Jon Mirandac476d6e2017-05-04 16:30:01 -07001075 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 }
1078
Sunny Goyale29897f52017-07-20 10:09:42 -07001079 public FolderIcon findFolderIcon(final long folderIconId) {
1080 return (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1081 @Override
1082 public boolean evaluate(ItemInfo info, View view) {
1083 return info != null && info.id == folderIconId;
1084 }
1085 });
1086 }
1087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001091 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001093 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001094 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1095
Adam Cohened66b2b2012-01-23 17:28:51 -08001096 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001097 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001098 }
Romain Guycbb89e42009-06-08 15:52:54 -07001099
Adam Cohen59400422014-03-05 18:07:04 -08001100 LauncherAppWidgetInfo launcherInfo;
1101 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001102 launcherInfo.spanX = itemInfo.spanX;
1103 launcherInfo.spanY = itemInfo.spanY;
1104 launcherInfo.minSpanX = itemInfo.minSpanX;
1105 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001106 launcherInfo.user = appWidgetInfo.getProfile();
Romain Guycbb89e42009-06-08 15:52:54 -07001107
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001108 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001109 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110
Sunny Goyal2100c782016-08-22 16:00:03 -07001111 if (hostView == null) {
1112 // Perform actual inflation because we're live
1113 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001115 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301116 prepareAppWidget(hostView, launcherInfo);
1117 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Sunny Goyald5462aa2016-11-22 16:52:39 +05301120 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001121 hostView.setTag(item);
1122 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001123 hostView.setFocusable(true);
1124 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001125 }
1126
Adam Cohended9f8d2010-11-03 13:25:16 -07001127 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1128 @Override
1129 public void onReceive(Context context, Intent intent) {
1130 final String action = intent.getAction();
1131 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001132 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001133 // processing a multi-step drop
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001134 if (mAppsView != null && mPendingRequestArgs == null) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001135 mStateManager.goToState(NORMAL);
Winson Chung785d2eb2011-04-14 16:08:02 -07001136 }
Jon Miranda7fda2852017-07-19 16:07:01 -07001137 mShouldFadeInScrim = true;
1138 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1139 // ACTION_USER_PRESENT is sent after onStart/onResume. This covers the case where
1140 // the user unlocked and the Launcher is not in the foreground.
1141 mShouldFadeInScrim = false;
Adam Cohended9f8d2010-11-03 13:25:16 -07001142 }
1143 }
1144 };
1145
Tony Wickham010d2552017-01-20 08:15:28 -08001146 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
Sunny Goyal29947f02017-12-18 13:49:44 -08001147 mWorkspace.updateIconBadges(updatedBadges);
1148 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001149
Sunny Goyal29947f02017-12-18 13:49:44 -08001150 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1151 if (popup != null) {
1152 popup.updateNotificationHeader(updatedBadges);
Tony Wickham010d2552017-01-20 08:15:28 -08001153 }
1154 }
1155
Adam Cohended9f8d2010-11-03 13:25:16 -07001156 @Override
1157 public void onAttachedToWindow() {
1158 super.onAttachedToWindow();
1159
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001160 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001161 if (mLauncherCallbacks != null) {
1162 mLauncherCallbacks.onAttachedToWindow();
1163 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001164 }
1165
1166 @Override
1167 public void onDetachedFromWindow() {
1168 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001169
1170 if (mLauncherCallbacks != null) {
1171 mLauncherCallbacks.onDetachedFromWindow();
1172 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001173 }
1174
Sunny Goyalc4fa8c32017-11-07 12:23:58 -08001175 public AllAppsTransitionController getAllAppsController() {
1176 return mAllAppsController;
1177 }
1178
Winson Chunga6945242014-01-08 14:04:34 -08001179 public DragLayer getDragLayer() {
1180 return mDragLayer;
1181 }
1182
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001183 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001184 return mAppsView;
1185 }
1186
Winson Chunga6945242014-01-08 14:04:34 -08001187 public Workspace getWorkspace() {
1188 return mWorkspace;
1189 }
1190
1191 public Hotseat getHotseat() {
1192 return mHotseat;
1193 }
1194
Winson Chung8ae41982017-11-10 11:42:13 -08001195 public <T extends ViewGroup> T getOverviewPanel() {
1196 return (T) mOverviewPanel;
Winson Chunga6945242014-01-08 14:04:34 -08001197 }
1198
Sunny Goyal47328fd2016-05-25 18:56:41 -07001199 public DropTargetBar getDropTargetBar() {
1200 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001201 }
1202
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001203 public LauncherAppWidgetHost getAppWidgetHost() {
1204 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
Romain Guycbb89e42009-06-08 15:52:54 -07001206
Winson Chunga9abd0e2010-10-27 17:18:37 -07001207 public LauncherModel getModel() {
1208 return mModel;
1209 }
1210
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001211 public ModelWriter getModelWriter() {
1212 return mModelWriter;
1213 }
1214
Adam Cohen79d90c52016-04-22 13:29:20 -07001215 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001216 return mSharedPrefs;
1217 }
1218
Jon Miranda6bed3502017-09-19 14:43:17 -07001219 public int getOrientation() { return mOrientation; }
1220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 @Override
1222 protected void onNewIntent(Intent intent) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001223 TraceHelper.beginSection("NEW_INTENT");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 super.onNewIntent(intent);
1225
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001226 boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
Winson15f8b172015-08-19 11:02:39 -07001227 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1228 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001229
1230 // Check this condition before handling isActionMain, as this will get reset.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001231 boolean shouldMoveToDefaultScreen = alreadyOnHome && isInState(NORMAL)
Sunny Goyalf9403d92017-10-18 10:55:56 -07001232 && AbstractFloatingView.getTopOpenView(this) == null;
Winson15f8b172015-08-19 11:02:39 -07001233 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
Sunny Goyald3864fa2017-11-14 15:06:36 -08001234 boolean internalStateHandled = InternalStateHandler
1235 .handleNewIntent(this, intent, alreadyOnHome);
1236
Winson15f8b172015-08-19 11:02:39 -07001237 if (isActionMain) {
Sunny Goyald3864fa2017-11-14 15:06:36 -08001238 if (!internalStateHandled) {
1239 // Note: There should be at most one log per method call. This is enforced
1240 // implicitly by using if-else statements.
1241 UserEventDispatcher ued = getUserEventDispatcher();
1242 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
1243 if (topOpenView != null) {
1244 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
1245 } else if (alreadyOnHome) {
1246 Target target = newContainerTarget(mStateManager.getState().containerType);
1247 target.pageIndex = mWorkspace.getCurrentPage();
1248 ued.logActionCommand(Action.Command.HOME_INTENT, target);
1249 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001250
Sunny Goyald3864fa2017-11-14 15:06:36 -08001251 // In all these cases, only animate if we're already on home
1252 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001253
Sunny Goyald3864fa2017-11-14 15:06:36 -08001254 mStateManager.goToState(NORMAL, alreadyOnHome /* animated */);
1255
1256 // Reset the apps view
1257 if (!alreadyOnHome && mAppsView != null) {
1258 mAppsView.reset();
1259 }
1260
1261 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()) {
1262 mWorkspace.post(mWorkspace::moveToDefaultScreen);
1263 }
1264 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001265
1266 final View v = getWindow().peekDecorView();
1267 if (v != null && v.getWindowToken() != null) {
Sunny Goyal326403e2017-10-02 12:45:10 -07001268 UiThreadHelper.hideKeyboardAsync(this, v.getWindowToken());
Adam Cohen6fecd412013-10-02 17:41:50 -07001269 }
1270
Adam Cohen9211d422014-10-07 18:14:53 -07001271 if (mLauncherCallbacks != null) {
1272 mLauncherCallbacks.onHomeIntent();
1273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
Winson15f8b172015-08-19 11:02:39 -07001275
Sunny Goyalfca6bc92017-09-28 16:28:34 -07001276 TraceHelper.endSection("NEW_INTENT");
Adam Coppa120b8e2013-11-12 16:26:04 +00001277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001280 public void onRestoreInstanceState(Bundle state) {
1281 super.onRestoreInstanceState(state);
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001282 mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
1285 @Override
1286 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001287 if (mWorkspace.getChildCount() > 0) {
Sunny Goyal7ce471b2017-08-02 03:37:39 -07001288 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001289
Adam Cohen21cd0022013-10-09 18:57:02 -07001290 }
Sunny Goyalea609262017-10-25 15:47:38 -07001291 outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001292
1293
1294 AbstractFloatingView widgets = AbstractFloatingView
1295 .getOpenView(this, AbstractFloatingView.TYPE_WIDGETS_FULL_SHEET);
1296 if (widgets != null) {
1297 SparseArray<Parcelable> widgetsState = new SparseArray<>();
1298 widgets.saveHierarchyState(widgetsState);
1299 outState.putSparseParcelableArray(RUNTIME_STATE_WIDGET_PANEL, widgetsState);
1300 } else {
1301 outState.remove(RUNTIME_STATE_WIDGET_PANEL);
1302 }
1303
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001304 // We close any open folders and shortcut containers since they will not be re-opened,
1305 // and we need to make sure this state is reflected.
1306 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001307
Sunny Goyal2100c782016-08-22 16:00:03 -07001308 if (mPendingRequestArgs != null) {
1309 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1310 }
1311 if (mPendingActivityResult != null) {
1312 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001315 super.onSaveInstanceState(outState);
1316
Adam Cohen9211d422014-10-07 18:14:53 -07001317 if (mLauncherCallbacks != null) {
1318 mLauncherCallbacks.onSaveInstanceState(outState);
1319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
1322 @Override
1323 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001325
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001326 unregisterReceiver(mReceiver);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001327 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001328
Winson Chungcd2b0142011-06-08 16:02:26 -07001329 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001330 // It's possible to receive onDestroy after a new Launcher activity has
1331 // been created. In this case, don't interfere with the new Launcher.
1332 if (mModel.isCurrentCallbacks(this)) {
1333 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001334 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001335 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001336
Sunny Goyal745bad92016-05-02 10:54:12 -07001337 if (mRotationPrefChangeHandler != null) {
1338 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1339 }
1340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001342 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001344 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 TextKeyListener.getInstance().release();
Mario Bertschler27288382017-05-24 15:35:09 -07001348 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1349
Michael Jurka2ecf9952012-06-18 12:52:28 -07001350 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001351
Tony2917a8b2017-07-31 23:29:42 -07001352 clearPendingBinds();
1353
Adam Cohen9211d422014-10-07 18:14:53 -07001354 if (mLauncherCallbacks != null) {
1355 mLauncherCallbacks.onDestroy();
1356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
Sunny Goyalae502842016-06-17 08:43:56 -07001359 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1360 return mAccessibilityDelegate;
1361 }
1362
Adam Cohena9cf38f2011-05-02 15:36:58 -07001363 public DragController getDragController() {
1364 return mDragController;
1365 }
1366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001368 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001369 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001370 }
1371
1372 @Override
1373 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1374 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001375 try {
1376 super.startIntentSenderForResult(intent, requestCode,
1377 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1378 } catch (IntentSender.SendIntentException e) {
1379 throw new ActivityNotFoundException();
1380 }
1381 }
1382
Winson Chung70d72102011-08-12 11:24:35 -07001383 /**
1384 * Indicates that we want global search for this activity by setting the globalSearch
1385 * argument for {@link #startSearch} to true.
1386 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001388 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 Bundle appSearchData, boolean globalSearch) {
1390 if (appSearchData == null) {
1391 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001392 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001394
Sunny Goyal6f28e712016-09-13 14:19:29 -07001395 if (mLauncherCallbacks == null ||
1396 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1397 // Starting search from the callbacks failed. Start the default global search.
Sunny Goyalf4aceab2017-12-06 16:16:31 -08001398 super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
Ian Parkinson047036e2014-09-01 15:40:53 +01001399 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001400
1401 // We need to show the workspace after starting the search
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001402 mStateManager.goToState(NORMAL);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001403 }
1404
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001406 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 }
1408
Adam Cohen517a7f52014-03-01 12:12:59 -08001409 public boolean isWorkspaceLoading() {
1410 return mWorkspaceLoading;
1411 }
1412
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001413 private void setWorkspaceLoading(boolean value) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001414 mWorkspaceLoading = value;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001415 }
1416
Sunny Goyal04a324a2017-01-20 21:08:59 -08001417 public void setWaitingForResult(PendingRequestArgs args) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001418 mPendingRequestArgs = args;
Adam Cohen9211d422014-10-07 18:14:53 -07001419 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001420
Sunny Goyal2100c782016-08-22 16:00:03 -07001421 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001422 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001423 if (LOGD) {
1424 Log.d(TAG, "Adding widget from drop");
1425 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001426 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001427 }
1428
Sunny Goyal2100c782016-08-22 16:00:03 -07001429 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08001430 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
1431 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
1432 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07001433
Adam Cohenad4e15c2013-10-17 16:21:35 -07001434 Runnable onComplete = new Runnable() {
1435 @Override
1436 public void run() {
1437 // Exit spring loaded mode if necessary after adding the widget
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001438 mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001439 }
1440 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08001441 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07001442 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444 }
Romain Guycbb89e42009-06-08 15:52:54 -07001445
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001446 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1447 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001448 info.container = container;
1449 info.screenId = screenId;
1450 if (cell != null) {
1451 info.cellX = cell[0];
1452 info.cellY = cell[1];
1453 }
1454 info.spanX = spanX;
1455 info.spanY = spanY;
1456
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001457 switch (info.itemType) {
1458 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1459 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07001460 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001461 break;
1462 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08001463 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001464 break;
1465 default:
1466 throw new IllegalStateException("Unknown item type: " + info.itemType);
1467 }
1468 }
1469
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 /**
1471 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001472 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08001473 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001474 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
1475 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001476 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
1477 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
1478 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001479 }
1480
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481 /**
1482 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07001483 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001484 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 AppWidgetHostView hostView = info.boundWidget;
Sunny Goyal952e63d2017-08-16 04:59:08 -07001486 final int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08001487 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08001488 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001489 // In the case where we've prebound the widget, we remove it from the DragLayer
1490 if (LOGD) {
1491 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
1492 }
1493 getDragLayer().removeView(hostView);
1494
Adam Cohened66b2b2012-01-23 17:28:51 -08001495 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08001496 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07001497
1498 // Clear the boundWidget so that it doesn't get destroyed.
1499 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001500 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001501 // In this case, we either need to start an activity to get permission to bind
1502 // the widget, or we need to start an activity to configure the widget, or both.
Sunny Goyal952e63d2017-08-16 04:59:08 -07001503 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS &&
1504 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) {
1505 appWidgetId = CustomWidgetParser.getWidgetIdForCustomProvider(
1506 this, info.componentName);
1507 } else {
1508 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1509 }
Adam Cohendd70d662012-10-04 16:53:44 -07001510 Bundle options = info.bindOptions;
1511
Sunny Goyalffe83f12014-08-14 17:39:34 -07001512 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
1513 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07001514 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001515 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07001516 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001517 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001518 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001520 }
1521
Adam Cohendcd297f2013-06-18 13:13:40 -07001522 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001523 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001524 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 folderInfo.title = getText(R.string.folder_name);
1526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001528 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001531 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301532 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07001533 // Force measure the new folder icon
1534 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
1535 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07001536 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Winsonc0b52fe2015-09-09 16:38:15 -07001539 /**
Winsonfa56b3f2015-09-14 12:01:13 -07001540 * Unbinds the view for the specified item, and removes the item and all its children.
1541 *
1542 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07001543 * @param itemInfo the {@link ItemInfo} for this view.
1544 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07001545 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001546 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07001547 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07001548 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07001549 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
1550 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07001551 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07001552 } else {
1553 mWorkspace.removeWorkspaceItem(v);
1554 }
Winsonc0b52fe2015-09-09 16:38:15 -07001555 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001556 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001557 }
1558 } else if (itemInfo instanceof FolderInfo) {
1559 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001560 if (v instanceof FolderIcon) {
1561 ((FolderIcon) v).removeListeners();
1562 }
Winsonc0b52fe2015-09-09 16:38:15 -07001563 mWorkspace.removeWorkspaceItem(v);
1564 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001565 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001566 }
1567 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
1568 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07001569 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07001570 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07001571 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001572 }
1573 } else {
1574 return false;
1575 }
1576 return true;
1577 }
1578
1579 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07001580 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07001581 */
Sunny Goyal56c73602015-09-25 12:55:01 -07001582 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07001583 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07001584 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07001585 // Deleting an app widget ID is a void call but writes to disk before returning
1586 // to the caller...
1587 new AsyncTask<Void, Void, Void>() {
1588 public Void doInBackground(Void ... args) {
1589 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
1590 return null;
1591 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07001592 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07001593 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001594 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07001595 }
1596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 @Override
1598 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001599 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
1601
Joe Onorato88ec0992009-11-19 13:16:06 -08001602 @Override
1603 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07001604 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
1605 return;
1606 }
1607
Sunny Goyal45478022015-06-08 16:52:41 -07001608 if (mDragController.isDragging()) {
1609 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08001610 return;
1611 }
1612
Jon Mirandafeba90f2016-10-06 10:53:29 -07001613 // Note: There should be at most one log per method call. This is enforced implicitly
1614 // by using if-else statements.
1615 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001616 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
1617 if (topView != null) {
Sunny Goyal37920962017-09-28 13:43:24 -07001618 topView.onBackPressed();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001619 } else if (!isInState(NORMAL)) {
Sunny Goyalea609262017-10-25 15:47:38 -07001620 ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001621 mStateManager.goToState(NORMAL);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001622 } else {
1623 // Back button is a no-op here, but give at least some feedback for the button press
1624 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001625 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001626 }
1627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 * Launches the intent referred by the clicked shortcut.
1630 *
1631 * @param v The view representing the clicked shortcut.
1632 */
1633 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001634 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1635 // view has detached (it's possible for this to happen if the view is removed mid touch).
1636 if (v.getWindowToken() == null) {
1637 return;
1638 }
1639
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001640 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001641 return;
1642 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001643
Adam Cohen1697b792013-09-17 19:08:21 -07001644 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001645 if (isInState(OVERVIEW)) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08001646 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1647 LauncherLogProto.Action.Direction.NONE,
1648 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001649 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001650 }
1651 return;
1652 }
1653
1654 if (v instanceof CellLayout) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001655 if (isInState(OVERVIEW)) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08001656 int page = mWorkspace.indexOfChild(v);
1657 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
1658 LauncherLogProto.Action.Direction.NONE,
1659 LauncherLogProto.ContainerType.OVERVIEW, page);
1660 mWorkspace.snapToPageFromOverView(page);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07001661 mStateManager.goToState(NORMAL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001662 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07001663 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001664 }
1665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001667 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001668 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001670 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001671 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001672 }
Sunny Goyal508da152014-08-14 10:53:27 -07001673 } else if (tag instanceof AppInfo) {
1674 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07001675 } else if (tag instanceof LauncherAppWidgetInfo) {
1676 if (v instanceof PendingAppWidgetHostView) {
1677 onClickPendingWidget((PendingAppWidgetHostView) v);
1678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680 }
1681
Sunny Goyal70660032015-05-14 00:07:08 -07001682 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07001683 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07001684 return false;
1685 }
1686
Michael Jurkaaf442092010-06-10 17:01:57 -07001687 /**
Sunny Goyalff572272014-07-23 13:58:07 -07001688 * Event handler for the app widget view which has not fully restored.
1689 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001690 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07001691 if (mIsSafeModeEnabled) {
1692 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
1693 return;
1694 }
1695
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001696 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07001697 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001698 LauncherAppWidgetProviderInfo appWidgetInfo =
1699 mAppWidgetManager.findProvider(info.providerName, info.user);
1700 if (appWidgetInfo == null) {
1701 return;
1702 }
1703 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
1704
Sunny Goyald478c832016-04-01 12:04:16 -07001705 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
1706 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
1707 // This should not happen, as we make sure that an Id is allocated during bind.
1708 return;
1709 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08001710 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
1711 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07001712 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08001713 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07001714 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001715 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001716 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08001717 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07001718 }
1719 }
1720
Sunny Goyale6e72002017-01-12 16:55:36 -08001721 private void onClickPendingAppItem(final View v, final String packageName,
1722 boolean downloadStarted) {
1723 if (downloadStarted) {
1724 // If the download has started, simply direct to the market app.
1725 startMarketIntentForPackage(v, packageName);
1726 return;
1727 }
Sunny Goyale03b8122014-10-08 09:55:24 -07001728 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001729 .setTitle(R.string.abandoned_promises_title)
1730 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08001731 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
1732 @Override
1733 public void onClick(DialogInterface dialogInterface, int i) {
1734 startMarketIntentForPackage(v, packageName);
1735 }
1736 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001737 .setNeutralButton(R.string.abandoned_clean_this,
1738 new DialogInterface.OnClickListener() {
1739 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001740 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001741 mWorkspace.removeAbandonedPromise(packageName, user);
1742 }
1743 })
1744 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08001745 }
1746
1747 private void startMarketIntentForPackage(View v, String packageName) {
1748 ItemInfo item = (ItemInfo) v.getTag();
1749 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001750 startActivitySafely(v, intent, item);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001751 }
1752
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001753 /**
1754 * Event handler for an app shortcut click.
1755 *
1756 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
1757 */
Chris Wren40c5ed32014-06-24 18:24:23 -04001758 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001759 if (LOGD) Log.d(TAG, "onClickAppShortcut");
1760 Object tag = v.getTag();
1761 if (!(tag instanceof ShortcutInfo)) {
1762 throw new IllegalArgumentException("Input must be a Shortcut");
1763 }
1764
1765 // Open shortcut
1766 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001767
Sunny Goyal076839c2017-10-30 13:52:20 -07001768 if (shortcut.isDisabled()) {
Tony Makf6308652017-12-02 16:56:22 +00001769 final int disabledFlags = shortcut.runtimeStatusFlags & ShortcutInfo.FLAG_DISABLED_MASK;
1770 if ((disabledFlags &
Sunny Goyal076839c2017-10-30 13:52:20 -07001771 ~FLAG_DISABLED_SUSPENDED &
1772 ~FLAG_DISABLED_QUIET_USER) == 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001773 // If the app is only disabled because of the above flags, launch activity anyway.
1774 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00001775 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07001776 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
1777 // Use a message specific to this shortcut, if it has one.
1778 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
1779 return;
1780 }
1781 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00001782 int error = R.string.activity_not_available;
Sunny Goyal076839c2017-10-30 13:52:20 -07001783 if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_SAFEMODE) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00001784 error = R.string.safemode_shortcut_error;
Sunny Goyal076839c2017-10-30 13:52:20 -07001785 } else if ((shortcut.runtimeStatusFlags & FLAG_DISABLED_BY_PUBLISHER) != 0 ||
1786 (shortcut.runtimeStatusFlags & FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07001787 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00001788 }
1789 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
1790 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001791 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001792 }
1793
Chris Wren40c5ed32014-06-24 18:24:23 -04001794 // Check for abandoned promise
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -07001795 if ((v instanceof BubbleTextView) && shortcut.hasPromiseIconUi()) {
Sunny Goyale6e72002017-01-12 16:55:36 -08001796 String packageName = shortcut.intent.getComponent() != null ?
1797 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
1798 if (!TextUtils.isEmpty(packageName)) {
1799 onClickPendingAppItem(v, packageName,
1800 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
1801 return;
1802 }
Chris Wren40c5ed32014-06-24 18:24:23 -04001803 }
1804
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001805 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07001806 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04001807 }
1808
Sunny Goyala7ce1662016-05-31 15:01:35 -07001809 private void startAppShortcutOrInfoActivity(View v) {
1810 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07001811 Intent intent;
1812 if (item instanceof PromiseAppInfo) {
1813 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
1814 intent = promiseAppInfo.getMarketIntent();
1815 } else {
1816 intent = item.getIntent();
1817 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001818 if (intent == null) {
1819 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07001820 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001821 startActivitySafely(v, intent, item);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001822 }
1823
1824 /**
1825 * Event handler for a folder icon click.
1826 *
1827 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
1828 */
1829 protected void onClickFolderIcon(View v) {
1830 if (LOGD) Log.d(TAG, "onClickFolder");
1831 if (!(v instanceof FolderIcon)){
1832 throw new IllegalArgumentException("Input must be a FolderIcon");
1833 }
1834
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001835 Folder folder = ((FolderIcon) v).getFolder();
1836 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001837 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001838 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001839 }
Michael Jurka5130e402011-10-13 04:55:35 -07001840 }
1841
Sandeep Siddharthad8058372014-01-28 10:41:15 -08001842 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001843 * Event handler for the wallpaper picker button that appears after a long press
1844 * on the home screen.
1845 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001846 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07001847 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07001848 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
1849 return;
1850 }
1851
Tony Wickham785f7a52015-08-31 17:28:32 -07001852 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
1853 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07001854 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001855 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001856 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001857
1858 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07001859 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
1860 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001861 intent.setPackage(pickerPackage);
1862 }
1863
Sunny Goyala5ace712017-11-15 17:12:51 -08001864 final Bundle launchOptions;
1865 if (v != null) {
1866 intent.setSourceBounds(getViewBounds(v));
1867 // If there is no target package, use the default intent chooser animation
1868 launchOptions = hasTargetPackage ? getActivityLaunchOptions(v) : null;
1869 } else {
1870 launchOptions = null;
1871 }
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001872 try {
Sunny Goyala5ace712017-11-15 17:12:51 -08001873 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, launchOptions);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01001874 } catch (ActivityNotFoundException e) {
1875 setWaitingForResult(null);
1876 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1877 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001878 }
1879
Sunny Goyala7ce1662016-05-31 15:01:35 -07001880 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001882 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
1883 try {
1884 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
1885 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
1886 // is enabled by default on NYC.
1887 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
1888 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001889
1890 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1891 String id = ((ShortcutInfo) info).getDeepShortcutId();
1892 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301893 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001894 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001895 } else {
1896 // Could be launching some bookkeeping activity
1897 startActivity(intent, optsBundle);
1898 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001899 } finally {
1900 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001903 // Due to legacy reasons, direct call shortcuts require Launchers to have the
1904 // corresponding permission. Show the appropriate permission prompt if that
1905 // is the case.
1906 if (intent.getComponent() == null
1907 && Intent.ACTION_CALL.equals(intent.getAction())
1908 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
1909 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001910
1911 setWaitingForResult(PendingRequestArgs
1912 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001913 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
1914 REQUEST_PERMISSION_CALL_PHONE);
1915 } else {
1916 // No idea why this was thrown.
1917 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07001918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001921
Sunny Goyalfe770c92016-09-27 14:38:58 -07001922 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07001923 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001924 if (Utilities.ATLEAST_MARSHMALLOW) {
1925 int left = 0, top = 0;
1926 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001927 if (v instanceof BubbleTextView) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001928 // Launch from center of icon, not entire view
Hyunyoung Song1322f9c2017-06-23 14:57:38 -07001929 Drawable icon = ((BubbleTextView) v).getIcon();
Sunny Goyala7ce1662016-05-31 15:01:35 -07001930 if (icon != null) {
1931 Rect bounds = icon.getBounds();
1932 left = (width - bounds.width()) / 2;
1933 top = v.getPaddingTop();
1934 width = bounds.width();
1935 height = bounds.height();
1936 }
1937 }
1938 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
1939 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
1940 // On L devices, we use the device default slide-up transition.
1941 // On L MR1 devices, we use a custom version of the slide-up transition which
1942 // doesn't have the delay present in the device default.
1943 return ActivityOptions.makeCustomAnimation(
1944 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
1945 }
1946 return null;
1947 }
1948
Tonye3c59252017-05-02 21:32:27 -07001949 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001950 int[] pos = new int[2];
1951 v.getLocationOnScreen(pos);
1952 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
1953 }
1954
Sunny Goyala7ce1662016-05-31 15:01:35 -07001955 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001956 mAppLaunchSuccess = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001957 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
1958 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001959 return mAppLaunchSuccess;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001960 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07001961 // Only launch using the new animation if the shortcut has not opted out (this is a
1962 // private contract between launcher and may be ignored in the future).
1963 boolean useLaunchAnimation = (v != null) &&
1964 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1965 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
1966
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001967 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07001968
1969 // Prepare intent
1970 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1971 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001972 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07001973 }
1974 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001975 if (Utilities.ATLEAST_MARSHMALLOW
1976 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001977 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001978 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08001979 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001980 // Shortcuts need some special checks due to legacy reasons.
1981 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001982 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07001983 // Could be launching some bookkeeping activity
1984 startActivity(intent, optsBundle);
1985 } else {
1986 LauncherAppsCompat.getInstance(this).startActivityForProfile(
1987 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
1988 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07001989
1990 if (v instanceof BubbleTextView) {
1991 // This is set to the view that launched the activity that navigated the user away
1992 // from launcher. Since there is no callback for when the activity has finished
1993 // launching, enable the press state and keep this reference to reset the press
1994 // state when we return to launcher.
1995 BubbleTextView btv = (BubbleTextView) v;
1996 btv.setStayPressed(true);
1997 setOnResumeCallback(btv);
1998 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07001999 mAppLaunchSuccess = true;
2000 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sunny Goyala7ce1662016-05-31 15:01:35 -07002001 } catch (ActivityNotFoundException|SecurityException e) {
2002 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2003 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2004 }
Hyunyoung Song7fb3ccc2017-10-17 15:39:46 -07002005 return mAppLaunchSuccess;
Michael Jurka86a720e2012-05-09 11:23:23 -07002006 }
2007
Tony Wickhamdadb3042016-02-24 11:07:00 -08002008 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002009 public boolean dispatchTouchEvent(MotionEvent ev) {
2010 mLastDispatchTouchEventX = ev.getX();
2011 return super.dispatchTouchEvent(ev);
2012 }
2013
2014 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002016 if (!isDraggingEnabled()) return false;
2017 if (isWorkspaceLocked()) return false;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002018 if (!isInState(NORMAL) && !isInState(OVERVIEW)) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019
Jon Miranda51f037d2016-11-07 08:37:20 -08002020 boolean ignoreLongPressToOverview =
2021 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002022
Adam Cohen1697b792013-09-17 19:08:21 -07002023 if (v instanceof Workspace) {
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002024 if (!isInState(OVERVIEW)) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002025 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302026 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2027 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002028 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002029 UiFactory.onWorkspaceLongPress(this);
Adam Cohen93c97562013-09-26 13:48:01 -07002030 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2031 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2032 return true;
2033 } else {
2034 return false;
2035 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002036 } else {
2037 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002038 }
Adam Cohen1697b792013-09-17 19:08:21 -07002039 }
2040
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002041 CellLayout.CellInfo longClickCellInfo = null;
2042 View itemUnderLongClick = null;
2043 if (v.getTag() instanceof ItemInfo) {
2044 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002045 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002046 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002047 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 }
2049
Winson Chung3d503fb2011-07-13 17:25:49 -07002050 // The hotseat touch handling does not go through Workspace, and we always allow long press
2051 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002052 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002053 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002054 // User long pressed on empty space
Sunny Goyalc99cb172017-10-19 16:15:09 -07002055 if (mWorkspace.isPageRearrangeEnabled()) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002056 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302057 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2058 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002059 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002060 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002061 return false;
2062 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302063 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2064 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002065 mWorkspace.getCurrentPage());
Sunny Goyala5ace712017-11-15 17:12:51 -08002066 UiFactory.onWorkspaceLongPress(this);
Adam Cohendedbd962013-07-11 14:21:49 -07002067 }
Jon Miranda379198e2016-09-23 08:54:40 -07002068 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2069 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002071 final boolean isAllAppsButton =
2072 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2073 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2074 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002075 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002077 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 }
2079 }
2080 }
2081 return true;
2082 }
2083
Winson Chung3d503fb2011-07-13 17:25:49 -07002084 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002085 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 return mHotseat != null && layout != null &&
2087 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2088 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002089
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 /**
2091 * Returns the CellLayout of the specified container at the specified screen.
2092 */
Sunny Goyal92820592015-03-02 11:31:35 -08002093 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002094 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2095 if (mHotseat != null) {
2096 return mHotseat.getLayout();
2097 } else {
2098 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002099 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002101 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002102 }
2103 }
2104
Michael Jurkae326f182011-11-21 14:05:46 -08002105 @Override
2106 public void onTrimMemory(int level) {
2107 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002108 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2109 // The widget preview db can result in holding onto over
2110 // 3MB of memory for caching which isn't necessary.
2111 SQLiteDatabase.releaseMemory();
2112
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002113 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002114 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002115 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002116 if (mLauncherCallbacks != null) {
2117 mLauncherCallbacks.onTrimMemory(level);
2118 }
Michael Jurkae326f182011-11-21 14:05:46 -08002119 }
2120
Michael Jurkad7c28052012-04-27 15:43:36 -07002121 @Override
2122 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07002123 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07002124 final List<CharSequence> text = event.getText();
2125 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07002126 // Populate event with a fake title based on the current state.
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002127 // TODO: When can workspace be null?
2128 text.add(mWorkspace == null
2129 ? getString(R.string.all_apps_home_button_label)
Sunny Goyalea609262017-10-25 15:47:38 -07002130 : mStateManager.getState().getDescription(this));
Michael Jurkad7c28052012-04-27 15:43:36 -07002131 return result;
2132 }
2133
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002134 public void setOnResumeCallback(OnResumeCallback callback) {
2135 if (mOnResumeCallback != null) {
2136 mOnResumeCallback.onLauncherResume();
2137 }
2138 mOnResumeCallback = callback;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002139 }
2140
Michael Jurka7607c2f2013-04-03 14:33:19 -07002141 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07002144 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002146 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002147 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002148 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07002149 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07002150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002152
Joe Onorato9c1289c2009-08-17 11:03:03 -04002153 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07002154 * Clear any pending bind callbacks. This is called when is loader is planning to
2155 * perform a full rebind from scratch.
2156 */
2157 @Override
2158 public void clearPendingBinds() {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002159 if (mPendingExecutor != null) {
2160 mPendingExecutor.markCompleted();
2161 mPendingExecutor = null;
2162 }
2163 }
2164
2165 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 * Refreshes the shortcuts shown on the workspace.
2167 *
2168 * Implementation of the method from LauncherModel.Callbacks.
2169 */
2170 public void startBinding() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002171 TraceHelper.beginSection("startBinding");
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002172 // Floating panels (except the full widget sheet) are associated with individual icons. If
2173 // we are starting a fresh bind, close all such panels as all the icons are about
2174 // to go away.
2175 AbstractFloatingView.closeOpenViews(this, true,
Sunny Goyal7ede6112017-12-05 15:11:21 -08002176 AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08002177
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08002179
Winson Chungd64d1762013-08-20 14:37:16 -07002180 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07002181 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07002182 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07002183
Winson Chung3d503fb2011-07-13 17:25:49 -07002184 if (mHotseat != null) {
2185 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 }
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002187 TraceHelper.endSection("startBinding");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
2189
Adam Cohendcd297f2013-06-18 13:13:40 -07002190 @Override
2191 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002192 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002193 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
2194 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002195 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
2196 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002197 LauncherModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002198 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
2199 // If there are no screens, we need to have an empty screen
2200 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07002201 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002202 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07002203
Winsonc7d2e832016-07-28 12:24:55 -07002204 // After we have added all the screens, if the wallpaper was locked to the default state,
2205 // then notify to indicate that it can be released and a proper wallpaper offset can be
2206 // computed before the next layout
2207 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07002208 }
2209
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002210 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 int count = orderedScreenIds.size();
2212 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002213 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07002214 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002215 // No need to bind the first screen, as its always bound.
2216 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
2217 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002218 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002219 }
2220
Sunny Goyalb23980c2017-08-17 07:45:25 -07002221 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002222 public void bindAppsAdded(ArrayList<Long> newScreens, ArrayList<ItemInfo> addNotAnimated,
2223 ArrayList<ItemInfo> addAnimated) {
Winson Chungd64d1762013-08-20 14:37:16 -07002224 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08002225 if (newScreens != null) {
2226 bindAddScreens(newScreens);
2227 }
Winson Chungd64d1762013-08-20 14:37:16 -07002228
2229 // We add the items without animation on non-visible pages, and with
2230 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08002231 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002232 bindItems(addNotAnimated, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002233 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002234 if (addAnimated != null && !addAnimated.isEmpty()) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002235 bindItems(addAnimated, true);
Winson Chungd64d1762013-08-20 14:37:16 -07002236 }
Winson Chungc58497e2013-09-03 17:48:37 -07002237
Winson Chung87412982013-10-03 18:34:14 -07002238 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07002239 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chungd64d1762013-08-20 14:37:16 -07002240 }
2241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002243 * Bind the items start-end from the list.
2244 *
2245 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07002247 @Override
Sunny Goyalb23980c2017-08-17 07:45:25 -07002248 public void bindItems(final List<ItemInfo> items, final boolean forceAnimateIcons) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002249 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07002250 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Rajeev Kumar43c0f582017-06-23 15:34:55 -07002251 final Collection<Animator> bounceAnims = new ArrayList<>();
Winson Chung997a9232013-07-24 15:33:46 -07002252 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002253 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002254 long newItemsScreenId = -1;
Sunny Goyalb23980c2017-08-17 07:45:25 -07002255 int end = items.size();
2256 for (int i = 0; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08002257 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002258
2259 // Short circuit if we are loading dock items for a configuration which has no dock
2260 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2261 mHotseat == null) {
2262 continue;
2263 }
2264
Sunny Goyal639e9062015-08-19 19:17:06 -07002265 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002266 switch (item.itemType) {
2267 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2268 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08002269 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07002270 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07002271 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002272 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002273 }
2274 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07002275 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002276 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002277 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002278 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002279 }
Sunny Goyal952e63d2017-08-16 04:59:08 -07002280 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2281 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: {
Sunny Goyala9a1a212017-08-18 01:03:21 -07002282 view = inflateAppWidget((LauncherAppWidgetInfo) item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002283 if (view == null) {
2284 continue;
Sunny Goyal3be633b2016-12-08 09:59:25 -08002285 }
Sunny Goyal3be633b2016-12-08 09:59:25 -08002286 break;
2287 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002288 default:
2289 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002290 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002291
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002292 /*
2293 * Remove colliding items.
2294 */
2295 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2296 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
2297 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
2298 View v = cl.getChildAt(item.cellX, item.cellY);
2299 Object tag = v.getTag();
2300 String desc = "Collision while binding workspace item: " + item
2301 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08002302 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002303 throw (new RuntimeException(desc));
2304 } else {
2305 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002306 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002307 continue;
2308 }
2309 }
2310 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302311 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07002312 if (animateIcons) {
2313 // Animate all the applications up now
2314 view.setAlpha(0f);
2315 view.setScaleX(0f);
2316 view.setScaleY(0f);
2317 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08002318 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07002319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002321
Winson Chung997a9232013-07-24 15:33:46 -07002322 if (animateIcons) {
2323 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08002324 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07002325 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08002326 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07002327 final Runnable startBounceAnimRunnable = new Runnable() {
2328 public void run() {
2329 anim.playTogether(bounceAnims);
2330 anim.start();
2331 }
2332 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08002333 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07002334 // We post the animation slightly delayed to prevent slowdowns
2335 // when we are loading right after we return to launcher.
2336 mWorkspace.postDelayed(new Runnable() {
2337 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00002338 if (mWorkspace != null) {
2339 mWorkspace.snapToPage(newScreenIndex);
2340 mWorkspace.postDelayed(startBounceAnimRunnable,
2341 NEW_APPS_ANIMATION_DELAY);
2342 }
Winson Chung94d67682013-09-25 16:29:40 -07002343 }
2344 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07002345 } else {
2346 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07002347 }
2348 }
Winson Chung64359a52013-07-08 17:17:08 -07002349 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002350 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 }
2352
Joe Onorato9c1289c2009-08-17 11:03:03 -04002353 /**
2354 * Add the views for a widget to the workspace.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002355 */
Sunny Goyala9a1a212017-08-18 01:03:21 -07002356 public void bindAppWidget(LauncherAppWidgetInfo item) {
2357 View view = inflateAppWidget(item);
2358 if (view != null) {
2359 mWorkspace.addInScreen(view, item);
2360 mWorkspace.requestLayout();
2361 }
2362 }
2363
2364 private View inflateAppWidget(LauncherAppWidgetInfo item) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002365 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302366 PendingAppWidgetHostView view =
2367 new PendingAppWidgetHostView(this, item, mIconCache, true);
2368 prepareAppWidget(view, item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002369 return view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002370 }
2371
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002372 TraceHelper.beginSection("BIND_WIDGET");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002373
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002374 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08002375
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002376 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2377 // If the provider is not ready, bind as a pending widget.
2378 appWidgetInfo = null;
2379 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2380 // The widget id is not valid. Try to find the widget based on the provider info.
2381 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
2382 } else {
2383 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
2384 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002385
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002386 // If the provider is ready, but the width is not yet restored, try to restore it.
2387 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
2388 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07002389 if (appWidgetInfo == null) {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002390 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
2391 + " belongs to component " + item.providerName
2392 + ", as the provider is null");
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002393 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002394 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002395 }
Sunny Goyalff572272014-07-23 13:58:07 -07002396
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002397 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002398 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07002399 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2400 // Id has not been allocated yet. Allocate a new id.
2401 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
2402 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07002403
Sunny Goyald478c832016-04-01 12:04:16 -07002404 // Also try to bind the widget. If the bind fails, the user will be shown
2405 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08002406 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07002407 pendingInfo.spanX = item.spanX;
2408 pendingInfo.spanY = item.spanY;
2409 pendingInfo.minSpanX = item.minSpanX;
2410 pendingInfo.minSpanY = item.minSpanY;
2411 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07002412
2413 boolean isDirectConfig =
2414 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
2415 if (isDirectConfig && item.bindOptions != null) {
2416 Bundle newOptions = item.bindOptions.getExtras();
2417 if (options != null) {
2418 newOptions.putAll(options);
2419 }
2420 options = newOptions;
2421 }
Sunny Goyald478c832016-04-01 12:04:16 -07002422 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2423 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002424
Sunny Goyal86df1382016-08-10 15:03:22 -07002425 // We tried to bind once. If we were not able to bind, we would need to
2426 // go through the permission dialog, which means we cannot skip the config
2427 // activity.
2428 item.bindOptions = null;
2429 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
2430
Sunny Goyald478c832016-04-01 12:04:16 -07002431 // Bind succeeded
2432 if (success) {
2433 // If the widget has a configure activity, it is still needs to set it up,
2434 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07002435 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07002436 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
2437 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002438 }
Sunny Goyald478c832016-04-01 12:04:16 -07002439
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002440 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002441 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002442 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002443 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002444 // The widget was marked as UI not ready, but there is no configure activity to
2445 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002446 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002447 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07002448 }
Sunny Goyalff572272014-07-23 13:58:07 -07002449 }
2450
Sunny Goyald5462aa2016-11-22 16:52:39 +05302451 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002452 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002453 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002454 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002455 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07002456 deleteWidgetInfo(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002457 return null;
Sunny Goyal56c73602015-09-25 12:55:01 -07002458 }
2459
Sunny Goyal233ee962015-08-03 13:05:01 -07002460 item.minSpanX = appWidgetInfo.minSpanX;
2461 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05302462 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07002463 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05302464 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07002465 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05302466 prepareAppWidget(view, item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002467
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002468 TraceHelper.endSection("BIND_WIDGET", "id=" + item.appWidgetId);
Sunny Goyalb23980c2017-08-17 07:45:25 -07002469 return view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002470 }
2471
Sunny Goyalff572272014-07-23 13:58:07 -07002472 /**
2473 * Restores a pending widget.
2474 *
2475 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07002476 */
Sunny Goyald478c832016-04-01 12:04:16 -07002477 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07002478 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
2479 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
2480 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07002481 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07002482 }
2483
Sunny Goyal0fc1be12014-08-11 17:05:23 -07002484 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07002485 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07002486 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
2487 info.pendingItemInfo = null;
2488 }
Sunny Goyalff572272014-07-23 13:58:07 -07002489
Sunny Goyalba47faa2017-10-04 16:50:57 -07002490 if (((PendingAppWidgetHostView) view).isReinflateIfNeeded()) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002491 view.reInflate();
Sunny Goyalba47faa2017-10-04 16:50:57 -07002492 }
2493
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002494 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07002495 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07002496 }
2497
Adam Cohen1462de32012-07-24 22:34:36 -07002498 public void onPageBoundSynchronously(int page) {
Sunny Goyalf4aceab2017-12-06 16:16:31 -08002499 mSynchronouslyBoundPage = page;
Adam Cohen1462de32012-07-24 22:34:36 -07002500 }
2501
Sunny Goyal527c7d32015-08-28 15:19:36 -07002502 @Override
2503 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
2504 if (mPendingExecutor != null) {
2505 mPendingExecutor.markCompleted();
2506 }
2507 mPendingExecutor = executor;
2508 executor.attachTo(this);
2509 }
2510
2511 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
2512 if (mPendingExecutor == executor) {
2513 mPendingExecutor = null;
2514 }
2515 }
2516
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002517 @Override
2518 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002519 if (mDragLayer.getAlpha() < 1) {
Sunny Goyal29947f02017-12-18 13:49:44 -08002520 mDragLayer.animate().alpha(1).withEndAction(
2521 executor == null ? null : executor::onLoadAnimationCompleted).start();
2522 } else if (executor != null) {
2523 executor.onLoadAnimationCompleted();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002524 }
2525 }
2526
Joe Onorato9c1289c2009-08-17 11:03:03 -04002527 /**
2528 * Callback saying that there aren't any more items to bind.
2529 *
2530 * Implementation of the method from LauncherModel.Callbacks.
2531 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002532 public void finishBindingItems() {
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002533 TraceHelper.beginSection("finishBindingItems");
Adam Cohen1462de32012-07-24 22:34:36 -07002534 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002535
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002536 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07002537
Sunny Goyal2100c782016-08-22 16:00:03 -07002538 if (mPendingActivityResult != null) {
2539 handleActivityResult(mPendingActivityResult.requestCode,
2540 mPendingActivityResult.resultCode, mPendingActivityResult.data);
2541 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07002542 }
2543
Sunny Goyala474a9b2017-05-04 16:47:11 -07002544 InstallShortcutReceiver.disableAndFlushInstallQueue(
2545 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07002546
Tony Wickham010d2552017-01-20 08:15:28 -08002547 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
Sunny Goyalfca6bc92017-09-28 16:28:34 -07002548 TraceHelper.endSection("finishBindingItems");
Winson Chungf0c6ae02012-03-21 16:10:31 -07002549 }
2550
Winson Chunga2413752012-04-03 14:22:34 -07002551 private boolean canRunNewAppsAnimation() {
2552 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07002553 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07002554 }
2555
Winson Chungc9168342013-06-26 14:54:55 -07002556 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002557 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07002558 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
2559 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07002560 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07002561 return bounceAnim;
2562 }
2563
Adam Cohen27772732013-11-11 14:00:56 +00002564 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07002565 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00002566 }
2567
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002568 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002569 * Add the icons for all apps.
2570 *
2571 * Implementation of the method from LauncherModel.Callbacks.
2572 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002573 public void bindAllApplications(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002574 if (mAppsView != null) {
Tony2917a8b2017-07-31 23:29:42 -07002575 Executor pendingExecutor = getPendingExecutor();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002576 if (pendingExecutor != null && !isInState(ALL_APPS)) {
Tony2917a8b2017-07-31 23:29:42 -07002577 // Wait until the fade in animation has finished before setting all apps list.
Sunny Goyal29947f02017-12-18 13:49:44 -08002578 pendingExecutor.execute(() -> bindAllApplications(apps));
Tony2917a8b2017-07-31 23:29:42 -07002579 return;
2580 }
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002581
Winson Chungb745afb2015-03-02 11:51:23 -08002582 mAppsView.setApps(apps);
2583 }
Adam Cohen9211d422014-10-07 18:14:53 -07002584 if (mLauncherCallbacks != null) {
2585 mLauncherCallbacks.bindAllApplications(apps);
2586 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002587 }
2588
2589 /**
Tony2917a8b2017-07-31 23:29:42 -07002590 * Returns an Executor that will run after the launcher is first drawn (including after the
2591 * initial fade in animation). Returns null if the first draw has already occurred.
2592 */
2593 public @Nullable Executor getPendingExecutor() {
2594 return mPendingExecutor != null && mPendingExecutor.canQueue() ? mPendingExecutor : null;
2595 }
2596
2597 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002598 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
2599 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
2600 */
2601 @Override
2602 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08002603 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07002604 }
2605
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002606 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002607 * A package was updated.
2608 *
2609 * Implementation of the method from LauncherModel.Callbacks.
2610 */
Sunny Goyal29947f02017-12-18 13:49:44 -08002611 @Override
2612 public void bindAppsAddedOrUpdated(ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08002613 if (mAppsView != null) {
Sunny Goyalb23980c2017-08-17 07:45:25 -07002614 mAppsView.addOrUpdateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07002615 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002616 }
2617
Sunny Goyal4390ace2014-10-13 11:33:11 -07002618 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002619 public void bindPromiseAppProgressUpdated(PromiseAppInfo app) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002620 if (mAppsView != null) {
2621 mAppsView.updatePromiseAppProgress(app);
2622 }
2623 }
2624
2625 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002626 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002627 mWorkspace.widgetsRestored(widgets);
2628 }
2629
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002631 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002632 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002633 *
2634 * @param updated list of shortcuts which have changed.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002635 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07002636 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002637 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, final UserHandle user) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002638 if (!updated.isEmpty()) {
2639 mWorkspace.updateShortcuts(updated);
2640 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002641 }
2642
2643 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002644 * Update the state of a package, typically related to install state.
2645 *
2646 * Implementation of the method from LauncherModel.Callbacks.
2647 */
Sunny Goyale755d462014-07-22 13:48:29 -07002648 @Override
Sunny Goyal29947f02017-12-18 13:49:44 -08002649 public void bindRestoreItemsChange(HashSet<ItemInfo> updates) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07002650 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07002651 }
2652
2653 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002654 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07002655 * in addition to specific applications to remove, the reason being that
2656 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002657 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07002658 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04002659 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07002660 @Override
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002661 public void bindWorkspaceComponentsRemoved(final ItemInfoMatcher matcher) {
Sunny Goyal6e13dd32017-08-17 03:01:19 -07002662 mWorkspace.removeItemsByMatcher(matcher);
2663 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002664 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002665
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002666 @Override
2667 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002668 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08002669 if (mAppsView != null) {
2670 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07002671 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002672 }
Joe Onoratobe386092009-11-17 17:32:16 -08002673
Sunny Goyal49fc3f72017-08-18 10:17:06 -07002674 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07002675 public void bindAllWidgets(final ArrayList<WidgetListRowEntry> allWidgets) {
2676 mPopupDataProvider.setAllWidgets(allWidgets);
Tony Wickham26b17462017-03-20 17:12:24 -07002677 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2678 if (topView != null) {
2679 topView.onWidgetsBound();
2680 }
2681 }
2682
Tony Wickham86222d22017-03-29 15:30:43 -07002683 /**
2684 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
2685 * refreshes the widgets and shortcuts associated with the given package/user
2686 */
2687 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07002688 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002689 }
2690
Sunny Goyalc99cb172017-10-19 16:15:09 -07002691 public boolean isRotationEnabled () {
2692 return mRotationEnabled;
Winson Chung400438b2011-01-16 17:53:48 -08002693 }
2694
Winson Chung80baf5a2010-08-09 16:03:15 -07002695 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002696 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08002697 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002698 @Override
2699 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2700 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002701
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002702 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2703 writer.println(prefix + "Workspace Items");
Sunny Goyal7ce471b2017-08-02 03:37:39 -07002704 for (int i = 0; i < mWorkspace.getPageCount(); i++) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002705 writer.println(prefix + " Homescreen " + i);
2706
2707 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
2708 for (int j = 0; j < layout.getChildCount(); j++) {
2709 Object tag = layout.getChildAt(j).getTag();
2710 if (tag != null) {
2711 writer.println(prefix + " " + tag.toString());
2712 }
2713 }
2714 }
2715
2716 writer.println(prefix + " Hotseat");
2717 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07002718 for (int j = 0; j < layout.getChildCount(); j++) {
2719 Object tag = layout.getChildAt(j).getTag();
2720 if (tag != null) {
2721 writer.println(prefix + " " + tag.toString());
2722 }
Adam Cohen4caf2982013-08-20 18:54:31 -07002723 }
Sunny Goyala1365452015-10-01 15:46:24 -07002724
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002725 try {
2726 FileLog.flushAll(writer);
2727 } catch (Exception e) {
2728 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07002729 }
2730 }
2731
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002732 writer.println(prefix + "Misc:");
2733 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
2734 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
2735 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
2736
2737 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07002738
Adam Cohen9211d422014-10-07 18:14:53 -07002739 if (mLauncherCallbacks != null) {
2740 mLauncherCallbacks.dump(prefix, fd, writer, args);
2741 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07002742 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002743
Sunny Goyal66b24572016-09-21 15:57:55 -07002744 @Override
2745 @TargetApi(Build.VERSION_CODES.N)
2746 public void onProvideKeyboardShortcuts(
2747 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
2748
2749 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002750 if (isInState(NORMAL)) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002751 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
2752 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
2753 }
2754 View currentFocus = getCurrentFocus();
2755 if (new CustomActionsPopup(this, currentFocus).canShow()) {
2756 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
2757 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
2758 }
Tony18c4aa42017-05-10 21:23:57 -05002759 if (currentFocus.getTag() instanceof ItemInfo
2760 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07002761 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
2762 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
2763 }
2764 if (!shortcutInfos.isEmpty()) {
2765 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
2766 }
2767
2768 super.onProvideKeyboardShortcuts(data, menu, deviceId);
2769 }
2770
2771 @Override
2772 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2773 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
2774 switch (keyCode) {
2775 case KeyEvent.KEYCODE_A:
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07002776 if (isInState(NORMAL)) {
2777 getStateManager().goToState(ALL_APPS);
Sunny Goyal66b24572016-09-21 15:57:55 -07002778 return true;
2779 }
2780 break;
2781 case KeyEvent.KEYCODE_S: {
2782 View focusedView = getCurrentFocus();
2783 if (focusedView instanceof BubbleTextView
2784 && focusedView.getTag() instanceof ItemInfo
2785 && mAccessibilityDelegate.performAction(focusedView,
2786 (ItemInfo) focusedView.getTag(),
2787 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal00ac9202017-11-09 15:24:06 -08002788 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07002789 return true;
2790 }
2791 break;
2792 }
2793 case KeyEvent.KEYCODE_O:
2794 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
2795 return true;
2796 }
2797 break;
2798 }
2799 }
2800 return super.onKeyShortcut(keyCode, event);
2801 }
2802
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07002803 public static Launcher getLauncher(Context context) {
2804 if (context instanceof Launcher) {
2805 return (Launcher) context;
2806 }
2807 return ((Launcher) ((ContextWrapper) context).getBaseContext());
2808 }
2809
Sunny Goyal5a81c382017-03-20 15:08:06 -07002810 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07002811
2812 @Override
2813 public void onSharedPreferenceChanged(
2814 SharedPreferences sharedPreferences, String key) {
2815 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07002816 // Recreate the activity so that it initializes the rotation preference again.
2817 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07002818 }
2819 }
Sunny Goyal745bad92016-05-02 10:54:12 -07002820 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -07002821
2822 /**
2823 * Callback for listening for onResume
2824 */
2825 public interface OnResumeCallback {
2826
2827 void onLauncherResume();
2828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829}