blob: bb32a4563bfeaed5b6da2e942090870952f12228 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Jon Miranda2d89ea82017-05-04 11:47:53 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070027import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080030import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080033import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080034import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040035import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070037import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040038import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070040import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070041import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070043import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070046import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070047import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070050import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020051import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080054import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070055import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040056import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070057import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070058import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070059import android.support.annotation.Nullable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.text.Selection;
61import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070062import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070064import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070066import android.view.HapticFeedbackConstants;
67import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070068import android.view.KeyboardShortcutGroup;
69import android.view.KeyboardShortcutInfo;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080070import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080074import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.ViewGroup;
76import android.view.ViewTreeObserver;
Mario Bertschleracbf5702017-03-03 10:58:06 -080077import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080078import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070079import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070080import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.inputmethod.InputMethodManager;
Chet Haasea8f996d2015-02-17 12:56:04 -080082import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070084
Sunny Goyal651077b2014-06-30 14:15:31 -070085import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070086import com.android.launcher3.LauncherSettings.Favorites;
Jon Mirandac476d6e2017-05-04 16:30:01 -070087import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyalae502842016-06-17 08:43:56 -070088import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070089import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070090import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyalb5e65c82016-10-26 18:32:38 -070091import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyalffe83f12014-08-14 17:39:34 -070092import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010093import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070094import com.android.launcher3.compat.LauncherAppsCompatVO;
Tony Wickhame0c33232016-02-08 11:37:04 -080095import com.android.launcher3.config.FeatureFlags;
Vadim Tryshevfedca432015-08-19 17:55:02 -070096import com.android.launcher3.dragndrop.DragController;
97import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070098import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070099import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800100import com.android.launcher3.dragndrop.PinItemDragListener;
Tony Wickham827cef22016-03-17 15:39:39 -0700101import com.android.launcher3.dynamicui.ExtractedColors;
Mario Bertschler27288382017-05-24 15:35:09 -0700102import com.android.launcher3.dynamicui.WallpaperColorInfo;
Sunny Goyal26119432016-02-18 22:09:23 +0000103import com.android.launcher3.folder.Folder;
104import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700105import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700106import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700107import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700108import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyala535ae42017-02-27 10:07:13 -0800109import com.android.launcher3.model.ModelWriter;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700110import com.android.launcher3.model.PackageItemInfo;
111import com.android.launcher3.model.WidgetItem;
Sunny Goyala535ae42017-02-27 10:07:13 -0800112import com.android.launcher3.notification.NotificationListener;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700113import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800114import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyala535ae42017-02-27 10:07:13 -0800115import com.android.launcher3.popup.PopupDataProvider;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700116import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700117import com.android.launcher3.shortcuts.ShortcutKey;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530119import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
120import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
121import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700124import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700125import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800127import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700128import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700129import com.android.launcher3.util.TestingUtils;
Mario Bertschlera6936942017-05-31 14:48:19 -0700130import com.android.launcher3.util.Themes;
Adam Cohen091440a2015-03-18 14:16:05 -0700131import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700132import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800133import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700134import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800135import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700136import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700138
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700139import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700140import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700141import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700142import java.util.Collection;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700143import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700144import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700145import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800146import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148/**
149 * Default launcher application.
150 */
Sunny Goyal27835952017-01-13 12:15:53 -0800151public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700152 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
153 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Mario Bertschler27288382017-05-24 15:35:09 -0700154 AccessibilityManager.AccessibilityStateChangeListener,
155 WallpaperColorInfo.OnThemeChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700156 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700157 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700159 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700160 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400161 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700164 private static final int REQUEST_CREATE_APPWIDGET = 5;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700165
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700166 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700167 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Michael Jurka8b805b12012-04-18 14:23:14 -0700169 private static final int REQUEST_BIND_APPWIDGET = 11;
Jon Mirandac476d6e2017-05-04 16:30:01 -0700170 private static final int REQUEST_BIND_PENDING_APPWIDGET = 12;
171 private static final int REQUEST_RECONFIGURE_APPWIDGET = 13;
Michael Jurka8b805b12012-04-18 14:23:14 -0700172
Jon Mirandac476d6e2017-05-04 16:30:01 -0700173 private static final int REQUEST_PERMISSION_CALL_PHONE = 14;
Sunny Goyal28c6b962015-10-12 11:42:05 -0700174
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700175 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
176
Mathew Inwood876a8462013-06-14 14:12:41 +0100177 /**
178 * IntentStarter uses request codes starting with this. This must be greater than all activity
179 * request codes used internally.
180 */
181 protected static final int REQUEST_LAST = 100;
182
Mario Bertschleracbf5702017-03-03 10:58:06 -0800183 private static final int SOFT_INPUT_MODE_DEFAULT =
Jon Mirandafd516322017-03-09 13:14:50 -0800184 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800185 private static final int SOFT_INPUT_MODE_ALL_APPS =
Jon Mirandafd516322017-03-09 13:14:50 -0800186 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Mario Bertschleracbf5702017-03-03 10:58:06 -0800187
Winson Chung2672ff92012-05-04 16:22:30 -0700188 // The Intent extra that defines whether to ignore the launch animation
189 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400190 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 // Type: int
193 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700194 // Type: int
195 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700196 // Type: PendingRequestArgs
197 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
198 // Type: ActivityResultInfo
199 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700201 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Sunny Goyal27835952017-01-13 12:15:53 -0800212 public static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Sunny Goyal60820d72017-05-09 12:40:11 -0700222 private final ExtractedColors mExtractedColors = new ExtractedColors();
223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700226 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800227 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700228
229 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Sunny Goyalffe83f12014-08-14 17:39:34 -0700231 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700232 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234 private int[] mTmpAddItemCellCoordinates = new int[2];
235
Sunny Goyal316490e2015-06-02 09:38:28 -0700236 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800237 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700238
Michael Jurka838a4ca2011-02-07 13:33:06 -0800239 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700240 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700241
Sunny Goyal47328fd2016-05-25 18:56:41 -0700242 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700243
244 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700245 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700246 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700247
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700248 // Main container view and the model for the widget tray screen.
249 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700250 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700251
Winson Chung4a2afa32012-07-19 14:53:05 -0700252 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
253 // scroll issues (because the workspace may not have been measured yet) and extra work.
254 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
255 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256
257 private SpannableStringBuilder mDefaultKeySsb = null;
258
Adam Cohen091440a2015-03-18 14:16:05 -0700259 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800261 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700262 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
Michael Jurka1e2f4652013-07-08 18:03:46 -0700264 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700265 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700266 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700267
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800269 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Sunny Goyalae502842016-06-17 08:43:56 -0700271 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700272 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700273 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700274 private boolean mHasFocus = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Jon Miranda2d89ea82017-05-04 11:47:53 -0700276 private ObjectAnimator mScrimAnimator;
277
Tony Wickham010d2552017-01-20 08:15:28 -0800278 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700279
Adam Cohen61f560d2013-09-30 15:58:20 -0700280 private View.OnTouchListener mHapticFeedbackTouchListener;
281
Winson Chung400438b2011-01-16 17:53:48 -0800282 // Determines how long to wait after a rotation before restoring the screen orientation to
283 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700284 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800285
Adam Cohen1462de32012-07-24 22:34:36 -0700286 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
287
Winson Chung46353de2012-02-16 14:05:10 -0800288 // We only want to get the SharedPreferences once since it does an FS stat each time we get
289 // it from the context.
290 private SharedPreferences mSharedPrefs;
291
Adam Cohen4b66bf32015-09-18 12:15:19 -0700292 private boolean mMoveToDefaultScreenFromNewIntent;
293
Winson Chung13eb5272015-05-11 16:30:13 -0700294 // This is set to the view that launched the activity that navigated the user away from
295 // launcher. Since there is no callback for when the activity has finished launching, enable
296 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800297 private BubbleTextView mWaitingForResume;
298
Adam Cohen59400422014-03-05 18:07:04 -0800299 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
300 new HashMap<String, CustomAppWidget>();
301
Adam Cohen59400422014-03-05 18:07:04 -0800302 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700303 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
304 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800305 }
306 }
307
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700308 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
309 // state transition. If another state transition happened during this delay,
310 // simply unregister this runnable.
311 private Runnable mExitSpringLoadedModeRunnable;
312
Adam Cohen091440a2015-03-18 14:16:05 -0700313 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700314 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700315 if (mWorkspace != null) {
316 mWorkspace.buildPageHardwareLayers();
317 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700318 }
319 };
320
Sunny Goyal2100c782016-08-22 16:00:03 -0700321 // Activity result which needs to be processed after workspace has loaded.
322 private ActivityResultInfo mPendingActivityResult;
323 /**
324 * Holds extra information required to handle a result from an external call, like
325 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
326 */
327 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800328
Jon Miranda379198e2016-09-23 08:54:40 -0700329 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700330
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700331 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700332 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400333
334 @Thunk void setOrientation() {
335 if (mRotationEnabled) {
336 unlockScreenOrientation(true);
337 } else {
338 setRequestedOrientation(
339 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700340 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400341 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700342
Sunny Goyal745bad92016-05-02 10:54:12 -0700343 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 @Override
346 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700347 if (DEBUG_STRICT_MODE) {
348 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
349 .detectDiskReads()
350 .detectDiskWrites()
351 .detectNetwork() // or .detectAll() for all detectable problems
352 .penaltyLog()
353 .build());
354 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
355 .detectLeakedSqlLiteObjects()
356 .detectLeakedClosableObjects()
357 .penaltyLog()
358 .penaltyDeath()
359 .build());
360 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700361 if (LauncherAppState.PROFILE_STARTUP) {
362 Trace.beginSection("Launcher-onCreate");
363 }
Winson Chunga2413752012-04-03 14:22:34 -0700364
Adam Cohen9211d422014-10-07 18:14:53 -0700365 if (mLauncherCallbacks != null) {
366 mLauncherCallbacks.preOnCreate();
367 }
368
Mario Bertschler27288382017-05-24 15:35:09 -0700369 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
370 wallpaperColorInfo.setOnThemeChangeListener(this);
Mario Bertschlera6936942017-05-31 14:48:19 -0700371 overrideTheme(wallpaperColorInfo.isDark(), wallpaperColorInfo.supportsDarkText());
Mario Bertschler27288382017-05-24 15:35:09 -0700372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400374
Sunny Goyal87f784c2017-01-11 10:48:34 -0800375 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700376
Adam Cohen2e6da152015-05-06 11:42:25 -0700377 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800378 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Jon Mirandafe964322017-03-22 10:25:17 -0700379 if (isInMultiWindowModeCompat()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700380 Display display = getWindowManager().getDefaultDisplay();
381 Point mwSize = new Point();
382 display.getSize(mwSize);
383 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
384 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700385
Sunny Goyalf7258242015-10-19 16:59:07 -0700386 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700387 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800388 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800389 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800390 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700391 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700392
Joe Onorato41a12d22009-10-31 18:30:00 -0400393 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700394 mAllAppsController = new AllAppsTransitionController(this);
395 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700396
Sunny Goyalffe83f12014-08-14 17:39:34 -0700397 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700398
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700399 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
400 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700401
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700402 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
403 // this also ensures that any synchronous binding below doesn't re-trigger another
404 // LauncherModel load.
405 mPaused = false;
406
Sunny Goyal60820d72017-05-09 12:40:11 -0700407 mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 setupViews();
Winson1f064272016-07-18 17:18:02 -0700410 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700411 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Tony Wickham010d2552017-01-20 08:15:28 -0800413 mPopupDataProvider = new PopupDataProvider(this);
414
Tony Wickhame2217252016-03-22 16:34:23 -0700415 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
416 .addAccessibilityStateChangeListener(this);
417
Joe Onorato7c312c12009-08-13 21:36:53 -0700418 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700419
Sunny Goyalfe770c92016-09-27 14:38:58 -0700420 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421
Sunny Goyale26d1002016-06-20 14:52:14 -0700422 if (LauncherAppState.PROFILE_STARTUP) {
423 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
Sunny Goyal2100c782016-08-22 16:00:03 -0700426 // We only load the page synchronously if the user rotates (or triggers a
427 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700428 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
429 if (savedInstanceState != null) {
430 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
431 }
432 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700433 // If we are not binding synchronously, show a fade in animation when
434 // the first page bind completes.
435 mDragLayer.setAlpha(0);
436 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700437 // Pages bound synchronously.
438 mWorkspace.setCurrentPage(currentScreen);
439
Sunny Goyal2100c782016-08-22 16:00:03 -0700440 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442
443 // For handling default keys
444 mDefaultKeySsb = new SpannableStringBuilder();
445 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800446
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800447 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700448 // In case we are on a device with locked rotation, we should look at preferences to check
449 // if the user has specifically allowed rotation.
450 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700451 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700452 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
453 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700454 }
455
Sunny Goyal5a81c382017-03-20 15:08:06 -0700456 if (PinItemDragListener.handleDragRequest(this, getIntent())) {
457 // Temporarily enable the rotation
458 mRotationEnabled = true;
459 }
460
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700461 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
462 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400463 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700464
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800465 setContentView(mLauncherView);
Adam Cohen9211d422014-10-07 18:14:53 -0700466 if (mLauncherCallbacks != null) {
467 mLauncherCallbacks.onCreate(savedInstanceState);
468 }
Mario Bertschlera6936942017-05-31 14:48:19 -0700469
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -0700470 // Listen for broadcasts screen off
471 registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Sunny Goyala616d2b2017-06-12 13:54:01 -0700472
Mario Bertschlera6936942017-05-31 14:48:19 -0700473 if (Themes.getAttrBoolean(this, R.attr.isWorkspaceDarkText)) {
474 activateLightSystemBars(true, true, true);
475 }
Adam Cohen9211d422014-10-07 18:14:53 -0700476 }
477
Sunny Goyal7779d622015-06-11 16:18:39 -0700478 @Override
Mario Bertschler27288382017-05-24 15:35:09 -0700479 public void onThemeChanged() {
480 recreate();
481 }
482
Mario Bertschlera6936942017-05-31 14:48:19 -0700483 protected void overrideTheme(boolean isDark, boolean supportsDarkText) {
Mario Bertschler27288382017-05-24 15:35:09 -0700484 if (isDark) {
485 setTheme(R.style.LauncherThemeDark);
Mario Bertschlera6936942017-05-31 14:48:19 -0700486 } else if (supportsDarkText) {
487 setTheme(R.style.LauncherThemeDarkText);
Mario Bertschler27288382017-05-24 15:35:09 -0700488 }
489 }
490
491 @Override
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800492 public View findViewById(int id) {
493 return mLauncherView.findViewById(id);
494 }
495
496 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700497 public void onExtractedColorsChanged() {
498 loadExtractedColorsAndColorItems();
Sunny Goyal60820d72017-05-09 12:40:11 -0700499 mExtractedColors.notifyChange();
Sunny Goyal5e7b2382017-04-25 23:09:06 -0700500 }
501
502 public ExtractedColors getExtractedColors() {
503 return mExtractedColors;
Tony Wickham827cef22016-03-17 15:39:39 -0700504 }
505
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700506 @Override
507 public void onAppWidgetHostReset() {
508 if (mAppWidgetHost != null) {
509 mAppWidgetHost.startListening();
510 }
511 }
512
Tony Wickham827cef22016-03-17 15:39:39 -0700513 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700514 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700515 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700516 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700517 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700518 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700519 }
520 }
521
Tony Wickham345bff32016-09-28 15:34:51 -0700522 /**
Tony Wickhamed687282017-03-20 16:49:37 -0700523 * Sets the status and/or nav bar to be light or not. Light status bar means dark icons.
524 * @param isLight make sure the system bar is light.
525 * @param statusBar if true, make the status bar theme match the isLight param.
526 * @param navBar if true, make the nav bar theme match the isLight param.
Tony Wickham345bff32016-09-28 15:34:51 -0700527 */
Tony Wickhamed687282017-03-20 16:49:37 -0700528 public void activateLightSystemBars(boolean isLight, boolean statusBar, boolean navBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700529 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
530 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickhamed687282017-03-20 16:49:37 -0700531 if (isLight) {
532 if (statusBar) {
533 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
534 }
535 if (navBar && Utilities.isAtLeastO()) {
Hyunyoung Song0ef74ab2017-05-25 10:46:47 -0700536 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
Hyunyoung Song3285da32016-12-19 14:46:21 -0800537 }
Tony Wickham93113872016-09-14 16:46:32 -0700538 } else {
Tony Wickhamed687282017-03-20 16:49:37 -0700539 if (statusBar) {
540 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
541 }
542 if (navBar && Utilities.isAtLeastO()) {
Hyunyoung Song0ef74ab2017-05-25 10:46:47 -0700543 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800544 }
545 }
Hyunyoung Song3285da32016-12-19 14:46:21 -0800546
Tony Wickham345bff32016-09-28 15:34:51 -0700547 if (newSystemUiFlags != oldSystemUiFlags) {
548 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
549 }
Tony Wickham93113872016-09-14 16:46:32 -0700550 }
551
Adam Cohen9211d422014-10-07 18:14:53 -0700552 private LauncherCallbacks mLauncherCallbacks;
553
554 public void onPostCreate(Bundle savedInstanceState) {
555 super.onPostCreate(savedInstanceState);
556 if (mLauncherCallbacks != null) {
557 mLauncherCallbacks.onPostCreate(savedInstanceState);
558 }
559 }
560
Winson1f064272016-07-18 17:18:02 -0700561 public void onInsetsChanged(Rect insets) {
562 mDeviceProfile.updateInsets(insets);
563 mDeviceProfile.layout(this, true /* notifyListeners */);
564 }
565
Sunny Goyal32554d12015-12-03 15:31:25 -0800566 /**
567 * Call this after onCreate to set or clear overlay.
568 */
569 public void setLauncherOverlay(LauncherOverlay overlay) {
570 if (overlay != null) {
571 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
572 }
573 mWorkspace.setLauncherOverlay(overlay);
574 }
575
Adam Cohen9211d422014-10-07 18:14:53 -0700576 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
577 mLauncherCallbacks = callbacks;
578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700582 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700608 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Hyunyoung Song3f471442015-04-08 19:01:34 -0700626 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700627 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
628 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700629 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700630 }
631
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000632 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700633 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
634 // This cast is safe as long as the id < 0x00FFFFFF
635 // Since we jail all the dynamically generated views, there should be no clashes
636 // with any other views.
637 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000638 }
639
Tony Wickham010d2552017-01-20 08:15:28 -0800640 public PopupDataProvider getPopupDataProvider() {
641 return mPopupDataProvider;
642 }
643
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000644 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
646 * a configuration step, this allows the proper animations to run after other transitions.
647 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 private long completeAdd(
649 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
650 long screenId = info.screenId;
651 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700652 // When the screen id represents an actual screen (as opposed to a rank) we make sure
653 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700655 }
Adam Cohendb364c32014-05-20 14:23:40 -0700656
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700659 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700660 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800661 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700663 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700664 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700666 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700667 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 int widgetId = appWidgetId;
669 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700670 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700672 // Since the view was just bound, also launch the configure activity if needed
673 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
674 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800675 if (provider != null) {
676 new WidgetAddFlowHandler(provider)
677 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700678 }
679 }
680 break;
681 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800682 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700683
Adam Cohendb364c32014-05-20 14:23:40 -0700684 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 }
686
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800687 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700688 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 if (isWorkspaceLoading()) {
690 // process the result once the workspace has loaded.
691 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
692 return;
693 }
694 mPendingActivityResult = null;
695
Winson Chunge341d302013-08-16 14:31:00 -0700696 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 final PendingRequestArgs requestArgs = mPendingRequestArgs;
698 setWaitingForResult(null);
699 if (requestArgs == null) {
700 return;
701 }
702
703 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700704
Adam Cohenad4e15c2013-10-17 16:21:35 -0700705 Runnable exitSpringLoaded = new Runnable() {
706 @Override
707 public void run() {
708 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
709 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
710 }
711 };
712
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700714 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700715 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
717 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700718 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700719 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700720 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700722 addAppWidgetImpl(
723 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800724 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700725 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700726 }
727 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200728 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
729 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700730 // User could have free-scrolled between pages before picking a wallpaper; make sure
731 // we move to the closest one now.
732 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700733 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 }
735 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700739 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700740
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 // We have special handling for widgets
742 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800743 final int appWidgetId;
744 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
745 : -1;
746 if (widgetId < 0) {
747 appWidgetId = pendingAddWidgetId;
748 } else {
749 appWidgetId = widgetId;
750 }
751
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800753 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700754 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
755 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700757 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700758 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 @Override
760 public void run() {
761 exitSpringLoadedDragModeDelayed(false, 0, null);
762 }
763 };
Adam Cohendb364c32014-05-20 14:23:40 -0700764
Sunny Goyal2100c782016-08-22 16:00:03 -0700765 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
766 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
767 } else {
768 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
769 // When the screen id represents an actual screen (as opposed to a rank)
770 // we make sure that the drop page actually exists.
771 requestArgs.screenId =
772 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700773 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700774 final CellLayout dropLayout =
775 mWorkspace.getScreenWithId(requestArgs.screenId);
776
777 dropLayout.setDropPending(true);
778 final Runnable onComplete = new Runnable() {
779 @Override
780 public void run() {
781 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
782 dropLayout.setDropPending(false);
783 }
784 };
785 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
786 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700787 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800788 return;
789 }
790
Sunny Goyald478c832016-04-01 12:04:16 -0700791 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
792 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700793 if (resultCode == RESULT_OK) {
794 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700795 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700796 }
797 // Leave the widget in the pending state if the user canceled the configure.
798 return;
799 }
800
Sunny Goyalaad90582015-07-09 14:22:50 -0700801 if (requestCode == REQUEST_CREATE_SHORTCUT) {
802 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700803 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
804 completeAdd(requestCode, data, -1, requestArgs);
805 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
806 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
807
Sunny Goyalaad90582015-07-09 14:22:50 -0700808 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700809 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800813 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800814 }
815
816 @Override
Sunny Goyaldec3a902017-01-25 18:23:36 -0800817 protected void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800818 final int requestCode, final int resultCode, final Intent data) {
819 handleActivityResult(requestCode, resultCode, data);
820 if (mLauncherCallbacks != null) {
821 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
822 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800823 }
824
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600825 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700826 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600827 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700828 PendingRequestArgs pendingArgs = mPendingRequestArgs;
829 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
830 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
831 setWaitingForResult(null);
832
Sunny Goyal28c6b962015-10-12 11:42:05 -0700833 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700834 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700835 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700836 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700837 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700838 Intent intent = pendingArgs.getPendingIntent();
839
Sunny Goyal28c6b962015-10-12 11:42:05 -0700840 if (grantResults.length > 0
841 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700842 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700843 } else {
844 // TODO: Show a snack bar with link to settings
845 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700846 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700847 }
848 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600849 if (mLauncherCallbacks != null) {
850 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
851 grantResults);
852 }
853 }
854
Adam Cohendb364c32014-05-20 14:23:40 -0700855 /**
856 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
857 *
858 * @param screenId the screen id to check
859 * @return the new screen, or screenId if it exists
860 */
861 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800862 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700863 if (dropLayout == null) {
864 // it's possible that the add screen was removed because it was
865 // empty and a re-bind occurred
866 mWorkspace.addExtraEmptyScreen();
867 return mWorkspace.commitExtraEmptyScreen();
868 } else {
869 return screenId;
870 }
871 }
872
Sunny Goyal2100c782016-08-22 16:00:03 -0700873 @Thunk void completeTwoStageWidgetDrop(
874 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
875 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 Runnable onCompleteRunnable = null;
877 int animationType = 0;
878
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700879 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 if (resultCode == RESULT_OK) {
881 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
882 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800883 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700884 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 onCompleteRunnable = new Runnable() {
886 @Override
887 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700888 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700889 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
890 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 }
892 };
893 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800894 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700898 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
900 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 // The animated view may be null in the case of a rotation during widget configuration
903 onCompleteRunnable.run();
904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700908 protected void onStop() {
909 super.onStop();
910 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700911
912 if (mLauncherCallbacks != null) {
913 mLauncherCallbacks.onStop();
914 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700915
Sunny Goyalf5e37442016-11-02 10:31:24 -0700916 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700917 mAppWidgetHost.stopListening();
918 }
Tony Wickham010d2552017-01-20 08:15:28 -0800919
920 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700921 }
922
923 @Override
924 protected void onStart() {
925 super.onStart();
926 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700927
928 if (mLauncherCallbacks != null) {
929 mLauncherCallbacks.onStart();
930 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700931
Sunny Goyalf5e37442016-11-02 10:31:24 -0700932 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700933 mAppWidgetHost.startListening();
934 }
Tony Wickham010d2552017-01-20 08:15:28 -0800935
936 if (!isWorkspaceLoading()) {
937 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
938 }
Jon Miranda2d89ea82017-05-04 11:47:53 -0700939
940 if (mIsResumeFromActionScreenOff && mDragLayer.getBackground() != null) {
941 if (mScrimAnimator != null) {
942 mScrimAnimator.cancel();
943 }
944 mDragLayer.getBackground().setAlpha(0);
945 mScrimAnimator = ObjectAnimator.ofInt(mDragLayer.getBackground(),
946 LauncherAnimUtils.DRAWABLE_ALPHA, 0, 255);
947 mScrimAnimator.addListener(new AnimatorListenerAdapter() {
948 @Override
949 public void onAnimationEnd(Animator animation) {
950 mScrimAnimator = null;
951 }
952 });
953 mScrimAnimator.setDuration(600);
954 mScrimAnimator.setStartDelay(getWindow().getTransitionBackgroundFadeDuration());
955 mScrimAnimator.start();
956 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700957 }
958
959 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200961 long startTime = 0;
962 if (DEBUG_RESUME_TIME) {
963 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400964 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200965 }
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.preOnResume();
969 }
970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700972 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700973
Winson Chung4a2afa32012-07-19 14:53:05 -0700974 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700975 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700976 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800977 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700978 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700979 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700980 // view after launching an app, as they may be depending on the UI to be static to
981 // switch to another app, otherwise, if it was
Sunny Goyal5606e072017-06-15 14:24:21 -0700982 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800983 } else if (mOnResumeState == State.WIDGETS) {
984 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700985 }
986 mOnResumeState = State.NONE;
987
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800988 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700989 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700990 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700991 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700992 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700994 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200995 // We might have postponed some bind calls until onResume (see waitUntilResume) --
996 // execute them here
997 long startTimeCallbacks = 0;
998 if (DEBUG_RESUME_TIME) {
999 startTimeCallbacks = System.currentTimeMillis();
1000 }
1001
Michael Jurka1e2f4652013-07-08 18:03:46 -07001002 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1003 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001006 if (DEBUG_RESUME_TIME) {
1007 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1008 (System.currentTimeMillis() - startTimeCallbacks));
1009 }
Michael Jurka447bf842013-05-15 14:52:15 +02001010 }
Michael Jurka54554252013-08-01 12:52:23 +02001011 if (mOnResumeCallbacks.size() > 0) {
1012 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1013 mOnResumeCallbacks.get(i).run();
1014 }
1015 mOnResumeCallbacks.clear();
1016 }
Winson Chunge4e50662012-01-23 14:45:13 -08001017
1018 // Reset the pressed state of icons that were locked in the press state while activities
1019 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001020 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001021 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001022 mWaitingForResume.setStayPressed(false);
1023 }
Winson Chung82963d52013-10-09 11:20:57 -07001024
Adam Cohen06dff352012-06-01 17:17:08 -07001025 // It is possible that widgets can receive updates while launcher is not in the foreground.
1026 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1027 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1028 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001029 if (!isWorkspaceLoading()) {
1030 getWorkspace().reinflateWidgetsIfNecessary();
1031 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001032
Michael Jurka447bf842013-05-15 14:52:15 +02001033 if (DEBUG_RESUME_TIME) {
1034 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1035 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001036
Adam Cohen4b66bf32015-09-18 12:15:19 -07001037 // We want to suppress callbacks about CustomContent being shown if we have just received
1038 // onNewIntent while the user was present within launcher. In that case, we post a call
1039 // to move the user to the main screen (which will occur after onResume). We don't want to
1040 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1041 // suppress here.
1042 if (mWorkspace.getCustomContentCallbacks() != null
1043 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001044 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001045 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001046 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1047 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001048 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001049 }
1050 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001051 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001052 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001053 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001054
Sunny Goyala474a9b2017-05-04 16:47:11 -07001055 // Process any items that were added while Launcher was away.
1056 InstallShortcutReceiver.disableAndFlushInstallQueue(
1057 InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED, this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001058
Sunny Goyala474a9b2017-05-04 16:47:11 -07001059 // Refresh shortcuts if the permission changed.
1060 mModel.refreshShortcutsIfRequired();
Adam Cohen9211d422014-10-07 18:14:53 -07001061
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001062 if (shouldShowDiscoveryBounce()) {
1063 mAllAppsController.showDiscoveryBounce();
1064 }
1065 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001066 if (mLauncherCallbacks != null) {
1067 mLauncherCallbacks.onResume();
1068 }
Mario Bertschler0fc6f682017-02-17 12:16:13 -08001069
Adam Cohen06dff352012-06-01 17:17:08 -07001070 }
1071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001074 // Ensure that items added to Launcher are queued until Launcher returns
Sunny Goyala474a9b2017-05-04 16:47:11 -07001075 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED);
Winson Chung997a9232013-07-24 15:33:46 -07001076
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001078 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001079 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001080 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001081
1082 // We call onHide() aggressively. The custom content callbacks should be able to
1083 // debounce excess onHide calls.
1084 if (mWorkspace.getCustomContentCallbacks() != null) {
1085 mWorkspace.getCustomContentCallbacks().onHide();
1086 }
Romain Guycbb89e42009-06-08 15:52:54 -07001087
Adam Cohen9211d422014-10-07 18:14:53 -07001088 if (mLauncherCallbacks != null) {
1089 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001090 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
1092
1093 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001094 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1095 // by a onResume or by scrolling otherwise.
1096 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001097
1098 // Custom content is completely hidden
1099 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001100
1101 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1102 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001103
1104 // Indicates whether the user is allowed to scroll away from the custom content.
1105 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001106 }
1107
Adam Cohenc2d6e892014-10-16 09:49:24 -07001108 public interface LauncherOverlay {
1109
1110 /**
1111 * Touch interaction leading to overscroll has begun
1112 */
1113 public void onScrollInteractionBegin();
1114
1115 /**
1116 * Touch interaction related to overscroll has ended
1117 */
1118 public void onScrollInteractionEnd();
1119
1120 /**
1121 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1122 * screen (or in the case of RTL, the rightmost screen).
1123 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001124 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001125
1126 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001127 * Called when the launcher is ready to use the overlay
1128 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001130 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 }
1132
1133 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001134 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 }
1136
1137 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1138
Sunny Goyalc86df472016-02-25 09:19:38 -08001139 public void onScrollChanged(float progress) {
1140 if (mWorkspace != null) {
1141 mWorkspace.onOverlayScrollChanged(progress);
1142 }
1143 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001144 }
1145
Jorim Jaggid017f882014-01-14 17:08:48 -08001146 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001147 if (mLauncherCallbacks != null) {
1148 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001149 } else {
Sunny Goyal4179e9b2017-03-08 14:25:09 -08001150 // On O and above we there is always some setting present settings (add icon to
1151 // home screen or icon badging). On earlier APIs we will have the allow rotation
1152 // setting, on devices with a locked orientation,
1153 return Utilities.isAtLeastO() || !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001154 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001155 }
1156
Adam Cohen9211d422014-10-07 18:14:53 -07001157 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001158 CustomContentCallbacks callbacks, String description) {
1159 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001160 }
1161
Adam Cohenedb40762013-07-18 16:45:45 -07001162 // The custom content needs to offset its content to account for the QSB
1163 public int getTopOffsetForCustomContent() {
1164 return mWorkspace.getPaddingTop();
1165 }
1166
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001167 @Override
1168 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001169 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001170 if (mModel.isCurrentCallbacks(this)) {
1171 mModel.stopLoader();
1172 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001173 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1174
Romain Guy13c2e7b2010-03-10 19:45:00 -08001175 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001176 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001177
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001178 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001179 @Override
1180 public void onWindowFocusChanged(boolean hasFocus) {
1181 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001182 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001183
1184 if (mLauncherCallbacks != null) {
1185 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1186 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001187 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 private boolean acceptFilter() {
1190 final InputMethodManager inputManager = (InputMethodManager)
1191 getSystemService(Context.INPUT_METHOD_SERVICE);
1192 return !inputManager.isFullscreenMode();
1193 }
1194
1195 @Override
1196 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001197 final int uniChar = event.getUnicodeChar();
1198 final boolean handled = super.onKeyDown(keyCode, event);
1199 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1200 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1202 keyCode, event);
1203 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001204 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001205 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001206 // showSearchDialog()
1207 // If there are multiple keystrokes before the search dialog takes focus,
1208 // onSearchRequested() will be called for every keystroke,
1209 // but it is idempotent, so it's fine.
1210 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
1212 }
1213
Joe Onorato8a9625e2010-01-28 15:55:35 -08001214 // Eat the long press event so the keyboard doesn't come up.
1215 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1216 return true;
1217 }
1218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 return handled;
1220 }
1221
Winson46163472015-09-22 17:31:56 -07001222 @Override
1223 public boolean onKeyUp(int keyCode, KeyEvent event) {
1224 if (keyCode == KeyEvent.KEYCODE_MENU) {
1225 // Ignore the menu key if we are currently dragging or are on the custom content screen
1226 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001227 // Close any open floating view
1228 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001229
Winson46163472015-09-22 17:31:56 -07001230 // Stop resizing any widgets
1231 mWorkspace.exitWidgetResizeMode();
1232
1233 // Show the overview mode if we are on the workspace
1234 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1235 !mWorkspace.isSwitchingState()) {
1236 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001237 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001238 }
1239 }
1240 return true;
1241 }
1242 return super.onKeyUp(keyCode, event);
1243 }
1244
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 private String getTypedText() {
1246 return mDefaultKeySsb.toString();
1247 }
1248
Sunny Goyal6f28e712016-09-13 14:19:29 -07001249 @Override
1250 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 mDefaultKeySsb.clear();
1252 mDefaultKeySsb.clearSpans();
1253 Selection.setSelection(mDefaultKeySsb, 0);
1254 }
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 /**
1257 * Restores the previous state, if it exists.
1258 *
1259 * @param savedState The previous state.
1260 */
1261 private void restoreState(Bundle savedState) {
1262 if (savedState == null) {
1263 return;
1264 }
1265
Sunny Goyalfe770c92016-09-27 14:38:58 -07001266 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1267 State[] stateValues = State.values();
1268 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1269 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001270 if (state == State.APPS || state == State.WIDGETS) {
1271 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001272 }
1273
Sunny Goyal2100c782016-08-22 16:00:03 -07001274 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1275 if (requestArgs != null) {
1276 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001278
1279 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281
1282 /**
1283 * Finds all the views we need and configure them properly.
1284 */
1285 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001286 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001287 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Sunny Goyal966d9012017-06-06 16:43:59 -07001288 mWorkspace = mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001289 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001290
Sunny Goyal784f9c32016-03-23 16:56:54 -07001291 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1292 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1293 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294
Winson Chung4c98d922011-05-31 16:50:48 -07001295 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001296 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001297
Winson Chung3d503fb2011-07-13 17:25:49 -07001298 // Setup the hotseat
1299 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1300 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001301 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001302 }
1303
Winsone9f27272015-10-13 10:47:51 -07001304 // Setup the overview panel
1305 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001306
Winson Chung4c98d922011-05-31 16:50:48 -07001307 // Setup the workspace
1308 mWorkspace.setHapticFeedbackEnabled(false);
1309 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001310 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001311 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1312 // default state, otherwise we will update to the wrong offsets in RTL
1313 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001314 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001315 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001316
Tony Wickham34d2c912015-09-11 09:27:58 -07001317 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001318 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001319
Winson Chungef7f8742015-06-04 17:18:17 -07001320 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001321 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001322 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001323
Winson Chung3d503fb2011-07-13 17:25:49 -07001324 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001325 mDragController.setMoveTarget(mWorkspace);
1326 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001327 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001328
Peter Schiller310a9882016-06-30 13:52:36 -07001329 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1330 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1331 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001332
Sunny Goyal322d5562015-06-25 19:35:49 -07001333 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1334 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
Winsone9f27272015-10-13 10:47:51 -07001338 private void setupOverviewPanel() {
1339 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1340
Winsone9f27272015-10-13 10:47:51 -07001341 // Bind wallpaper button actions
1342 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301343 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001344 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001345 public void handleViewClick(View view) {
1346 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001347 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001348 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001349 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1350
1351 // Bind widget button actions
1352 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301353 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001354 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001355 public void handleViewClick(View view) {
1356 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001357 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001358 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001359 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1360
1361 // Bind settings actions
1362 View settingsButton = findViewById(R.id.settings_button);
1363 boolean hasSettings = hasSettings();
1364 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301365 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001366 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001367 public void handleViewClick(View view) {
1368 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001369 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001370 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001371 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1372 } else {
1373 settingsButton.setVisibility(View.GONE);
1374 }
1375
1376 mOverviewPanel.setAlpha(0f);
1377 }
1378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001380 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001381 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001382 */
1383 public void setAllAppsButton(View allAppsButton) {
1384 mAllAppsButton = allAppsButton;
1385 }
1386
Sunny Goyalbb011da2016-06-15 15:42:29 -07001387 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001388 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001389 }
1390
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001391 public View getWidgetsButton() {
1392 return mWidgetsButton;
1393 }
1394
Anjali Koppal5ad44842014-03-10 20:34:39 -07001395 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 * Creates a view representing a shortcut.
1397 *
1398 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001400 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001401 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
1404 /**
1405 * Creates a view representing a shortcut inflated from the specified resource.
1406 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 * @param parent The group the shortcut belongs to.
1408 * @param info The data structure describing the shortcut.
1409 *
1410 * @return A View inflated from layoutResId.
1411 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001412 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001413 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1414 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001415 favorite.applyFromShortcutInfo(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001417 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 return favorite;
1419 }
1420
1421 /**
Jon Mirandac476d6e2017-05-04 16:30:01 -07001422 * Add a shortcut to the workspace or to a Folder.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 *
1424 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001426 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001427 int cellY, PendingRequestArgs args) {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001428 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
1429 || args.getPendingIntent().getComponent() == null) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001430 return;
1431 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001432
Jon Mirandac476d6e2017-05-04 16:30:01 -07001433 int[] cellXY = mTmpAddItemCellCoordinates;
1434 CellLayout layout = getCellLayout(container, screenId);
1435
Sunny Goyal782f0c92017-01-19 10:27:54 -08001436 ShortcutInfo info = null;
1437 if (Utilities.isAtLeastO()) {
Sunny Goyal22ca9ec2017-05-18 15:03:13 -07001438 info = LauncherAppsCompatVO.createShortcutInfoFromPinItemRequest(
1439 this, LauncherAppsCompatVO.getPinItemRequest(data), 0);
Sunny Goyal782f0c92017-01-19 10:27:54 -08001440 }
1441
1442 if (info == null) {
Sunny Goyalb57645f2017-03-20 13:57:28 -07001443 // Legacy shortcuts are only supported for primary profile.
1444 info = Process.myUserHandle().equals(args.user)
1445 ? InstallShortcutReceiver.fromShortcutIntent(this, data) : null;
Sunny Goyal782f0c92017-01-19 10:27:54 -08001446
Sunny Goyalb57645f2017-03-20 13:57:28 -07001447 if (info == null) {
1448 Log.e(TAG, "Unable to parse a valid custom shortcut result");
1449 return;
1450 } else if (!new PackageManagerHelper(this).hasPermissionForActivity(
Sunny Goyal342e4662017-02-02 15:21:08 -08001451 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001452 // The app is trying to add a shortcut without sufficient permissions
1453 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1454 return;
1455 }
1456 }
1457
Jon Mirandac476d6e2017-05-04 16:30:01 -07001458 if (container < 0) {
1459 // Adding a shortcut to the Workspace.
1460 final View view = createShortcut(info);
1461 boolean foundCellSpan = false;
1462 // First we check if we already know the exact location where we want to add this item.
1463 if (cellX >= 0 && cellY >= 0) {
1464 cellXY[0] = cellX;
1465 cellXY[1] = cellY;
1466 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001467
Jon Mirandac476d6e2017-05-04 16:30:01 -07001468 // If appropriate, either create a folder or add to an existing folder
1469 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1470 true, null, null)) {
1471 return;
1472 }
1473 DragObject dragObject = new DragObject();
1474 dragObject.dragInfo = info;
1475 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1476 true)) {
1477 return;
1478 }
1479 } else {
1480 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1481 }
1482
1483 if (!foundCellSpan) {
1484 mWorkspace.onNoCellFound(layout);
Adam Cohen558baaf2011-08-15 15:22:57 -07001485 return;
1486 }
Jon Mirandac476d6e2017-05-04 16:30:01 -07001487
1488 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
1489 mWorkspace.addInScreen(view, info);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001490 } else {
Jon Mirandac476d6e2017-05-04 16:30:01 -07001491 // Adding a shortcut to a Folder.
1492 final long folderIconId = container;
1493 FolderIcon folderIcon = (FolderIcon) mWorkspace.getFirstMatch(new ItemOperator() {
1494 @Override
1495 public boolean evaluate(ItemInfo info, View view) {
1496 return info != null && info.id == folderIconId;
1497 }
1498 });
Michael Jurkad3ef3062010-11-23 16:23:58 -08001499
Jon Mirandac476d6e2017-05-04 16:30:01 -07001500 if (folderIcon != null) {
1501 FolderInfo folderInfo = (FolderInfo) folderIcon.getTag();
1502 folderInfo.add(info, args.rank, false);
1503 } else {
1504 Log.e(TAG, "Could not find folder with id " + folderIconId + " to add shortcut.");
1505 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001510 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001512 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001514 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001515 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1516
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001518 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 }
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Adam Cohen59400422014-03-05 18:07:04 -08001521 if (appWidgetInfo.isCustomWidget) {
1522 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001523 }
1524
Adam Cohen59400422014-03-05 18:07:04 -08001525 LauncherAppWidgetInfo launcherInfo;
1526 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001527 launcherInfo.spanX = itemInfo.spanX;
1528 launcherInfo.spanY = itemInfo.spanY;
1529 launcherInfo.minSpanX = itemInfo.minSpanX;
1530 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyala52ecb02016-12-16 15:04:51 -08001531 launcherInfo.user = appWidgetInfo.getUser();
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001533 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001534 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
Sunny Goyal2100c782016-08-22 16:00:03 -07001536 if (hostView == null) {
1537 // Perform actual inflation because we're live
1538 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001540 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301541 prepareAppWidget(hostView, launcherInfo);
1542 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Sunny Goyald5462aa2016-11-22 16:52:39 +05301545 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001546 hostView.setTag(item);
1547 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001548 hostView.setFocusable(true);
1549 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001550 }
1551
Adam Cohended9f8d2010-11-03 13:25:16 -07001552 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1553 @Override
1554 public void onReceive(Context context, Intent intent) {
1555 final String action = intent.getAction();
1556 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001557 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001558
Winson Chungc100e8e2011-08-09 16:02:43 -07001559 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001560 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001561 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001562 if (!showWorkspace(false)) {
1563 // If we are already on the workspace, then manually reset all apps
1564 mAppsView.reset();
1565 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001566 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001567 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001568 }
1569 }
1570 };
1571
Tony Wickham010d2552017-01-20 08:15:28 -08001572 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1573 Runnable r = new Runnable() {
1574 @Override
1575 public void run() {
1576 mWorkspace.updateIconBadges(updatedBadges);
1577 mAppsView.updateIconBadges(updatedBadges);
Tony Wickham2fe09f22017-04-25 12:46:04 -07001578
1579 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(Launcher.this);
1580 if (popup != null) {
1581 popup.updateNotificationHeader(updatedBadges);
1582 }
Tony Wickham010d2552017-01-20 08:15:28 -08001583 }
1584 };
1585 if (!waitUntilResume(r)) {
1586 r.run();
1587 }
1588 }
1589
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 @Override
1591 public void onAttachedToWindow() {
1592 super.onAttachedToWindow();
1593
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001594 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyalc86df472016-02-25 09:19:38 -08001595 if (mLauncherCallbacks != null) {
1596 mLauncherCallbacks.onAttachedToWindow();
1597 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 }
1599
1600 @Override
1601 public void onDetachedFromWindow() {
1602 super.onDetachedFromWindow();
Sunny Goyalc86df472016-02-25 09:19:38 -08001603
1604 if (mLauncherCallbacks != null) {
1605 mLauncherCallbacks.onDetachedFromWindow();
1606 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 }
1608
1609 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001610 // The following code used to be in onResume, but it turns out onResume is called when
1611 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1612 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001613 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001614 if (!mWorkspaceLoading) {
1615 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001616 // We want to let Launcher draw itself at least once before we force it to build
1617 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001618 // apps is nice and speedy.
1619 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001620 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001621 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001622 if (mStarted) return;
1623 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001624 // We delay the layer building a bit in order to give
1625 // other message processing a time to run. In particular
1626 // this avoids a delay in hiding the IME if it was
1627 // currently shown, because doing that may involve
1628 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001629 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001630 final ViewTreeObserver.OnDrawListener listener = this;
1631 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001632 public void run() {
1633 if (mWorkspace != null &&
1634 mWorkspace.getViewTreeObserver() != null) {
1635 mWorkspace.getViewTreeObserver().
1636 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001637 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001638 }
1639 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001640 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001641 }
1642 });
1643 }
1644 clearTypedText();
1645 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001646 }
1647
Winson Chunga6945242014-01-08 14:04:34 -08001648 public DragLayer getDragLayer() {
1649 return mDragLayer;
1650 }
1651
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001652 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001653 return mAppsView;
1654 }
1655
Hyunyoung Song3f471442015-04-08 19:01:34 -07001656 public WidgetsContainerView getWidgetsView() {
1657 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001658 }
1659
Winson Chunga6945242014-01-08 14:04:34 -08001660 public Workspace getWorkspace() {
1661 return mWorkspace;
1662 }
1663
1664 public Hotseat getHotseat() {
1665 return mHotseat;
1666 }
1667
Jorim Jaggid017f882014-01-14 17:08:48 -08001668 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001669 return mOverviewPanel;
1670 }
1671
Sunny Goyal47328fd2016-05-25 18:56:41 -07001672 public DropTargetBar getDropTargetBar() {
1673 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001674 }
1675
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001676 public LauncherAppWidgetHost getAppWidgetHost() {
1677 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 }
Romain Guycbb89e42009-06-08 15:52:54 -07001679
Winson Chunga9abd0e2010-10-27 17:18:37 -07001680 public LauncherModel getModel() {
1681 return mModel;
1682 }
1683
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001684 public ModelWriter getModelWriter() {
1685 return mModelWriter;
1686 }
1687
Adam Cohen79d90c52016-04-22 13:29:20 -07001688 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001689 return mSharedPrefs;
1690 }
1691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 @Override
1693 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001694 long startTime = 0;
1695 if (DEBUG_RESUME_TIME) {
1696 startTime = System.currentTimeMillis();
1697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 super.onNewIntent(intent);
1699
Winson15f8b172015-08-19 11:02:39 -07001700 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1701 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1702 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001703
1704 // Check this condition before handling isActionMain, as this will get reset.
1705 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001706 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001707
Winson15f8b172015-08-19 11:02:39 -07001708 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1709 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001710 if (mWorkspace == null) {
1711 // Can be cases where mWorkspace is null, this prevents a NPE
1712 return;
1713 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001714
1715 // Note: There should be at most one log per method call. This is enforced implicitly
1716 // by using if-else statements.
1717 UserEventDispatcher ued = getUserEventDispatcher();
1718
1719 // TODO: Log this case.
Adam Cohen6fecd412013-10-02 17:41:50 -07001720 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001721
Jon Mirandafeba90f2016-10-06 10:53:29 -07001722 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham540913e2017-01-23 11:47:51 -08001723 if (topOpenView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301724 ued.logActionCommand(Action.Command.HOME_INTENT,
1725 topOpenView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001726 } else if (topOpenView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301727 ued.logActionCommand(Action.Command.HOME_INTENT,
1728 ((Folder) topOpenView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001729 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301730 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001731 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1732 }
1733
1734 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001735 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001736 exitSpringLoadedDragMode();
1737
1738 // If we are already on home, then just animate back to the workspace,
1739 // otherwise, just wait until onResume to set the state back to Workspace
1740 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001741 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001742 } else {
1743 mOnResumeState = State.WORKSPACE;
1744 }
1745
1746 final View v = getWindow().peekDecorView();
1747 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001748 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001749 INPUT_METHOD_SERVICE);
1750 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1751 }
1752
Winson Chungb745afb2015-03-02 11:51:23 -08001753 // Reset the apps view
1754 if (!alreadyOnHome && mAppsView != null) {
Sunny Goyal161f96b2017-05-07 11:56:00 -07001755 mAppsView.reset();
Winson Chungb745afb2015-03-02 11:51:23 -08001756 }
1757
Hyunyoung Song3f471442015-04-08 19:01:34 -07001758 // Reset the widgets view
1759 if (!alreadyOnHome && mWidgetsView != null) {
1760 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001761 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001762
Adam Cohen9211d422014-10-07 18:14:53 -07001763 if (mLauncherCallbacks != null) {
1764 mLauncherCallbacks.onHomeIntent();
1765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
Sunny Goyal5a81c382017-03-20 15:08:06 -07001767 PinItemDragListener.handleDragRequest(this, intent);
Adam Cohened307df2013-10-02 09:37:31 -07001768
Adam Cohen9211d422014-10-07 18:14:53 -07001769 if (mLauncherCallbacks != null) {
1770 mLauncherCallbacks.onNewIntent(intent);
1771 }
Winson15f8b172015-08-19 11:02:39 -07001772
1773 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1774 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1775 // animation.
1776 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001777 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001778 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001779 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1780 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001781
1782 // We use this flag to suppress noisy callbacks above custom content state
1783 // from onResume.
1784 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001785 mWorkspace.post(new Runnable() {
1786 @Override
1787 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001788 if (mWorkspace != null) {
1789 mWorkspace.moveToDefaultScreen(true);
1790 }
Winson15f8b172015-08-19 11:02:39 -07001791 }
1792 });
1793 }
1794 }
1795
1796 if (DEBUG_RESUME_TIME) {
1797 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1798 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001799 }
1800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001802 public void onRestoreInstanceState(Bundle state) {
1803 super.onRestoreInstanceState(state);
1804 for (int page: mSynchronouslyBoundPages) {
1805 mWorkspace.restoreInstanceStateForChild(page);
1806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 }
1808
1809 @Override
1810 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001811 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001812 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1813 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001814
Adam Cohen21cd0022013-10-09 18:57:02 -07001815 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001816 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817
Michael Jurka883f55b2010-10-21 15:47:14 -07001818 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001819 // We close any open folders and shortcut containers since they will not be re-opened,
1820 // and we need to make sure this state is reflected.
1821 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001822
Sunny Goyal2100c782016-08-22 16:00:03 -07001823 if (mPendingRequestArgs != null) {
1824 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1825 }
1826 if (mPendingActivityResult != null) {
1827 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
1829
Adam Cohen9211d422014-10-07 18:14:53 -07001830 if (mLauncherCallbacks != null) {
1831 mLauncherCallbacks.onSaveInstanceState(outState);
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
1834
1835 @Override
1836 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Sunny Goyal5cc7bbd2017-06-12 09:50:39 -07001839 unregisterReceiver(mReceiver);
Michael Jurka9d906c72011-10-14 06:25:36 -07001840 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001841 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001842
Winson Chungcd2b0142011-06-08 16:02:26 -07001843 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001844 // It's possible to receive onDestroy after a new Launcher activity has
1845 // been created. In this case, don't interfere with the new Launcher.
1846 if (mModel.isCurrentCallbacks(this)) {
1847 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001848 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001849 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001850
Sunny Goyal745bad92016-05-02 10:54:12 -07001851 if (mRotationPrefChangeHandler != null) {
1852 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1853 }
1854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001856 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001858 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001860 mAppWidgetHost = null;
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 TextKeyListener.getInstance().release();
1863
Tony Wickhame2217252016-03-22 16:34:23 -07001864 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1865 .removeAccessibilityStateChangeListener(this);
1866
Mario Bertschler27288382017-05-24 15:35:09 -07001867 WallpaperColorInfo.getInstance(this).setOnThemeChangeListener(null);
1868
Michael Jurka2ecf9952012-06-18 12:52:28 -07001869 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001870
1871 if (mLauncherCallbacks != null) {
1872 mLauncherCallbacks.onDestroy();
1873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875
Sunny Goyalae502842016-06-17 08:43:56 -07001876 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1877 return mAccessibilityDelegate;
1878 }
1879
Adam Cohena9cf38f2011-05-02 15:36:58 -07001880 public DragController getDragController() {
1881 return mDragController;
1882 }
1883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001885 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001886 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001887 }
1888
1889 @Override
1890 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1891 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001892 try {
1893 super.startIntentSenderForResult(intent, requestCode,
1894 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1895 } catch (IntentSender.SendIntentException e) {
1896 throw new ActivityNotFoundException();
1897 }
1898 }
1899
Winson Chung70d72102011-08-12 11:24:35 -07001900 /**
1901 * Indicates that we want global search for this activity by setting the globalSearch
1902 * argument for {@link #startSearch} to true.
1903 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001905 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001907
Karl Rosaen138a0412009-04-23 19:00:21 -07001908 if (initialQuery == null) {
1909 // Use any text typed in the launcher as the initial query
1910 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 if (appSearchData == null) {
1913 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001914 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001916
Sunny Goyal6f28e712016-09-13 14:19:29 -07001917 if (mLauncherCallbacks == null ||
1918 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1919 // Starting search from the callbacks failed. Start the default global search.
1920 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001921 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001922
1923 // We need to show the workspace after starting the search
1924 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001925 }
1926
Ian Parkinson047036e2014-09-01 15:40:53 +01001927 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001928 * Starts the global search activity. This code is a copied from SearchManager
1929 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001930 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001931 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001932 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001933 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1934 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1935 if (globalSearchActivity == null) {
1936 Log.w(TAG, "No global search activity found.");
1937 return;
1938 }
1939 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1940 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1941 intent.setComponent(globalSearchActivity);
1942 // Make sure that we have a Bundle to put source in
1943 if (appSearchData == null) {
1944 appSearchData = new Bundle();
1945 } else {
1946 appSearchData = new Bundle(appSearchData);
1947 }
Adam Cohen9211d422014-10-07 18:14:53 -07001948 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001949 if (!appSearchData.containsKey("source")) {
1950 appSearchData.putString("source", getPackageName());
1951 }
1952 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1953 if (!TextUtils.isEmpty(initialQuery)) {
1954 intent.putExtra(SearchManager.QUERY, initialQuery);
1955 }
1956 if (selectInitialQuery) {
1957 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1958 }
1959 intent.setSourceBounds(sourceBounds);
1960 try {
1961 startActivity(intent);
1962 } catch (ActivityNotFoundException ex) {
1963 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966
Yura4f93ec62014-02-04 14:15:21 +00001967 public boolean isOnCustomContent() {
1968 return mWorkspace.isOnOrMovingToCustomContent();
1969 }
1970
Winson Chung70d72102011-08-12 11:24:35 -07001971 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001972 public boolean onPrepareOptionsMenu(Menu menu) {
1973 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001974 if (mLauncherCallbacks != null) {
1975 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1976 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001977 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001980 @Override
1981 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001982 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001983 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001984 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001985 }
1986
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001988 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 }
1990
Adam Cohen517a7f52014-03-01 12:12:59 -08001991 public boolean isWorkspaceLoading() {
1992 return mWorkspaceLoading;
1993 }
1994
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001995 private void setWorkspaceLoading(boolean value) {
1996 boolean isLocked = isWorkspaceLocked();
1997 mWorkspaceLoading = value;
1998 if (isLocked != isWorkspaceLocked()) {
1999 onWorkspaceLockedChanged();
2000 }
2001 }
2002
Sunny Goyal04a324a2017-01-20 21:08:59 -08002003 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002004 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002005 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002006 if (isLocked != isWorkspaceLocked()) {
2007 onWorkspaceLockedChanged();
2008 }
2009 }
2010
Adam Cohen9211d422014-10-07 18:14:53 -07002011 protected void onWorkspaceLockedChanged() {
2012 if (mLauncherCallbacks != null) {
2013 mLauncherCallbacks.onWorkspaceLockedChanged();
2014 }
2015 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002016
Sunny Goyal2100c782016-08-22 16:00:03 -07002017 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002018 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002019 if (LOGD) {
2020 Log.d(TAG, "Adding widget from drop");
2021 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002022 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002023 }
2024
Sunny Goyal2100c782016-08-22 16:00:03 -07002025 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002026 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
2027 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
2028 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07002029
Adam Cohenad4e15c2013-10-17 16:21:35 -07002030 Runnable onComplete = new Runnable() {
2031 @Override
2032 public void run() {
2033 // Exit spring loaded mode if necessary after adding the widget
2034 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2035 null);
2036 }
2037 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08002038 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07002039 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
2041 }
Romain Guycbb89e42009-06-08 15:52:54 -07002042
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002043 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002044 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002045 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002046 mWorkspace.moveToCustomContentScreen(animate);
2047 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002048
2049 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2050 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002051 info.container = container;
2052 info.screenId = screenId;
2053 if (cell != null) {
2054 info.cellX = cell[0];
2055 info.cellY = cell[1];
2056 }
2057 info.spanX = spanX;
2058 info.spanY = spanY;
2059
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002060 switch (info.itemType) {
2061 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2062 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002063 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002064 break;
2065 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08002066 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002067 break;
2068 default:
2069 throw new IllegalStateException("Unknown item type: " + info.itemType);
2070 }
2071 }
2072
Adam Cohenfbba09b2011-07-18 21:43:05 -07002073 /**
2074 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002075 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08002076 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002077 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2078 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08002079 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
2080 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
2081 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002082 }
2083
Adam Cohenfbba09b2011-07-18 21:43:05 -07002084 /**
2085 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002086 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002087 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002088 AppWidgetHostView hostView = info.boundWidget;
2089 int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08002090 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08002091 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002092 // In the case where we've prebound the widget, we remove it from the DragLayer
2093 if (LOGD) {
2094 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2095 }
2096 getDragLayer().removeView(hostView);
2097
Adam Cohened66b2b2012-01-23 17:28:51 -08002098 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08002099 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002100
2101 // Clear the boundWidget so that it doesn't get destroyed.
2102 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002103 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002104 // In this case, we either need to start an activity to get permission to bind
2105 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002106 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002107 Bundle options = info.bindOptions;
2108
Sunny Goyalffe83f12014-08-14 17:39:34 -07002109 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2110 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002111 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002112 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07002113 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002114 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07002115 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002116 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002117 }
2118
Adam Cohendcd297f2013-06-18 13:13:40 -07002119 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002120 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002121 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 folderInfo.title = getText(R.string.folder_name);
2123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002125 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126
2127 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002128 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302129 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07002130 // Force measure the new folder icon
2131 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2132 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002133 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 }
Romain Guycbb89e42009-06-08 15:52:54 -07002135
Winsonc0b52fe2015-09-09 16:38:15 -07002136 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002137 * Unbinds the view for the specified item, and removes the item and all its children.
2138 *
2139 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002140 * @param itemInfo the {@link ItemInfo} for this view.
2141 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002142 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002143 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002144 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002145 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002146 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2147 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002148 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002149 } else {
2150 mWorkspace.removeWorkspaceItem(v);
2151 }
Winsonc0b52fe2015-09-09 16:38:15 -07002152 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002153 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002154 }
2155 } else if (itemInfo instanceof FolderInfo) {
2156 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002157 if (v instanceof FolderIcon) {
2158 ((FolderIcon) v).removeListeners();
2159 }
Winsonc0b52fe2015-09-09 16:38:15 -07002160 mWorkspace.removeWorkspaceItem(v);
2161 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002162 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002163 }
2164 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2165 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002166 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002167 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002168 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002169 }
2170 } else {
2171 return false;
2172 }
2173 return true;
2174 }
2175
2176 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002177 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002178 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002179 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002180 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002181 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002182 // Deleting an app widget ID is a void call but writes to disk before returning
2183 // to the caller...
2184 new AsyncTask<Void, Void, Void>() {
2185 public Void doInBackground(Void ... args) {
2186 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2187 return null;
2188 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002189 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002190 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002191 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002192 }
2193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 @Override
2195 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002196 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198
Joe Onorato88ec0992009-11-19 13:16:06 -08002199 @Override
2200 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002201 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2202 return;
2203 }
2204
Sunny Goyal45478022015-06-08 16:52:41 -07002205 if (mDragController.isDragging()) {
2206 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002207 return;
2208 }
2209
Jon Mirandafeba90f2016-10-06 10:53:29 -07002210 // Note: There should be at most one log per method call. This is enforced implicitly
2211 // by using if-else statements.
2212 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002213 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2214 if (topView != null) {
2215 if (topView.getActiveTextView() != null) {
2216 topView.getActiveTextView().dispatchBackKey();
2217 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002218 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302219 ued.logActionCommand(Action.Command.BACK,
2220 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002221 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302222 ued.logActionCommand(Action.Command.BACK,
2223 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002224 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002225 topView.close(true);
2226 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002227 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302228 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002229 showWorkspace(true);
2230 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302231 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002232 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002233 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302234 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002235 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002236 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002237 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002238 mWorkspace.exitWidgetResizeMode();
2239
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002240 // Back button is a no-op here, but give at least some feedback for the button press
2241 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002242 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002243 }
2244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 * Launches the intent referred by the clicked shortcut.
2247 *
2248 * @param v The view representing the clicked shortcut.
2249 */
2250 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002251 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2252 // view has detached (it's possible for this to happen if the view is removed mid touch).
2253 if (v.getWindowToken() == null) {
2254 return;
2255 }
2256
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002257 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002258 return;
2259 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002260
Adam Cohen1697b792013-09-17 19:08:21 -07002261 if (v instanceof Workspace) {
2262 if (mWorkspace.isInOverviewMode()) {
Jon Miranda2e61fba2017-02-24 09:12:59 -08002263 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2264 LauncherLogProto.Action.Direction.NONE,
2265 LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002266 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002267 }
2268 return;
2269 }
2270
2271 if (v instanceof CellLayout) {
2272 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002273 int page = mWorkspace.indexOfChild(v);
2274 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2275 LauncherLogProto.Action.Direction.NONE,
2276 LauncherLogProto.ContainerType.OVERVIEW, page);
2277 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002278 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002279 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002280 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002281 }
2282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002284 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002285 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002287 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002288 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002289 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002290 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2291 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002292 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002293 } else if (tag instanceof AppInfo) {
2294 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002295 } else if (tag instanceof LauncherAppWidgetInfo) {
2296 if (v instanceof PendingAppWidgetHostView) {
2297 onClickPendingWidget((PendingAppWidgetHostView) v);
2298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 }
2300 }
2301
Sunny Goyal70660032015-05-14 00:07:08 -07002302 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002303 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002304 return false;
2305 }
2306
Michael Jurkaaf442092010-06-10 17:01:57 -07002307 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002308 * Event handler for the app widget view which has not fully restored.
2309 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002310 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002311 if (mIsSafeModeEnabled) {
2312 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2313 return;
2314 }
2315
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002316 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002317 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002318 LauncherAppWidgetProviderInfo appWidgetInfo =
2319 mAppWidgetManager.findProvider(info.providerName, info.user);
2320 if (appWidgetInfo == null) {
2321 return;
2322 }
2323 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2324
Sunny Goyald478c832016-04-01 12:04:16 -07002325 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2326 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2327 // This should not happen, as we make sure that an Id is allocated during bind.
2328 return;
2329 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002330 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2331 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002332 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002333 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002334 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002335 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002336 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002337 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002338 }
2339 }
2340
2341 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002342 * Event handler for the "grid" button that appears on the home screen, which
2343 * enters all apps mode.
2344 *
2345 * @param v The view that was clicked.
2346 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002347 protected void onClickAllAppsButton(View v) {
2348 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002349 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302350 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2351 ControlType.ALL_APPS_BUTTON);
Sunny Goyal5606e072017-06-15 14:24:21 -07002352 showAppsView(true /* animated */, true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002353 }
2354 }
2355
Sunny Goyale6e72002017-01-12 16:55:36 -08002356 private void onClickPendingAppItem(final View v, final String packageName,
2357 boolean downloadStarted) {
2358 if (downloadStarted) {
2359 // If the download has started, simply direct to the market app.
2360 startMarketIntentForPackage(v, packageName);
2361 return;
2362 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002363 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002364 .setTitle(R.string.abandoned_promises_title)
2365 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002366 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2367 @Override
2368 public void onClick(DialogInterface dialogInterface, int i) {
2369 startMarketIntentForPackage(v, packageName);
2370 }
2371 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002372 .setNeutralButton(R.string.abandoned_clean_this,
2373 new DialogInterface.OnClickListener() {
2374 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002375 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002376 mWorkspace.removeAbandonedPromise(packageName, user);
2377 }
2378 })
2379 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002380 }
2381
2382 private void startMarketIntentForPackage(View v, String packageName) {
2383 ItemInfo item = (ItemInfo) v.getTag();
2384 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2385 boolean success = startActivitySafely(v, intent, item);
2386 if (success && v instanceof BubbleTextView) {
2387 mWaitingForResume = (BubbleTextView) v;
2388 mWaitingForResume.setStayPressed(true);
2389 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002390 }
2391
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002392 /**
2393 * Event handler for an app shortcut click.
2394 *
2395 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2396 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002397 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002398 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2399 Object tag = v.getTag();
2400 if (!(tag instanceof ShortcutInfo)) {
2401 throw new IllegalArgumentException("Input must be a Shortcut");
2402 }
2403
2404 // Open shortcut
2405 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002406
2407 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002408 if ((shortcut.isDisabled &
2409 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2410 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2411 // If the app is only disabled because of the above flags, launch activity anyway.
2412 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002413 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002414 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2415 // Use a message specific to this shortcut, if it has one.
2416 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2417 return;
2418 }
2419 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002420 int error = R.string.activity_not_available;
2421 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2422 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002423 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2424 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002425 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002426 }
2427 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2428 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002429 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002430 }
2431
Chris Wren40c5ed32014-06-24 18:24:23 -04002432 // Check for abandoned promise
Sunny Goyale6e72002017-01-12 16:55:36 -08002433 if ((v instanceof BubbleTextView) && shortcut.isPromise()) {
2434 String packageName = shortcut.intent.getComponent() != null ?
2435 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2436 if (!TextUtils.isEmpty(packageName)) {
2437 onClickPendingAppItem(v, packageName,
2438 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2439 return;
2440 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002441 }
2442
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002443 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002444 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002445 }
2446
Sunny Goyala7ce1662016-05-31 15:01:35 -07002447 private void startAppShortcutOrInfoActivity(View v) {
2448 ItemInfo item = (ItemInfo) v.getTag();
Mario Bertschler08ffaae2017-03-20 11:30:27 -07002449 Intent intent;
2450 if (item instanceof PromiseAppInfo) {
2451 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
2452 intent = promiseAppInfo.getMarketIntent();
2453 } else {
2454 intent = item.getIntent();
2455 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002456 if (intent == null) {
2457 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002458 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002459 boolean success = startActivitySafely(v, intent, item);
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002460 getUserEventDispatcher().logAppLaunch(v, intent); // TODO for discovered apps b/35802115
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002461
2462 if (success && v instanceof BubbleTextView) {
2463 mWaitingForResume = (BubbleTextView) v;
2464 mWaitingForResume.setStayPressed(true);
2465 }
2466 }
2467
2468 /**
2469 * Event handler for a folder icon click.
2470 *
2471 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2472 */
2473 protected void onClickFolderIcon(View v) {
2474 if (LOGD) Log.d(TAG, "onClickFolder");
2475 if (!(v instanceof FolderIcon)){
2476 throw new IllegalArgumentException("Input must be a FolderIcon");
2477 }
2478
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002479 Folder folder = ((FolderIcon) v).getFolder();
2480 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002481 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002482 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002483 }
Michael Jurka5130e402011-10-13 04:55:35 -07002484 }
2485
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002486 /**
2487 * Event handler for the (Add) Widgets button that appears after a long press
2488 * on the home screen.
2489 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002490 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002491 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002492 if (mIsSafeModeEnabled) {
2493 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2494 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002495 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002496 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002497 }
2498
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 /**
2500 * Event handler for the wallpaper picker button that appears after a long press
2501 * on the home screen.
2502 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002503 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002504 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002505 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2506 return;
2507 }
2508
Tony Wickham785f7a52015-08-31 17:28:32 -07002509 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2510 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002511 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002512 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002513 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002514
2515 String pickerPackage = getString(R.string.wallpaper_picker_package);
Tony85efb4b2017-06-02 13:06:10 -07002516 boolean hasTargetPackage = !TextUtils.isEmpty(pickerPackage);
2517 if (hasTargetPackage) {
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002518 intent.setPackage(pickerPackage);
2519 }
2520
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002521 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002522 try {
2523 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2524 // If there is no target package, use the default intent chooser animation
2525 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2526 } catch (ActivityNotFoundException e) {
2527 setWaitingForResult(null);
2528 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2529 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 }
2531
2532 /**
2533 * Event handler for a click on the settings button that appears after a long press
2534 * on the home screen.
2535 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002536 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002537 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002538 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2539 .setPackage(getPackageName());
2540 intent.setSourceBounds(getViewBounds(v));
Sunny Goyal53f93b92017-05-04 11:23:29 -07002541 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002542 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002543 }
2544
Adam Cohen61f560d2013-09-30 15:58:20 -07002545 public View.OnTouchListener getHapticFeedbackTouchListener() {
2546 if (mHapticFeedbackTouchListener == null) {
2547 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002548 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002549 @Override
2550 public boolean onTouch(View v, MotionEvent event) {
2551 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2552 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2553 }
2554 return false;
2555 }
2556 };
2557 }
2558 return mHapticFeedbackTouchListener;
2559 }
2560
Tony Wickhame2217252016-03-22 16:34:23 -07002561 @Override
2562 public void onAccessibilityStateChanged(boolean enabled) {
2563 mDragLayer.onAccessibilityStateChanged(enabled);
2564 }
2565
Sunny Goyal06e21a22016-08-11 16:02:02 -07002566 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002567 if (isOnCustomContent()) {
2568 // Custom content screen doesn't participate in drag and drop. If on custom
2569 // content screen, move to default.
2570 moveWorkspaceToDefaultScreen();
2571 }
Adam Cohen9211d422014-10-07 18:14:53 -07002572 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002573
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002574 /**
2575 * Called when the user stops interacting with the launcher.
2576 * This implies that the user is now on the homescreen and is not doing housekeeping.
2577 */
Adam Cohen9211d422014-10-07 18:14:53 -07002578 protected void onInteractionEnd() {
2579 if (mLauncherCallbacks != null) {
2580 mLauncherCallbacks.onInteractionEnd();
2581 }
2582 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002583
2584 /**
2585 * Called when the user starts interacting with the launcher.
2586 * The possible interactions are:
2587 * - open all apps
2588 * - reorder an app shortcut, or a widget
2589 * - open the overview mode.
2590 * This is a good time to stop doing things that only make sense
2591 * when the user is on the homescreen and not doing housekeeping.
2592 */
Adam Cohen9211d422014-10-07 18:14:53 -07002593 protected void onInteractionBegin() {
2594 if (mLauncherCallbacks != null) {
2595 mLauncherCallbacks.onInteractionBegin();
2596 }
2597 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002598
Winson Chungcd99cd32015-04-29 11:03:24 -07002599 /** Updates the interaction state. */
2600 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002601 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002602 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002603 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2604 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002605 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002606 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002607 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002608 onInteractionEnd();
2609 }
2610 }
2611
Sunny Goyala7ce1662016-05-31 15:01:35 -07002612 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002614 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2615 try {
2616 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2617 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2618 // is enabled by default on NYC.
2619 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2620 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002621
2622 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2623 String id = ((ShortcutInfo) info).getDeepShortcutId();
2624 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302625 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002626 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002627 } else {
2628 // Could be launching some bookkeeping activity
2629 startActivity(intent, optsBundle);
2630 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002631 } finally {
2632 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002635 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2636 // corresponding permission. Show the appropriate permission prompt if that
2637 // is the case.
2638 if (intent.getComponent() == null
2639 && Intent.ACTION_CALL.equals(intent.getAction())
2640 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2641 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002642
2643 setWaitingForResult(PendingRequestArgs
2644 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002645 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2646 REQUEST_PERMISSION_CALL_PHONE);
2647 } else {
2648 // No idea why this was thrown.
2649 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002651 }
2652 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002653
Sunny Goyalfe770c92016-09-27 14:38:58 -07002654 @TargetApi(Build.VERSION_CODES.M)
Tonye3c59252017-05-02 21:32:27 -07002655 public Bundle getActivityLaunchOptions(View v) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002656 if (Utilities.ATLEAST_MARSHMALLOW) {
2657 int left = 0, top = 0;
2658 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2659 if (v instanceof TextView) {
2660 // Launch from center of icon, not entire view
2661 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2662 if (icon != null) {
2663 Rect bounds = icon.getBounds();
2664 left = (width - bounds.width()) / 2;
2665 top = v.getPaddingTop();
2666 width = bounds.width();
2667 height = bounds.height();
2668 }
2669 }
2670 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2671 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2672 // On L devices, we use the device default slide-up transition.
2673 // On L MR1 devices, we use a custom version of the slide-up transition which
2674 // doesn't have the delay present in the device default.
2675 return ActivityOptions.makeCustomAnimation(
2676 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2677 }
2678 return null;
2679 }
2680
Tonye3c59252017-05-02 21:32:27 -07002681 public Rect getViewBounds(View v) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002682 int[] pos = new int[2];
2683 v.getLocationOnScreen(pos);
2684 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2685 }
2686
Sunny Goyala7ce1662016-05-31 15:01:35 -07002687 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002688 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2689 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2690 return false;
2691 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002692 // Only launch using the new animation if the shortcut has not opted out (this is a
2693 // private contract between launcher and may be ignored in the future).
2694 boolean useLaunchAnimation = (v != null) &&
2695 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2696 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2697
Sunny Goyal24bb66a2017-03-21 15:12:01 -07002698 UserHandle user = item == null ? null : item.user;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002699
2700 // Prepare intent
2701 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2702 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002703 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002704 }
2705 try {
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002706 if (Utilities.ATLEAST_MARSHMALLOW
2707 && (item instanceof ShortcutInfo)
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002708 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Mario Bertschler0fc6f682017-02-17 12:16:13 -08002709 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002710 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002711 // Shortcuts need some special checks due to legacy reasons.
2712 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002713 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002714 // Could be launching some bookkeeping activity
2715 startActivity(intent, optsBundle);
2716 } else {
2717 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2718 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2719 }
2720 return true;
2721 } catch (ActivityNotFoundException|SecurityException e) {
2722 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2723 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2724 }
2725 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002726 }
2727
Tony Wickhamdadb3042016-02-24 11:07:00 -08002728 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002729 public boolean dispatchTouchEvent(MotionEvent ev) {
2730 mLastDispatchTouchEventX = ev.getX();
2731 return super.dispatchTouchEvent(ev);
2732 }
2733
2734 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002736 if (!isDraggingEnabled()) return false;
2737 if (isWorkspaceLocked()) return false;
2738 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002739
Jon Miranda51f037d2016-11-07 08:37:20 -08002740 boolean ignoreLongPressToOverview =
2741 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002742
Adam Cohen1697b792013-09-17 19:08:21 -07002743 if (v instanceof Workspace) {
2744 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002745 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302746 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2747 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002748 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002749 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002750 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2751 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2752 return true;
2753 } else {
2754 return false;
2755 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002756 } else {
2757 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002758 }
Adam Cohen1697b792013-09-17 19:08:21 -07002759 }
2760
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002761 CellLayout.CellInfo longClickCellInfo = null;
2762 View itemUnderLongClick = null;
2763 if (v.getTag() instanceof ItemInfo) {
2764 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002765 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002766 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002767 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002768 }
2769
Winson Chung3d503fb2011-07-13 17:25:49 -07002770 // The hotseat touch handling does not go through Workspace, and we always allow long press
2771 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002772 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002773 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002774 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002775 if (mWorkspace.isInOverviewMode()) {
2776 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302777 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2778 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002779 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002780 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002781 return false;
2782 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302783 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2784 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002785 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002786 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002787 }
Jon Miranda379198e2016-09-23 08:54:40 -07002788 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2789 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002791 final boolean isAllAppsButton =
2792 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2793 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2794 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002795 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002797 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 }
2799 }
2800 }
2801 return true;
2802 }
2803
Winson Chung3d503fb2011-07-13 17:25:49 -07002804 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002805 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002806 return mHotseat != null && layout != null &&
2807 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2808 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002809
Winson Chung3d503fb2011-07-13 17:25:49 -07002810 /**
2811 * Returns the CellLayout of the specified container at the specified screen.
2812 */
Sunny Goyal92820592015-03-02 11:31:35 -08002813 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002814 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2815 if (mHotseat != null) {
2816 return mHotseat.getLayout();
2817 } else {
2818 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002819 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002820 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002821 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002822 }
2823 }
2824
Winson Chungb745afb2015-03-02 11:51:23 -08002825 /**
2826 * For overridden classes.
2827 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002828 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002829 return isAppsViewVisible();
2830 }
2831
2832 public boolean isAppsViewVisible() {
2833 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2834 }
2835
2836 public boolean isWidgetsViewVisible() {
2837 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002838 }
2839
Michael Jurkae326f182011-11-21 14:05:46 -08002840 @Override
2841 public void onTrimMemory(int level) {
2842 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002843 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2844 // The widget preview db can result in holding onto over
2845 // 3MB of memory for caching which isn't necessary.
2846 SQLiteDatabase.releaseMemory();
2847
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002848 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002849 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002850 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002851 if (mLauncherCallbacks != null) {
2852 mLauncherCallbacks.onTrimMemory(level);
2853 }
Michael Jurkae326f182011-11-21 14:05:46 -08002854 }
2855
Winson5c6bdbb2015-09-03 11:36:19 -07002856 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002857 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002858 }
2859
Winson5c6bdbb2015-09-03 11:36:19 -07002860 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002861 boolean changed = mState != State.WORKSPACE ||
2862 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002863 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002864 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002865 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002866 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002867
Michael Jurkab3e22d92011-10-31 15:58:33 -07002868 // Set focus to the AppsCustomize button
2869 if (mAllAppsButton != null) {
2870 mAllAppsButton.requestFocus();
2871 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002872 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002873
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002874 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002875 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002876
Winson Chung0f785722015-04-08 10:27:49 -07002877 if (changed) {
2878 // Send an accessibility event to announce the context change
2879 getWindow().getDecorView()
2880 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002881 }
Winson5c6bdbb2015-09-03 11:36:19 -07002882 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002883 }
2884
Winsone9f27272015-10-13 10:47:51 -07002885 /**
2886 * Shows the overview button.
2887 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002888 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002889 showOverviewMode(animated, false);
2890 }
2891
2892 /**
2893 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2894 * onto one of the overview panel buttons.
2895 */
2896 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2897 Runnable postAnimRunnable = null;
2898 if (requestButtonFocus) {
2899 postAnimRunnable = new Runnable() {
2900 @Override
2901 public void run() {
2902 // Hitting the menu button when in touch mode does not trigger touch mode to
2903 // be disabled, so if requested, force focus on one of the overview panel
2904 // buttons.
2905 mOverviewPanel.requestFocusFromTouch();
2906 }
2907 };
2908 }
Adam Cohened307df2013-10-02 09:37:31 -07002909 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002910 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002911 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002912 setState(State.WORKSPACE);
2913
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002914 // If animated from long press, then don't allow any of the controller in the drag
2915 // layer to intercept any remaining touch.
2916 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002917 }
2918
Mario Bertschleracbf5702017-03-03 10:58:06 -08002919 private void setState(State state) {
2920 this.mState = state;
2921 updateSoftInputMode();
2922 }
2923
2924 private void updateSoftInputMode() {
2925 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2926 final int mode;
2927 if (isAppsViewVisible()) {
2928 mode = SOFT_INPUT_MODE_ALL_APPS;
2929 } else {
2930 mode = SOFT_INPUT_MODE_DEFAULT;
2931 }
2932 getWindow().setSoftInputMode(mode);
2933 }
2934 }
2935
Winson Chungb745afb2015-03-02 11:51:23 -08002936 /**
2937 * Shows the apps view.
2938 */
Sunny Goyal5606e072017-06-15 14:24:21 -07002939 public void showAppsView(boolean animated, boolean updatePredictedApps) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002940 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002941 if (updatePredictedApps) {
2942 tryAndUpdatePredictedApps();
2943 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002944 showAppsOrWidgets(State.APPS, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002945 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002946
Winson Chungb745afb2015-03-02 11:51:23 -08002947 /**
2948 * Shows the widgets view.
2949 */
2950 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002951 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002952 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002953 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002954 }
Sunny Goyal5606e072017-06-15 14:24:21 -07002955 showAppsOrWidgets(State.WIDGETS, animated);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002956
2957 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002958 @Override
2959 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002960 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002961 }
2962 });
Winson Chungb745afb2015-03-02 11:51:23 -08002963 }
2964
2965 /**
2966 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002967 *
2968 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002969 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002970 // TODO: calling method should use the return value so that when {@code false} is returned
2971 // the workspace transition doesn't fall into invalid state.
Sunny Goyal5606e072017-06-15 14:24:21 -07002972 private boolean showAppsOrWidgets(State toState, boolean animated) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002973 if (!(mState == State.WORKSPACE ||
2974 mState == State.APPS_SPRING_LOADED ||
2975 mState == State.WIDGETS_SPRING_LOADED ||
2976 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002977 return false;
2978 }
2979 if (toState != State.APPS && toState != State.WIDGETS) {
2980 return false;
2981 }
Winson Chungb745afb2015-03-02 11:51:23 -08002982
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002983 // This is a safe and supported transition to bypass spring_loaded mode.
2984 if (mExitSpringLoadedModeRunnable != null) {
2985 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2986 mExitSpringLoadedModeRunnable = null;
2987 }
2988
Winson Chungb745afb2015-03-02 11:51:23 -08002989 if (toState == State.APPS) {
Sunny Goyal5606e072017-06-15 14:24:21 -07002990 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002991 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002992 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002993 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002994
Michael Jurkab3e22d92011-10-31 15:58:33 -07002995 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002996 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002997 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998
2999 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003000 getWindow().getDecorView()
3001 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003002 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003003 }
3004
Winson Chungcd99cd32015-04-29 11:03:24 -07003005 /**
3006 * Updates the workspace and interaction state on state change, and return the animation to this
3007 * new state.
3008 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003009 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07003010 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003011 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003012 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003013 updateInteraction(fromState, toState);
3014 return anim;
3015 }
3016
Hyunyoung Song3f471442015-04-08 19:01:34 -07003017 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003018 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003019 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003020 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003021 }
Winson Chungb745afb2015-03-02 11:51:23 -08003022
Winson Chung006ee262015-08-03 14:40:11 -07003023 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003024 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003025 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08003026 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08003027 }
Adam Cohen7777d962011-08-18 18:58:38 -07003028
Hyunyoung Song3f471442015-04-08 19:01:34 -07003029 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003030 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003031 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003032
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003033 if (mExitSpringLoadedModeRunnable != null) {
3034 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3035 }
3036 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003037 @Override
3038 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003039 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003040 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3041 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003042 // Before we show workspace, hide all apps again because
3043 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3044 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003045 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003046 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003047 } else {
3048 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003049 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003050 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003051 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003052 };
3053 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003054 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003055
Tony Wickhame0c33232016-02-08 11:37:04 -08003056 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003057 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3058 || mState == State.WIDGETS_SPRING_LOADED;
3059 }
3060
Sunny Goyal0f76b562016-12-13 19:37:10 -08003061 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003062 if (mState == State.APPS_SPRING_LOADED) {
Sunny Goyal5606e072017-06-15 14:24:21 -07003063 showAppsView(true /* animated */, false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003064 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003065 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003066 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3067 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003068 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003069 }
3070
Winson Chung4ac30062015-05-08 17:34:17 -07003071 /**
3072 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3073 * resumed.
3074 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003075 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003076 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003077 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003078 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003079 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003080 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003081 }
3082 }
3083 }
3084
Michael Jurkab3e22d92011-10-31 15:58:33 -07003085 void lockAllApps() {
3086 // TODO
3087 }
3088
3089 void unlockAllApps() {
3090 // TODO
3091 }
3092
Michael Jurkad7c28052012-04-27 15:43:36 -07003093 @Override
3094 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003095 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003096 final List<CharSequence> text = event.getText();
3097 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003098 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003099 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003100 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003101 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003102 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003103 } else if (mWorkspace != null) {
3104 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003105 } else {
3106 text.add(getString(R.string.all_apps_home_button_label));
3107 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003108 return result;
3109 }
3110
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003111 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003112 * If the activity is currently paused, signal that we need to run the passed Runnable
3113 * in onResume.
3114 *
3115 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003116 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3117 * wrong when we're not running, and if the activity comes back to what the configuration was
3118 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003119 *
3120 * Implementation of the method from LauncherModel.Callbacks.
3121 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003122 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003123 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003124 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003125 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003126 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003127 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003128 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003129 }
3130 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003131 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003132 return true;
3133 } else {
3134 return false;
3135 }
3136 }
3137
Michael Jurkac402cd92013-05-20 15:49:32 +02003138 private boolean waitUntilResume(Runnable run) {
3139 return waitUntilResume(run, false);
3140 }
3141
Michael Jurka1e2f4652013-07-08 18:03:46 -07003142 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003143 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003144 }
3145
Michael Jurka7607c2f2013-04-03 14:33:19 -07003146 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003147 * If the activity is currently paused, signal that we need to re-run the loader
3148 * in onResume.
3149 *
3150 * This needs to be called from incoming places where resources might have been loaded
3151 * while we are paused. That is becaues the Configuration might be wrong
3152 * when we're not running, and if it comes back to what it was when we
3153 * were paused, we are not restarted.
3154 *
3155 * Implementation of the method from LauncherModel.Callbacks.
3156 *
3157 * @return true if we are currently paused. The caller might be able to
3158 * skip some work in that case since we will come back again.
3159 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003160 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003161 public boolean setLoadOnResume() {
3162 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003163 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003164 mOnResumeNeedsLoad = true;
3165 return true;
3166 } else {
3167 return false;
3168 }
3169 }
3170
3171 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003174 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003175 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003176 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003177 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003178 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003179 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003180 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003181 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003182
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003184 * Clear any pending bind callbacks. This is called when is loader is planning to
3185 * perform a full rebind from scratch.
3186 */
3187 @Override
3188 public void clearPendingBinds() {
3189 mBindOnResumeCallbacks.clear();
3190 if (mPendingExecutor != null) {
3191 mPendingExecutor.markCompleted();
3192 mPendingExecutor = null;
3193 }
3194 }
3195
3196 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 * Refreshes the shortcuts shown on the workspace.
3198 *
3199 * Implementation of the method from LauncherModel.Callbacks.
3200 */
3201 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003202 if (LauncherAppState.PROFILE_STARTUP) {
3203 Trace.beginSection("Starting page bind");
3204 }
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08003205
3206 AbstractFloatingView.closeAllOpenViews(this);
3207
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003208 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003209
Winson Chungd64d1762013-08-20 14:37:16 -07003210 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003211 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003212 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003213
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 if (mHotseat != null) {
3215 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003217 if (LauncherAppState.PROFILE_STARTUP) {
3218 Trace.endSection();
3219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 }
3221
Adam Cohendcd297f2013-06-18 13:13:40 -07003222 @Override
3223 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003224 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003225 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3226 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003227 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3228 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3229 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003230 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3231 // If there are no screens, we need to have an empty screen
3232 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003233 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003234 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003235
3236 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003237 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003238 if (hasCustomContentToLeft()) {
3239 mWorkspace.createCustomContentContainer();
3240 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003241 }
Winsonc7d2e832016-07-28 12:24:55 -07003242
3243 // After we have added all the screens, if the wallpaper was locked to the default state,
3244 // then notify to indicate that it can be released and a proper wallpaper offset can be
3245 // computed before the next layout
3246 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003247 }
3248
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003249 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003250 int count = orderedScreenIds.size();
3251 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003252 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003253 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003254 // No need to bind the first screen, as its always bound.
3255 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3256 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003257 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003258 }
3259
Winson Chungd64d1762013-08-20 14:37:16 -07003260 public void bindAppsAdded(final ArrayList<Long> newScreens,
3261 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003262 final ArrayList<ItemInfo> addAnimated,
3263 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003264 Runnable r = new Runnable() {
3265 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003266 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003267 }
3268 };
3269 if (waitUntilResume(r)) {
3270 return;
3271 }
3272
Winson Chungd64d1762013-08-20 14:37:16 -07003273 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003274 if (newScreens != null) {
3275 bindAddScreens(newScreens);
3276 }
Winson Chungd64d1762013-08-20 14:37:16 -07003277
3278 // We add the items without animation on non-visible pages, and with
3279 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003280 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003281 bindItems(addNotAnimated, 0,
3282 addNotAnimated.size(), false);
3283 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003284 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003285 bindItems(addAnimated, 0,
3286 addAnimated.size(), true);
3287 }
Winson Chungc58497e2013-09-03 17:48:37 -07003288
Winson Chung87412982013-10-03 18:34:14 -07003289 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003290 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003291
Winson Chungb745afb2015-03-02 11:51:23 -08003292 if (addedApps != null && mAppsView != null) {
3293 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003294 }
Winson Chungd64d1762013-08-20 14:37:16 -07003295 }
3296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003297 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 * Bind the items start-end from the list.
3299 *
3300 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003302 @Override
Sunny Goyal3be633b2016-12-08 09:59:25 -08003303 public void bindItems(final ArrayList<ItemInfo> items, final int start, final int end,
Winson Chung64359a52013-07-08 17:17:08 -07003304 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003305 Runnable r = new Runnable() {
3306 public void run() {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003307 bindItems(items, start, end, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003308 }
3309 };
3310 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003311 return;
3312 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003313
Sunny Goyal3be633b2016-12-08 09:59:25 -08003314 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003315 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3316 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003317 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003318 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003319 long newItemsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003320 for (int i = start; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003321 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003322
3323 // Short circuit if we are loading dock items for a configuration which has no dock
3324 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3325 mHotseat == null) {
3326 continue;
3327 }
3328
Sunny Goyal639e9062015-08-19 19:17:06 -07003329 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 switch (item.itemType) {
3331 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3332 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003333 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003334 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003335 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003337 }
3338 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003339 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003340 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003341 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003343 }
3344 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
3345 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
3346 if (mIsSafeModeEnabled) {
3347 view = new PendingAppWidgetHostView(this, info, mIconCache, true);
3348 } else {
3349 LauncherAppWidgetProviderInfo providerInfo =
3350 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
3351 if (providerInfo == null) {
3352 deleteWidgetInfo(info);
3353 continue;
3354 }
3355 view = mAppWidgetHost.createView(this, info.appWidgetId, providerInfo);
3356 }
3357 prepareAppWidget((AppWidgetHostView) view, info);
3358 break;
3359 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003360 default:
3361 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003363
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003364 /*
3365 * Remove colliding items.
3366 */
3367 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3368 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3369 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3370 View v = cl.getChildAt(item.cellX, item.cellY);
3371 Object tag = v.getTag();
3372 String desc = "Collision while binding workspace item: " + item
3373 + ". Collides with " + tag;
Sunny Goyal3d706ad2017-03-06 16:56:39 -08003374 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003375 throw (new RuntimeException(desc));
3376 } else {
3377 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003378 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003379 continue;
3380 }
3381 }
3382 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303383 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003384 if (animateIcons) {
3385 // Animate all the applications up now
3386 view.setAlpha(0f);
3387 view.setScaleX(0f);
3388 view.setScaleY(0f);
3389 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003390 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003392 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003393
Winson Chung997a9232013-07-24 15:33:46 -07003394 if (animateIcons) {
3395 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003396 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003397 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003398 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003399 final Runnable startBounceAnimRunnable = new Runnable() {
3400 public void run() {
3401 anim.playTogether(bounceAnims);
3402 anim.start();
3403 }
3404 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003405 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003406 // We post the animation slightly delayed to prevent slowdowns
3407 // when we are loading right after we return to launcher.
3408 mWorkspace.postDelayed(new Runnable() {
3409 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003410 if (mWorkspace != null) {
3411 mWorkspace.snapToPage(newScreenIndex);
3412 mWorkspace.postDelayed(startBounceAnimRunnable,
3413 NEW_APPS_ANIMATION_DELAY);
3414 }
Winson Chung94d67682013-09-25 16:29:40 -07003415 }
3416 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003417 } else {
3418 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003419 }
3420 }
Winson Chung64359a52013-07-08 17:17:08 -07003421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003423 }
3424
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 /**
3426 * Add the views for a widget to the workspace.
3427 *
3428 * Implementation of the method from LauncherModel.Callbacks.
3429 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003430 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003431 Runnable r = new Runnable() {
3432 public void run() {
3433 bindAppWidget(item);
3434 }
3435 };
3436 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003437 return;
3438 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003439
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003440 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303441 PendingAppWidgetHostView view =
3442 new PendingAppWidgetHostView(this, item, mIconCache, true);
3443 prepareAppWidget(view, item);
3444 mWorkspace.addInScreen(view, item);
3445 mWorkspace.requestLayout();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003446 return;
3447 }
3448
Daniel Sandler843e8602010-06-07 14:59:01 -04003449 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3450 if (DEBUG_WIDGETS) {
3451 Log.d(TAG, "bindAppWidget: " + item);
3452 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003453
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003454 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003455
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003456 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3457 // If the provider is not ready, bind as a pending widget.
3458 appWidgetInfo = null;
3459 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3460 // The widget id is not valid. Try to find the widget based on the provider info.
3461 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3462 } else {
3463 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3464 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003465
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003466 // If the provider is ready, but the width is not yet restored, try to restore it.
3467 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3468 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003469 if (appWidgetInfo == null) {
3470 if (DEBUG_WIDGETS) {
3471 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3472 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003473 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003474 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003475 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003476 return;
3477 }
Sunny Goyalff572272014-07-23 13:58:07 -07003478
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003479 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003480 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003481 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3482 // Id has not been allocated yet. Allocate a new id.
3483 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3484 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003485
Sunny Goyald478c832016-04-01 12:04:16 -07003486 // Also try to bind the widget. If the bind fails, the user will be shown
3487 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003488 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003489 pendingInfo.spanX = item.spanX;
3490 pendingInfo.spanY = item.spanY;
3491 pendingInfo.minSpanX = item.minSpanX;
3492 pendingInfo.minSpanY = item.minSpanY;
3493 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003494
3495 boolean isDirectConfig =
3496 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3497 if (isDirectConfig && item.bindOptions != null) {
3498 Bundle newOptions = item.bindOptions.getExtras();
3499 if (options != null) {
3500 newOptions.putAll(options);
3501 }
3502 options = newOptions;
3503 }
Sunny Goyald478c832016-04-01 12:04:16 -07003504 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3505 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003506
Sunny Goyal86df1382016-08-10 15:03:22 -07003507 // We tried to bind once. If we were not able to bind, we would need to
3508 // go through the permission dialog, which means we cannot skip the config
3509 // activity.
3510 item.bindOptions = null;
3511 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3512
Sunny Goyald478c832016-04-01 12:04:16 -07003513 // Bind succeeded
3514 if (success) {
3515 // If the widget has a configure activity, it is still needs to set it up,
3516 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003517 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003518 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3519 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003520 }
Sunny Goyald478c832016-04-01 12:04:16 -07003521
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003522 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003523 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003524 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003525 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003526 // The widget was marked as UI not ready, but there is no configure activity to
3527 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003528 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003529 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003530 }
Sunny Goyalff572272014-07-23 13:58:07 -07003531 }
3532
Sunny Goyald5462aa2016-11-22 16:52:39 +05303533 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003534 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003535 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003536 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3537 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003538 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003539
Sunny Goyal56c73602015-09-25 12:55:01 -07003540 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003541 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003542 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003543 deleteWidgetInfo(item);
3544 return;
3545 }
3546
Sunny Goyal233ee962015-08-03 13:05:01 -07003547 item.minSpanX = appWidgetInfo.minSpanX;
3548 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303549 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003550 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303551 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003552 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303553 prepareAppWidget(view, item);
3554 mWorkspace.addInScreen(view, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003555 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556
Daniel Sandler843e8602010-06-07 14:59:01 -04003557 if (DEBUG_WIDGETS) {
3558 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3559 + (SystemClock.uptimeMillis()-start) + "ms");
3560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 }
3562
Sunny Goyalff572272014-07-23 13:58:07 -07003563 /**
3564 * Restores a pending widget.
3565 *
3566 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003567 */
Sunny Goyald478c832016-04-01 12:04:16 -07003568 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003569 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3570 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3571 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003572 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003573 }
3574
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003575 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003576 info.restoreStatus = finalRestoreFlag;
Jon Miranda2b823f42017-05-02 18:36:18 -07003577 if (info.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
3578 info.pendingItemInfo = null;
3579 }
Sunny Goyalff572272014-07-23 13:58:07 -07003580
3581 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003582 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003583 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003584 }
3585
Adam Cohen1462de32012-07-24 22:34:36 -07003586 public void onPageBoundSynchronously(int page) {
3587 mSynchronouslyBoundPages.add(page);
3588 }
3589
Sunny Goyal527c7d32015-08-28 15:19:36 -07003590 @Override
3591 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3592 if (mPendingExecutor != null) {
3593 mPendingExecutor.markCompleted();
3594 }
3595 mPendingExecutor = executor;
3596 executor.attachTo(this);
3597 }
3598
3599 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3600 if (mPendingExecutor == executor) {
3601 mPendingExecutor = null;
3602 }
3603 }
3604
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003605 @Override
3606 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3607 Runnable r = new Runnable() {
3608 public void run() {
3609 finishFirstPageBind(executor);
3610 }
3611 };
3612 if (waitUntilResume(r)) {
3613 return;
3614 }
3615
3616 Runnable onComplete = new Runnable() {
3617 @Override
3618 public void run() {
3619 if (executor != null) {
3620 executor.onLoadAnimationCompleted();
3621 }
3622 }
3623 };
3624 if (mDragLayer.getAlpha() < 1) {
3625 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3626 } else {
3627 onComplete.run();
3628 }
3629 }
3630
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 /**
3632 * Callback saying that there aren't any more items to bind.
3633 *
3634 * Implementation of the method from LauncherModel.Callbacks.
3635 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003636 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003637 Runnable r = new Runnable() {
3638 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003639 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003640 }
3641 };
3642 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003643 return;
3644 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003645 if (LauncherAppState.PROFILE_STARTUP) {
3646 Trace.beginSection("Page bind completed");
3647 }
Adam Cohen1462de32012-07-24 22:34:36 -07003648 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003650 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003651
Sunny Goyal2100c782016-08-22 16:00:03 -07003652 if (mPendingActivityResult != null) {
3653 handleActivityResult(mPendingActivityResult.requestCode,
3654 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3655 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003656 }
3657
Sunny Goyala474a9b2017-05-04 16:47:11 -07003658 InstallShortcutReceiver.disableAndFlushInstallQueue(
3659 InstallShortcutReceiver.FLAG_LOADER_RUNNING, this);
Adam Cohen9211d422014-10-07 18:14:53 -07003660
Tony Wickham010d2552017-01-20 08:15:28 -08003661 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3662
Adam Cohen9211d422014-10-07 18:14:53 -07003663 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003664 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003665 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003666 if (LauncherAppState.PROFILE_STARTUP) {
3667 Trace.endSection();
3668 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003669 }
3670
Winson Chunga2413752012-04-03 14:22:34 -07003671 private boolean canRunNewAppsAnimation() {
3672 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003673 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003674 }
3675
Winson Chungc9168342013-06-26 14:54:55 -07003676 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003677 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003678 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3679 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003680 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003681 return bounceAnim;
3682 }
3683
Adam Cohen27772732013-11-11 14:00:56 +00003684 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003685 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003686 }
3687
Tony Wickham55616cd2015-09-23 14:55:17 -07003688 public int getSearchBarHeight() {
3689 if (mLauncherCallbacks != null) {
3690 return mLauncherCallbacks.getSearchBarHeight();
3691 }
3692 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3693 }
3694
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003695 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003696 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3697 * multiple calls to bind the same list.)
3698 */
3699 @Thunk ArrayList<AppInfo> mTmpAppsList;
3700 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3701 public void run() {
3702 bindAllApplications(mTmpAppsList);
3703 mTmpAppsList = null;
3704 }
3705 };
3706
3707 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003708 * Add the icons for all apps.
3709 *
3710 * Implementation of the method from LauncherModel.Callbacks.
3711 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003712 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003713 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3714 mTmpAppsList = apps;
3715 return;
3716 }
3717
Winson Chungb745afb2015-03-02 11:51:23 -08003718 if (mAppsView != null) {
3719 mAppsView.setApps(apps);
3720 }
Adam Cohen9211d422014-10-07 18:14:53 -07003721 if (mLauncherCallbacks != null) {
3722 mLauncherCallbacks.bindAllApplications(apps);
3723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 }
3725
3726 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003727 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3728 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3729 */
3730 @Override
3731 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003732 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003733 }
3734
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003735 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 * A package was updated.
3737 *
3738 * Implementation of the method from LauncherModel.Callbacks.
3739 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003740 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003741 Runnable r = new Runnable() {
3742 public void run() {
3743 bindAppsUpdated(apps);
3744 }
3745 };
3746 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003747 return;
3748 }
3749
Winson Chungb745afb2015-03-02 11:51:23 -08003750 if (mAppsView != null) {
3751 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 }
3754
Sunny Goyal4390ace2014-10-13 11:33:11 -07003755 @Override
Mario Bertschler08ffaae2017-03-20 11:30:27 -07003756 public void bindPromiseAppProgressUpdated(final PromiseAppInfo app) {
3757 Runnable r = new Runnable() {
3758 public void run() {
3759 bindPromiseAppProgressUpdated(app);
3760 }
3761 };
3762 if (waitUntilResume(r)) {
3763 return;
3764 }
3765
3766 if (mAppsView != null) {
3767 mAppsView.updatePromiseAppProgress(app);
3768 }
3769 }
3770
3771 @Override
Sunny Goyal4390ace2014-10-13 11:33:11 -07003772 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3773 Runnable r = new Runnable() {
3774 public void run() {
3775 bindWidgetsRestored(widgets);
3776 }
3777 };
3778 if (waitUntilResume(r)) {
3779 return;
3780 }
3781 mWorkspace.widgetsRestored(widgets);
3782 }
3783
Joe Onorato9c1289c2009-08-17 11:03:03 -04003784 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003785 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003786 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003787 *
3788 * @param updated list of shortcuts which have changed.
3789 * @param removed list of shortcuts which were deleted in the background. This can happen when
3790 * an app gets removed from the system or some of its components are no longer
3791 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003792 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003793 @Override
3794 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003795 final ArrayList<ShortcutInfo> removed, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003796 Runnable r = new Runnable() {
3797 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003798 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003799 }
3800 };
3801 if (waitUntilResume(r)) {
3802 return;
3803 }
3804
Sunny Goyal4390ace2014-10-13 11:33:11 -07003805 if (!updated.isEmpty()) {
3806 mWorkspace.updateShortcuts(updated);
3807 }
3808
3809 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003810 HashSet<ComponentName> removedComponents = new HashSet<>();
3811 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3812
Sunny Goyal4390ace2014-10-13 11:33:11 -07003813 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003814 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08003815 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003816 } else {
3817 removedComponents.add(si.getTargetComponent());
3818 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003819 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003820
3821 if (!removedComponents.isEmpty()) {
3822 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3823 mWorkspace.removeItemsByMatcher(matcher);
3824 mDragController.onAppsRemoved(matcher);
3825 }
3826
3827 if (!removedDeepShortcuts.isEmpty()) {
3828 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3829 mWorkspace.removeItemsByMatcher(matcher);
3830 mDragController.onAppsRemoved(matcher);
3831 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003832 }
3833 }
3834
3835 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003836 * Update the state of a package, typically related to install state.
3837 *
3838 * Implementation of the method from LauncherModel.Callbacks.
3839 */
Sunny Goyale755d462014-07-22 13:48:29 -07003840 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003841 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3842 Runnable r = new Runnable() {
3843 public void run() {
3844 bindRestoreItemsChange(updates);
3845 }
3846 };
3847 if (waitUntilResume(r)) {
3848 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003849 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003850
Sunny Goyal756adbc2015-04-16 15:20:51 -07003851 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003852 }
3853
3854 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003855 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003856 * in addition to specific applications to remove, the reason being that
3857 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003858 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003859 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003861 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003862 public void bindWorkspaceComponentsRemoved(
3863 final HashSet<String> packageNames, final HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003864 final UserHandle user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003865 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003866 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003867 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003868 }
Winson Chungd64d1762013-08-20 14:37:16 -07003869 };
3870 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003871 return;
3872 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003873 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003874 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3875 mWorkspace.removeItemsByMatcher(matcher);
3876 mDragController.onAppsRemoved(matcher);
3877
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003878 }
3879 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003880 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3881 mWorkspace.removeItemsByMatcher(matcher);
3882 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003883 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003884 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003885
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003886 @Override
3887 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3888 Runnable r = new Runnable() {
3889 public void run() {
3890 bindAppInfosRemoved(appInfos);
3891 }
3892 };
3893 if (waitUntilResume(r)) {
3894 return;
Joe Onorato36115782010-06-17 13:28:48 -04003895 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003896
Winson Chungdf95eb12013-10-16 14:57:07 -07003897 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003898 if (mAppsView != null) {
3899 mAppsView.removeApps(appInfos);
Hyunyoung Song51114f22017-05-02 16:06:28 -07003900 tryAndUpdatePredictedApps();
Winson Chungc58497e2013-09-03 17:48:37 -07003901 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902 }
Joe Onoratobe386092009-11-17 17:32:16 -08003903
Sunny Goyald164b7f2016-10-12 20:49:31 -07003904 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003905 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003906 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003907 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003908 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003909
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003910 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003911 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3912 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3913 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003914 return;
3915 }
3916
Sunny Goyald164b7f2016-10-12 20:49:31 -07003917 if (mWidgetsView != null && allWidgets != null) {
3918 mWidgetsView.setWidgets(allWidgets);
3919 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003920 }
Tony Wickham26b17462017-03-20 17:12:24 -07003921
3922 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
3923 if (topView != null) {
3924 topView.onWidgetsBound();
3925 }
3926 }
3927
3928 public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
3929 return mWidgetsView.getWidgetsForPackageUser(packageUserKey);
Winson Chung80baf5a2010-08-09 16:03:15 -07003930 }
3931
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003932 @Override
3933 public void notifyWidgetProvidersChanged() {
Tony Wickham86222d22017-03-29 15:30:43 -07003934 if (mWorkspace.getState().shouldUpdateWidget) {
3935 refreshAndBindWidgetsForPackageUser(null);
3936 }
Tony Wickham26b17462017-03-20 17:12:24 -07003937 }
3938
Tony Wickham86222d22017-03-29 15:30:43 -07003939 /**
3940 * @param packageUser if null, refreshes all widgets and shortcuts, otherwise only
3941 * refreshes the widgets and shortcuts associated with the given package/user
3942 */
3943 public void refreshAndBindWidgetsForPackageUser(@Nullable PackageUserKey packageUser) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07003944 mModel.refreshAndBindWidgetsAndShortcuts(packageUser);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003945 }
3946
Winson Chung4b919f82012-05-01 10:44:08 -07003947 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003948 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003949 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003950 }
3951 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003952
Winson Chung4b919f82012-05-01 10:44:08 -07003953 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003954 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003955 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003956 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003957 } else {
3958 mHandler.postDelayed(new Runnable() {
3959 public void run() {
3960 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3961 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003962 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003963 }
Winson Chung4b919f82012-05-01 10:44:08 -07003964 }
Winson Chung400438b2011-01-16 17:53:48 -08003965 }
3966
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003967 private void markAppsViewShown() {
3968 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3969 return;
3970 }
3971 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3972 }
3973
3974 private boolean shouldShowDiscoveryBounce() {
3975 if (mState != mState.WORKSPACE) {
3976 return false;
3977 }
3978 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3979 return true;
3980 }
3981 if (!mIsResumeFromActionScreenOff) {
3982 return false;
3983 }
3984 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3985 return false;
3986 }
3987 return true;
3988 }
3989
Winson Chung5ffd51d2015-06-25 11:36:50 -07003990 protected void moveWorkspaceToDefaultScreen() {
3991 mWorkspace.moveToDefaultScreen(false);
3992 }
3993
Winson Chung80baf5a2010-08-09 16:03:15 -07003994 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003995 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003996 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003997 @Override
3998 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3999 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004000
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08004001 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
4002 writer.println(prefix + "Workspace Items");
4003 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4004 writer.println(prefix + " Homescreen " + i);
4005
4006 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4007 for (int j = 0; j < layout.getChildCount(); j++) {
4008 Object tag = layout.getChildAt(j).getTag();
4009 if (tag != null) {
4010 writer.println(prefix + " " + tag.toString());
4011 }
4012 }
4013 }
4014
4015 writer.println(prefix + " Hotseat");
4016 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07004017 for (int j = 0; j < layout.getChildCount(); j++) {
4018 Object tag = layout.getChildAt(j).getTag();
4019 if (tag != null) {
4020 writer.println(prefix + " " + tag.toString());
4021 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004022 }
Sunny Goyala1365452015-10-01 15:46:24 -07004023
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08004024 try {
4025 FileLog.flushAll(writer);
4026 } catch (Exception e) {
4027 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004028 }
4029 }
4030
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08004031 writer.println(prefix + "Misc:");
4032 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
4033 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
4034 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
4035
4036 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004037
Adam Cohen9211d422014-10-07 18:14:53 -07004038 if (mLauncherCallbacks != null) {
4039 mLauncherCallbacks.dump(prefix, fd, writer, args);
4040 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004041 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004042
Sunny Goyal66b24572016-09-21 15:57:55 -07004043 @Override
4044 @TargetApi(Build.VERSION_CODES.N)
4045 public void onProvideKeyboardShortcuts(
4046 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4047
4048 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4049 if (mState == State.WORKSPACE) {
4050 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4051 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4052 }
4053 View currentFocus = getCurrentFocus();
4054 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4055 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4056 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4057 }
Tony18c4aa42017-05-10 21:23:57 -05004058 if (currentFocus.getTag() instanceof ItemInfo
4059 && DeepShortcutManager.supportsShortcuts((ItemInfo) currentFocus.getTag())) {
Sunny Goyal66b24572016-09-21 15:57:55 -07004060 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4061 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4062 }
4063 if (!shortcutInfos.isEmpty()) {
4064 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4065 }
4066
4067 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4068 }
4069
4070 @Override
4071 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4072 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4073 switch (keyCode) {
4074 case KeyEvent.KEYCODE_A:
4075 if (mState == State.WORKSPACE) {
Sunny Goyal5606e072017-06-15 14:24:21 -07004076 showAppsView(true, true);
Sunny Goyal66b24572016-09-21 15:57:55 -07004077 return true;
4078 }
4079 break;
4080 case KeyEvent.KEYCODE_S: {
4081 View focusedView = getCurrentFocus();
4082 if (focusedView instanceof BubbleTextView
4083 && focusedView.getTag() instanceof ItemInfo
4084 && mAccessibilityDelegate.performAction(focusedView,
4085 (ItemInfo) focusedView.getTag(),
4086 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08004087 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004088 return true;
4089 }
4090 break;
4091 }
4092 case KeyEvent.KEYCODE_O:
4093 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4094 return true;
4095 }
4096 break;
4097 }
4098 }
4099 return super.onKeyShortcut(keyCode, event);
4100 }
4101
Adam Cohen59400422014-03-05 18:07:04 -08004102 public static CustomAppWidget getCustomAppWidget(String name) {
4103 return sCustomAppWidgets.get(name);
4104 }
4105
4106 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4107 return sCustomAppWidgets;
4108 }
4109
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004110 public static Launcher getLauncher(Context context) {
4111 if (context instanceof Launcher) {
4112 return (Launcher) context;
4113 }
4114 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4115 }
4116
Sunny Goyal5a81c382017-03-20 15:08:06 -07004117 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener {
Sunny Goyal745bad92016-05-02 10:54:12 -07004118
4119 @Override
4120 public void onSharedPreferenceChanged(
4121 SharedPreferences sharedPreferences, String key) {
4122 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
Sunny Goyale13d4642017-05-22 15:58:42 -07004123 // Recreate the activity so that it initializes the rotation preference again.
4124 recreate();
Sunny Goyal745bad92016-05-02 10:54:12 -07004125 }
4126 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004128}