blob: d963f4379e471031e3c48a907298c719ca4842ac [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070023import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000024import android.annotation.TargetApi;
Winson Chungc7450e32012-04-17 17:34:08 -070025import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040026import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080028import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070029import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080031import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080032import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040033import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070035import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040036import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070038import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070039import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070040import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070041import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.pm.PackageManager;
Adam Cohen3f9c9712014-10-31 11:48:25 -070044import android.database.sqlite.SQLiteDatabase;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070045import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070046import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070048import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020049import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.os.Handler;
Sunny Goyalb38fab72017-01-20 19:32:31 -080052import android.os.Parcelable;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080053import android.os.Process;
Winson Chunga2413752012-04-03 14:22:34 -070054import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040055import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070056import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070057import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070060import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070063import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.HapticFeedbackConstants;
65import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070066import android.view.KeyboardShortcutGroup;
67import android.view.KeyboardShortcutInfo;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080071import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.ViewGroup;
73import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080074import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070075import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070076import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.inputmethod.InputMethodManager;
Chet Haasea8f996d2015-02-17 12:56:04 -080078import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070080
Sunny Goyal651077b2014-06-30 14:15:31 -070081import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070082import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070083import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070084import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070085import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080086import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalb5e65c82016-10-26 18:32:38 -070087import com.android.launcher3.anim.AnimationLayerSet;
Tony Wickham9438ed42017-01-20 09:38:25 -080088import com.android.launcher3.notification.NotificationListener;
Tony Wickham010d2552017-01-20 08:15:28 -080089import com.android.launcher3.popup.PopupDataProvider;
Sunny Goyalffe83f12014-08-14 17:39:34 -070090import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010091import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal782f0c92017-01-19 10:27:54 -080092import com.android.launcher3.compat.PinItemRequestCompat;
Kenny Guyed131872014-04-30 03:02:21 +010093import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080094import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070095import com.android.launcher3.config.ProviderConfig;
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;
Sunny Goyal26119432016-02-18 22:09:23 +0000102import com.android.launcher3.folder.Folder;
103import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700104import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700105import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700106import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700107import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700108import com.android.launcher3.model.PackageItemInfo;
109import com.android.launcher3.model.WidgetItem;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700110import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800111import com.android.launcher3.popup.PopupContainerWithArrow;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700112import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700113import com.android.launcher3.shortcuts.ShortcutKey;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530114import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
115import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
116import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700117import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700118import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700119import com.android.launcher3.util.ItemInfoMatcher;
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -0800120import com.android.launcher3.util.LogConfig;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700121import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800123import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700124import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700125import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700126import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700127import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800128import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800130import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700131import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700132import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700133
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700134import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700135import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700137import java.util.Collection;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700139import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700140import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800141import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143/**
144 * Default launcher application.
145 */
Sunny Goyal27835952017-01-13 12:15:53 -0800146public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700147 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
148 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700149 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700150 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700151 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700153 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700154 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400155 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700158 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700159 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700160 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Michael Jurka8b805b12012-04-18 14:23:14 -0700162 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700163 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700164 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700165
Sunny Goyal28c6b962015-10-12 11:42:05 -0700166 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Winson Chung2672ff92012-05-04 16:22:30 -0700176 // The Intent extra that defines whether to ignore the launch animation
177 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400178 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
181 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700182 // Type: int
183 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700184 // Type: PendingRequestArgs
185 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
186 // Type: ActivityResultInfo
187 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700189 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800190
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700191 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700192 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
193 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700194
Adam Cohen091440a2015-03-18 14:16:05 -0700195 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700196 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700197
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700198 private boolean mIsSafeModeEnabled;
199
Sunny Goyal27835952017-01-13 12:15:53 -0800200 public static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800201 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700202 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700203 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Winson Chunga2413752012-04-03 14:22:34 -0700205 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700206 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
207 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700211 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800213 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700214 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700215
216 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Sunny Goyalffe83f12014-08-14 17:39:34 -0700218 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700219 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700220
Michael Jurka0280c3b2010-09-17 15:00:07 -0700221 private int[] mTmpAddItemCellCoordinates = new int[2];
222
Sunny Goyal316490e2015-06-02 09:38:28 -0700223 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800224 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700225
Michael Jurka838a4ca2011-02-07 13:33:06 -0800226 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700227 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700228
Sunny Goyal47328fd2016-05-25 18:56:41 -0700229 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700230
231 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700232 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700233 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700235 // Main container view and the model for the widget tray screen.
236 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700237 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700238
Winson Chung4a2afa32012-07-19 14:53:05 -0700239 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
240 // scroll issues (because the workspace may not have been measured yet) and extra work.
241 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
242 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243
244 private SpannableStringBuilder mDefaultKeySsb = null;
245
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800248 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700249 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250
Michael Jurka1e2f4652013-07-08 18:03:46 -0700251 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700252 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700253 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700254
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800256 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700257 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700258 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700259 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700260 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700261 private boolean mHasFocus = false;
262 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263
Tony Wickham010d2552017-01-20 08:15:28 -0800264 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700265
Adam Cohen61f560d2013-09-30 15:58:20 -0700266 private View.OnTouchListener mHapticFeedbackTouchListener;
267
Winson Chung400438b2011-01-16 17:53:48 -0800268 // Determines how long to wait after a rotation before restoring the screen orientation to
269 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700270 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800271
Adam Cohen1462de32012-07-24 22:34:36 -0700272 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
273
Winson Chung46353de2012-02-16 14:05:10 -0800274 // We only want to get the SharedPreferences once since it does an FS stat each time we get
275 // it from the context.
276 private SharedPreferences mSharedPrefs;
277
Adam Cohen4b66bf32015-09-18 12:15:19 -0700278 private boolean mMoveToDefaultScreenFromNewIntent;
279
Winson Chung13eb5272015-05-11 16:30:13 -0700280 // This is set to the view that launched the activity that navigated the user away from
281 // launcher. Since there is no callback for when the activity has finished launching, enable
282 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800283 private BubbleTextView mWaitingForResume;
284
Adam Cohen59400422014-03-05 18:07:04 -0800285 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
286 new HashMap<String, CustomAppWidget>();
287
Adam Cohen59400422014-03-05 18:07:04 -0800288 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700289 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
290 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800291 }
292 }
293
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700294 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
295 // state transition. If another state transition happened during this delay,
296 // simply unregister this runnable.
297 private Runnable mExitSpringLoadedModeRunnable;
298
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700300 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700301 if (mWorkspace != null) {
302 mWorkspace.buildPageHardwareLayers();
303 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700304 }
305 };
306
Sunny Goyal2100c782016-08-22 16:00:03 -0700307 // Activity result which needs to be processed after workspace has loaded.
308 private ActivityResultInfo mPendingActivityResult;
309 /**
310 * Holds extra information required to handle a result from an external call, like
311 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
312 */
313 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800314
Hyunyoung Songaa953652016-04-19 18:30:24 -0700315 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800316
Jon Miranda379198e2016-09-23 08:54:40 -0700317 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700318
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700319 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700320 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400321
322 @Thunk void setOrientation() {
323 if (mRotationEnabled) {
324 unlockScreenOrientation(true);
325 } else {
326 setRequestedOrientation(
327 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700328 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400329 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700330
Sunny Goyal745bad92016-05-02 10:54:12 -0700331 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 @Override
334 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700335 if (DEBUG_STRICT_MODE) {
336 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
337 .detectDiskReads()
338 .detectDiskWrites()
339 .detectNetwork() // or .detectAll() for all detectable problems
340 .penaltyLog()
341 .build());
342 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
343 .detectLeakedSqlLiteObjects()
344 .detectLeakedClosableObjects()
345 .penaltyLog()
346 .penaltyDeath()
347 .build());
348 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700349 if (LauncherAppState.PROFILE_STARTUP) {
350 Trace.beginSection("Launcher-onCreate");
351 }
Winson Chunga2413752012-04-03 14:22:34 -0700352
Adam Cohen9211d422014-10-07 18:14:53 -0700353 if (mLauncherCallbacks != null) {
354 mLauncherCallbacks.preOnCreate();
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400358
Sunny Goyal87f784c2017-01-11 10:48:34 -0800359 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700360
Adam Cohen2e6da152015-05-06 11:42:25 -0700361 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800362 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Sunny Goyalf5e37442016-11-02 10:31:24 -0700363 if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700364 Display display = getWindowManager().getDefaultDisplay();
365 Point mwSize = new Point();
366 display.getSize(mwSize);
367 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
368 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700369
Sunny Goyalf7258242015-10-19 16:59:07 -0700370 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700371 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800372 mModel = app.setLauncher(this);
373 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700374 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700375
Joe Onorato41a12d22009-10-31 18:30:00 -0400376 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700377 mAllAppsController = new AllAppsTransitionController(this);
378 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700379
Sunny Goyalffe83f12014-08-14 17:39:34 -0700380 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700381
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
383 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
386 // this also ensures that any synchronous binding below doesn't re-trigger another
387 // LauncherModel load.
388 mPaused = false;
389
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800390 mLauncherView = getLayoutInflater().inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 setupViews();
Winson1f064272016-07-18 17:18:02 -0700393 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700394 mExtractedColors = new ExtractedColors();
395 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
Tony Wickham010d2552017-01-20 08:15:28 -0800397 mPopupDataProvider = new PopupDataProvider(this);
398
Tony Wickhame2217252016-03-22 16:34:23 -0700399 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
400 .addAccessibilityStateChangeListener(this);
401
Joe Onorato7c312c12009-08-13 21:36:53 -0700402 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700403
Sunny Goyalfe770c92016-09-27 14:38:58 -0700404 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405
Sunny Goyale26d1002016-06-20 14:52:14 -0700406 if (LauncherAppState.PROFILE_STARTUP) {
407 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
Sunny Goyal2100c782016-08-22 16:00:03 -0700410 // We only load the page synchronously if the user rotates (or triggers a
411 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700412 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
413 if (savedInstanceState != null) {
414 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
415 }
416 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700417 // If we are not binding synchronously, show a fade in animation when
418 // the first page bind completes.
419 mDragLayer.setAlpha(0);
420 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700421 // Pages bound synchronously.
422 mWorkspace.setCurrentPage(currentScreen);
423
Sunny Goyal2100c782016-08-22 16:00:03 -0700424 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
426
427 // For handling default keys
428 mDefaultKeySsb = new SpannableStringBuilder();
429 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800430
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800431 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700432 // In case we are on a device with locked rotation, we should look at preferences to check
433 // if the user has specifically allowed rotation.
434 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700435 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700436 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
437 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700438 }
439
440 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
441 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400442 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700443
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800444 setContentView(mLauncherView);
Adam Cohen9211d422014-10-07 18:14:53 -0700445 if (mLauncherCallbacks != null) {
446 mLauncherCallbacks.onCreate(savedInstanceState);
447 }
448 }
449
Sunny Goyal7779d622015-06-11 16:18:39 -0700450 @Override
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800451 public View findViewById(int id) {
452 return mLauncherView.findViewById(id);
453 }
454
455 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700456 public void onExtractedColorsChanged() {
457 loadExtractedColorsAndColorItems();
458 }
459
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700460 @Override
461 public void onAppWidgetHostReset() {
462 if (mAppWidgetHost != null) {
463 mAppWidgetHost.startListening();
464 }
465 }
466
Tony Wickham827cef22016-03-17 15:39:39 -0700467 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700468 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700469 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700470 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700471 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700472 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800473 boolean lightStatusBar = (FeatureFlags.LIGHT_STATUS_BAR
474 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
475 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700476 // It's possible that All Apps is visible when this is run,
Hyunyoung Song3285da32016-12-19 14:46:21 -0800477 // so always use light status bar in that case. Only change nav bar color to status bar
478 // color when All Apps is visible.
479 activateLightStatusBar(lightStatusBar || isAllAppsVisible(), isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700480 }
481 }
482
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800483 // TODO: use platform flag on API >= 26
484 private static final int SYSTEM_UI_FLAG_LIGHT_NAV_BAR = 0x10;
485
Tony Wickham345bff32016-09-28 15:34:51 -0700486 /**
487 * Sets the status bar to be light or not. Light status bar means dark icons.
Hyunyoung Song3285da32016-12-19 14:46:21 -0800488 * @param lightStatusBar make sure the status bar is light
489 * @param changeNavBar if true, make the nav bar theme in sync with status bar.
Tony Wickham345bff32016-09-28 15:34:51 -0700490 */
Hyunyoung Song3285da32016-12-19 14:46:21 -0800491 public void activateLightStatusBar(boolean lightStatusBar, boolean changeNavBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700492 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
493 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700494 if (lightStatusBar) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800495 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR ;
Hyunyoung Song3285da32016-12-19 14:46:21 -0800496 if (changeNavBar && Utilities.isAtLeastO()) {
497 newSystemUiFlags |= SYSTEM_UI_FLAG_LIGHT_NAV_BAR;
498 }
Tony Wickham93113872016-09-14 16:46:32 -0700499 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700500 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800501 if (changeNavBar && Utilities.isAtLeastO()) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800502 newSystemUiFlags &= ~(SYSTEM_UI_FLAG_LIGHT_NAV_BAR);
503 }
504 }
Hyunyoung Song3285da32016-12-19 14:46:21 -0800505
Tony Wickham345bff32016-09-28 15:34:51 -0700506 if (newSystemUiFlags != oldSystemUiFlags) {
507 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
508 }
Tony Wickham93113872016-09-14 16:46:32 -0700509 }
510
Adam Cohen9211d422014-10-07 18:14:53 -0700511 private LauncherCallbacks mLauncherCallbacks;
512
513 public void onPostCreate(Bundle savedInstanceState) {
514 super.onPostCreate(savedInstanceState);
515 if (mLauncherCallbacks != null) {
516 mLauncherCallbacks.onPostCreate(savedInstanceState);
517 }
518 }
519
Winson1f064272016-07-18 17:18:02 -0700520 public void onInsetsChanged(Rect insets) {
521 mDeviceProfile.updateInsets(insets);
522 mDeviceProfile.layout(this, true /* notifyListeners */);
523 }
524
Sunny Goyal32554d12015-12-03 15:31:25 -0800525 /**
526 * Call this after onCreate to set or clear overlay.
527 */
528 public void setLauncherOverlay(LauncherOverlay overlay) {
529 if (overlay != null) {
530 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
531 }
532 mWorkspace.setLauncherOverlay(overlay);
533 }
534
Adam Cohen9211d422014-10-07 18:14:53 -0700535 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
536 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 private boolean mWorkspaceImportanceStored = false;
539 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800541 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
543
544 @Override
545 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 return;
548 }
549 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700550 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspace != null) {
552 mWorkspaceImportanceForAccessibility =
553 mWorkspace.getImportantForAccessibility();
554 mWorkspace.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mWorkspaceImportanceStored = true;
557 }
558 if (mHotseat != null) {
559 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
560 mHotseat.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mHotseatImportanceStored = true;
563 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565
566 @Override
567 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspaceImportanceStored && mWorkspace != null) {
569 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
570 }
571 if (mHotseatImportanceStored && mHotseat != null) {
572 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
573 }
574 mWorkspaceImportanceStored = false;
575 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 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 Songaa953652016-04-19 18:30:24 -0700626 public UserEventDispatcher getUserEventDispatcher() {
627 if (mLauncherCallbacks != null) {
628 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
629 if (dispatcher != null) {
630 return dispatcher;
631 }
632 }
633
Sunny Goyal64976d52016-06-20 14:56:28 -0700634 // Logger object is a singleton and does not have to be coupled with the foreground
635 // activity. Since most user event logging is done on the UI, the object is retrieved
636 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700638 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 }
640 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100642
Hyunyoung Song3f471442015-04-08 19:01:34 -0700643 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700644 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
645 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700646 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700647 }
648
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000649 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700650 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
651 // This cast is safe as long as the id < 0x00FFFFFF
652 // Since we jail all the dynamically generated views, there should be no clashes
653 // with any other views.
654 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000655 }
656
Tony Wickham010d2552017-01-20 08:15:28 -0800657 public PopupDataProvider getPopupDataProvider() {
658 return mPopupDataProvider;
659 }
660
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000661 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700662 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
663 * a configuration step, this allows the proper animations to run after other transitions.
664 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 private long completeAdd(
666 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
667 long screenId = info.screenId;
668 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700669 // When the screen id represents an actual screen (as opposed to a rank) we make sure
670 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700672 }
Adam Cohendb364c32014-05-20 14:23:40 -0700673
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800675 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700676 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700677 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700680 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700681 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700683 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700684 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700685 int widgetId = appWidgetId;
686 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700687 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700689 // Since the view was just bound, also launch the configure activity if needed
690 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
691 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800692 if (provider != null) {
693 new WidgetAddFlowHandler(provider)
694 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700695 }
696 }
697 break;
698 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800699 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700700
Adam Cohendb364c32014-05-20 14:23:40 -0700701 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 }
703
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800704 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700706 if (isWorkspaceLoading()) {
707 // process the result once the workspace has loaded.
708 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
709 return;
710 }
711 mPendingActivityResult = null;
712
Winson Chunge341d302013-08-16 14:31:00 -0700713 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700714 final PendingRequestArgs requestArgs = mPendingRequestArgs;
715 setWaitingForResult(null);
716 if (requestArgs == null) {
717 return;
718 }
719
720 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700721
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 Runnable exitSpringLoaded = new Runnable() {
723 @Override
724 public void run() {
725 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
726 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
727 }
728 };
729
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700731 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
734 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700735 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700736 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700739 addAppWidgetImpl(
740 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800741 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700742 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700743 }
744 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200745 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
746 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700747 // User could have free-scrolled between pages before picking a wallpaper; make sure
748 // we move to the closest one now.
749 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700750 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200751 }
752 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700753 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200754
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700756 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700757
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 // We have special handling for widgets
759 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800760 final int appWidgetId;
761 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
762 : -1;
763 if (widgetId < 0) {
764 appWidgetId = pendingAddWidgetId;
765 } else {
766 appWidgetId = widgetId;
767 }
768
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800770 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700771 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
772 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700774 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700775 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 @Override
777 public void run() {
778 exitSpringLoadedDragModeDelayed(false, 0, null);
779 }
780 };
Adam Cohendb364c32014-05-20 14:23:40 -0700781
Sunny Goyal2100c782016-08-22 16:00:03 -0700782 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
784 } else {
785 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
786 // When the screen id represents an actual screen (as opposed to a rank)
787 // we make sure that the drop page actually exists.
788 requestArgs.screenId =
789 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700790 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700791 final CellLayout dropLayout =
792 mWorkspace.getScreenWithId(requestArgs.screenId);
793
794 dropLayout.setDropPending(true);
795 final Runnable onComplete = new Runnable() {
796 @Override
797 public void run() {
798 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
799 dropLayout.setDropPending(false);
800 }
801 };
802 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
803 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700804 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 return;
806 }
807
Sunny Goyald478c832016-04-01 12:04:16 -0700808 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
809 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700810 if (resultCode == RESULT_OK) {
811 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700812 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700813 }
814 // Leave the widget in the pending state if the user canceled the configure.
815 return;
816 }
817
Sunny Goyalaad90582015-07-09 14:22:50 -0700818 if (requestCode == REQUEST_CREATE_SHORTCUT) {
819 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700820 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
821 completeAdd(requestCode, data, -1, requestArgs);
822 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
823 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
824
Sunny Goyalaad90582015-07-09 14:22:50 -0700825 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700826 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
827 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800830 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800831 }
832
833 @Override
Sunny Goyaldec3a902017-01-25 18:23:36 -0800834 protected void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800835 final int requestCode, final int resultCode, final Intent data) {
836 handleActivityResult(requestCode, resultCode, data);
837 if (mLauncherCallbacks != null) {
838 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
839 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 }
841
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600842 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700843 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600844 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700845 PendingRequestArgs pendingArgs = mPendingRequestArgs;
846 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
847 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
848 setWaitingForResult(null);
849
Sunny Goyal28c6b962015-10-12 11:42:05 -0700850 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700851 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700852 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700853 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700854 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700855 Intent intent = pendingArgs.getPendingIntent();
856
Sunny Goyal28c6b962015-10-12 11:42:05 -0700857 if (grantResults.length > 0
858 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700859 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700860 } else {
861 // TODO: Show a snack bar with link to settings
862 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700863 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700864 }
865 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600866 if (mLauncherCallbacks != null) {
867 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
868 grantResults);
869 }
870 }
871
Adam Cohendb364c32014-05-20 14:23:40 -0700872 /**
873 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
874 *
875 * @param screenId the screen id to check
876 * @return the new screen, or screenId if it exists
877 */
878 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800879 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700880 if (dropLayout == null) {
881 // it's possible that the add screen was removed because it was
882 // empty and a re-bind occurred
883 mWorkspace.addExtraEmptyScreen();
884 return mWorkspace.commitExtraEmptyScreen();
885 } else {
886 return screenId;
887 }
888 }
889
Sunny Goyal2100c782016-08-22 16:00:03 -0700890 @Thunk void completeTwoStageWidgetDrop(
891 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
892 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 Runnable onCompleteRunnable = null;
894 int animationType = 0;
895
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 if (resultCode == RESULT_OK) {
898 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
899 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800900 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700901 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 onCompleteRunnable = new Runnable() {
903 @Override
904 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700905 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700906 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
907 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 }
909 };
910 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800911 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700915 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
917 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700918 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700919 // The animated view may be null in the case of a rotation during widget configuration
920 onCompleteRunnable.run();
921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923
924 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700925 protected void onStop() {
926 super.onStop();
927 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700928
929 if (mLauncherCallbacks != null) {
930 mLauncherCallbacks.onStop();
931 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700932
Sunny Goyalf5e37442016-11-02 10:31:24 -0700933 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700934 mAppWidgetHost.stopListening();
935 }
Tony Wickham010d2552017-01-20 08:15:28 -0800936
937 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700938 }
939
940 @Override
941 protected void onStart() {
942 super.onStart();
943 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700944
945 if (mLauncherCallbacks != null) {
946 mLauncherCallbacks.onStart();
947 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700948
Sunny Goyalf5e37442016-11-02 10:31:24 -0700949 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700950 mAppWidgetHost.startListening();
951 }
Tony Wickham010d2552017-01-20 08:15:28 -0800952
953 if (!isWorkspaceLoading()) {
954 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
955 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700956 }
957
958 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200960 long startTime = 0;
961 if (DEBUG_RESUME_TIME) {
962 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400963 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200964 }
Adam Cohen9211d422014-10-07 18:14:53 -0700965
966 if (mLauncherCallbacks != null) {
967 mLauncherCallbacks.preOnResume();
968 }
969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700971 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700972
Winson Chung4a2afa32012-07-19 14:53:05 -0700973 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700974 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700975 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800976 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700977 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700978 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700979 // view after launching an app, as they may be depending on the UI to be static to
980 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700981 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700982 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
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 Goyal756adbc2015-04-16 15:20:51 -07001055 if (!isWorkspaceLoading()) {
1056 // Process any items that were added while Launcher was away.
1057 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001058
1059 // Refresh shortcuts if the permission changed.
1060 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001061 }
Adam Cohen9211d422014-10-07 18:14:53 -07001062
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001063 if (shouldShowDiscoveryBounce()) {
1064 mAllAppsController.showDiscoveryBounce();
1065 }
1066 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001067 if (mLauncherCallbacks != null) {
1068 mLauncherCallbacks.onResume();
1069 }
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
1075 InstallShortcutReceiver.enableInstallQueue();
1076
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
Jun Mukai8af0cd82015-05-12 12:32:12 -07001133 public interface LauncherSearchCallbacks {
1134 /**
1135 * Called when the search overlay is shown.
1136 */
1137 public void onSearchOverlayOpened();
1138
1139 /**
1140 * Called when the search overlay is dismissed.
1141 */
1142 public void onSearchOverlayClosed();
1143 }
1144
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001146 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147 }
1148
1149 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1150
Sunny Goyalc86df472016-02-25 09:19:38 -08001151 public void onScrollChanged(float progress) {
1152 if (mWorkspace != null) {
1153 mWorkspace.onOverlayScrollChanged(progress);
1154 }
1155 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001156 }
1157
Jorim Jaggid017f882014-01-14 17:08:48 -08001158 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001159 if (mLauncherCallbacks != null) {
1160 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001161 } else {
1162 // On devices with a locked orientation, we will at least have the allow rotation
1163 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001164 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001165 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001166 }
1167
Adam Cohen9211d422014-10-07 18:14:53 -07001168 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001169 CustomContentCallbacks callbacks, String description) {
1170 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001171 }
1172
Adam Cohenedb40762013-07-18 16:45:45 -07001173 // The custom content needs to offset its content to account for the QSB
1174 public int getTopOffsetForCustomContent() {
1175 return mWorkspace.getPaddingTop();
1176 }
1177
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001178 @Override
1179 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001181 if (mModel.isCurrentCallbacks(this)) {
1182 mModel.stopLoader();
1183 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001184 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1185
Romain Guy13c2e7b2010-03-10 19:45:00 -08001186 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001187 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001189 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001190 @Override
1191 public void onWindowFocusChanged(boolean hasFocus) {
1192 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001193 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001194
1195 if (mLauncherCallbacks != null) {
1196 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1197 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001198 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 private boolean acceptFilter() {
1201 final InputMethodManager inputManager = (InputMethodManager)
1202 getSystemService(Context.INPUT_METHOD_SERVICE);
1203 return !inputManager.isFullscreenMode();
1204 }
1205
1206 @Override
1207 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001208 final int uniChar = event.getUnicodeChar();
1209 final boolean handled = super.onKeyDown(keyCode, event);
1210 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1211 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1213 keyCode, event);
1214 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001215 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001216 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001217 // showSearchDialog()
1218 // If there are multiple keystrokes before the search dialog takes focus,
1219 // onSearchRequested() will be called for every keystroke,
1220 // but it is idempotent, so it's fine.
1221 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223 }
1224
Joe Onorato8a9625e2010-01-28 15:55:35 -08001225 // Eat the long press event so the keyboard doesn't come up.
1226 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1227 return true;
1228 }
1229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 return handled;
1231 }
1232
Winson46163472015-09-22 17:31:56 -07001233 @Override
1234 public boolean onKeyUp(int keyCode, KeyEvent event) {
1235 if (keyCode == KeyEvent.KEYCODE_MENU) {
1236 // Ignore the menu key if we are currently dragging or are on the custom content screen
1237 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001238 // Close any open floating view
1239 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001240
Winson46163472015-09-22 17:31:56 -07001241 // Stop resizing any widgets
1242 mWorkspace.exitWidgetResizeMode();
1243
1244 // Show the overview mode if we are on the workspace
1245 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1246 !mWorkspace.isSwitchingState()) {
1247 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001248 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001249 }
1250 }
1251 return true;
1252 }
1253 return super.onKeyUp(keyCode, event);
1254 }
1255
Karl Rosaen138a0412009-04-23 19:00:21 -07001256 private String getTypedText() {
1257 return mDefaultKeySsb.toString();
1258 }
1259
Sunny Goyal6f28e712016-09-13 14:19:29 -07001260 @Override
1261 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001262 mDefaultKeySsb.clear();
1263 mDefaultKeySsb.clearSpans();
1264 Selection.setSelection(mDefaultKeySsb, 0);
1265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 /**
1268 * Restores the previous state, if it exists.
1269 *
1270 * @param savedState The previous state.
1271 */
1272 private void restoreState(Bundle savedState) {
1273 if (savedState == null) {
1274 return;
1275 }
1276
Sunny Goyalfe770c92016-09-27 14:38:58 -07001277 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1278 State[] stateValues = State.values();
1279 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1280 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001281 if (state == State.APPS || state == State.WIDGETS) {
1282 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001283 }
1284
Sunny Goyal2100c782016-08-22 16:00:03 -07001285 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1286 if (requestArgs != null) {
1287 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001289
1290 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292
1293 /**
1294 * Finds all the views we need and configure them properly.
1295 */
1296 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001297 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001298 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001299 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001300 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1301 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001302 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001303
Sunny Goyal784f9c32016-03-23 16:56:54 -07001304 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1305 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1306 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307
Winson Chung4c98d922011-05-31 16:50:48 -07001308 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001309 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001310
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 // Setup the hotseat
1312 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1313 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001314 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 }
1316
Winsone9f27272015-10-13 10:47:51 -07001317 // Setup the overview panel
1318 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001319
Winson Chung4c98d922011-05-31 16:50:48 -07001320 // Setup the workspace
1321 mWorkspace.setHapticFeedbackEnabled(false);
1322 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001323 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001324 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1325 // default state, otherwise we will update to the wrong offsets in RTL
1326 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001327 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001328 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001329
Tony Wickham34d2c912015-09-11 09:27:58 -07001330 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001331 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001332
Winson Chungef7f8742015-06-04 17:18:17 -07001333 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001334 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001335 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001336 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1337 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1338 } else {
1339 mAppsView.setSearchBarController(new DefaultAppSearchController());
1340 }
Craig Mautner360310b2012-10-26 15:13:08 -07001341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001343 mDragController.setMoveTarget(mWorkspace);
1344 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001345 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001346
Peter Schiller310a9882016-06-30 13:52:36 -07001347 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1348 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1349 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001350
Sunny Goyal322d5562015-06-25 19:35:49 -07001351 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1352 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355
Winsone9f27272015-10-13 10:47:51 -07001356 private void setupOverviewPanel() {
1357 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1358
Winsone9f27272015-10-13 10:47:51 -07001359 // Bind wallpaper button actions
1360 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301361 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001362 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001363 public void handleViewClick(View view) {
1364 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001365 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001366 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001367 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1368
1369 // Bind widget button actions
1370 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301371 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001372 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001373 public void handleViewClick(View view) {
1374 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001375 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001376 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001377 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1378
1379 // Bind settings actions
1380 View settingsButton = findViewById(R.id.settings_button);
1381 boolean hasSettings = hasSettings();
1382 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301383 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001384 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001385 public void handleViewClick(View view) {
1386 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001387 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001388 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001389 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1390 } else {
1391 settingsButton.setVisibility(View.GONE);
1392 }
1393
1394 mOverviewPanel.setAlpha(0f);
1395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001398 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001399 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001400 */
1401 public void setAllAppsButton(View allAppsButton) {
1402 mAllAppsButton = allAppsButton;
1403 }
1404
Sunny Goyalbb011da2016-06-15 15:42:29 -07001405 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001406 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001407 }
1408
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001409 public View getWidgetsButton() {
1410 return mWidgetsButton;
1411 }
1412
Anjali Koppal5ad44842014-03-10 20:34:39 -07001413 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 * Creates a view representing a shortcut.
1415 *
1416 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001418 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001419 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421
1422 /**
1423 * Creates a view representing a shortcut inflated from the specified resource.
1424 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 * @param parent The group the shortcut belongs to.
1426 * @param info The data structure describing the shortcut.
1427 *
1428 * @return A View inflated from layoutResId.
1429 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001430 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001431 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001432 parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001433 favorite.applyFromShortcutInfo(info);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001434 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001436 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 return favorite;
1438 }
1439
1440 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 * Add a shortcut to the workspace.
1442 *
1443 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001445 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001446 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001447 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001448 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001449
Sunny Goyal782f0c92017-01-19 10:27:54 -08001450 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001451 args.getPendingIntent().getComponent() == null) {
1452 return;
1453 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001454
Sunny Goyal782f0c92017-01-19 10:27:54 -08001455 ShortcutInfo info = null;
1456 if (Utilities.isAtLeastO()) {
Sunny Goyaldec3a902017-01-25 18:23:36 -08001457 info = LauncherAppsCompat.createShortcutInfoFromPinItemRequest(
1458 this, PinItemRequestCompat.getPinItemRequest(data));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001459 }
1460
1461 if (info == null) {
1462 info = InstallShortcutReceiver.fromShortcutIntent(this, data);
1463
1464 if (info == null || !PackageManagerHelper.hasPermissionForActivity(
1465 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1466 // The app is trying to add a shortcut without sufficient permissions
1467 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1468 return;
1469 }
1470 }
1471
1472 final View view = createShortcut(info);
Sunny Goyal5c97f512015-05-19 16:03:28 -07001473 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001474 // First we check if we already know the exact location where we want to add this item.
1475 if (cellX >= 0 && cellY >= 0) {
1476 cellXY[0] = cellX;
1477 cellXY[1] = cellY;
1478 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001479
1480 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001481 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001482 true, null,null)) {
1483 return;
1484 }
1485 DragObject dragObject = new DragObject();
1486 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001487 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1488 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001489 return;
1490 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001491 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001492 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001493 }
1494
1495 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001496 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001497 return;
1498 }
1499
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001500 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301501 mWorkspace.addInScreen(view, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
1503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001505 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001507 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001509 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001510 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1511
Adam Cohened66b2b2012-01-23 17:28:51 -08001512 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001513 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001514 }
Romain Guycbb89e42009-06-08 15:52:54 -07001515
Adam Cohen59400422014-03-05 18:07:04 -08001516 if (appWidgetInfo.isCustomWidget) {
1517 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001518 }
1519
Adam Cohen59400422014-03-05 18:07:04 -08001520 LauncherAppWidgetInfo launcherInfo;
1521 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001522 launcherInfo.spanX = itemInfo.spanX;
1523 launcherInfo.spanY = itemInfo.spanY;
1524 launcherInfo.minSpanX = itemInfo.minSpanX;
1525 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyala52ecb02016-12-16 15:04:51 -08001526 launcherInfo.user = appWidgetInfo.getUser();
Romain Guycbb89e42009-06-08 15:52:54 -07001527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001529 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
Sunny Goyal2100c782016-08-22 16:00:03 -07001531 if (hostView == null) {
1532 // Perform actual inflation because we're live
1533 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001535 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301536 prepareAppWidget(hostView, launcherInfo);
1537 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
Romain Guycbb89e42009-06-08 15:52:54 -07001539
Sunny Goyald5462aa2016-11-22 16:52:39 +05301540 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001541 hostView.setTag(item);
1542 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001543 hostView.setFocusable(true);
1544 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001545 }
1546
Adam Cohended9f8d2010-11-03 13:25:16 -07001547 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1548 @Override
1549 public void onReceive(Context context, Intent intent) {
1550 final String action = intent.getAction();
1551 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001552 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001553
Winson Chungc100e8e2011-08-09 16:02:43 -07001554 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001555 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001556 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001557 if (!showWorkspace(false)) {
1558 // If we are already on the workspace, then manually reset all apps
1559 mAppsView.reset();
1560 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001561 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001562 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001563 }
1564 }
1565 };
1566
Tony Wickham010d2552017-01-20 08:15:28 -08001567 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1568 Runnable r = new Runnable() {
1569 @Override
1570 public void run() {
1571 mWorkspace.updateIconBadges(updatedBadges);
1572 mAppsView.updateIconBadges(updatedBadges);
1573 }
1574 };
1575 if (!waitUntilResume(r)) {
1576 r.run();
1577 }
1578 }
1579
Adam Cohended9f8d2010-11-03 13:25:16 -07001580 @Override
1581 public void onAttachedToWindow() {
1582 super.onAttachedToWindow();
1583
1584 // Listen for broadcasts related to user-presence
1585 final IntentFilter filter = new IntentFilter();
1586 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001587 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001588 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001589 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001590
1591 if (mLauncherCallbacks != null) {
1592 mLauncherCallbacks.onAttachedToWindow();
1593 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 }
1595
1596 @Override
1597 public void onDetachedFromWindow() {
1598 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001599 if (mAttached) {
1600 unregisterReceiver(mReceiver);
1601 mAttached = false;
1602 }
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
Sunny Goyal6178f132016-07-11 17:30:03 -07001664 public View getQsbContainer() {
1665 return mQsbContainer;
1666 }
1667
Winson Chunga6945242014-01-08 14:04:34 -08001668 public Hotseat getHotseat() {
1669 return mHotseat;
1670 }
1671
Jorim Jaggid017f882014-01-14 17:08:48 -08001672 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001673 return mOverviewPanel;
1674 }
1675
Sunny Goyal47328fd2016-05-25 18:56:41 -07001676 public DropTargetBar getDropTargetBar() {
1677 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001678 }
1679
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001680 public LauncherAppWidgetHost getAppWidgetHost() {
1681 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
Romain Guycbb89e42009-06-08 15:52:54 -07001683
Winson Chunga9abd0e2010-10-27 17:18:37 -07001684 public LauncherModel getModel() {
1685 return mModel;
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) {
1755 mAppsView.scrollToTop();
1756 }
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 }
Sunny Goyalb38fab72017-01-20 19:32:31 -08001766
1767 Parcelable dragExtra = intent
1768 .getParcelableExtra(PinItemDragListener.EXTRA_PIN_ITEM_DRAG_LISTENER);
1769 if (dragExtra instanceof PinItemDragListener) {
1770 PinItemDragListener dragListener = (PinItemDragListener) dragExtra;
1771 dragListener.setLauncher(this);
1772 mDragLayer.setOnDragListener(dragListener);
1773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
Adam Cohened307df2013-10-02 09:37:31 -07001775
Adam Cohen9211d422014-10-07 18:14:53 -07001776 if (mLauncherCallbacks != null) {
1777 mLauncherCallbacks.onNewIntent(intent);
1778 }
Winson15f8b172015-08-19 11:02:39 -07001779
1780 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1781 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1782 // animation.
1783 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001784 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001785 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001786 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1787 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001788
1789 // We use this flag to suppress noisy callbacks above custom content state
1790 // from onResume.
1791 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001792 mWorkspace.post(new Runnable() {
1793 @Override
1794 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001795 if (mWorkspace != null) {
1796 mWorkspace.moveToDefaultScreen(true);
1797 }
Winson15f8b172015-08-19 11:02:39 -07001798 }
1799 });
1800 }
1801 }
1802
1803 if (DEBUG_RESUME_TIME) {
1804 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1805 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001806 }
1807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001809 public void onRestoreInstanceState(Bundle state) {
1810 super.onRestoreInstanceState(state);
1811 for (int page: mSynchronouslyBoundPages) {
1812 mWorkspace.restoreInstanceStateForChild(page);
1813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
1815
1816 @Override
1817 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001818 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001819 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1820 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001821
Adam Cohen21cd0022013-10-09 18:57:02 -07001822 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001823 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824
Michael Jurka883f55b2010-10-21 15:47:14 -07001825 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001826 // We close any open folders and shortcut containers since they will not be re-opened,
1827 // and we need to make sure this state is reflected.
1828 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001829
Sunny Goyal2100c782016-08-22 16:00:03 -07001830 if (mPendingRequestArgs != null) {
1831 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1832 }
1833 if (mPendingActivityResult != null) {
1834 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
1836
Adam Cohen9211d422014-10-07 18:14:53 -07001837 if (mLauncherCallbacks != null) {
1838 mLauncherCallbacks.onSaveInstanceState(outState);
1839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841
1842 @Override
1843 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001845
Michael Jurka9d906c72011-10-14 06:25:36 -07001846 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001847 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001848
Winson Chungcd2b0142011-06-08 16:02:26 -07001849 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001850 // It's possible to receive onDestroy after a new Launcher activity has
1851 // been created. In this case, don't interfere with the new Launcher.
1852 if (mModel.isCurrentCallbacks(this)) {
1853 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001854 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001855 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001856
Sunny Goyal745bad92016-05-02 10:54:12 -07001857 if (mRotationPrefChangeHandler != null) {
1858 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1859 }
1860
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001862 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001864 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001866 mAppWidgetHost = null;
1867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 TextKeyListener.getInstance().release();
1869
Tony Wickhame2217252016-03-22 16:34:23 -07001870 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1871 .removeAccessibilityStateChangeListener(this);
1872
Michael Jurka2ecf9952012-06-18 12:52:28 -07001873 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001874
1875 if (mLauncherCallbacks != null) {
1876 mLauncherCallbacks.onDestroy();
1877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
1879
Sunny Goyalae502842016-06-17 08:43:56 -07001880 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1881 return mAccessibilityDelegate;
1882 }
1883
Adam Cohena9cf38f2011-05-02 15:36:58 -07001884 public DragController getDragController() {
1885 return mDragController;
1886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001889 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001890 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001891 }
1892
1893 @Override
1894 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1895 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001896 try {
1897 super.startIntentSenderForResult(intent, requestCode,
1898 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1899 } catch (IntentSender.SendIntentException e) {
1900 throw new ActivityNotFoundException();
1901 }
1902 }
1903
Winson Chung70d72102011-08-12 11:24:35 -07001904 /**
1905 * Indicates that we want global search for this activity by setting the globalSearch
1906 * argument for {@link #startSearch} to true.
1907 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001909 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001911
Karl Rosaen138a0412009-04-23 19:00:21 -07001912 if (initialQuery == null) {
1913 // Use any text typed in the launcher as the initial query
1914 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 if (appSearchData == null) {
1917 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001918 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001920
Sunny Goyal6f28e712016-09-13 14:19:29 -07001921 if (mLauncherCallbacks == null ||
1922 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1923 // Starting search from the callbacks failed. Start the default global search.
1924 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001925 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001926
1927 // We need to show the workspace after starting the search
1928 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001929 }
1930
Ian Parkinson047036e2014-09-01 15:40:53 +01001931 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001932 * Starts the global search activity. This code is a copied from SearchManager
1933 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001934 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001935 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001936 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001937 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1938 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1939 if (globalSearchActivity == null) {
1940 Log.w(TAG, "No global search activity found.");
1941 return;
1942 }
1943 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1944 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1945 intent.setComponent(globalSearchActivity);
1946 // Make sure that we have a Bundle to put source in
1947 if (appSearchData == null) {
1948 appSearchData = new Bundle();
1949 } else {
1950 appSearchData = new Bundle(appSearchData);
1951 }
Adam Cohen9211d422014-10-07 18:14:53 -07001952 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001953 if (!appSearchData.containsKey("source")) {
1954 appSearchData.putString("source", getPackageName());
1955 }
1956 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1957 if (!TextUtils.isEmpty(initialQuery)) {
1958 intent.putExtra(SearchManager.QUERY, initialQuery);
1959 }
1960 if (selectInitialQuery) {
1961 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1962 }
1963 intent.setSourceBounds(sourceBounds);
1964 try {
1965 startActivity(intent);
1966 } catch (ActivityNotFoundException ex) {
1967 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
Yura4f93ec62014-02-04 14:15:21 +00001971 public boolean isOnCustomContent() {
1972 return mWorkspace.isOnOrMovingToCustomContent();
1973 }
1974
Winson Chung70d72102011-08-12 11:24:35 -07001975 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001976 public boolean onPrepareOptionsMenu(Menu menu) {
1977 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001978 if (mLauncherCallbacks != null) {
1979 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1980 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001981 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001984 @Override
1985 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001986 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001987 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001988 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001989 }
1990
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001992 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 }
1994
Adam Cohen517a7f52014-03-01 12:12:59 -08001995 public boolean isWorkspaceLoading() {
1996 return mWorkspaceLoading;
1997 }
1998
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001999 private void setWorkspaceLoading(boolean value) {
2000 boolean isLocked = isWorkspaceLocked();
2001 mWorkspaceLoading = value;
2002 if (isLocked != isWorkspaceLocked()) {
2003 onWorkspaceLockedChanged();
2004 }
2005 }
2006
Sunny Goyal04a324a2017-01-20 21:08:59 -08002007 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002008 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002009 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002010 if (isLocked != isWorkspaceLocked()) {
2011 onWorkspaceLockedChanged();
2012 }
2013 }
2014
Adam Cohen9211d422014-10-07 18:14:53 -07002015 protected void onWorkspaceLockedChanged() {
2016 if (mLauncherCallbacks != null) {
2017 mLauncherCallbacks.onWorkspaceLockedChanged();
2018 }
2019 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002020
Sunny Goyal2100c782016-08-22 16:00:03 -07002021 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002022 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002023 if (LOGD) {
2024 Log.d(TAG, "Adding widget from drop");
2025 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002026 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002027 }
2028
Sunny Goyal2100c782016-08-22 16:00:03 -07002029 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002030 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
2031 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
2032 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07002033
Adam Cohenad4e15c2013-10-17 16:21:35 -07002034 Runnable onComplete = new Runnable() {
2035 @Override
2036 public void run() {
2037 // Exit spring loaded mode if necessary after adding the widget
2038 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2039 null);
2040 }
2041 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08002042 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07002043 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
2045 }
Romain Guycbb89e42009-06-08 15:52:54 -07002046
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002047 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002048 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002049 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002050 mWorkspace.moveToCustomContentScreen(animate);
2051 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002052
2053 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2054 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002055 info.container = container;
2056 info.screenId = screenId;
2057 if (cell != null) {
2058 info.cellX = cell[0];
2059 info.cellY = cell[1];
2060 }
2061 info.spanX = spanX;
2062 info.spanY = spanY;
2063
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002064 switch (info.itemType) {
2065 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2066 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002067 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002068 break;
2069 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08002070 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002071 break;
2072 default:
2073 throw new IllegalStateException("Unknown item type: " + info.itemType);
2074 }
2075 }
2076
Adam Cohenfbba09b2011-07-18 21:43:05 -07002077 /**
2078 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002079 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08002080 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002081 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2082 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08002083 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
2084 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
2085 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002086 }
2087
Adam Cohenfbba09b2011-07-18 21:43:05 -07002088 /**
2089 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002090 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002091 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002092 AppWidgetHostView hostView = info.boundWidget;
2093 int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08002094 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08002095 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002096 // In the case where we've prebound the widget, we remove it from the DragLayer
2097 if (LOGD) {
2098 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2099 }
2100 getDragLayer().removeView(hostView);
2101
Adam Cohened66b2b2012-01-23 17:28:51 -08002102 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08002103 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002104
2105 // Clear the boundWidget so that it doesn't get destroyed.
2106 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002107 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002108 // In this case, we either need to start an activity to get permission to bind
2109 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002110 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002111 Bundle options = info.bindOptions;
2112
Sunny Goyalffe83f12014-08-14 17:39:34 -07002113 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2114 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002115 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002116 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07002117 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002118 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07002119 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002120 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002121 }
2122
Adam Cohendcd297f2013-06-18 13:13:40 -07002123 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002125 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 folderInfo.title = getText(R.string.folder_name);
2127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002129 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2130 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131
2132 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002133 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302134 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07002135 // Force measure the new folder icon
2136 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2137 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002138 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 }
Romain Guycbb89e42009-06-08 15:52:54 -07002140
Winsonc0b52fe2015-09-09 16:38:15 -07002141 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002142 * Unbinds the view for the specified item, and removes the item and all its children.
2143 *
2144 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002145 * @param itemInfo the {@link ItemInfo} for this view.
2146 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002147 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002148 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002149 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002150 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002151 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2152 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002153 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002154 } else {
2155 mWorkspace.removeWorkspaceItem(v);
2156 }
Winsonc0b52fe2015-09-09 16:38:15 -07002157 if (deleteFromDb) {
2158 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2159 }
2160 } else if (itemInfo instanceof FolderInfo) {
2161 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002162 if (v instanceof FolderIcon) {
2163 ((FolderIcon) v).removeListeners();
2164 }
Winsonc0b52fe2015-09-09 16:38:15 -07002165 mWorkspace.removeWorkspaceItem(v);
2166 if (deleteFromDb) {
2167 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2168 }
2169 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2170 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002171 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002172 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002173 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002174 }
2175 } else {
2176 return false;
2177 }
2178 return true;
2179 }
2180
2181 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002182 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002183 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002184 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002185 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002186 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002187 // Deleting an app widget ID is a void call but writes to disk before returning
2188 // to the caller...
2189 new AsyncTask<Void, Void, Void>() {
2190 public Void doInBackground(Void ... args) {
2191 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2192 return null;
2193 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002194 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002195 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002196 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002197 }
2198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 @Override
2200 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002201 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
2203
Joe Onorato88ec0992009-11-19 13:16:06 -08002204 @Override
2205 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002206 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2207 return;
2208 }
2209
Sunny Goyal45478022015-06-08 16:52:41 -07002210 if (mDragController.isDragging()) {
2211 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002212 return;
2213 }
2214
Jon Mirandafeba90f2016-10-06 10:53:29 -07002215 // Note: There should be at most one log per method call. This is enforced implicitly
2216 // by using if-else statements.
2217 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002218 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2219 if (topView != null) {
2220 if (topView.getActiveTextView() != null) {
2221 topView.getActiveTextView().dispatchBackKey();
2222 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002223 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302224 ued.logActionCommand(Action.Command.BACK,
2225 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002226 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302227 ued.logActionCommand(Action.Command.BACK,
2228 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002229 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002230 topView.close(true);
2231 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002232 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302233 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002234 showWorkspace(true);
2235 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302236 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002237 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002238 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302239 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002240 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002241 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002242 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002243 mWorkspace.exitWidgetResizeMode();
2244
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002245 // Back button is a no-op here, but give at least some feedback for the button press
2246 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002247 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002248 }
2249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 * Launches the intent referred by the clicked shortcut.
2252 *
2253 * @param v The view representing the clicked shortcut.
2254 */
2255 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002256 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2257 // view has detached (it's possible for this to happen if the view is removed mid touch).
2258 if (v.getWindowToken() == null) {
2259 return;
2260 }
2261
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002262 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002263 return;
2264 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002265
Adam Cohen1697b792013-09-17 19:08:21 -07002266 if (v instanceof Workspace) {
2267 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002268 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002269 }
2270 return;
2271 }
2272
2273 if (v instanceof CellLayout) {
2274 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002275 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2276 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002277 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002278 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002279 }
2280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002282 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002283 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002285 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002286 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002287 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002288 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2289 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002290 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002291 } else if (tag instanceof AppInfo) {
2292 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002293 } else if (tag instanceof LauncherAppWidgetInfo) {
2294 if (v instanceof PendingAppWidgetHostView) {
2295 onClickPendingWidget((PendingAppWidgetHostView) v);
2296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 }
2298 }
2299
Sunny Goyal70660032015-05-14 00:07:08 -07002300 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002301 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002302 return false;
2303 }
2304
Michael Jurkaaf442092010-06-10 17:01:57 -07002305 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002306 * Event handler for the app widget view which has not fully restored.
2307 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002308 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002309 if (mIsSafeModeEnabled) {
2310 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2311 return;
2312 }
2313
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002314 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002315 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002316 LauncherAppWidgetProviderInfo appWidgetInfo =
2317 mAppWidgetManager.findProvider(info.providerName, info.user);
2318 if (appWidgetInfo == null) {
2319 return;
2320 }
2321 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2322
Sunny Goyald478c832016-04-01 12:04:16 -07002323 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2324 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2325 // This should not happen, as we make sure that an Id is allocated during bind.
2326 return;
2327 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002328 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2329 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002330 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002331 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002332 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002333 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002334 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002335 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002336 }
2337 }
2338
2339 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002340 * Event handler for the "grid" button that appears on the home screen, which
2341 * enters all apps mode.
2342 *
2343 * @param v The view that was clicked.
2344 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002345 protected void onClickAllAppsButton(View v) {
2346 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002347 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302348 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2349 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002350 showAppsView(true /* animated */, true /* updatePredictedApps */,
2351 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002352 }
2353 }
2354
2355 protected void onLongClickAllAppsButton(View v) {
2356 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2357 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302358 getUserEventDispatcher().logActionOnControl(Action.Touch.LONGPRESS,
2359 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002360 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002361 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002362 }
2363 }
2364
Sunny Goyale6e72002017-01-12 16:55:36 -08002365 private void onClickPendingAppItem(final View v, final String packageName,
2366 boolean downloadStarted) {
2367 if (downloadStarted) {
2368 // If the download has started, simply direct to the market app.
2369 startMarketIntentForPackage(v, packageName);
2370 return;
2371 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002372 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002373 .setTitle(R.string.abandoned_promises_title)
2374 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002375 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2376 @Override
2377 public void onClick(DialogInterface dialogInterface, int i) {
2378 startMarketIntentForPackage(v, packageName);
2379 }
2380 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002381 .setNeutralButton(R.string.abandoned_clean_this,
2382 new DialogInterface.OnClickListener() {
2383 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002384 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002385 mWorkspace.removeAbandonedPromise(packageName, user);
2386 }
2387 })
2388 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002389 }
2390
2391 private void startMarketIntentForPackage(View v, String packageName) {
2392 ItemInfo item = (ItemInfo) v.getTag();
2393 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2394 boolean success = startActivitySafely(v, intent, item);
2395 if (success && v instanceof BubbleTextView) {
2396 mWaitingForResume = (BubbleTextView) v;
2397 mWaitingForResume.setStayPressed(true);
2398 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002399 }
2400
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002401 /**
2402 * Event handler for an app shortcut click.
2403 *
2404 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2405 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002406 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002407 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2408 Object tag = v.getTag();
2409 if (!(tag instanceof ShortcutInfo)) {
2410 throw new IllegalArgumentException("Input must be a Shortcut");
2411 }
2412
2413 // Open shortcut
2414 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002415
2416 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002417 if ((shortcut.isDisabled &
2418 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2419 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2420 // If the app is only disabled because of the above flags, launch activity anyway.
2421 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002422 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002423 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2424 // Use a message specific to this shortcut, if it has one.
2425 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2426 return;
2427 }
2428 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002429 int error = R.string.activity_not_available;
2430 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2431 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002432 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2433 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002434 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002435 }
2436 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2437 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002438 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002439 }
2440
Chris Wren40c5ed32014-06-24 18:24:23 -04002441 // Check for abandoned promise
Sunny Goyale6e72002017-01-12 16:55:36 -08002442 if ((v instanceof BubbleTextView) && shortcut.isPromise()) {
2443 String packageName = shortcut.intent.getComponent() != null ?
2444 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2445 if (!TextUtils.isEmpty(packageName)) {
2446 onClickPendingAppItem(v, packageName,
2447 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2448 return;
2449 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002450 }
2451
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002452 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002453 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002454 }
2455
Sunny Goyala7ce1662016-05-31 15:01:35 -07002456 private void startAppShortcutOrInfoActivity(View v) {
2457 ItemInfo item = (ItemInfo) v.getTag();
2458 Intent intent = item.getIntent();
2459 if (intent == null) {
2460 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002461 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002462 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002463 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002464
2465 if (success && v instanceof BubbleTextView) {
2466 mWaitingForResume = (BubbleTextView) v;
2467 mWaitingForResume.setStayPressed(true);
2468 }
2469 }
2470
2471 /**
2472 * Event handler for a folder icon click.
2473 *
2474 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2475 */
2476 protected void onClickFolderIcon(View v) {
2477 if (LOGD) Log.d(TAG, "onClickFolder");
2478 if (!(v instanceof FolderIcon)){
2479 throw new IllegalArgumentException("Input must be a FolderIcon");
2480 }
2481
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002482 Folder folder = ((FolderIcon) v).getFolder();
2483 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002484 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002485 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002486 }
Michael Jurka5130e402011-10-13 04:55:35 -07002487 }
2488
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002489 /**
2490 * Event handler for the (Add) Widgets button that appears after a long press
2491 * on the home screen.
2492 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002493 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002494 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002495 if (mIsSafeModeEnabled) {
2496 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2497 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002498 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002499 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002500 }
2501
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 /**
2503 * Event handler for the wallpaper picker button that appears after a long press
2504 * on the home screen.
2505 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002506 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002507 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002508 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2509 return;
2510 }
2511
Tony Wickham785f7a52015-08-31 17:28:32 -07002512 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2513 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002514 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002515 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002516 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002517
2518 String pickerPackage = getString(R.string.wallpaper_picker_package);
2519 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2520 if (!hasTargetPackage) {
2521 intent.setPackage(pickerPackage);
2522 }
2523
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002524 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002525 try {
2526 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2527 // If there is no target package, use the default intent chooser animation
2528 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2529 } catch (ActivityNotFoundException e) {
2530 setWaitingForResult(null);
2531 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2532 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002533 }
2534
2535 /**
2536 * Event handler for a click on the settings button that appears after a long press
2537 * on the home screen.
2538 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002539 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002540 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002541 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2542 .setPackage(getPackageName());
2543 intent.setSourceBounds(getViewBounds(v));
2544 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 }
2546
Adam Cohen61f560d2013-09-30 15:58:20 -07002547 public View.OnTouchListener getHapticFeedbackTouchListener() {
2548 if (mHapticFeedbackTouchListener == null) {
2549 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002550 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002551 @Override
2552 public boolean onTouch(View v, MotionEvent event) {
2553 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2554 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2555 }
2556 return false;
2557 }
2558 };
2559 }
2560 return mHapticFeedbackTouchListener;
2561 }
2562
Tony Wickhame2217252016-03-22 16:34:23 -07002563 @Override
2564 public void onAccessibilityStateChanged(boolean enabled) {
2565 mDragLayer.onAccessibilityStateChanged(enabled);
2566 }
2567
Sunny Goyal06e21a22016-08-11 16:02:02 -07002568 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002569 if (isOnCustomContent()) {
2570 // Custom content screen doesn't participate in drag and drop. If on custom
2571 // content screen, move to default.
2572 moveWorkspaceToDefaultScreen();
2573 }
Adam Cohen9211d422014-10-07 18:14:53 -07002574 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002575
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002576 /**
2577 * Called when the user stops interacting with the launcher.
2578 * This implies that the user is now on the homescreen and is not doing housekeeping.
2579 */
Adam Cohen9211d422014-10-07 18:14:53 -07002580 protected void onInteractionEnd() {
2581 if (mLauncherCallbacks != null) {
2582 mLauncherCallbacks.onInteractionEnd();
2583 }
2584 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002585
2586 /**
2587 * Called when the user starts interacting with the launcher.
2588 * The possible interactions are:
2589 * - open all apps
2590 * - reorder an app shortcut, or a widget
2591 * - open the overview mode.
2592 * This is a good time to stop doing things that only make sense
2593 * when the user is on the homescreen and not doing housekeeping.
2594 */
Adam Cohen9211d422014-10-07 18:14:53 -07002595 protected void onInteractionBegin() {
2596 if (mLauncherCallbacks != null) {
2597 mLauncherCallbacks.onInteractionBegin();
2598 }
2599 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002600
Winson Chungcd99cd32015-04-29 11:03:24 -07002601 /** Updates the interaction state. */
2602 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002603 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002604 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002605 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2606 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002607 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002608 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002609 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002610 onInteractionEnd();
2611 }
2612 }
2613
Sunny Goyala7ce1662016-05-31 15:01:35 -07002614 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002616 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2617 try {
2618 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2619 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2620 // is enabled by default on NYC.
2621 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2622 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002623
2624 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2625 String id = ((ShortcutInfo) info).getDeepShortcutId();
2626 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302627 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002628 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002629 } else {
2630 // Could be launching some bookkeeping activity
2631 startActivity(intent, optsBundle);
2632 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002633 } finally {
2634 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002637 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2638 // corresponding permission. Show the appropriate permission prompt if that
2639 // is the case.
2640 if (intent.getComponent() == null
2641 && Intent.ACTION_CALL.equals(intent.getAction())
2642 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2643 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002644
2645 setWaitingForResult(PendingRequestArgs
2646 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002647 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2648 REQUEST_PERMISSION_CALL_PHONE);
2649 } else {
2650 // No idea why this was thrown.
2651 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653 }
2654 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002655
Sunny Goyalfe770c92016-09-27 14:38:58 -07002656 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002657 private Bundle getActivityLaunchOptions(View v) {
2658 if (Utilities.ATLEAST_MARSHMALLOW) {
2659 int left = 0, top = 0;
2660 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2661 if (v instanceof TextView) {
2662 // Launch from center of icon, not entire view
2663 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2664 if (icon != null) {
2665 Rect bounds = icon.getBounds();
2666 left = (width - bounds.width()) / 2;
2667 top = v.getPaddingTop();
2668 width = bounds.width();
2669 height = bounds.height();
2670 }
2671 }
2672 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2673 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2674 // On L devices, we use the device default slide-up transition.
2675 // On L MR1 devices, we use a custom version of the slide-up transition which
2676 // doesn't have the delay present in the device default.
2677 return ActivityOptions.makeCustomAnimation(
2678 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2679 }
2680 return null;
2681 }
2682
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002683 private Rect getViewBounds(View v) {
2684 int[] pos = new int[2];
2685 v.getLocationOnScreen(pos);
2686 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2687 }
2688
Sunny Goyala7ce1662016-05-31 15:01:35 -07002689 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002690 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2691 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2692 return false;
2693 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002694 // Only launch using the new animation if the shortcut has not opted out (this is a
2695 // private contract between launcher and may be ignored in the future).
2696 boolean useLaunchAnimation = (v != null) &&
2697 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2698 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2699
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002700 UserHandle user = null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002701 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2702 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2703 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002704 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002705
2706 // Prepare intent
2707 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2708 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002709 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002710 }
2711 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002712 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2713 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002714 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002715 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002716 // Shortcuts need some special checks due to legacy reasons.
2717 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002718 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002719 // Could be launching some bookkeeping activity
2720 startActivity(intent, optsBundle);
2721 } else {
2722 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2723 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2724 }
2725 return true;
2726 } catch (ActivityNotFoundException|SecurityException e) {
2727 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2728 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2729 }
2730 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002731 }
2732
Tony Wickhamdadb3042016-02-24 11:07:00 -08002733 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002734 public boolean dispatchTouchEvent(MotionEvent ev) {
2735 mLastDispatchTouchEventX = ev.getX();
2736 return super.dispatchTouchEvent(ev);
2737 }
2738
2739 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002741 if (!isDraggingEnabled()) return false;
2742 if (isWorkspaceLocked()) return false;
2743 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002745 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2746 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002747 onLongClickAllAppsButton(v);
2748 return true;
2749 }
2750
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002751
Jon Miranda51f037d2016-11-07 08:37:20 -08002752 boolean ignoreLongPressToOverview =
2753 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002754
Adam Cohen1697b792013-09-17 19:08:21 -07002755 if (v instanceof Workspace) {
2756 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002757 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302758 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2759 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002760 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002761 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002762 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2763 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2764 return true;
2765 } else {
2766 return false;
2767 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002768 } else {
2769 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002770 }
Adam Cohen1697b792013-09-17 19:08:21 -07002771 }
2772
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002773 CellLayout.CellInfo longClickCellInfo = null;
2774 View itemUnderLongClick = null;
2775 if (v.getTag() instanceof ItemInfo) {
2776 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002777 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002778 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002779 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 }
2781
Winson Chung3d503fb2011-07-13 17:25:49 -07002782 // The hotseat touch handling does not go through Workspace, and we always allow long press
2783 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002784 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002785 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002786 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002787 if (mWorkspace.isInOverviewMode()) {
2788 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302789 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2790 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002791 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002792 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002793 return false;
2794 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302795 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2796 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002797 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002798 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002799 }
Jon Miranda379198e2016-09-23 08:54:40 -07002800 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2801 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002802 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002803 final boolean isAllAppsButton =
2804 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2805 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2806 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002807 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002808 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002809 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 }
2811 }
2812 }
2813 return true;
2814 }
2815
Winson Chung3d503fb2011-07-13 17:25:49 -07002816 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002817 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002818 return mHotseat != null && layout != null &&
2819 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2820 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002821
Winson Chung3d503fb2011-07-13 17:25:49 -07002822 /**
2823 * Returns the CellLayout of the specified container at the specified screen.
2824 */
Sunny Goyal92820592015-03-02 11:31:35 -08002825 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002826 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2827 if (mHotseat != null) {
2828 return mHotseat.getLayout();
2829 } else {
2830 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002831 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002832 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002833 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002834 }
2835 }
2836
Winson Chungb745afb2015-03-02 11:51:23 -08002837 /**
2838 * For overridden classes.
2839 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002840 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002841 return isAppsViewVisible();
2842 }
2843
2844 public boolean isAppsViewVisible() {
2845 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2846 }
2847
2848 public boolean isWidgetsViewVisible() {
2849 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002850 }
2851
Michael Jurkae326f182011-11-21 14:05:46 -08002852 @Override
2853 public void onTrimMemory(int level) {
2854 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002855 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2856 // The widget preview db can result in holding onto over
2857 // 3MB of memory for caching which isn't necessary.
2858 SQLiteDatabase.releaseMemory();
2859
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002860 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002861 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002862 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002863 if (mLauncherCallbacks != null) {
2864 mLauncherCallbacks.onTrimMemory(level);
2865 }
Michael Jurkae326f182011-11-21 14:05:46 -08002866 }
2867
Winson5c6bdbb2015-09-03 11:36:19 -07002868 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002869 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002870 }
2871
Winson5c6bdbb2015-09-03 11:36:19 -07002872 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002873 boolean changed = mState != State.WORKSPACE ||
2874 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002875 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002876 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002877 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002878 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002879
Michael Jurkab3e22d92011-10-31 15:58:33 -07002880 // Set focus to the AppsCustomize button
2881 if (mAllAppsButton != null) {
2882 mAllAppsButton.requestFocus();
2883 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002884 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002885
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002886 // Change the state *after* we've called all the transition code
2887 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002888
Winson Chung0f785722015-04-08 10:27:49 -07002889 if (changed) {
2890 // Send an accessibility event to announce the context change
2891 getWindow().getDecorView()
2892 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002893 }
Winson5c6bdbb2015-09-03 11:36:19 -07002894 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002895 }
2896
Winsone9f27272015-10-13 10:47:51 -07002897 /**
2898 * Shows the overview button.
2899 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002900 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002901 showOverviewMode(animated, false);
2902 }
2903
2904 /**
2905 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2906 * onto one of the overview panel buttons.
2907 */
2908 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2909 Runnable postAnimRunnable = null;
2910 if (requestButtonFocus) {
2911 postAnimRunnable = new Runnable() {
2912 @Override
2913 public void run() {
2914 // Hitting the menu button when in touch mode does not trigger touch mode to
2915 // be disabled, so if requested, force focus on one of the overview panel
2916 // buttons.
2917 mOverviewPanel.requestFocusFromTouch();
2918 }
2919 };
2920 }
Adam Cohened307df2013-10-02 09:37:31 -07002921 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002922 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002923 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002924 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002925 // If animated from long press, then don't allow any of the controller in the drag
2926 // layer to intercept any remaining touch.
2927 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002928 }
2929
Winson Chungb745afb2015-03-02 11:51:23 -08002930 /**
2931 * Shows the apps view.
2932 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002933 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002934 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002935 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002936 if (updatePredictedApps) {
2937 tryAndUpdatePredictedApps();
2938 }
Winson Chung76648c52015-07-10 14:33:23 -07002939 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002940 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002941
Winson Chungb745afb2015-03-02 11:51:23 -08002942 /**
2943 * Shows the widgets view.
2944 */
2945 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002946 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002947 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002948 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002949 }
Winson Chung76648c52015-07-10 14:33:23 -07002950 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002951
2952 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002953 @Override
2954 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002955 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002956 }
2957 });
Winson Chungb745afb2015-03-02 11:51:23 -08002958 }
2959
2960 /**
2961 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002962 *
2963 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002964 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002965 // TODO: calling method should use the return value so that when {@code false} is returned
2966 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002967 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002968 if (!(mState == State.WORKSPACE ||
2969 mState == State.APPS_SPRING_LOADED ||
2970 mState == State.WIDGETS_SPRING_LOADED ||
2971 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002972 return false;
2973 }
2974 if (toState != State.APPS && toState != State.WIDGETS) {
2975 return false;
2976 }
Winson Chungb745afb2015-03-02 11:51:23 -08002977
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002978 // This is a safe and supported transition to bypass spring_loaded mode.
2979 if (mExitSpringLoadedModeRunnable != null) {
2980 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2981 mExitSpringLoadedModeRunnable = null;
2982 }
2983
Winson Chungb745afb2015-03-02 11:51:23 -08002984 if (toState == State.APPS) {
Sunny Goyaldb364372016-10-26 19:12:47 -07002985 mStateTransitionAnimation.startAnimationToAllApps(animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002986 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002987 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002988 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002989
Michael Jurkab3e22d92011-10-31 15:58:33 -07002990 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002991 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002992 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002993
2994 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002995 getWindow().getDecorView()
2996 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002997 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998 }
2999
Winson Chungcd99cd32015-04-29 11:03:24 -07003000 /**
3001 * Updates the workspace and interaction state on state change, and return the animation to this
3002 * new state.
3003 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003004 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07003005 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003006 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003007 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003008 updateInteraction(fromState, toState);
3009 return anim;
3010 }
3011
Hyunyoung Song3f471442015-04-08 19:01:34 -07003012 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003013 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003014 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003015 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003016 }
Winson Chungb745afb2015-03-02 11:51:23 -08003017
Winson Chung006ee262015-08-03 14:40:11 -07003018 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003019 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003020 null /* onCompleteRunnable */);
Sunny Goyalf5440cb2016-12-14 15:13:00 -08003021 mState = State.WORKSPACE_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003022 }
Adam Cohen7777d962011-08-18 18:58:38 -07003023
Hyunyoung Song3f471442015-04-08 19:01:34 -07003024 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003025 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003026 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003027
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003028 if (mExitSpringLoadedModeRunnable != null) {
3029 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3030 }
3031 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003032 @Override
3033 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003034 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003035 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3036 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003037 // Before we show workspace, hide all apps again because
3038 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3039 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003040 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003041 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003042 } else {
3043 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003044 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003045 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003046 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003047 };
3048 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003049 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003050
Tony Wickhame0c33232016-02-08 11:37:04 -08003051 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003052 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3053 || mState == State.WIDGETS_SPRING_LOADED;
3054 }
3055
Sunny Goyal0f76b562016-12-13 19:37:10 -08003056 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003057 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003058 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003059 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003060 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003061 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003062 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3063 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003064 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003065 }
3066
Winson Chung4ac30062015-05-08 17:34:17 -07003067 /**
3068 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3069 * resumed.
3070 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003071 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003072 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003073 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003074 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003075 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003076 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003077 }
3078 }
3079 }
3080
Michael Jurkab3e22d92011-10-31 15:58:33 -07003081 void lockAllApps() {
3082 // TODO
3083 }
3084
3085 void unlockAllApps() {
3086 // TODO
3087 }
3088
Michael Jurkad7c28052012-04-27 15:43:36 -07003089 @Override
3090 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003091 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003092 final List<CharSequence> text = event.getText();
3093 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003094 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003095 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003096 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003097 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003098 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003099 } else if (mWorkspace != null) {
3100 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003101 } else {
3102 text.add(getString(R.string.all_apps_home_button_label));
3103 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003104 return result;
3105 }
3106
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003107 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003108 * If the activity is currently paused, signal that we need to run the passed Runnable
3109 * in onResume.
3110 *
3111 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003112 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3113 * wrong when we're not running, and if the activity comes back to what the configuration was
3114 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003115 *
3116 * Implementation of the method from LauncherModel.Callbacks.
3117 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003118 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003119 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003120 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003121 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003122 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003123 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003124 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003125 }
3126 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003127 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003128 return true;
3129 } else {
3130 return false;
3131 }
3132 }
3133
Michael Jurkac402cd92013-05-20 15:49:32 +02003134 private boolean waitUntilResume(Runnable run) {
3135 return waitUntilResume(run, false);
3136 }
3137
Michael Jurka1e2f4652013-07-08 18:03:46 -07003138 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003139 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003140 }
3141
Michael Jurka7607c2f2013-04-03 14:33:19 -07003142 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003143 * If the activity is currently paused, signal that we need to re-run the loader
3144 * in onResume.
3145 *
3146 * This needs to be called from incoming places where resources might have been loaded
3147 * while we are paused. That is becaues the Configuration might be wrong
3148 * when we're not running, and if it comes back to what it was when we
3149 * were paused, we are not restarted.
3150 *
3151 * Implementation of the method from LauncherModel.Callbacks.
3152 *
3153 * @return true if we are currently paused. The caller might be able to
3154 * skip some work in that case since we will come back again.
3155 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003156 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003157 public boolean setLoadOnResume() {
3158 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003159 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003160 mOnResumeNeedsLoad = true;
3161 return true;
3162 } else {
3163 return false;
3164 }
3165 }
3166
3167 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003170 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003172 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003173 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003174 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003175 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003176 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003177 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003178
Joe Onorato9c1289c2009-08-17 11:03:03 -04003179 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003180 * Clear any pending bind callbacks. This is called when is loader is planning to
3181 * perform a full rebind from scratch.
3182 */
3183 @Override
3184 public void clearPendingBinds() {
3185 mBindOnResumeCallbacks.clear();
3186 if (mPendingExecutor != null) {
3187 mPendingExecutor.markCompleted();
3188 mPendingExecutor = null;
3189 }
3190 }
3191
3192 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 * Refreshes the shortcuts shown on the workspace.
3194 *
3195 * Implementation of the method from LauncherModel.Callbacks.
3196 */
3197 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003198 if (LauncherAppState.PROFILE_STARTUP) {
3199 Trace.beginSection("Starting page bind");
3200 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003201 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003202
Winson Chungd64d1762013-08-20 14:37:16 -07003203 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003204 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003205 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003206
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 if (mHotseat != null) {
3208 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003210 if (LauncherAppState.PROFILE_STARTUP) {
3211 Trace.endSection();
3212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 }
3214
Adam Cohendcd297f2013-06-18 13:13:40 -07003215 @Override
3216 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003217 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003218 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3219 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003220 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3221 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3222 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003223 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3224 // If there are no screens, we need to have an empty screen
3225 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003226 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003227 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003228
3229 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003230 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003231 if (hasCustomContentToLeft()) {
3232 mWorkspace.createCustomContentContainer();
3233 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003234 }
Winsonc7d2e832016-07-28 12:24:55 -07003235
3236 // After we have added all the screens, if the wallpaper was locked to the default state,
3237 // then notify to indicate that it can be released and a proper wallpaper offset can be
3238 // computed before the next layout
3239 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003240 }
3241
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003242 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003243 int count = orderedScreenIds.size();
3244 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003245 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003246 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003247 // No need to bind the first screen, as its always bound.
3248 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3249 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003250 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003251 }
3252
Winson Chungd64d1762013-08-20 14:37:16 -07003253 public void bindAppsAdded(final ArrayList<Long> newScreens,
3254 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003255 final ArrayList<ItemInfo> addAnimated,
3256 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003257 Runnable r = new Runnable() {
3258 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003259 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003260 }
3261 };
3262 if (waitUntilResume(r)) {
3263 return;
3264 }
3265
Winson Chungd64d1762013-08-20 14:37:16 -07003266 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003267 if (newScreens != null) {
3268 bindAddScreens(newScreens);
3269 }
Winson Chungd64d1762013-08-20 14:37:16 -07003270
3271 // We add the items without animation on non-visible pages, and with
3272 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003273 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003274 bindItems(addNotAnimated, 0,
3275 addNotAnimated.size(), false);
3276 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003277 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003278 bindItems(addAnimated, 0,
3279 addAnimated.size(), true);
3280 }
Winson Chungc58497e2013-09-03 17:48:37 -07003281
Winson Chung87412982013-10-03 18:34:14 -07003282 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003283 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003284
Winson Chungb745afb2015-03-02 11:51:23 -08003285 if (addedApps != null && mAppsView != null) {
3286 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003287 }
Winson Chungd64d1762013-08-20 14:37:16 -07003288 }
3289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003290 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291 * Bind the items start-end from the list.
3292 *
3293 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003294 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003295 @Override
Sunny Goyal3be633b2016-12-08 09:59:25 -08003296 public void bindItems(final ArrayList<ItemInfo> items, final int start, final int end,
Winson Chung64359a52013-07-08 17:17:08 -07003297 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003298 Runnable r = new Runnable() {
3299 public void run() {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003300 bindItems(items, start, end, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003301 }
3302 };
3303 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003304 return;
3305 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003306
Sunny Goyal3be633b2016-12-08 09:59:25 -08003307 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003308 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3309 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003310 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003311 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003312 long newItemsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003313 for (int i = start; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003314 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003315
3316 // Short circuit if we are loading dock items for a configuration which has no dock
3317 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3318 mHotseat == null) {
3319 continue;
3320 }
3321
Sunny Goyal639e9062015-08-19 19:17:06 -07003322 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323 switch (item.itemType) {
3324 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3325 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003326 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003327 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003328 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003330 }
3331 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003332 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003333 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003334 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003336 }
3337 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
3338 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
3339 if (mIsSafeModeEnabled) {
3340 view = new PendingAppWidgetHostView(this, info, mIconCache, true);
3341 } else {
3342 LauncherAppWidgetProviderInfo providerInfo =
3343 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
3344 if (providerInfo == null) {
3345 deleteWidgetInfo(info);
3346 continue;
3347 }
3348 view = mAppWidgetHost.createView(this, info.appWidgetId, providerInfo);
3349 }
3350 prepareAppWidget((AppWidgetHostView) view, info);
3351 break;
3352 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003353 default:
3354 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003356
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003357 /*
3358 * Remove colliding items.
3359 */
3360 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3361 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3362 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3363 View v = cl.getChildAt(item.cellX, item.cellY);
3364 Object tag = v.getTag();
3365 String desc = "Collision while binding workspace item: " + item
3366 + ". Collides with " + tag;
3367 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3368 throw (new RuntimeException(desc));
3369 } else {
3370 Log.d(TAG, desc);
3371 LauncherModel.deleteItemFromDatabase(this, item);
3372 continue;
3373 }
3374 }
3375 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303376 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003377 if (animateIcons) {
3378 // Animate all the applications up now
3379 view.setAlpha(0f);
3380 view.setScaleX(0f);
3381 view.setScaleY(0f);
3382 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003383 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003385 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003386
Winson Chung997a9232013-07-24 15:33:46 -07003387 if (animateIcons) {
3388 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003389 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003390 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003391 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003392 final Runnable startBounceAnimRunnable = new Runnable() {
3393 public void run() {
3394 anim.playTogether(bounceAnims);
3395 anim.start();
3396 }
3397 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003398 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003399 // We post the animation slightly delayed to prevent slowdowns
3400 // when we are loading right after we return to launcher.
3401 mWorkspace.postDelayed(new Runnable() {
3402 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003403 if (mWorkspace != null) {
3404 mWorkspace.snapToPage(newScreenIndex);
3405 mWorkspace.postDelayed(startBounceAnimRunnable,
3406 NEW_APPS_ANIMATION_DELAY);
3407 }
Winson Chung94d67682013-09-25 16:29:40 -07003408 }
3409 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003410 } else {
3411 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003412 }
3413 }
Winson Chung64359a52013-07-08 17:17:08 -07003414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003416 }
3417
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 /**
3419 * Add the views for a widget to the workspace.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003423 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003424 Runnable r = new Runnable() {
3425 public void run() {
3426 bindAppWidget(item);
3427 }
3428 };
3429 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003430 return;
3431 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003432
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003433 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303434 PendingAppWidgetHostView view =
3435 new PendingAppWidgetHostView(this, item, mIconCache, true);
3436 prepareAppWidget(view, item);
3437 mWorkspace.addInScreen(view, item);
3438 mWorkspace.requestLayout();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003439 return;
3440 }
3441
Daniel Sandler843e8602010-06-07 14:59:01 -04003442 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3443 if (DEBUG_WIDGETS) {
3444 Log.d(TAG, "bindAppWidget: " + item);
3445 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003447 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003448
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003449 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3450 // If the provider is not ready, bind as a pending widget.
3451 appWidgetInfo = null;
3452 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3453 // The widget id is not valid. Try to find the widget based on the provider info.
3454 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3455 } else {
3456 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3457 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003458
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003459 // If the provider is ready, but the width is not yet restored, try to restore it.
3460 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3461 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003462 if (appWidgetInfo == null) {
3463 if (DEBUG_WIDGETS) {
3464 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3465 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003466 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003467 }
3468 LauncherModel.deleteItemFromDatabase(this, item);
3469 return;
3470 }
Sunny Goyalff572272014-07-23 13:58:07 -07003471
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003472 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003473 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003474 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3475 // Id has not been allocated yet. Allocate a new id.
3476 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3477 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003478
Sunny Goyald478c832016-04-01 12:04:16 -07003479 // Also try to bind the widget. If the bind fails, the user will be shown
3480 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003481 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003482 pendingInfo.spanX = item.spanX;
3483 pendingInfo.spanY = item.spanY;
3484 pendingInfo.minSpanX = item.minSpanX;
3485 pendingInfo.minSpanY = item.minSpanY;
3486 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003487
3488 boolean isDirectConfig =
3489 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3490 if (isDirectConfig && item.bindOptions != null) {
3491 Bundle newOptions = item.bindOptions.getExtras();
3492 if (options != null) {
3493 newOptions.putAll(options);
3494 }
3495 options = newOptions;
3496 }
Sunny Goyald478c832016-04-01 12:04:16 -07003497 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3498 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003499
Sunny Goyal86df1382016-08-10 15:03:22 -07003500 // We tried to bind once. If we were not able to bind, we would need to
3501 // go through the permission dialog, which means we cannot skip the config
3502 // activity.
3503 item.bindOptions = null;
3504 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3505
Sunny Goyald478c832016-04-01 12:04:16 -07003506 // Bind succeeded
3507 if (success) {
3508 // If the widget has a configure activity, it is still needs to set it up,
3509 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003510 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003511 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3512 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003513 }
Sunny Goyald478c832016-04-01 12:04:16 -07003514
3515 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003516 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003517 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003518 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003519 // The widget was marked as UI not ready, but there is no configure activity to
3520 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003521 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3522 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003523 }
Sunny Goyalff572272014-07-23 13:58:07 -07003524 }
3525
Sunny Goyald5462aa2016-11-22 16:52:39 +05303526 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003527 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003528 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003529 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3530 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003531 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003532
Sunny Goyal56c73602015-09-25 12:55:01 -07003533 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003534 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003535 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003536 deleteWidgetInfo(item);
3537 return;
3538 }
3539
Sunny Goyal233ee962015-08-03 13:05:01 -07003540 item.minSpanX = appWidgetInfo.minSpanX;
3541 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303542 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003543 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303544 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003545 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303546 prepareAppWidget(view, item);
3547 mWorkspace.addInScreen(view, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003548 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003549
Daniel Sandler843e8602010-06-07 14:59:01 -04003550 if (DEBUG_WIDGETS) {
3551 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3552 + (SystemClock.uptimeMillis()-start) + "ms");
3553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 }
3555
Sunny Goyalff572272014-07-23 13:58:07 -07003556 /**
3557 * Restores a pending widget.
3558 *
3559 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003560 */
Sunny Goyald478c832016-04-01 12:04:16 -07003561 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003562 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3563 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3564 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003565 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003566 }
3567
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003568 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003569 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003570
3571 mWorkspace.reinflateWidgetsIfNecessary();
3572 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003573 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003574 }
3575
Adam Cohen1462de32012-07-24 22:34:36 -07003576 public void onPageBoundSynchronously(int page) {
3577 mSynchronouslyBoundPages.add(page);
3578 }
3579
Sunny Goyal527c7d32015-08-28 15:19:36 -07003580 @Override
3581 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3582 if (mPendingExecutor != null) {
3583 mPendingExecutor.markCompleted();
3584 }
3585 mPendingExecutor = executor;
3586 executor.attachTo(this);
3587 }
3588
3589 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3590 if (mPendingExecutor == executor) {
3591 mPendingExecutor = null;
3592 }
3593 }
3594
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003595 @Override
3596 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3597 Runnable r = new Runnable() {
3598 public void run() {
3599 finishFirstPageBind(executor);
3600 }
3601 };
3602 if (waitUntilResume(r)) {
3603 return;
3604 }
3605
3606 Runnable onComplete = new Runnable() {
3607 @Override
3608 public void run() {
3609 if (executor != null) {
3610 executor.onLoadAnimationCompleted();
3611 }
3612 }
3613 };
3614 if (mDragLayer.getAlpha() < 1) {
3615 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3616 } else {
3617 onComplete.run();
3618 }
3619 }
3620
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 /**
3622 * Callback saying that there aren't any more items to bind.
3623 *
3624 * Implementation of the method from LauncherModel.Callbacks.
3625 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003626 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003627 Runnable r = new Runnable() {
3628 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003629 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003630 }
3631 };
3632 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003633 return;
3634 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003635 if (LauncherAppState.PROFILE_STARTUP) {
3636 Trace.beginSection("Page bind completed");
3637 }
Adam Cohen1462de32012-07-24 22:34:36 -07003638 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003640 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003641
Sunny Goyal2100c782016-08-22 16:00:03 -07003642 if (mPendingActivityResult != null) {
3643 handleActivityResult(mPendingActivityResult.requestCode,
3644 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3645 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003646 }
3647
Sunny Goyal756adbc2015-04-16 15:20:51 -07003648 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003649
Tony Wickham010d2552017-01-20 08:15:28 -08003650 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3651
Adam Cohen9211d422014-10-07 18:14:53 -07003652 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003653 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003654 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003655 if (LauncherAppState.PROFILE_STARTUP) {
3656 Trace.endSection();
3657 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003658 }
3659
Winson Chunga2413752012-04-03 14:22:34 -07003660 private boolean canRunNewAppsAnimation() {
3661 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003662 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003663 }
3664
Winson Chungc9168342013-06-26 14:54:55 -07003665 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003666 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003667 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3668 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003669 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003670 return bounceAnim;
3671 }
3672
Adam Cohen27772732013-11-11 14:00:56 +00003673 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003674 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003675 }
3676
Tony Wickham55616cd2015-09-23 14:55:17 -07003677 public int getSearchBarHeight() {
3678 if (mLauncherCallbacks != null) {
3679 return mLauncherCallbacks.getSearchBarHeight();
3680 }
3681 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3682 }
3683
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003684 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003685 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3686 * multiple calls to bind the same list.)
3687 */
3688 @Thunk ArrayList<AppInfo> mTmpAppsList;
3689 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3690 public void run() {
3691 bindAllApplications(mTmpAppsList);
3692 mTmpAppsList = null;
3693 }
3694 };
3695
3696 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 * Add the icons for all apps.
3698 *
3699 * Implementation of the method from LauncherModel.Callbacks.
3700 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003701 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003702 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3703 mTmpAppsList = apps;
3704 return;
3705 }
3706
Winson Chungb745afb2015-03-02 11:51:23 -08003707 if (mAppsView != null) {
3708 mAppsView.setApps(apps);
3709 }
Adam Cohen9211d422014-10-07 18:14:53 -07003710 if (mLauncherCallbacks != null) {
3711 mLauncherCallbacks.bindAllApplications(apps);
3712 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 }
3714
3715 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003716 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3717 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3718 */
3719 @Override
3720 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003721 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003722 }
3723
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003724 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 * A package was updated.
3726 *
3727 * Implementation of the method from LauncherModel.Callbacks.
3728 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003729 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003730 Runnable r = new Runnable() {
3731 public void run() {
3732 bindAppsUpdated(apps);
3733 }
3734 };
3735 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003736 return;
3737 }
3738
Winson Chungb745afb2015-03-02 11:51:23 -08003739 if (mAppsView != null) {
3740 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003742 }
3743
Sunny Goyal4390ace2014-10-13 11:33:11 -07003744 @Override
3745 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3746 Runnable r = new Runnable() {
3747 public void run() {
3748 bindWidgetsRestored(widgets);
3749 }
3750 };
3751 if (waitUntilResume(r)) {
3752 return;
3753 }
3754 mWorkspace.widgetsRestored(widgets);
3755 }
3756
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003758 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003759 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003760 *
3761 * @param updated list of shortcuts which have changed.
3762 * @param removed list of shortcuts which were deleted in the background. This can happen when
3763 * an app gets removed from the system or some of its components are no longer
3764 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003765 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003766 @Override
3767 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003768 final ArrayList<ShortcutInfo> removed, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003769 Runnable r = new Runnable() {
3770 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003771 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003772 }
3773 };
3774 if (waitUntilResume(r)) {
3775 return;
3776 }
3777
Sunny Goyal4390ace2014-10-13 11:33:11 -07003778 if (!updated.isEmpty()) {
3779 mWorkspace.updateShortcuts(updated);
3780 }
3781
3782 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003783 HashSet<ComponentName> removedComponents = new HashSet<>();
3784 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3785
Sunny Goyal4390ace2014-10-13 11:33:11 -07003786 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003787 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08003788 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003789 } else {
3790 removedComponents.add(si.getTargetComponent());
3791 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003792 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003793
3794 if (!removedComponents.isEmpty()) {
3795 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3796 mWorkspace.removeItemsByMatcher(matcher);
3797 mDragController.onAppsRemoved(matcher);
3798 }
3799
3800 if (!removedDeepShortcuts.isEmpty()) {
3801 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3802 mWorkspace.removeItemsByMatcher(matcher);
3803 mDragController.onAppsRemoved(matcher);
3804 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003805 }
3806 }
3807
3808 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003809 * Update the state of a package, typically related to install state.
3810 *
3811 * Implementation of the method from LauncherModel.Callbacks.
3812 */
Sunny Goyale755d462014-07-22 13:48:29 -07003813 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003814 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3815 Runnable r = new Runnable() {
3816 public void run() {
3817 bindRestoreItemsChange(updates);
3818 }
3819 };
3820 if (waitUntilResume(r)) {
3821 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003822 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003823
Sunny Goyal756adbc2015-04-16 15:20:51 -07003824 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003825 }
3826
3827 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003828 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003829 * in addition to specific applications to remove, the reason being that
3830 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003831 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003832 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003834 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003835 public void bindWorkspaceComponentsRemoved(
3836 final HashSet<String> packageNames, final HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003837 final UserHandle user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003838 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003839 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003840 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003841 }
Winson Chungd64d1762013-08-20 14:37:16 -07003842 };
3843 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003844 return;
3845 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003846 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003847 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3848 mWorkspace.removeItemsByMatcher(matcher);
3849 mDragController.onAppsRemoved(matcher);
3850
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003851 }
3852 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003853 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3854 mWorkspace.removeItemsByMatcher(matcher);
3855 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003856 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003857 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003858
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003859 @Override
3860 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3861 Runnable r = new Runnable() {
3862 public void run() {
3863 bindAppInfosRemoved(appInfos);
3864 }
3865 };
3866 if (waitUntilResume(r)) {
3867 return;
Joe Onorato36115782010-06-17 13:28:48 -04003868 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003869
Winson Chungdf95eb12013-10-16 14:57:07 -07003870 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003871 if (mAppsView != null) {
3872 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003873 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 }
Joe Onoratobe386092009-11-17 17:32:16 -08003875
Sunny Goyald164b7f2016-10-12 20:49:31 -07003876 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003877 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003878 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003879 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003880 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003881
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003882 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003883 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3884 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3885 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003886 return;
3887 }
3888
Sunny Goyald164b7f2016-10-12 20:49:31 -07003889 if (mWidgetsView != null && allWidgets != null) {
3890 mWidgetsView.setWidgets(allWidgets);
3891 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003892 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003893 }
3894
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003895 @Override
3896 public void notifyWidgetProvidersChanged() {
3897 if (mWorkspace.getState().shouldUpdateWidget) {
3898 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3899 }
3900 }
3901
Winson Chung4b919f82012-05-01 10:44:08 -07003902 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003903 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003904 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003905 }
3906 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003907
Winson Chung4b919f82012-05-01 10:44:08 -07003908 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003909 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003910 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003911 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003912 } else {
3913 mHandler.postDelayed(new Runnable() {
3914 public void run() {
3915 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3916 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003917 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003918 }
Winson Chung4b919f82012-05-01 10:44:08 -07003919 }
Winson Chung400438b2011-01-16 17:53:48 -08003920 }
3921
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003922 private void markAppsViewShown() {
3923 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3924 return;
3925 }
3926 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3927 }
3928
3929 private boolean shouldShowDiscoveryBounce() {
3930 if (mState != mState.WORKSPACE) {
3931 return false;
3932 }
3933 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3934 return true;
3935 }
3936 if (!mIsResumeFromActionScreenOff) {
3937 return false;
3938 }
3939 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3940 return false;
3941 }
3942 return true;
3943 }
3944
Winson Chung5ffd51d2015-06-25 11:36:50 -07003945 protected void moveWorkspaceToDefaultScreen() {
3946 mWorkspace.moveToDefaultScreen(false);
3947 }
3948
Winson Chung80baf5a2010-08-09 16:03:15 -07003949 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003950 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003951 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003952 @Override
3953 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3954 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003955
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003956 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3957 writer.println(prefix + "Workspace Items");
3958 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
3959 writer.println(prefix + " Homescreen " + i);
3960
3961 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3962 for (int j = 0; j < layout.getChildCount(); j++) {
3963 Object tag = layout.getChildAt(j).getTag();
3964 if (tag != null) {
3965 writer.println(prefix + " " + tag.toString());
3966 }
3967 }
3968 }
3969
3970 writer.println(prefix + " Hotseat");
3971 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003972 for (int j = 0; j < layout.getChildCount(); j++) {
3973 Object tag = layout.getChildAt(j).getTag();
3974 if (tag != null) {
3975 writer.println(prefix + " " + tag.toString());
3976 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003977 }
Sunny Goyala1365452015-10-01 15:46:24 -07003978
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003979 try {
3980 FileLog.flushAll(writer);
3981 } catch (Exception e) {
3982 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003983 }
3984 }
3985
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003986 writer.println(prefix + "Misc:");
3987 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
3988 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
3989 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
3990
3991 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003992
Adam Cohen9211d422014-10-07 18:14:53 -07003993 if (mLauncherCallbacks != null) {
3994 mLauncherCallbacks.dump(prefix, fd, writer, args);
3995 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003996 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003997
Sunny Goyal66b24572016-09-21 15:57:55 -07003998 @Override
3999 @TargetApi(Build.VERSION_CODES.N)
4000 public void onProvideKeyboardShortcuts(
4001 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4002
4003 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4004 if (mState == State.WORKSPACE) {
4005 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4006 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4007 }
4008 View currentFocus = getCurrentFocus();
4009 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4010 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4011 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4012 }
4013 if (currentFocus instanceof BubbleTextView &&
4014 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4015 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4016 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4017 }
4018 if (!shortcutInfos.isEmpty()) {
4019 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4020 }
4021
4022 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4023 }
4024
4025 @Override
4026 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4027 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4028 switch (keyCode) {
4029 case KeyEvent.KEYCODE_A:
4030 if (mState == State.WORKSPACE) {
4031 showAppsView(true, true, false);
4032 return true;
4033 }
4034 break;
4035 case KeyEvent.KEYCODE_S: {
4036 View focusedView = getCurrentFocus();
4037 if (focusedView instanceof BubbleTextView
4038 && focusedView.getTag() instanceof ItemInfo
4039 && mAccessibilityDelegate.performAction(focusedView,
4040 (ItemInfo) focusedView.getTag(),
4041 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08004042 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004043 return true;
4044 }
4045 break;
4046 }
4047 case KeyEvent.KEYCODE_O:
4048 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4049 return true;
4050 }
4051 break;
4052 }
4053 }
4054 return super.onKeyShortcut(keyCode, event);
4055 }
4056
Adam Cohen59400422014-03-05 18:07:04 -08004057 public static CustomAppWidget getCustomAppWidget(String name) {
4058 return sCustomAppWidgets.get(name);
4059 }
4060
4061 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4062 return sCustomAppWidgets;
4063 }
4064
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004065 public static Launcher getLauncher(Context context) {
4066 if (context instanceof Launcher) {
4067 return (Launcher) context;
4068 }
4069 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4070 }
4071
Sunny Goyal745bad92016-05-02 10:54:12 -07004072 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4073
4074 @Override
4075 public void onSharedPreferenceChanged(
4076 SharedPreferences sharedPreferences, String key) {
4077 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4078 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4079 if (!waitUntilResume(this, true)) {
4080 run();
4081 }
4082 }
4083 }
4084
4085 @Override
4086 public void run() {
4087 setOrientation();
4088 }
4089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004090}