blob: 069362b3acd09ee0c0a16d3895f4313f7d6d88b2 [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;
Mario Bertschleracbf5702017-03-03 10:58:06 -080074import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080075import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070076import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070077import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.inputmethod.InputMethodManager;
Chet Haasea8f996d2015-02-17 12:56:04 -080079import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070081
Sunny Goyal651077b2014-06-30 14:15:31 -070082import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070083import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070084import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070085import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070086import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080087import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalb5e65c82016-10-26 18:32:38 -070088import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080089import com.android.launcher3.model.ModelWriter;
Tony Wickham9438ed42017-01-20 09:38:25 -080090import com.android.launcher3.notification.NotificationListener;
Tony Wickham010d2552017-01-20 08:15:28 -080091import com.android.launcher3.popup.PopupDataProvider;
Sunny Goyalffe83f12014-08-14 17:39:34 -070092import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010093import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal782f0c92017-01-19 10:27:54 -080094import com.android.launcher3.compat.PinItemRequestCompat;
Kenny Guyed131872014-04-30 03:02:21 +010095import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080096import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070097import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -070098import com.android.launcher3.dragndrop.DragController;
99import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700100import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700101import com.android.launcher3.dragndrop.DragView;
Sunny Goyalb38fab72017-01-20 19:32:31 -0800102import com.android.launcher3.dragndrop.PinItemDragListener;
Tony Wickham827cef22016-03-17 15:39:39 -0700103import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000104import com.android.launcher3.folder.Folder;
105import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700106import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700107import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700108import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700109import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700110import com.android.launcher3.model.PackageItemInfo;
111import com.android.launcher3.model.WidgetItem;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700112import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham540913e2017-01-23 11:47:51 -0800113import com.android.launcher3.popup.PopupContainerWithArrow;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700114import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700115import com.android.launcher3.shortcuts.ShortcutKey;
Jon Mirandac6cf4932017-02-07 17:12:36 -0800116import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530117import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
118import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
119import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Sunny Goyal2100c782016-08-22 16:00:03 -0700120import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700122import com.android.launcher3.util.ItemInfoMatcher;
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -0800123import com.android.launcher3.util.LogConfig;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700124import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700125import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham010d2552017-01-20 08:15:28 -0800126import com.android.launcher3.util.PackageUserKey;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700128import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700129import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700130import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal782f0c92017-01-19 10:27:54 -0800131import com.android.launcher3.widget.PendingAddShortcutInfo;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700132import com.android.launcher3.widget.PendingAddWidgetInfo;
Sunny Goyal04a324a2017-01-20 21:08:59 -0800133import com.android.launcher3.widget.WidgetAddFlowHandler;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700134import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700135import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700136
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700137import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700138import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700139import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700140import java.util.Collection;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700141import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700142import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700143import java.util.List;
Tony Wickham010d2552017-01-20 08:15:28 -0800144import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146/**
147 * Default launcher application.
148 */
Sunny Goyal27835952017-01-13 12:15:53 -0800149public class Launcher extends BaseActivity
Adam Cohen79d90c52016-04-22 13:29:20 -0700150 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
151 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700152 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700153 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700154 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700156 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700157 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400158 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700161 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700162 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700163 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Michael Jurka8b805b12012-04-18 14:23:14 -0700165 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700166 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700167 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700168
Sunny Goyal28c6b962015-10-12 11:42:05 -0700169 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
170
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700171 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
172
Mathew Inwood876a8462013-06-14 14:12:41 +0100173 /**
174 * IntentStarter uses request codes starting with this. This must be greater than all activity
175 * request codes used internally.
176 */
177 protected static final int REQUEST_LAST = 100;
178
Mario Bertschleracbf5702017-03-03 10:58:06 -0800179 private static final int SOFT_INPUT_MODE_DEFAULT =
180 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
181 | WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED;
182 private static final int SOFT_INPUT_MODE_ALL_APPS =
183 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
184 | WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED;
185
Winson Chung2672ff92012-05-04 16:22:30 -0700186 // The Intent extra that defines whether to ignore the launch animation
187 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400188 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700194 // Type: PendingRequestArgs
195 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
196 // Type: ActivityResultInfo
197 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700199 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Sunny Goyal27835952017-01-13 12:15:53 -0800210 public static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700213 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700216 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700221 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700224 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700225
226 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Sunny Goyalffe83f12014-08-14 17:39:34 -0700228 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700229 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
Sunny Goyal316490e2015-06-02 09:38:28 -0700233 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800234 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700235
Michael Jurka838a4ca2011-02-07 13:33:06 -0800236 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700237 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700238
Sunny Goyal47328fd2016-05-25 18:56:41 -0700239 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700240
241 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700242 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700243 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700244
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700245 // Main container view and the model for the widget tray screen.
246 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700247 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700248
Winson Chung4a2afa32012-07-19 14:53:05 -0700249 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
250 // scroll issues (because the workspace may not have been measured yet) and extra work.
251 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
252 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
254 private SpannableStringBuilder mDefaultKeySsb = null;
255
Adam Cohen091440a2015-03-18 14:16:05 -0700256 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800258 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700263 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700264
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800266 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800267 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700268 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700269 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700270 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700271 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700272 private boolean mHasFocus = false;
273 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Tony Wickham010d2552017-01-20 08:15:28 -0800275 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700276
Adam Cohen61f560d2013-09-30 15:58:20 -0700277 private View.OnTouchListener mHapticFeedbackTouchListener;
278
Winson Chung400438b2011-01-16 17:53:48 -0800279 // Determines how long to wait after a rotation before restoring the screen orientation to
280 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700281 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800282
Adam Cohen1462de32012-07-24 22:34:36 -0700283 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
284
Winson Chung46353de2012-02-16 14:05:10 -0800285 // We only want to get the SharedPreferences once since it does an FS stat each time we get
286 // it from the context.
287 private SharedPreferences mSharedPrefs;
288
Adam Cohen4b66bf32015-09-18 12:15:19 -0700289 private boolean mMoveToDefaultScreenFromNewIntent;
290
Winson Chung13eb5272015-05-11 16:30:13 -0700291 // This is set to the view that launched the activity that navigated the user away from
292 // launcher. Since there is no callback for when the activity has finished launching, enable
293 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800294 private BubbleTextView mWaitingForResume;
295
Adam Cohen59400422014-03-05 18:07:04 -0800296 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
297 new HashMap<String, CustomAppWidget>();
298
Adam Cohen59400422014-03-05 18:07:04 -0800299 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700300 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
301 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800302 }
303 }
304
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700305 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
306 // state transition. If another state transition happened during this delay,
307 // simply unregister this runnable.
308 private Runnable mExitSpringLoadedModeRunnable;
309
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700311 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700312 if (mWorkspace != null) {
313 mWorkspace.buildPageHardwareLayers();
314 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700315 }
316 };
317
Sunny Goyal2100c782016-08-22 16:00:03 -0700318 // Activity result which needs to be processed after workspace has loaded.
319 private ActivityResultInfo mPendingActivityResult;
320 /**
321 * Holds extra information required to handle a result from an external call, like
322 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
323 */
324 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800325
Hyunyoung Songaa953652016-04-19 18:30:24 -0700326 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800327
Jon Miranda379198e2016-09-23 08:54:40 -0700328 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700329
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700330 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700331 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400332
333 @Thunk void setOrientation() {
334 if (mRotationEnabled) {
335 unlockScreenOrientation(true);
336 } else {
337 setRequestedOrientation(
338 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700339 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400340 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700341
Sunny Goyal745bad92016-05-02 10:54:12 -0700342 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 @Override
345 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700346 if (DEBUG_STRICT_MODE) {
347 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
348 .detectDiskReads()
349 .detectDiskWrites()
350 .detectNetwork() // or .detectAll() for all detectable problems
351 .penaltyLog()
352 .build());
353 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
354 .detectLeakedSqlLiteObjects()
355 .detectLeakedClosableObjects()
356 .penaltyLog()
357 .penaltyDeath()
358 .build());
359 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700360 if (LauncherAppState.PROFILE_STARTUP) {
361 Trace.beginSection("Launcher-onCreate");
362 }
Winson Chunga2413752012-04-03 14:22:34 -0700363
Adam Cohen9211d422014-10-07 18:14:53 -0700364 if (mLauncherCallbacks != null) {
365 mLauncherCallbacks.preOnCreate();
366 }
367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400369
Sunny Goyal87f784c2017-01-11 10:48:34 -0800370 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700371
Adam Cohen2e6da152015-05-06 11:42:25 -0700372 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800373 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Sunny Goyalf5e37442016-11-02 10:31:24 -0700374 if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700375 Display display = getWindowManager().getDefaultDisplay();
376 Point mwSize = new Point();
377 display.getSize(mwSize);
378 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
379 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700380
Sunny Goyalf7258242015-10-19 16:59:07 -0700381 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700382 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800383 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800384 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800385 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700386 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700387
Joe Onorato41a12d22009-10-31 18:30:00 -0400388 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700389 mAllAppsController = new AllAppsTransitionController(this);
390 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700391
Sunny Goyalffe83f12014-08-14 17:39:34 -0700392 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700393
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
395 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700396
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700397 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
398 // this also ensures that any synchronous binding below doesn't re-trigger another
399 // LauncherModel load.
400 mPaused = false;
401
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800402 mLauncherView = getLayoutInflater().inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 setupViews();
Winson1f064272016-07-18 17:18:02 -0700405 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700406 mExtractedColors = new ExtractedColors();
407 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408
Tony Wickham010d2552017-01-20 08:15:28 -0800409 mPopupDataProvider = new PopupDataProvider(this);
410
Tony Wickhame2217252016-03-22 16:34:23 -0700411 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
412 .addAccessibilityStateChangeListener(this);
413
Joe Onorato7c312c12009-08-13 21:36:53 -0700414 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700415
Sunny Goyalfe770c92016-09-27 14:38:58 -0700416 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417
Sunny Goyale26d1002016-06-20 14:52:14 -0700418 if (LauncherAppState.PROFILE_STARTUP) {
419 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421
Sunny Goyal2100c782016-08-22 16:00:03 -0700422 // We only load the page synchronously if the user rotates (or triggers a
423 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700424 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
425 if (savedInstanceState != null) {
426 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
427 }
428 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700429 // If we are not binding synchronously, show a fade in animation when
430 // the first page bind completes.
431 mDragLayer.setAlpha(0);
432 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700433 // Pages bound synchronously.
434 mWorkspace.setCurrentPage(currentScreen);
435
Sunny Goyal2100c782016-08-22 16:00:03 -0700436 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 }
438
439 // For handling default keys
440 mDefaultKeySsb = new SpannableStringBuilder();
441 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800442
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800443 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700444 // In case we are on a device with locked rotation, we should look at preferences to check
445 // if the user has specifically allowed rotation.
446 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700447 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700448 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
449 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700450 }
451
452 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
453 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400454 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700455
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800456 setContentView(mLauncherView);
Adam Cohen9211d422014-10-07 18:14:53 -0700457 if (mLauncherCallbacks != null) {
458 mLauncherCallbacks.onCreate(savedInstanceState);
459 }
460 }
461
Sunny Goyal7779d622015-06-11 16:18:39 -0700462 @Override
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800463 public View findViewById(int id) {
464 return mLauncherView.findViewById(id);
465 }
466
467 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700468 public void onExtractedColorsChanged() {
469 loadExtractedColorsAndColorItems();
470 }
471
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700472 @Override
473 public void onAppWidgetHostReset() {
474 if (mAppWidgetHost != null) {
475 mAppWidgetHost.startListening();
476 }
477 }
478
Tony Wickham827cef22016-03-17 15:39:39 -0700479 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700480 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700481 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700482 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700483 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700484 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800485 boolean lightStatusBar = (FeatureFlags.LIGHT_STATUS_BAR
486 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
487 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700488 // It's possible that All Apps is visible when this is run,
Hyunyoung Song3285da32016-12-19 14:46:21 -0800489 // so always use light status bar in that case. Only change nav bar color to status bar
490 // color when All Apps is visible.
491 activateLightStatusBar(lightStatusBar || isAllAppsVisible(), isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700492 }
493 }
494
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800495 // TODO: use platform flag on API >= 26
496 private static final int SYSTEM_UI_FLAG_LIGHT_NAV_BAR = 0x10;
497
Tony Wickham345bff32016-09-28 15:34:51 -0700498 /**
499 * Sets the status bar to be light or not. Light status bar means dark icons.
Hyunyoung Song3285da32016-12-19 14:46:21 -0800500 * @param lightStatusBar make sure the status bar is light
501 * @param changeNavBar if true, make the nav bar theme in sync with status bar.
Tony Wickham345bff32016-09-28 15:34:51 -0700502 */
Hyunyoung Song3285da32016-12-19 14:46:21 -0800503 public void activateLightStatusBar(boolean lightStatusBar, boolean changeNavBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700504 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
505 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700506 if (lightStatusBar) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800507 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR ;
Hyunyoung Song3285da32016-12-19 14:46:21 -0800508 if (changeNavBar && Utilities.isAtLeastO()) {
509 newSystemUiFlags |= SYSTEM_UI_FLAG_LIGHT_NAV_BAR;
510 }
Tony Wickham93113872016-09-14 16:46:32 -0700511 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700512 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800513 if (changeNavBar && Utilities.isAtLeastO()) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800514 newSystemUiFlags &= ~(SYSTEM_UI_FLAG_LIGHT_NAV_BAR);
515 }
516 }
Hyunyoung Song3285da32016-12-19 14:46:21 -0800517
Tony Wickham345bff32016-09-28 15:34:51 -0700518 if (newSystemUiFlags != oldSystemUiFlags) {
519 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
520 }
Tony Wickham93113872016-09-14 16:46:32 -0700521 }
522
Adam Cohen9211d422014-10-07 18:14:53 -0700523 private LauncherCallbacks mLauncherCallbacks;
524
525 public void onPostCreate(Bundle savedInstanceState) {
526 super.onPostCreate(savedInstanceState);
527 if (mLauncherCallbacks != null) {
528 mLauncherCallbacks.onPostCreate(savedInstanceState);
529 }
530 }
531
Winson1f064272016-07-18 17:18:02 -0700532 public void onInsetsChanged(Rect insets) {
533 mDeviceProfile.updateInsets(insets);
534 mDeviceProfile.layout(this, true /* notifyListeners */);
535 }
536
Sunny Goyal32554d12015-12-03 15:31:25 -0800537 /**
538 * Call this after onCreate to set or clear overlay.
539 */
540 public void setLauncherOverlay(LauncherOverlay overlay) {
541 if (overlay != null) {
542 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
543 }
544 mWorkspace.setLauncherOverlay(overlay);
545 }
546
Adam Cohen9211d422014-10-07 18:14:53 -0700547 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
548 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 private boolean mWorkspaceImportanceStored = false;
551 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700552 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800553 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
555
556 @Override
557 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700558 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700559 return;
560 }
561 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700562 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700563 if (mWorkspace != null) {
564 mWorkspaceImportanceForAccessibility =
565 mWorkspace.getImportantForAccessibility();
566 mWorkspace.setImportantForAccessibility(
567 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
568 mWorkspaceImportanceStored = true;
569 }
570 if (mHotseat != null) {
571 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
572 mHotseat.setImportantForAccessibility(
573 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
574 mHotseatImportanceStored = true;
575 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577
578 @Override
579 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700580 if (mWorkspaceImportanceStored && mWorkspace != null) {
581 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
582 }
583 if (mHotseatImportanceStored && mHotseat != null) {
584 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
585 }
586 mWorkspaceImportanceStored = false;
587 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700588 }
589 });
Adam Cohen9211d422014-10-07 18:14:53 -0700590 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700591 }
592
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700593 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700594 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700595 if (mLauncherCallbacks != null) {
596 mLauncherCallbacks.onLauncherProviderChange();
597 }
598 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700599
Adam Cohen9211d422014-10-07 18:14:53 -0700600 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700601 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 return mLauncherCallbacks.hasCustomContentToLeft();
604 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700605 return false;
606 }
607
Dave Hawkeya8881582013-09-17 15:55:33 -0600608 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500609 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 * {@link #addToCustomContentPage}. This will only be invoked if
611 * {@link #hasCustomContentToLeft()} is {@code true}.
612 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500613 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700614 if (mLauncherCallbacks != null) {
615 mLauncherCallbacks.populateCustomContentContainer();
616 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 }
618
619 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700620 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 * ensure the custom content page is added or removed if necessary.
622 */
623 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600624 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600625 // Not bound yet, wait for bindScreens to be called.
626 return;
627 }
628
629 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
630 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500631 mWorkspace.createCustomContentContainer();
632 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600633 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
634 mWorkspace.removeCustomContentPage();
635 }
636 }
637
Hyunyoung Songaa953652016-04-19 18:30:24 -0700638 public UserEventDispatcher getUserEventDispatcher() {
639 if (mLauncherCallbacks != null) {
640 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
641 if (dispatcher != null) {
642 return dispatcher;
643 }
644 }
645
Sunny Goyal64976d52016-06-20 14:56:28 -0700646 // Logger object is a singleton and does not have to be coupled with the foreground
647 // activity. Since most user event logging is done on the UI, the object is retrieved
648 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700649 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700650 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700651 }
652 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800653 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100654
Hyunyoung Song3f471442015-04-08 19:01:34 -0700655 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700656 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
657 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700658 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700659 }
660
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000661 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700662 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
663 // This cast is safe as long as the id < 0x00FFFFFF
664 // Since we jail all the dynamically generated views, there should be no clashes
665 // with any other views.
666 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000667 }
668
Tony Wickham010d2552017-01-20 08:15:28 -0800669 public PopupDataProvider getPopupDataProvider() {
670 return mPopupDataProvider;
671 }
672
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000673 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
675 * a configuration step, this allows the proper animations to run after other transitions.
676 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 private long completeAdd(
678 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
679 long screenId = info.screenId;
680 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700681 // When the screen id represents an actual screen (as opposed to a rank) we make sure
682 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700684 }
Adam Cohendb364c32014-05-20 14:23:40 -0700685
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800687 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700688 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700689 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700691 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700692 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700693 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700694 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700695 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700696 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 int widgetId = appWidgetId;
698 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700699 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700700 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700701 // Since the view was just bound, also launch the configure activity if needed
702 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
703 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800704 if (provider != null) {
705 new WidgetAddFlowHandler(provider)
706 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700707 }
708 }
709 break;
710 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800711 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700712
Adam Cohendb364c32014-05-20 14:23:40 -0700713 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
715
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800716 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700718 if (isWorkspaceLoading()) {
719 // process the result once the workspace has loaded.
720 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
721 return;
722 }
723 mPendingActivityResult = null;
724
Winson Chunge341d302013-08-16 14:31:00 -0700725 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 final PendingRequestArgs requestArgs = mPendingRequestArgs;
727 setWaitingForResult(null);
728 if (requestArgs == null) {
729 return;
730 }
731
732 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700733
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 Runnable exitSpringLoaded = new Runnable() {
735 @Override
736 public void run() {
737 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
738 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
739 }
740 };
741
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700743 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
746 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700747 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700748 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 addAppWidgetImpl(
752 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800753 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700754 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 }
756 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
758 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700759 // User could have free-scrolled between pages before picking a wallpaper; make sure
760 // we move to the closest one now.
761 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700762 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200763 }
764 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200766
Adam Cohened66b2b2012-01-23 17:28:51 -0800767 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700768 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700769
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 // We have special handling for widgets
771 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800772 final int appWidgetId;
773 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
774 : -1;
775 if (widgetId < 0) {
776 appWidgetId = pendingAddWidgetId;
777 } else {
778 appWidgetId = widgetId;
779 }
780
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800782 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700783 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
784 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700786 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700787 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 @Override
789 public void run() {
790 exitSpringLoadedDragModeDelayed(false, 0, null);
791 }
792 };
Adam Cohendb364c32014-05-20 14:23:40 -0700793
Sunny Goyal2100c782016-08-22 16:00:03 -0700794 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
796 } else {
797 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
798 // When the screen id represents an actual screen (as opposed to a rank)
799 // we make sure that the drop page actually exists.
800 requestArgs.screenId =
801 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700802 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700803 final CellLayout dropLayout =
804 mWorkspace.getScreenWithId(requestArgs.screenId);
805
806 dropLayout.setDropPending(true);
807 final Runnable onComplete = new Runnable() {
808 @Override
809 public void run() {
810 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
811 dropLayout.setDropPending(false);
812 }
813 };
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700816 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800817 return;
818 }
819
Sunny Goyald478c832016-04-01 12:04:16 -0700820 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
821 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700822 if (resultCode == RESULT_OK) {
823 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700824 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700825 }
826 // Leave the widget in the pending state if the user canceled the configure.
827 return;
828 }
829
Sunny Goyalaad90582015-07-09 14:22:50 -0700830 if (requestCode == REQUEST_CREATE_SHORTCUT) {
831 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700832 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
833 completeAdd(requestCode, data, -1, requestArgs);
834 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
835 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
836
Sunny Goyalaad90582015-07-09 14:22:50 -0700837 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700838 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
839 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800843 }
844
845 @Override
Sunny Goyaldec3a902017-01-25 18:23:36 -0800846 protected void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800847 final int requestCode, final int resultCode, final Intent data) {
848 handleActivityResult(requestCode, resultCode, data);
849 if (mLauncherCallbacks != null) {
850 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
851 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 }
853
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700855 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600856 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700857 PendingRequestArgs pendingArgs = mPendingRequestArgs;
858 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
859 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
860 setWaitingForResult(null);
861
Sunny Goyal28c6b962015-10-12 11:42:05 -0700862 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700863 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700864 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700865 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700866 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700867 Intent intent = pendingArgs.getPendingIntent();
868
Sunny Goyal28c6b962015-10-12 11:42:05 -0700869 if (grantResults.length > 0
870 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700871 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700872 } else {
873 // TODO: Show a snack bar with link to settings
874 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700875 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700876 }
877 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600878 if (mLauncherCallbacks != null) {
879 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
880 grantResults);
881 }
882 }
883
Adam Cohendb364c32014-05-20 14:23:40 -0700884 /**
885 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
886 *
887 * @param screenId the screen id to check
888 * @return the new screen, or screenId if it exists
889 */
890 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800891 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700892 if (dropLayout == null) {
893 // it's possible that the add screen was removed because it was
894 // empty and a re-bind occurred
895 mWorkspace.addExtraEmptyScreen();
896 return mWorkspace.commitExtraEmptyScreen();
897 } else {
898 return screenId;
899 }
900 }
901
Sunny Goyal2100c782016-08-22 16:00:03 -0700902 @Thunk void completeTwoStageWidgetDrop(
903 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
904 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 Runnable onCompleteRunnable = null;
906 int animationType = 0;
907
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700908 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 if (resultCode == RESULT_OK) {
910 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
911 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800912 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 onCompleteRunnable = new Runnable() {
915 @Override
916 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700917 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700918 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
919 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 }
921 };
922 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800923 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700927 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
929 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700930 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 // The animated view may be null in the case of a rotation during widget configuration
932 onCompleteRunnable.run();
933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
935
936 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700937 protected void onStop() {
938 super.onStop();
939 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700940
941 if (mLauncherCallbacks != null) {
942 mLauncherCallbacks.onStop();
943 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700944
Sunny Goyalf5e37442016-11-02 10:31:24 -0700945 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700946 mAppWidgetHost.stopListening();
947 }
Tony Wickham010d2552017-01-20 08:15:28 -0800948
949 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 }
951
952 @Override
953 protected void onStart() {
954 super.onStart();
955 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStart();
959 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700960
Sunny Goyalf5e37442016-11-02 10:31:24 -0700961 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700962 mAppWidgetHost.startListening();
963 }
Tony Wickham010d2552017-01-20 08:15:28 -0800964
965 if (!isWorkspaceLoading()) {
966 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
967 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700968 }
969
970 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200972 long startTime = 0;
973 if (DEBUG_RESUME_TIME) {
974 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400975 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200976 }
Adam Cohen9211d422014-10-07 18:14:53 -0700977
978 if (mLauncherCallbacks != null) {
979 mLauncherCallbacks.preOnResume();
980 }
981
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700983 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700984
Winson Chung4a2afa32012-07-19 14:53:05 -0700985 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700986 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700987 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800988 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700989 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700990 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700991 // view after launching an app, as they may be depending on the UI to be static to
992 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700993 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700994 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800995 } else if (mOnResumeState == State.WIDGETS) {
996 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700997 }
998 mOnResumeState = State.NONE;
999
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001000 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -07001001 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001002 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001003 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001004 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1008 // execute them here
1009 long startTimeCallbacks = 0;
1010 if (DEBUG_RESUME_TIME) {
1011 startTimeCallbacks = System.currentTimeMillis();
1012 }
1013
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1015 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001016 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001017 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001018 if (DEBUG_RESUME_TIME) {
1019 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1020 (System.currentTimeMillis() - startTimeCallbacks));
1021 }
Michael Jurka447bf842013-05-15 14:52:15 +02001022 }
Michael Jurka54554252013-08-01 12:52:23 +02001023 if (mOnResumeCallbacks.size() > 0) {
1024 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1025 mOnResumeCallbacks.get(i).run();
1026 }
1027 mOnResumeCallbacks.clear();
1028 }
Winson Chunge4e50662012-01-23 14:45:13 -08001029
1030 // Reset the pressed state of icons that were locked in the press state while activities
1031 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001032 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001033 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001034 mWaitingForResume.setStayPressed(false);
1035 }
Winson Chung82963d52013-10-09 11:20:57 -07001036
Adam Cohen06dff352012-06-01 17:17:08 -07001037 // It is possible that widgets can receive updates while launcher is not in the foreground.
1038 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1039 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1040 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001041 if (!isWorkspaceLoading()) {
1042 getWorkspace().reinflateWidgetsIfNecessary();
1043 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001044
Michael Jurka447bf842013-05-15 14:52:15 +02001045 if (DEBUG_RESUME_TIME) {
1046 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1047 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001048
Adam Cohen4b66bf32015-09-18 12:15:19 -07001049 // We want to suppress callbacks about CustomContent being shown if we have just received
1050 // onNewIntent while the user was present within launcher. In that case, we post a call
1051 // to move the user to the main screen (which will occur after onResume). We don't want to
1052 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1053 // suppress here.
1054 if (mWorkspace.getCustomContentCallbacks() != null
1055 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001056 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001057 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1059 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001060 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001061 }
1062 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001063 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001064 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001065 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001066
Sunny Goyal756adbc2015-04-16 15:20:51 -07001067 if (!isWorkspaceLoading()) {
1068 // Process any items that were added while Launcher was away.
1069 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001070
1071 // Refresh shortcuts if the permission changed.
1072 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001073 }
Adam Cohen9211d422014-10-07 18:14:53 -07001074
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001075 if (shouldShowDiscoveryBounce()) {
1076 mAllAppsController.showDiscoveryBounce();
1077 }
1078 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001079 if (mLauncherCallbacks != null) {
1080 mLauncherCallbacks.onResume();
1081 }
Adam Cohen06dff352012-06-01 17:17:08 -07001082 }
1083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001085 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001086 // Ensure that items added to Launcher are queued until Launcher returns
1087 InstallShortcutReceiver.enableInstallQueue();
1088
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001090 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001091 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001092 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001093
1094 // We call onHide() aggressively. The custom content callbacks should be able to
1095 // debounce excess onHide calls.
1096 if (mWorkspace.getCustomContentCallbacks() != null) {
1097 mWorkspace.getCustomContentCallbacks().onHide();
1098 }
Romain Guycbb89e42009-06-08 15:52:54 -07001099
Adam Cohen9211d422014-10-07 18:14:53 -07001100 if (mLauncherCallbacks != null) {
1101 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001102 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001103 }
1104
1105 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001106 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1107 // by a onResume or by scrolling otherwise.
1108 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001109
1110 // Custom content is completely hidden
1111 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001112
1113 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1114 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001115
1116 // Indicates whether the user is allowed to scroll away from the custom content.
1117 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001118 }
1119
Adam Cohenc2d6e892014-10-16 09:49:24 -07001120 public interface LauncherOverlay {
1121
1122 /**
1123 * Touch interaction leading to overscroll has begun
1124 */
1125 public void onScrollInteractionBegin();
1126
1127 /**
1128 * Touch interaction related to overscroll has ended
1129 */
1130 public void onScrollInteractionEnd();
1131
1132 /**
1133 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1134 * screen (or in the case of RTL, the rightmost screen).
1135 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001136 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001137
1138 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001139 * Called when the launcher is ready to use the overlay
1140 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001141 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001142 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001143 }
1144
Jun Mukai8af0cd82015-05-12 12:32:12 -07001145 public interface LauncherSearchCallbacks {
1146 /**
1147 * Called when the search overlay is shown.
1148 */
1149 public void onSearchOverlayOpened();
1150
1151 /**
1152 * Called when the search overlay is dismissed.
1153 */
1154 public void onSearchOverlayClosed();
1155 }
1156
Adam Cohenc2d6e892014-10-16 09:49:24 -07001157 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001158 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 }
1160
1161 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1162
Sunny Goyalc86df472016-02-25 09:19:38 -08001163 public void onScrollChanged(float progress) {
1164 if (mWorkspace != null) {
1165 mWorkspace.onOverlayScrollChanged(progress);
1166 }
1167 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 }
1169
Jorim Jaggid017f882014-01-14 17:08:48 -08001170 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001171 if (mLauncherCallbacks != null) {
1172 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001173 } else {
1174 // On devices with a locked orientation, we will at least have the allow rotation
1175 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001176 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001177 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001178 }
1179
Adam Cohen9211d422014-10-07 18:14:53 -07001180 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001181 CustomContentCallbacks callbacks, String description) {
1182 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001183 }
1184
Adam Cohenedb40762013-07-18 16:45:45 -07001185 // The custom content needs to offset its content to account for the QSB
1186 public int getTopOffsetForCustomContent() {
1187 return mWorkspace.getPaddingTop();
1188 }
1189
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001190 @Override
1191 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001193 if (mModel.isCurrentCallbacks(this)) {
1194 mModel.stopLoader();
1195 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001196 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1197
Romain Guy13c2e7b2010-03-10 19:45:00 -08001198 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001199 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001200
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001201 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001202 @Override
1203 public void onWindowFocusChanged(boolean hasFocus) {
1204 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001205 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001206
1207 if (mLauncherCallbacks != null) {
1208 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1209 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 private boolean acceptFilter() {
1213 final InputMethodManager inputManager = (InputMethodManager)
1214 getSystemService(Context.INPUT_METHOD_SERVICE);
1215 return !inputManager.isFullscreenMode();
1216 }
1217
1218 @Override
1219 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001220 final int uniChar = event.getUnicodeChar();
1221 final boolean handled = super.onKeyDown(keyCode, event);
1222 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1223 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1225 keyCode, event);
1226 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001227 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001228 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 // showSearchDialog()
1230 // If there are multiple keystrokes before the search dialog takes focus,
1231 // onSearchRequested() will be called for every keystroke,
1232 // but it is idempotent, so it's fine.
1233 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235 }
1236
Joe Onorato8a9625e2010-01-28 15:55:35 -08001237 // Eat the long press event so the keyboard doesn't come up.
1238 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1239 return true;
1240 }
1241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 return handled;
1243 }
1244
Winson46163472015-09-22 17:31:56 -07001245 @Override
1246 public boolean onKeyUp(int keyCode, KeyEvent event) {
1247 if (keyCode == KeyEvent.KEYCODE_MENU) {
1248 // Ignore the menu key if we are currently dragging or are on the custom content screen
1249 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001250 // Close any open floating view
1251 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001252
Winson46163472015-09-22 17:31:56 -07001253 // Stop resizing any widgets
1254 mWorkspace.exitWidgetResizeMode();
1255
1256 // Show the overview mode if we are on the workspace
1257 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1258 !mWorkspace.isSwitchingState()) {
1259 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001260 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001261 }
1262 }
1263 return true;
1264 }
1265 return super.onKeyUp(keyCode, event);
1266 }
1267
Karl Rosaen138a0412009-04-23 19:00:21 -07001268 private String getTypedText() {
1269 return mDefaultKeySsb.toString();
1270 }
1271
Sunny Goyal6f28e712016-09-13 14:19:29 -07001272 @Override
1273 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001274 mDefaultKeySsb.clear();
1275 mDefaultKeySsb.clearSpans();
1276 Selection.setSelection(mDefaultKeySsb, 0);
1277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 /**
1280 * Restores the previous state, if it exists.
1281 *
1282 * @param savedState The previous state.
1283 */
1284 private void restoreState(Bundle savedState) {
1285 if (savedState == null) {
1286 return;
1287 }
1288
Sunny Goyalfe770c92016-09-27 14:38:58 -07001289 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1290 State[] stateValues = State.values();
1291 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1292 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001293 if (state == State.APPS || state == State.WIDGETS) {
1294 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001295 }
1296
Sunny Goyal2100c782016-08-22 16:00:03 -07001297 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1298 if (requestArgs != null) {
1299 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001301
1302 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304
1305 /**
1306 * Finds all the views we need and configure them properly.
1307 */
1308 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001309 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001310 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001311 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001312 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1313 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001314 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001315
Sunny Goyal784f9c32016-03-23 16:56:54 -07001316 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1317 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1318 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
Winson Chung4c98d922011-05-31 16:50:48 -07001320 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001321 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001322
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 // Setup the hotseat
1324 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1325 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001326 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001327 }
1328
Winsone9f27272015-10-13 10:47:51 -07001329 // Setup the overview panel
1330 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001331
Winson Chung4c98d922011-05-31 16:50:48 -07001332 // Setup the workspace
1333 mWorkspace.setHapticFeedbackEnabled(false);
1334 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001335 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001336 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1337 // default state, otherwise we will update to the wrong offsets in RTL
1338 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001339 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001340 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001341
Tony Wickham34d2c912015-09-11 09:27:58 -07001342 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001343 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001344
Winson Chungef7f8742015-06-04 17:18:17 -07001345 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001346 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001347 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001348 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1349 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1350 } else {
1351 mAppsView.setSearchBarController(new DefaultAppSearchController());
1352 }
Craig Mautner360310b2012-10-26 15:13:08 -07001353
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001355 mDragController.setMoveTarget(mWorkspace);
1356 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001357 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001358
Peter Schiller310a9882016-06-30 13:52:36 -07001359 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1360 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1361 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001362
Sunny Goyal322d5562015-06-25 19:35:49 -07001363 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1364 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367
Winsone9f27272015-10-13 10:47:51 -07001368 private void setupOverviewPanel() {
1369 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1370
Winsone9f27272015-10-13 10:47:51 -07001371 // Bind wallpaper button actions
1372 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301373 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001374 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001375 public void handleViewClick(View view) {
1376 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001377 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001378 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001379 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1380
1381 // Bind widget button actions
1382 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301383 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001384 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001385 public void handleViewClick(View view) {
1386 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001387 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001388 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001389 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1390
1391 // Bind settings actions
1392 View settingsButton = findViewById(R.id.settings_button);
1393 boolean hasSettings = hasSettings();
1394 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301395 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001396 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001397 public void handleViewClick(View view) {
1398 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001399 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001400 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001401 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1402 } else {
1403 settingsButton.setVisibility(View.GONE);
1404 }
1405
1406 mOverviewPanel.setAlpha(0f);
1407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001410 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001411 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001412 */
1413 public void setAllAppsButton(View allAppsButton) {
1414 mAllAppsButton = allAppsButton;
1415 }
1416
Sunny Goyalbb011da2016-06-15 15:42:29 -07001417 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001418 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001419 }
1420
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001421 public View getWidgetsButton() {
1422 return mWidgetsButton;
1423 }
1424
Anjali Koppal5ad44842014-03-10 20:34:39 -07001425 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 * Creates a view representing a shortcut.
1427 *
1428 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001430 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001431 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433
1434 /**
1435 * Creates a view representing a shortcut inflated from the specified resource.
1436 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 * @param parent The group the shortcut belongs to.
1438 * @param info The data structure describing the shortcut.
1439 *
1440 * @return A View inflated from layoutResId.
1441 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001442 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001443 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001444 parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001445 favorite.applyFromShortcutInfo(info);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001446 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001448 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 return favorite;
1450 }
1451
1452 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 * Add a shortcut to the workspace.
1454 *
1455 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001457 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001458 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001459 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001461
Sunny Goyal782f0c92017-01-19 10:27:54 -08001462 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001463 args.getPendingIntent().getComponent() == null) {
1464 return;
1465 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001466
Sunny Goyal782f0c92017-01-19 10:27:54 -08001467 ShortcutInfo info = null;
1468 if (Utilities.isAtLeastO()) {
Sunny Goyaldec3a902017-01-25 18:23:36 -08001469 info = LauncherAppsCompat.createShortcutInfoFromPinItemRequest(
1470 this, PinItemRequestCompat.getPinItemRequest(data));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001471 }
1472
1473 if (info == null) {
1474 info = InstallShortcutReceiver.fromShortcutIntent(this, data);
1475
Sunny Goyal342e4662017-02-02 15:21:08 -08001476 if (info == null || !new PackageManagerHelper(this).hasPermissionForActivity(
1477 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001478 // The app is trying to add a shortcut without sufficient permissions
1479 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1480 return;
1481 }
1482 }
1483
1484 final View view = createShortcut(info);
Sunny Goyal5c97f512015-05-19 16:03:28 -07001485 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001486 // First we check if we already know the exact location where we want to add this item.
1487 if (cellX >= 0 && cellY >= 0) {
1488 cellXY[0] = cellX;
1489 cellXY[1] = cellY;
1490 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001491
1492 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001493 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001494 true, null,null)) {
1495 return;
1496 }
1497 DragObject dragObject = new DragObject();
1498 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001499 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1500 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001501 return;
1502 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001505 }
1506
1507 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001508 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 return;
1510 }
1511
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001512 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301513 mWorkspace.addInScreen(view, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001517 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001519 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001521 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001522 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1523
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001525 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Adam Cohen59400422014-03-05 18:07:04 -08001528 if (appWidgetInfo.isCustomWidget) {
1529 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001530 }
1531
Adam Cohen59400422014-03-05 18:07:04 -08001532 LauncherAppWidgetInfo launcherInfo;
1533 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001534 launcherInfo.spanX = itemInfo.spanX;
1535 launcherInfo.spanY = itemInfo.spanY;
1536 launcherInfo.minSpanX = itemInfo.minSpanX;
1537 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyala52ecb02016-12-16 15:04:51 -08001538 launcherInfo.user = appWidgetInfo.getUser();
Romain Guycbb89e42009-06-08 15:52:54 -07001539
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001540 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001541 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
Sunny Goyal2100c782016-08-22 16:00:03 -07001543 if (hostView == null) {
1544 // Perform actual inflation because we're live
1545 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001547 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301548 prepareAppWidget(hostView, launcherInfo);
1549 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
Romain Guycbb89e42009-06-08 15:52:54 -07001551
Sunny Goyald5462aa2016-11-22 16:52:39 +05301552 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001553 hostView.setTag(item);
1554 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001555 hostView.setFocusable(true);
1556 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001557 }
1558
Adam Cohended9f8d2010-11-03 13:25:16 -07001559 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1560 @Override
1561 public void onReceive(Context context, Intent intent) {
1562 final String action = intent.getAction();
1563 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001564 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001565
Winson Chungc100e8e2011-08-09 16:02:43 -07001566 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001567 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001568 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001569 if (!showWorkspace(false)) {
1570 // If we are already on the workspace, then manually reset all apps
1571 mAppsView.reset();
1572 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001573 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001574 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001575 }
1576 }
1577 };
1578
Tony Wickham010d2552017-01-20 08:15:28 -08001579 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1580 Runnable r = new Runnable() {
1581 @Override
1582 public void run() {
1583 mWorkspace.updateIconBadges(updatedBadges);
1584 mAppsView.updateIconBadges(updatedBadges);
1585 }
1586 };
1587 if (!waitUntilResume(r)) {
1588 r.run();
1589 }
1590 }
1591
Adam Cohended9f8d2010-11-03 13:25:16 -07001592 @Override
1593 public void onAttachedToWindow() {
1594 super.onAttachedToWindow();
1595
1596 // Listen for broadcasts related to user-presence
1597 final IntentFilter filter = new IntentFilter();
1598 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001600 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001601 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001602
1603 if (mLauncherCallbacks != null) {
1604 mLauncherCallbacks.onAttachedToWindow();
1605 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 }
1607
1608 @Override
1609 public void onDetachedFromWindow() {
1610 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001611 if (mAttached) {
1612 unregisterReceiver(mReceiver);
1613 mAttached = false;
1614 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001615
1616 if (mLauncherCallbacks != null) {
1617 mLauncherCallbacks.onDetachedFromWindow();
1618 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001619 }
1620
1621 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001622 // The following code used to be in onResume, but it turns out onResume is called when
1623 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1624 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001625 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001626 if (!mWorkspaceLoading) {
1627 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001628 // We want to let Launcher draw itself at least once before we force it to build
1629 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001630 // apps is nice and speedy.
1631 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001632 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001633 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001634 if (mStarted) return;
1635 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001636 // We delay the layer building a bit in order to give
1637 // other message processing a time to run. In particular
1638 // this avoids a delay in hiding the IME if it was
1639 // currently shown, because doing that may involve
1640 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001641 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001642 final ViewTreeObserver.OnDrawListener listener = this;
1643 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001644 public void run() {
1645 if (mWorkspace != null &&
1646 mWorkspace.getViewTreeObserver() != null) {
1647 mWorkspace.getViewTreeObserver().
1648 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001649 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001650 }
1651 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001652 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001653 }
1654 });
1655 }
1656 clearTypedText();
1657 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001658 }
1659
Winson Chunga6945242014-01-08 14:04:34 -08001660 public DragLayer getDragLayer() {
1661 return mDragLayer;
1662 }
1663
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001664 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001665 return mAppsView;
1666 }
1667
Hyunyoung Song3f471442015-04-08 19:01:34 -07001668 public WidgetsContainerView getWidgetsView() {
1669 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001670 }
1671
Winson Chunga6945242014-01-08 14:04:34 -08001672 public Workspace getWorkspace() {
1673 return mWorkspace;
1674 }
1675
Sunny Goyal6178f132016-07-11 17:30:03 -07001676 public View getQsbContainer() {
1677 return mQsbContainer;
1678 }
1679
Winson Chunga6945242014-01-08 14:04:34 -08001680 public Hotseat getHotseat() {
1681 return mHotseat;
1682 }
1683
Jorim Jaggid017f882014-01-14 17:08:48 -08001684 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001685 return mOverviewPanel;
1686 }
1687
Sunny Goyal47328fd2016-05-25 18:56:41 -07001688 public DropTargetBar getDropTargetBar() {
1689 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001690 }
1691
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001692 public LauncherAppWidgetHost getAppWidgetHost() {
1693 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
Romain Guycbb89e42009-06-08 15:52:54 -07001695
Winson Chunga9abd0e2010-10-27 17:18:37 -07001696 public LauncherModel getModel() {
1697 return mModel;
1698 }
1699
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001700 public ModelWriter getModelWriter() {
1701 return mModelWriter;
1702 }
1703
Adam Cohen79d90c52016-04-22 13:29:20 -07001704 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001705 return mSharedPrefs;
1706 }
1707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 @Override
1709 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001710 long startTime = 0;
1711 if (DEBUG_RESUME_TIME) {
1712 startTime = System.currentTimeMillis();
1713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 super.onNewIntent(intent);
1715
Winson15f8b172015-08-19 11:02:39 -07001716 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1717 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1718 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001719
1720 // Check this condition before handling isActionMain, as this will get reset.
1721 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001722 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001723
Winson15f8b172015-08-19 11:02:39 -07001724 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1725 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001726 if (mWorkspace == null) {
1727 // Can be cases where mWorkspace is null, this prevents a NPE
1728 return;
1729 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001730
1731 // Note: There should be at most one log per method call. This is enforced implicitly
1732 // by using if-else statements.
1733 UserEventDispatcher ued = getUserEventDispatcher();
1734
1735 // TODO: Log this case.
Adam Cohen6fecd412013-10-02 17:41:50 -07001736 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001737
Jon Mirandafeba90f2016-10-06 10:53:29 -07001738 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham540913e2017-01-23 11:47:51 -08001739 if (topOpenView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301740 ued.logActionCommand(Action.Command.HOME_INTENT,
1741 topOpenView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001742 } else if (topOpenView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301743 ued.logActionCommand(Action.Command.HOME_INTENT,
1744 ((Folder) topOpenView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001745 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301746 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001747 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1748 }
1749
1750 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001751 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001752 exitSpringLoadedDragMode();
1753
1754 // If we are already on home, then just animate back to the workspace,
1755 // otherwise, just wait until onResume to set the state back to Workspace
1756 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001757 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001758 } else {
1759 mOnResumeState = State.WORKSPACE;
1760 }
1761
1762 final View v = getWindow().peekDecorView();
1763 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001764 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001765 INPUT_METHOD_SERVICE);
1766 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1767 }
1768
Winson Chungb745afb2015-03-02 11:51:23 -08001769 // Reset the apps view
1770 if (!alreadyOnHome && mAppsView != null) {
1771 mAppsView.scrollToTop();
1772 }
1773
Hyunyoung Song3f471442015-04-08 19:01:34 -07001774 // Reset the widgets view
1775 if (!alreadyOnHome && mWidgetsView != null) {
1776 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001777 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001778
Adam Cohen9211d422014-10-07 18:14:53 -07001779 if (mLauncherCallbacks != null) {
1780 mLauncherCallbacks.onHomeIntent();
1781 }
Sunny Goyalb38fab72017-01-20 19:32:31 -08001782
1783 Parcelable dragExtra = intent
1784 .getParcelableExtra(PinItemDragListener.EXTRA_PIN_ITEM_DRAG_LISTENER);
1785 if (dragExtra instanceof PinItemDragListener) {
1786 PinItemDragListener dragListener = (PinItemDragListener) dragExtra;
1787 dragListener.setLauncher(this);
1788 mDragLayer.setOnDragListener(dragListener);
1789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
Adam Cohened307df2013-10-02 09:37:31 -07001791
Adam Cohen9211d422014-10-07 18:14:53 -07001792 if (mLauncherCallbacks != null) {
1793 mLauncherCallbacks.onNewIntent(intent);
1794 }
Winson15f8b172015-08-19 11:02:39 -07001795
1796 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1797 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1798 // animation.
1799 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001800 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001801 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001802 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1803 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001804
1805 // We use this flag to suppress noisy callbacks above custom content state
1806 // from onResume.
1807 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001808 mWorkspace.post(new Runnable() {
1809 @Override
1810 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001811 if (mWorkspace != null) {
1812 mWorkspace.moveToDefaultScreen(true);
1813 }
Winson15f8b172015-08-19 11:02:39 -07001814 }
1815 });
1816 }
1817 }
1818
1819 if (DEBUG_RESUME_TIME) {
1820 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1821 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001822 }
1823
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001825 public void onRestoreInstanceState(Bundle state) {
1826 super.onRestoreInstanceState(state);
1827 for (int page: mSynchronouslyBoundPages) {
1828 mWorkspace.restoreInstanceStateForChild(page);
1829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 }
1831
1832 @Override
1833 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001834 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001835 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1836 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001837
Adam Cohen21cd0022013-10-09 18:57:02 -07001838 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001839 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840
Michael Jurka883f55b2010-10-21 15:47:14 -07001841 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001842 // We close any open folders and shortcut containers since they will not be re-opened,
1843 // and we need to make sure this state is reflected.
1844 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001845
Sunny Goyal2100c782016-08-22 16:00:03 -07001846 if (mPendingRequestArgs != null) {
1847 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1848 }
1849 if (mPendingActivityResult != null) {
1850 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
1852
Adam Cohen9211d422014-10-07 18:14:53 -07001853 if (mLauncherCallbacks != null) {
1854 mLauncherCallbacks.onSaveInstanceState(outState);
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 }
1857
1858 @Override
1859 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001861
Michael Jurka9d906c72011-10-14 06:25:36 -07001862 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001863 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001864
Winson Chungcd2b0142011-06-08 16:02:26 -07001865 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001866 // It's possible to receive onDestroy after a new Launcher activity has
1867 // been created. In this case, don't interfere with the new Launcher.
1868 if (mModel.isCurrentCallbacks(this)) {
1869 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001870 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001871 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001872
Sunny Goyal745bad92016-05-02 10:54:12 -07001873 if (mRotationPrefChangeHandler != null) {
1874 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1875 }
1876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001878 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001880 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001882 mAppWidgetHost = null;
1883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 TextKeyListener.getInstance().release();
1885
Tony Wickhame2217252016-03-22 16:34:23 -07001886 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1887 .removeAccessibilityStateChangeListener(this);
1888
Michael Jurka2ecf9952012-06-18 12:52:28 -07001889 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001890
1891 if (mLauncherCallbacks != null) {
1892 mLauncherCallbacks.onDestroy();
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 }
1895
Sunny Goyalae502842016-06-17 08:43:56 -07001896 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1897 return mAccessibilityDelegate;
1898 }
1899
Adam Cohena9cf38f2011-05-02 15:36:58 -07001900 public DragController getDragController() {
1901 return mDragController;
1902 }
1903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001905 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001906 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001907 }
1908
1909 @Override
1910 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1911 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001912 try {
1913 super.startIntentSenderForResult(intent, requestCode,
1914 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1915 } catch (IntentSender.SendIntentException e) {
1916 throw new ActivityNotFoundException();
1917 }
1918 }
1919
Winson Chung70d72102011-08-12 11:24:35 -07001920 /**
1921 * Indicates that we want global search for this activity by setting the globalSearch
1922 * argument for {@link #startSearch} to true.
1923 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001925 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001927
Karl Rosaen138a0412009-04-23 19:00:21 -07001928 if (initialQuery == null) {
1929 // Use any text typed in the launcher as the initial query
1930 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 if (appSearchData == null) {
1933 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001934 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001936
Sunny Goyal6f28e712016-09-13 14:19:29 -07001937 if (mLauncherCallbacks == null ||
1938 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1939 // Starting search from the callbacks failed. Start the default global search.
1940 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001941 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001942
1943 // We need to show the workspace after starting the search
1944 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001945 }
1946
Ian Parkinson047036e2014-09-01 15:40:53 +01001947 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001948 * Starts the global search activity. This code is a copied from SearchManager
1949 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001950 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001951 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001952 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001953 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1954 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1955 if (globalSearchActivity == null) {
1956 Log.w(TAG, "No global search activity found.");
1957 return;
1958 }
1959 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1960 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1961 intent.setComponent(globalSearchActivity);
1962 // Make sure that we have a Bundle to put source in
1963 if (appSearchData == null) {
1964 appSearchData = new Bundle();
1965 } else {
1966 appSearchData = new Bundle(appSearchData);
1967 }
Adam Cohen9211d422014-10-07 18:14:53 -07001968 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001969 if (!appSearchData.containsKey("source")) {
1970 appSearchData.putString("source", getPackageName());
1971 }
1972 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1973 if (!TextUtils.isEmpty(initialQuery)) {
1974 intent.putExtra(SearchManager.QUERY, initialQuery);
1975 }
1976 if (selectInitialQuery) {
1977 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1978 }
1979 intent.setSourceBounds(sourceBounds);
1980 try {
1981 startActivity(intent);
1982 } catch (ActivityNotFoundException ex) {
1983 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986
Yura4f93ec62014-02-04 14:15:21 +00001987 public boolean isOnCustomContent() {
1988 return mWorkspace.isOnOrMovingToCustomContent();
1989 }
1990
Winson Chung70d72102011-08-12 11:24:35 -07001991 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001992 public boolean onPrepareOptionsMenu(Menu menu) {
1993 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001994 if (mLauncherCallbacks != null) {
1995 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1996 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001997 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002000 @Override
2001 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002002 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002003 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002004 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002005 }
2006
Joe Onorato9c1289c2009-08-17 11:03:03 -04002007 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002008 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002009 }
2010
Adam Cohen517a7f52014-03-01 12:12:59 -08002011 public boolean isWorkspaceLoading() {
2012 return mWorkspaceLoading;
2013 }
2014
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002015 private void setWorkspaceLoading(boolean value) {
2016 boolean isLocked = isWorkspaceLocked();
2017 mWorkspaceLoading = value;
2018 if (isLocked != isWorkspaceLocked()) {
2019 onWorkspaceLockedChanged();
2020 }
2021 }
2022
Sunny Goyal04a324a2017-01-20 21:08:59 -08002023 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002024 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002025 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002026 if (isLocked != isWorkspaceLocked()) {
2027 onWorkspaceLockedChanged();
2028 }
2029 }
2030
Adam Cohen9211d422014-10-07 18:14:53 -07002031 protected void onWorkspaceLockedChanged() {
2032 if (mLauncherCallbacks != null) {
2033 mLauncherCallbacks.onWorkspaceLockedChanged();
2034 }
2035 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002036
Sunny Goyal2100c782016-08-22 16:00:03 -07002037 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002038 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002039 if (LOGD) {
2040 Log.d(TAG, "Adding widget from drop");
2041 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002042 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002043 }
2044
Sunny Goyal2100c782016-08-22 16:00:03 -07002045 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002046 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
2047 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
2048 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07002049
Adam Cohenad4e15c2013-10-17 16:21:35 -07002050 Runnable onComplete = new Runnable() {
2051 @Override
2052 public void run() {
2053 // Exit spring loaded mode if necessary after adding the widget
2054 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2055 null);
2056 }
2057 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08002058 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07002059 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061 }
Romain Guycbb89e42009-06-08 15:52:54 -07002062
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002063 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002064 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002065 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002066 mWorkspace.moveToCustomContentScreen(animate);
2067 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002068
2069 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2070 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002071 info.container = container;
2072 info.screenId = screenId;
2073 if (cell != null) {
2074 info.cellX = cell[0];
2075 info.cellY = cell[1];
2076 }
2077 info.spanX = spanX;
2078 info.spanY = spanY;
2079
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002080 switch (info.itemType) {
2081 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2082 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002083 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002084 break;
2085 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08002086 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002087 break;
2088 default:
2089 throw new IllegalStateException("Unknown item type: " + info.itemType);
2090 }
2091 }
2092
Adam Cohenfbba09b2011-07-18 21:43:05 -07002093 /**
2094 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002095 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08002096 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002097 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2098 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08002099 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
2100 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
2101 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002102 }
2103
Adam Cohenfbba09b2011-07-18 21:43:05 -07002104 /**
2105 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002106 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002107 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002108 AppWidgetHostView hostView = info.boundWidget;
2109 int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08002110 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08002111 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002112 // In the case where we've prebound the widget, we remove it from the DragLayer
2113 if (LOGD) {
2114 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2115 }
2116 getDragLayer().removeView(hostView);
2117
Adam Cohened66b2b2012-01-23 17:28:51 -08002118 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08002119 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002120
2121 // Clear the boundWidget so that it doesn't get destroyed.
2122 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002123 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002124 // In this case, we either need to start an activity to get permission to bind
2125 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002126 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002127 Bundle options = info.bindOptions;
2128
Sunny Goyalffe83f12014-08-14 17:39:34 -07002129 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2130 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002131 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002132 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07002133 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002134 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07002135 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002136 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002137 }
2138
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002140 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002141 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 folderInfo.title = getText(R.string.folder_name);
2143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002145 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146
2147 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002148 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302149 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07002150 // Force measure the new folder icon
2151 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2152 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002153 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 }
Romain Guycbb89e42009-06-08 15:52:54 -07002155
Winsonc0b52fe2015-09-09 16:38:15 -07002156 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002157 * Unbinds the view for the specified item, and removes the item and all its children.
2158 *
2159 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002160 * @param itemInfo the {@link ItemInfo} for this view.
2161 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002162 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002163 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002164 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002165 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002166 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2167 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002168 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002169 } else {
2170 mWorkspace.removeWorkspaceItem(v);
2171 }
Winsonc0b52fe2015-09-09 16:38:15 -07002172 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002173 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002174 }
2175 } else if (itemInfo instanceof FolderInfo) {
2176 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002177 if (v instanceof FolderIcon) {
2178 ((FolderIcon) v).removeListeners();
2179 }
Winsonc0b52fe2015-09-09 16:38:15 -07002180 mWorkspace.removeWorkspaceItem(v);
2181 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002182 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002183 }
2184 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2185 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002186 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002187 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002188 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002189 }
2190 } else {
2191 return false;
2192 }
2193 return true;
2194 }
2195
2196 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002197 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002198 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002199 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002200 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002201 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002202 // Deleting an app widget ID is a void call but writes to disk before returning
2203 // to the caller...
2204 new AsyncTask<Void, Void, Void>() {
2205 public Void doInBackground(Void ... args) {
2206 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2207 return null;
2208 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002209 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002210 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002211 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002212 }
2213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 @Override
2215 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002216 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 }
2218
Joe Onorato88ec0992009-11-19 13:16:06 -08002219 @Override
2220 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002221 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2222 return;
2223 }
2224
Sunny Goyal45478022015-06-08 16:52:41 -07002225 if (mDragController.isDragging()) {
2226 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002227 return;
2228 }
2229
Jon Mirandafeba90f2016-10-06 10:53:29 -07002230 // Note: There should be at most one log per method call. This is enforced implicitly
2231 // by using if-else statements.
2232 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002233 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2234 if (topView != null) {
2235 if (topView.getActiveTextView() != null) {
2236 topView.getActiveTextView().dispatchBackKey();
2237 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002238 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302239 ued.logActionCommand(Action.Command.BACK,
2240 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002241 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302242 ued.logActionCommand(Action.Command.BACK,
2243 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002244 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002245 topView.close(true);
2246 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002247 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302248 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002249 showWorkspace(true);
2250 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302251 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002252 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002253 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302254 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002255 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002256 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002257 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002258 mWorkspace.exitWidgetResizeMode();
2259
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002260 // Back button is a no-op here, but give at least some feedback for the button press
2261 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002262 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002263 }
2264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 * Launches the intent referred by the clicked shortcut.
2267 *
2268 * @param v The view representing the clicked shortcut.
2269 */
2270 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002271 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2272 // view has detached (it's possible for this to happen if the view is removed mid touch).
2273 if (v.getWindowToken() == null) {
2274 return;
2275 }
2276
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002277 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002278 return;
2279 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002280
Adam Cohen1697b792013-09-17 19:08:21 -07002281 if (v instanceof Workspace) {
2282 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002283 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002284 }
2285 return;
2286 }
2287
2288 if (v instanceof CellLayout) {
2289 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002290 int page = mWorkspace.indexOfChild(v);
2291 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2292 LauncherLogProto.Action.Direction.NONE,
2293 LauncherLogProto.ContainerType.OVERVIEW, page);
2294 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002295 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002296 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002297 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002298 }
2299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002301 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002302 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002304 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002305 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002306 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002307 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2308 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002309 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002310 } else if (tag instanceof AppInfo) {
2311 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002312 } else if (tag instanceof LauncherAppWidgetInfo) {
2313 if (v instanceof PendingAppWidgetHostView) {
2314 onClickPendingWidget((PendingAppWidgetHostView) v);
2315 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 }
2317 }
2318
Sunny Goyal70660032015-05-14 00:07:08 -07002319 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002320 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002321 return false;
2322 }
2323
Michael Jurkaaf442092010-06-10 17:01:57 -07002324 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002325 * Event handler for the app widget view which has not fully restored.
2326 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002327 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002328 if (mIsSafeModeEnabled) {
2329 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2330 return;
2331 }
2332
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002333 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002334 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002335 LauncherAppWidgetProviderInfo appWidgetInfo =
2336 mAppWidgetManager.findProvider(info.providerName, info.user);
2337 if (appWidgetInfo == null) {
2338 return;
2339 }
2340 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2341
Sunny Goyald478c832016-04-01 12:04:16 -07002342 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2343 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2344 // This should not happen, as we make sure that an Id is allocated during bind.
2345 return;
2346 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002347 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2348 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002349 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002350 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002351 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002352 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002353 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002354 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002355 }
2356 }
2357
2358 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002359 * Event handler for the "grid" button that appears on the home screen, which
2360 * enters all apps mode.
2361 *
2362 * @param v The view that was clicked.
2363 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002364 protected void onClickAllAppsButton(View v) {
2365 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002366 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302367 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2368 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002369 showAppsView(true /* animated */, true /* updatePredictedApps */,
2370 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002371 }
2372 }
2373
2374 protected void onLongClickAllAppsButton(View v) {
2375 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2376 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302377 getUserEventDispatcher().logActionOnControl(Action.Touch.LONGPRESS,
2378 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002379 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002380 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002381 }
2382 }
2383
Sunny Goyale6e72002017-01-12 16:55:36 -08002384 private void onClickPendingAppItem(final View v, final String packageName,
2385 boolean downloadStarted) {
2386 if (downloadStarted) {
2387 // If the download has started, simply direct to the market app.
2388 startMarketIntentForPackage(v, packageName);
2389 return;
2390 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002391 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002392 .setTitle(R.string.abandoned_promises_title)
2393 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002394 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2395 @Override
2396 public void onClick(DialogInterface dialogInterface, int i) {
2397 startMarketIntentForPackage(v, packageName);
2398 }
2399 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002400 .setNeutralButton(R.string.abandoned_clean_this,
2401 new DialogInterface.OnClickListener() {
2402 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002403 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002404 mWorkspace.removeAbandonedPromise(packageName, user);
2405 }
2406 })
2407 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002408 }
2409
2410 private void startMarketIntentForPackage(View v, String packageName) {
2411 ItemInfo item = (ItemInfo) v.getTag();
2412 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2413 boolean success = startActivitySafely(v, intent, item);
2414 if (success && v instanceof BubbleTextView) {
2415 mWaitingForResume = (BubbleTextView) v;
2416 mWaitingForResume.setStayPressed(true);
2417 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002418 }
2419
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002420 /**
2421 * Event handler for an app shortcut click.
2422 *
2423 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2424 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002425 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002426 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2427 Object tag = v.getTag();
2428 if (!(tag instanceof ShortcutInfo)) {
2429 throw new IllegalArgumentException("Input must be a Shortcut");
2430 }
2431
2432 // Open shortcut
2433 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002434
2435 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002436 if ((shortcut.isDisabled &
2437 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2438 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2439 // If the app is only disabled because of the above flags, launch activity anyway.
2440 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002441 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002442 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2443 // Use a message specific to this shortcut, if it has one.
2444 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2445 return;
2446 }
2447 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002448 int error = R.string.activity_not_available;
2449 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2450 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002451 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2452 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002453 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002454 }
2455 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2456 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002457 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002458 }
2459
Chris Wren40c5ed32014-06-24 18:24:23 -04002460 // Check for abandoned promise
Sunny Goyale6e72002017-01-12 16:55:36 -08002461 if ((v instanceof BubbleTextView) && shortcut.isPromise()) {
2462 String packageName = shortcut.intent.getComponent() != null ?
2463 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2464 if (!TextUtils.isEmpty(packageName)) {
2465 onClickPendingAppItem(v, packageName,
2466 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2467 return;
2468 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002469 }
2470
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002471 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002472 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002473 }
2474
Sunny Goyala7ce1662016-05-31 15:01:35 -07002475 private void startAppShortcutOrInfoActivity(View v) {
2476 ItemInfo item = (ItemInfo) v.getTag();
2477 Intent intent = item.getIntent();
2478 if (intent == null) {
2479 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002480 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002481 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002482 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002483
2484 if (success && v instanceof BubbleTextView) {
2485 mWaitingForResume = (BubbleTextView) v;
2486 mWaitingForResume.setStayPressed(true);
2487 }
2488 }
2489
2490 /**
2491 * Event handler for a folder icon click.
2492 *
2493 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2494 */
2495 protected void onClickFolderIcon(View v) {
2496 if (LOGD) Log.d(TAG, "onClickFolder");
2497 if (!(v instanceof FolderIcon)){
2498 throw new IllegalArgumentException("Input must be a FolderIcon");
2499 }
2500
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002501 Folder folder = ((FolderIcon) v).getFolder();
2502 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002503 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002504 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 }
Michael Jurka5130e402011-10-13 04:55:35 -07002506 }
2507
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002508 /**
2509 * Event handler for the (Add) Widgets button that appears after a long press
2510 * on the home screen.
2511 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002512 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002514 if (mIsSafeModeEnabled) {
2515 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2516 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002517 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002518 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002519 }
2520
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002521 /**
2522 * Event handler for the wallpaper picker button that appears after a long press
2523 * on the home screen.
2524 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002525 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002526 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002527 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2528 return;
2529 }
2530
Tony Wickham785f7a52015-08-31 17:28:32 -07002531 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2532 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002533 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002534 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002535 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002536
2537 String pickerPackage = getString(R.string.wallpaper_picker_package);
2538 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2539 if (!hasTargetPackage) {
2540 intent.setPackage(pickerPackage);
2541 }
2542
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002543 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002544 try {
2545 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2546 // If there is no target package, use the default intent chooser animation
2547 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2548 } catch (ActivityNotFoundException e) {
2549 setWaitingForResult(null);
2550 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2551 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002552 }
2553
2554 /**
2555 * Event handler for a click on the settings button that appears after a long press
2556 * on the home screen.
2557 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002558 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002559 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002560 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2561 .setPackage(getPackageName());
2562 intent.setSourceBounds(getViewBounds(v));
2563 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002564 }
2565
Adam Cohen61f560d2013-09-30 15:58:20 -07002566 public View.OnTouchListener getHapticFeedbackTouchListener() {
2567 if (mHapticFeedbackTouchListener == null) {
2568 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002569 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002570 @Override
2571 public boolean onTouch(View v, MotionEvent event) {
2572 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2573 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2574 }
2575 return false;
2576 }
2577 };
2578 }
2579 return mHapticFeedbackTouchListener;
2580 }
2581
Tony Wickhame2217252016-03-22 16:34:23 -07002582 @Override
2583 public void onAccessibilityStateChanged(boolean enabled) {
2584 mDragLayer.onAccessibilityStateChanged(enabled);
2585 }
2586
Sunny Goyal06e21a22016-08-11 16:02:02 -07002587 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002588 if (isOnCustomContent()) {
2589 // Custom content screen doesn't participate in drag and drop. If on custom
2590 // content screen, move to default.
2591 moveWorkspaceToDefaultScreen();
2592 }
Adam Cohen9211d422014-10-07 18:14:53 -07002593 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002594
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002595 /**
2596 * Called when the user stops interacting with the launcher.
2597 * This implies that the user is now on the homescreen and is not doing housekeeping.
2598 */
Adam Cohen9211d422014-10-07 18:14:53 -07002599 protected void onInteractionEnd() {
2600 if (mLauncherCallbacks != null) {
2601 mLauncherCallbacks.onInteractionEnd();
2602 }
2603 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002604
2605 /**
2606 * Called when the user starts interacting with the launcher.
2607 * The possible interactions are:
2608 * - open all apps
2609 * - reorder an app shortcut, or a widget
2610 * - open the overview mode.
2611 * This is a good time to stop doing things that only make sense
2612 * when the user is on the homescreen and not doing housekeeping.
2613 */
Adam Cohen9211d422014-10-07 18:14:53 -07002614 protected void onInteractionBegin() {
2615 if (mLauncherCallbacks != null) {
2616 mLauncherCallbacks.onInteractionBegin();
2617 }
2618 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002619
Winson Chungcd99cd32015-04-29 11:03:24 -07002620 /** Updates the interaction state. */
2621 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002622 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002623 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002624 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2625 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002626 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002627 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002628 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002629 onInteractionEnd();
2630 }
2631 }
2632
Sunny Goyala7ce1662016-05-31 15:01:35 -07002633 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002635 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2636 try {
2637 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2638 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2639 // is enabled by default on NYC.
2640 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2641 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002642
2643 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2644 String id = ((ShortcutInfo) info).getDeepShortcutId();
2645 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302646 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002647 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002648 } else {
2649 // Could be launching some bookkeeping activity
2650 startActivity(intent, optsBundle);
2651 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002652 } finally {
2653 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002656 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2657 // corresponding permission. Show the appropriate permission prompt if that
2658 // is the case.
2659 if (intent.getComponent() == null
2660 && Intent.ACTION_CALL.equals(intent.getAction())
2661 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2662 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002663
2664 setWaitingForResult(PendingRequestArgs
2665 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002666 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2667 REQUEST_PERMISSION_CALL_PHONE);
2668 } else {
2669 // No idea why this was thrown.
2670 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 }
2673 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002674
Sunny Goyalfe770c92016-09-27 14:38:58 -07002675 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002676 private Bundle getActivityLaunchOptions(View v) {
2677 if (Utilities.ATLEAST_MARSHMALLOW) {
2678 int left = 0, top = 0;
2679 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2680 if (v instanceof TextView) {
2681 // Launch from center of icon, not entire view
2682 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2683 if (icon != null) {
2684 Rect bounds = icon.getBounds();
2685 left = (width - bounds.width()) / 2;
2686 top = v.getPaddingTop();
2687 width = bounds.width();
2688 height = bounds.height();
2689 }
2690 }
2691 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2692 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2693 // On L devices, we use the device default slide-up transition.
2694 // On L MR1 devices, we use a custom version of the slide-up transition which
2695 // doesn't have the delay present in the device default.
2696 return ActivityOptions.makeCustomAnimation(
2697 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2698 }
2699 return null;
2700 }
2701
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002702 private Rect getViewBounds(View v) {
2703 int[] pos = new int[2];
2704 v.getLocationOnScreen(pos);
2705 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2706 }
2707
Sunny Goyala7ce1662016-05-31 15:01:35 -07002708 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002709 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2710 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2711 return false;
2712 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002713 // Only launch using the new animation if the shortcut has not opted out (this is a
2714 // private contract between launcher and may be ignored in the future).
2715 boolean useLaunchAnimation = (v != null) &&
2716 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2717 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2718
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002719 UserHandle user = null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002720 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2721 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2722 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002723 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002724
2725 // Prepare intent
2726 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2727 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002728 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002729 }
2730 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002731 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2732 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002733 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002734 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002735 // Shortcuts need some special checks due to legacy reasons.
2736 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002737 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002738 // Could be launching some bookkeeping activity
2739 startActivity(intent, optsBundle);
2740 } else {
2741 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2742 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2743 }
2744 return true;
2745 } catch (ActivityNotFoundException|SecurityException e) {
2746 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2747 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2748 }
2749 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002750 }
2751
Tony Wickhamdadb3042016-02-24 11:07:00 -08002752 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002753 public boolean dispatchTouchEvent(MotionEvent ev) {
2754 mLastDispatchTouchEventX = ev.getX();
2755 return super.dispatchTouchEvent(ev);
2756 }
2757
2758 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002759 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002760 if (!isDraggingEnabled()) return false;
2761 if (isWorkspaceLocked()) return false;
2762 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002764 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2765 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002766 onLongClickAllAppsButton(v);
2767 return true;
2768 }
2769
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002770
Jon Miranda51f037d2016-11-07 08:37:20 -08002771 boolean ignoreLongPressToOverview =
2772 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002773
Adam Cohen1697b792013-09-17 19:08:21 -07002774 if (v instanceof Workspace) {
2775 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002776 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302777 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2778 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002779 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002780 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002781 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2782 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2783 return true;
2784 } else {
2785 return false;
2786 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002787 } else {
2788 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002789 }
Adam Cohen1697b792013-09-17 19:08:21 -07002790 }
2791
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002792 CellLayout.CellInfo longClickCellInfo = null;
2793 View itemUnderLongClick = null;
2794 if (v.getTag() instanceof ItemInfo) {
2795 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002796 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002797 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002798 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002799 }
2800
Winson Chung3d503fb2011-07-13 17:25:49 -07002801 // The hotseat touch handling does not go through Workspace, and we always allow long press
2802 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002803 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002804 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002805 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002806 if (mWorkspace.isInOverviewMode()) {
2807 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302808 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2809 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002810 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002811 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002812 return false;
2813 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302814 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2815 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002816 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002817 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002818 }
Jon Miranda379198e2016-09-23 08:54:40 -07002819 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2820 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002822 final boolean isAllAppsButton =
2823 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2824 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2825 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002826 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002827 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002828 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 }
2830 }
2831 }
2832 return true;
2833 }
2834
Winson Chung3d503fb2011-07-13 17:25:49 -07002835 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002836 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002837 return mHotseat != null && layout != null &&
2838 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2839 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002840
Winson Chung3d503fb2011-07-13 17:25:49 -07002841 /**
2842 * Returns the CellLayout of the specified container at the specified screen.
2843 */
Sunny Goyal92820592015-03-02 11:31:35 -08002844 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002845 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2846 if (mHotseat != null) {
2847 return mHotseat.getLayout();
2848 } else {
2849 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002850 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002851 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002852 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002853 }
2854 }
2855
Winson Chungb745afb2015-03-02 11:51:23 -08002856 /**
2857 * For overridden classes.
2858 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002859 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002860 return isAppsViewVisible();
2861 }
2862
2863 public boolean isAppsViewVisible() {
2864 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2865 }
2866
2867 public boolean isWidgetsViewVisible() {
2868 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002869 }
2870
Michael Jurkae326f182011-11-21 14:05:46 -08002871 @Override
2872 public void onTrimMemory(int level) {
2873 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002874 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2875 // The widget preview db can result in holding onto over
2876 // 3MB of memory for caching which isn't necessary.
2877 SQLiteDatabase.releaseMemory();
2878
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002879 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002880 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002881 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002882 if (mLauncherCallbacks != null) {
2883 mLauncherCallbacks.onTrimMemory(level);
2884 }
Michael Jurkae326f182011-11-21 14:05:46 -08002885 }
2886
Winson5c6bdbb2015-09-03 11:36:19 -07002887 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002888 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002889 }
2890
Winson5c6bdbb2015-09-03 11:36:19 -07002891 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002892 boolean changed = mState != State.WORKSPACE ||
2893 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002894 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002895 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002896 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002897 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002898
Michael Jurkab3e22d92011-10-31 15:58:33 -07002899 // Set focus to the AppsCustomize button
2900 if (mAllAppsButton != null) {
2901 mAllAppsButton.requestFocus();
2902 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002903 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002904
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002905 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08002906 setState(State.WORKSPACE);
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002907
Winson Chung0f785722015-04-08 10:27:49 -07002908 if (changed) {
2909 // Send an accessibility event to announce the context change
2910 getWindow().getDecorView()
2911 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002912 }
Winson5c6bdbb2015-09-03 11:36:19 -07002913 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002914 }
2915
Winsone9f27272015-10-13 10:47:51 -07002916 /**
2917 * Shows the overview button.
2918 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002919 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002920 showOverviewMode(animated, false);
2921 }
2922
2923 /**
2924 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2925 * onto one of the overview panel buttons.
2926 */
2927 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2928 Runnable postAnimRunnable = null;
2929 if (requestButtonFocus) {
2930 postAnimRunnable = new Runnable() {
2931 @Override
2932 public void run() {
2933 // Hitting the menu button when in touch mode does not trigger touch mode to
2934 // be disabled, so if requested, force focus on one of the overview panel
2935 // buttons.
2936 mOverviewPanel.requestFocusFromTouch();
2937 }
2938 };
2939 }
Adam Cohened307df2013-10-02 09:37:31 -07002940 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002941 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002942 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Mario Bertschleracbf5702017-03-03 10:58:06 -08002943 setState(State.WORKSPACE);
2944
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002945 // If animated from long press, then don't allow any of the controller in the drag
2946 // layer to intercept any remaining touch.
2947 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002948 }
2949
Mario Bertschleracbf5702017-03-03 10:58:06 -08002950 private void setState(State state) {
2951 this.mState = state;
2952 updateSoftInputMode();
2953 }
2954
2955 private void updateSoftInputMode() {
2956 if (FeatureFlags.LAUNCHER3_UPDATE_SOFT_INPUT_MODE) {
2957 final int mode;
2958 if (isAppsViewVisible()) {
2959 mode = SOFT_INPUT_MODE_ALL_APPS;
2960 } else {
2961 mode = SOFT_INPUT_MODE_DEFAULT;
2962 }
2963 getWindow().setSoftInputMode(mode);
2964 }
2965 }
2966
Winson Chungb745afb2015-03-02 11:51:23 -08002967 /**
2968 * Shows the apps view.
2969 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002970 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002971 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002972 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002973 if (updatePredictedApps) {
2974 tryAndUpdatePredictedApps();
2975 }
Winson Chung76648c52015-07-10 14:33:23 -07002976 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002977 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978
Winson Chungb745afb2015-03-02 11:51:23 -08002979 /**
2980 * Shows the widgets view.
2981 */
2982 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002983 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002984 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002985 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002986 }
Winson Chung76648c52015-07-10 14:33:23 -07002987 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002988
2989 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002990 @Override
2991 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002992 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002993 }
2994 });
Winson Chungb745afb2015-03-02 11:51:23 -08002995 }
2996
2997 /**
2998 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002999 *
3000 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003001 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003002 // TODO: calling method should use the return value so that when {@code false} is returned
3003 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003004 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003005 if (!(mState == State.WORKSPACE ||
3006 mState == State.APPS_SPRING_LOADED ||
3007 mState == State.WIDGETS_SPRING_LOADED ||
3008 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003009 return false;
3010 }
3011 if (toState != State.APPS && toState != State.WIDGETS) {
3012 return false;
3013 }
Winson Chungb745afb2015-03-02 11:51:23 -08003014
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003015 // This is a safe and supported transition to bypass spring_loaded mode.
3016 if (mExitSpringLoadedModeRunnable != null) {
3017 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3018 mExitSpringLoadedModeRunnable = null;
3019 }
3020
Winson Chungb745afb2015-03-02 11:51:23 -08003021 if (toState == State.APPS) {
Sunny Goyaldb364372016-10-26 19:12:47 -07003022 mStateTransitionAnimation.startAnimationToAllApps(animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003023 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07003024 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003025 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003026
Michael Jurkab3e22d92011-10-31 15:58:33 -07003027 // Change the state *after* we've called all the transition code
Mario Bertschleracbf5702017-03-03 10:58:06 -08003028 setState(toState);
Sunny Goyal740ac7f2016-09-28 16:47:32 -07003029 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003030
3031 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003032 getWindow().getDecorView()
3033 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003034 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003035 }
3036
Winson Chungcd99cd32015-04-29 11:03:24 -07003037 /**
3038 * Updates the workspace and interaction state on state change, and return the animation to this
3039 * new state.
3040 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003041 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07003042 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003043 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003044 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003045 updateInteraction(fromState, toState);
3046 return anim;
3047 }
3048
Hyunyoung Song3f471442015-04-08 19:01:34 -07003049 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003050 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003051 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003052 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003053 }
Winson Chungb745afb2015-03-02 11:51:23 -08003054
Winson Chung006ee262015-08-03 14:40:11 -07003055 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003056 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003057 null /* onCompleteRunnable */);
Mario Bertschleracbf5702017-03-03 10:58:06 -08003058 setState(State.WORKSPACE_SPRING_LOADED);
Michael Jurkad3ef3062010-11-23 16:23:58 -08003059 }
Adam Cohen7777d962011-08-18 18:58:38 -07003060
Hyunyoung Song3f471442015-04-08 19:01:34 -07003061 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003062 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003063 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003064
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003065 if (mExitSpringLoadedModeRunnable != null) {
3066 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3067 }
3068 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003069 @Override
3070 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003071 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003072 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3073 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003074 // Before we show workspace, hide all apps again because
3075 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3076 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003077 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003078 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003079 } else {
3080 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003081 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003082 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003083 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003084 };
3085 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003086 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003087
Tony Wickhame0c33232016-02-08 11:37:04 -08003088 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003089 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3090 || mState == State.WIDGETS_SPRING_LOADED;
3091 }
3092
Sunny Goyal0f76b562016-12-13 19:37:10 -08003093 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003094 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003095 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003096 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003097 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003098 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003099 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3100 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003102 }
3103
Winson Chung4ac30062015-05-08 17:34:17 -07003104 /**
3105 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3106 * resumed.
3107 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003108 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003109 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003110 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003111 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003112 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003113 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003114 }
3115 }
3116 }
3117
Michael Jurkab3e22d92011-10-31 15:58:33 -07003118 void lockAllApps() {
3119 // TODO
3120 }
3121
3122 void unlockAllApps() {
3123 // TODO
3124 }
3125
Michael Jurkad7c28052012-04-27 15:43:36 -07003126 @Override
3127 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003128 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003129 final List<CharSequence> text = event.getText();
3130 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003131 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003132 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003133 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003134 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003135 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003136 } else if (mWorkspace != null) {
3137 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003138 } else {
3139 text.add(getString(R.string.all_apps_home_button_label));
3140 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003141 return result;
3142 }
3143
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003144 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003145 * If the activity is currently paused, signal that we need to run the passed Runnable
3146 * in onResume.
3147 *
3148 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003149 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3150 * wrong when we're not running, and if the activity comes back to what the configuration was
3151 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003152 *
3153 * Implementation of the method from LauncherModel.Callbacks.
3154 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003155 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003156 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003157 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003158 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003159 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003160 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003161 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003162 }
3163 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003164 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003165 return true;
3166 } else {
3167 return false;
3168 }
3169 }
3170
Michael Jurkac402cd92013-05-20 15:49:32 +02003171 private boolean waitUntilResume(Runnable run) {
3172 return waitUntilResume(run, false);
3173 }
3174
Michael Jurka1e2f4652013-07-08 18:03:46 -07003175 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003176 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003177 }
3178
Michael Jurka7607c2f2013-04-03 14:33:19 -07003179 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003180 * If the activity is currently paused, signal that we need to re-run the loader
3181 * in onResume.
3182 *
3183 * This needs to be called from incoming places where resources might have been loaded
3184 * while we are paused. That is becaues the Configuration might be wrong
3185 * when we're not running, and if it comes back to what it was when we
3186 * were paused, we are not restarted.
3187 *
3188 * Implementation of the method from LauncherModel.Callbacks.
3189 *
3190 * @return true if we are currently paused. The caller might be able to
3191 * skip some work in that case since we will come back again.
3192 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003193 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003194 public boolean setLoadOnResume() {
3195 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003196 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003197 mOnResumeNeedsLoad = true;
3198 return true;
3199 } else {
3200 return false;
3201 }
3202 }
3203
3204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003207 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003208 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003209 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003210 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003211 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003212 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003214 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003215
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003217 * Clear any pending bind callbacks. This is called when is loader is planning to
3218 * perform a full rebind from scratch.
3219 */
3220 @Override
3221 public void clearPendingBinds() {
3222 mBindOnResumeCallbacks.clear();
3223 if (mPendingExecutor != null) {
3224 mPendingExecutor.markCompleted();
3225 mPendingExecutor = null;
3226 }
3227 }
3228
3229 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 * Refreshes the shortcuts shown on the workspace.
3231 *
3232 * Implementation of the method from LauncherModel.Callbacks.
3233 */
3234 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003235 if (LauncherAppState.PROFILE_STARTUP) {
3236 Trace.beginSection("Starting page bind");
3237 }
Jon Miranda6dc7f1e2017-02-21 12:37:51 -08003238
3239 AbstractFloatingView.closeAllOpenViews(this);
3240
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003241 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003242
Winson Chungd64d1762013-08-20 14:37:16 -07003243 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003244 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003245 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003246
Winson Chung3d503fb2011-07-13 17:25:49 -07003247 if (mHotseat != null) {
3248 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003249 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003250 if (LauncherAppState.PROFILE_STARTUP) {
3251 Trace.endSection();
3252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 }
3254
Adam Cohendcd297f2013-06-18 13:13:40 -07003255 @Override
3256 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003257 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003258 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3259 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003260 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3261 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3262 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003263 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3264 // If there are no screens, we need to have an empty screen
3265 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003266 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003267 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003268
3269 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003270 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003271 if (hasCustomContentToLeft()) {
3272 mWorkspace.createCustomContentContainer();
3273 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003274 }
Winsonc7d2e832016-07-28 12:24:55 -07003275
3276 // After we have added all the screens, if the wallpaper was locked to the default state,
3277 // then notify to indicate that it can be released and a proper wallpaper offset can be
3278 // computed before the next layout
3279 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003280 }
3281
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003282 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003283 int count = orderedScreenIds.size();
3284 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003285 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003286 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003287 // No need to bind the first screen, as its always bound.
3288 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3289 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003290 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003291 }
3292
Winson Chungd64d1762013-08-20 14:37:16 -07003293 public void bindAppsAdded(final ArrayList<Long> newScreens,
3294 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003295 final ArrayList<ItemInfo> addAnimated,
3296 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003297 Runnable r = new Runnable() {
3298 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003299 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003300 }
3301 };
3302 if (waitUntilResume(r)) {
3303 return;
3304 }
3305
Winson Chungd64d1762013-08-20 14:37:16 -07003306 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003307 if (newScreens != null) {
3308 bindAddScreens(newScreens);
3309 }
Winson Chungd64d1762013-08-20 14:37:16 -07003310
3311 // We add the items without animation on non-visible pages, and with
3312 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003313 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003314 bindItems(addNotAnimated, 0,
3315 addNotAnimated.size(), false);
3316 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003317 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003318 bindItems(addAnimated, 0,
3319 addAnimated.size(), true);
3320 }
Winson Chungc58497e2013-09-03 17:48:37 -07003321
Winson Chung87412982013-10-03 18:34:14 -07003322 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003323 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003324
Winson Chungb745afb2015-03-02 11:51:23 -08003325 if (addedApps != null && mAppsView != null) {
3326 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003327 }
Winson Chungd64d1762013-08-20 14:37:16 -07003328 }
3329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003330 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 * Bind the items start-end from the list.
3332 *
3333 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003334 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003335 @Override
Sunny Goyal3be633b2016-12-08 09:59:25 -08003336 public void bindItems(final ArrayList<ItemInfo> items, final int start, final int end,
Winson Chung64359a52013-07-08 17:17:08 -07003337 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003338 Runnable r = new Runnable() {
3339 public void run() {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003340 bindItems(items, start, end, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003341 }
3342 };
3343 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003344 return;
3345 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003346
Sunny Goyal3be633b2016-12-08 09:59:25 -08003347 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003348 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3349 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003350 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003351 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003352 long newItemsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003353 for (int i = start; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003354 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003355
3356 // Short circuit if we are loading dock items for a configuration which has no dock
3357 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3358 mHotseat == null) {
3359 continue;
3360 }
3361
Sunny Goyal639e9062015-08-19 19:17:06 -07003362 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 switch (item.itemType) {
3364 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3365 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003366 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003367 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003368 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003370 }
3371 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003372 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003373 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003374 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003376 }
3377 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
3378 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
3379 if (mIsSafeModeEnabled) {
3380 view = new PendingAppWidgetHostView(this, info, mIconCache, true);
3381 } else {
3382 LauncherAppWidgetProviderInfo providerInfo =
3383 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
3384 if (providerInfo == null) {
3385 deleteWidgetInfo(info);
3386 continue;
3387 }
3388 view = mAppWidgetHost.createView(this, info.appWidgetId, providerInfo);
3389 }
3390 prepareAppWidget((AppWidgetHostView) view, info);
3391 break;
3392 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003393 default:
3394 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003396
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003397 /*
3398 * Remove colliding items.
3399 */
3400 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3401 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3402 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3403 View v = cl.getChildAt(item.cellX, item.cellY);
3404 Object tag = v.getTag();
3405 String desc = "Collision while binding workspace item: " + item
3406 + ". Collides with " + tag;
3407 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3408 throw (new RuntimeException(desc));
3409 } else {
3410 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003411 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003412 continue;
3413 }
3414 }
3415 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303416 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003417 if (animateIcons) {
3418 // Animate all the applications up now
3419 view.setAlpha(0f);
3420 view.setScaleX(0f);
3421 view.setScaleY(0f);
3422 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003423 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003425 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003426
Winson Chung997a9232013-07-24 15:33:46 -07003427 if (animateIcons) {
3428 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003429 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003430 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003431 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003432 final Runnable startBounceAnimRunnable = new Runnable() {
3433 public void run() {
3434 anim.playTogether(bounceAnims);
3435 anim.start();
3436 }
3437 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003438 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003439 // We post the animation slightly delayed to prevent slowdowns
3440 // when we are loading right after we return to launcher.
3441 mWorkspace.postDelayed(new Runnable() {
3442 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003443 if (mWorkspace != null) {
3444 mWorkspace.snapToPage(newScreenIndex);
3445 mWorkspace.postDelayed(startBounceAnimRunnable,
3446 NEW_APPS_ANIMATION_DELAY);
3447 }
Winson Chung94d67682013-09-25 16:29:40 -07003448 }
3449 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003450 } else {
3451 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003452 }
3453 }
Winson Chung64359a52013-07-08 17:17:08 -07003454 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003456 }
3457
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 /**
3459 * Add the views for a widget to the workspace.
3460 *
3461 * Implementation of the method from LauncherModel.Callbacks.
3462 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003463 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003464 Runnable r = new Runnable() {
3465 public void run() {
3466 bindAppWidget(item);
3467 }
3468 };
3469 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003470 return;
3471 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003472
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003473 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303474 PendingAppWidgetHostView view =
3475 new PendingAppWidgetHostView(this, item, mIconCache, true);
3476 prepareAppWidget(view, item);
3477 mWorkspace.addInScreen(view, item);
3478 mWorkspace.requestLayout();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003479 return;
3480 }
3481
Daniel Sandler843e8602010-06-07 14:59:01 -04003482 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3483 if (DEBUG_WIDGETS) {
3484 Log.d(TAG, "bindAppWidget: " + item);
3485 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003487 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003488
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003489 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3490 // If the provider is not ready, bind as a pending widget.
3491 appWidgetInfo = null;
3492 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3493 // The widget id is not valid. Try to find the widget based on the provider info.
3494 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3495 } else {
3496 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3497 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003498
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003499 // If the provider is ready, but the width is not yet restored, try to restore it.
3500 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3501 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003502 if (appWidgetInfo == null) {
3503 if (DEBUG_WIDGETS) {
3504 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3505 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003506 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003507 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003508 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003509 return;
3510 }
Sunny Goyalff572272014-07-23 13:58:07 -07003511
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003512 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003513 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003514 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3515 // Id has not been allocated yet. Allocate a new id.
3516 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3517 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003518
Sunny Goyald478c832016-04-01 12:04:16 -07003519 // Also try to bind the widget. If the bind fails, the user will be shown
3520 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003521 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003522 pendingInfo.spanX = item.spanX;
3523 pendingInfo.spanY = item.spanY;
3524 pendingInfo.minSpanX = item.minSpanX;
3525 pendingInfo.minSpanY = item.minSpanY;
3526 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003527
3528 boolean isDirectConfig =
3529 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3530 if (isDirectConfig && item.bindOptions != null) {
3531 Bundle newOptions = item.bindOptions.getExtras();
3532 if (options != null) {
3533 newOptions.putAll(options);
3534 }
3535 options = newOptions;
3536 }
Sunny Goyald478c832016-04-01 12:04:16 -07003537 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3538 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003539
Sunny Goyal86df1382016-08-10 15:03:22 -07003540 // We tried to bind once. If we were not able to bind, we would need to
3541 // go through the permission dialog, which means we cannot skip the config
3542 // activity.
3543 item.bindOptions = null;
3544 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3545
Sunny Goyald478c832016-04-01 12:04:16 -07003546 // Bind succeeded
3547 if (success) {
3548 // If the widget has a configure activity, it is still needs to set it up,
3549 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003550 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003551 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3552 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003553 }
Sunny Goyald478c832016-04-01 12:04:16 -07003554
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003555 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003556 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003557 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003558 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003559 // The widget was marked as UI not ready, but there is no configure activity to
3560 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003561 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003562 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003563 }
Sunny Goyalff572272014-07-23 13:58:07 -07003564 }
3565
Sunny Goyald5462aa2016-11-22 16:52:39 +05303566 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003567 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003568 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003569 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3570 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003571 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003572
Sunny Goyal56c73602015-09-25 12:55:01 -07003573 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003574 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003575 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003576 deleteWidgetInfo(item);
3577 return;
3578 }
3579
Sunny Goyal233ee962015-08-03 13:05:01 -07003580 item.minSpanX = appWidgetInfo.minSpanX;
3581 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303582 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003583 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303584 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003585 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303586 prepareAppWidget(view, item);
3587 mWorkspace.addInScreen(view, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003588 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589
Daniel Sandler843e8602010-06-07 14:59:01 -04003590 if (DEBUG_WIDGETS) {
3591 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3592 + (SystemClock.uptimeMillis()-start) + "ms");
3593 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 }
3595
Sunny Goyalff572272014-07-23 13:58:07 -07003596 /**
3597 * Restores a pending widget.
3598 *
3599 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003600 */
Sunny Goyald478c832016-04-01 12:04:16 -07003601 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003602 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3603 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3604 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003605 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003606 }
3607
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003608 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003609 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003610
3611 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003612 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003613 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003614 }
3615
Adam Cohen1462de32012-07-24 22:34:36 -07003616 public void onPageBoundSynchronously(int page) {
3617 mSynchronouslyBoundPages.add(page);
3618 }
3619
Sunny Goyal527c7d32015-08-28 15:19:36 -07003620 @Override
3621 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3622 if (mPendingExecutor != null) {
3623 mPendingExecutor.markCompleted();
3624 }
3625 mPendingExecutor = executor;
3626 executor.attachTo(this);
3627 }
3628
3629 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3630 if (mPendingExecutor == executor) {
3631 mPendingExecutor = null;
3632 }
3633 }
3634
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003635 @Override
3636 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3637 Runnable r = new Runnable() {
3638 public void run() {
3639 finishFirstPageBind(executor);
3640 }
3641 };
3642 if (waitUntilResume(r)) {
3643 return;
3644 }
3645
3646 Runnable onComplete = new Runnable() {
3647 @Override
3648 public void run() {
3649 if (executor != null) {
3650 executor.onLoadAnimationCompleted();
3651 }
3652 }
3653 };
3654 if (mDragLayer.getAlpha() < 1) {
3655 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3656 } else {
3657 onComplete.run();
3658 }
3659 }
3660
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 /**
3662 * Callback saying that there aren't any more items to bind.
3663 *
3664 * Implementation of the method from LauncherModel.Callbacks.
3665 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003666 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003667 Runnable r = new Runnable() {
3668 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003669 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003670 }
3671 };
3672 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003673 return;
3674 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003675 if (LauncherAppState.PROFILE_STARTUP) {
3676 Trace.beginSection("Page bind completed");
3677 }
Adam Cohen1462de32012-07-24 22:34:36 -07003678 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003680 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003681
Sunny Goyal2100c782016-08-22 16:00:03 -07003682 if (mPendingActivityResult != null) {
3683 handleActivityResult(mPendingActivityResult.requestCode,
3684 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3685 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003686 }
3687
Sunny Goyal756adbc2015-04-16 15:20:51 -07003688 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003689
Tony Wickham010d2552017-01-20 08:15:28 -08003690 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3691
Adam Cohen9211d422014-10-07 18:14:53 -07003692 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003693 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003694 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003695 if (LauncherAppState.PROFILE_STARTUP) {
3696 Trace.endSection();
3697 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003698 }
3699
Winson Chunga2413752012-04-03 14:22:34 -07003700 private boolean canRunNewAppsAnimation() {
3701 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003702 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003703 }
3704
Winson Chungc9168342013-06-26 14:54:55 -07003705 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003706 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003707 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3708 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003709 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003710 return bounceAnim;
3711 }
3712
Adam Cohen27772732013-11-11 14:00:56 +00003713 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003714 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003715 }
3716
Tony Wickham55616cd2015-09-23 14:55:17 -07003717 public int getSearchBarHeight() {
3718 if (mLauncherCallbacks != null) {
3719 return mLauncherCallbacks.getSearchBarHeight();
3720 }
3721 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3722 }
3723
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003724 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003725 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3726 * multiple calls to bind the same list.)
3727 */
3728 @Thunk ArrayList<AppInfo> mTmpAppsList;
3729 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3730 public void run() {
3731 bindAllApplications(mTmpAppsList);
3732 mTmpAppsList = null;
3733 }
3734 };
3735
3736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 * Add the icons for all apps.
3738 *
3739 * Implementation of the method from LauncherModel.Callbacks.
3740 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003741 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003742 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3743 mTmpAppsList = apps;
3744 return;
3745 }
3746
Winson Chungb745afb2015-03-02 11:51:23 -08003747 if (mAppsView != null) {
3748 mAppsView.setApps(apps);
3749 }
Adam Cohen9211d422014-10-07 18:14:53 -07003750 if (mLauncherCallbacks != null) {
3751 mLauncherCallbacks.bindAllApplications(apps);
3752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 }
3754
3755 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003756 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3757 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3758 */
3759 @Override
3760 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003761 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003762 }
3763
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003764 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 * A package was updated.
3766 *
3767 * Implementation of the method from LauncherModel.Callbacks.
3768 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003769 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003770 Runnable r = new Runnable() {
3771 public void run() {
3772 bindAppsUpdated(apps);
3773 }
3774 };
3775 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003776 return;
3777 }
3778
Winson Chungb745afb2015-03-02 11:51:23 -08003779 if (mAppsView != null) {
3780 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 }
3783
Sunny Goyal4390ace2014-10-13 11:33:11 -07003784 @Override
3785 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3786 Runnable r = new Runnable() {
3787 public void run() {
3788 bindWidgetsRestored(widgets);
3789 }
3790 };
3791 if (waitUntilResume(r)) {
3792 return;
3793 }
3794 mWorkspace.widgetsRestored(widgets);
3795 }
3796
Joe Onorato9c1289c2009-08-17 11:03:03 -04003797 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003798 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003799 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003800 *
3801 * @param updated list of shortcuts which have changed.
3802 * @param removed list of shortcuts which were deleted in the background. This can happen when
3803 * an app gets removed from the system or some of its components are no longer
3804 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003805 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003806 @Override
3807 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003808 final ArrayList<ShortcutInfo> removed, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003809 Runnable r = new Runnable() {
3810 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003811 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003812 }
3813 };
3814 if (waitUntilResume(r)) {
3815 return;
3816 }
3817
Sunny Goyal4390ace2014-10-13 11:33:11 -07003818 if (!updated.isEmpty()) {
3819 mWorkspace.updateShortcuts(updated);
3820 }
3821
3822 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003823 HashSet<ComponentName> removedComponents = new HashSet<>();
3824 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3825
Sunny Goyal4390ace2014-10-13 11:33:11 -07003826 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003827 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08003828 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003829 } else {
3830 removedComponents.add(si.getTargetComponent());
3831 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003832 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003833
3834 if (!removedComponents.isEmpty()) {
3835 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3836 mWorkspace.removeItemsByMatcher(matcher);
3837 mDragController.onAppsRemoved(matcher);
3838 }
3839
3840 if (!removedDeepShortcuts.isEmpty()) {
3841 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3842 mWorkspace.removeItemsByMatcher(matcher);
3843 mDragController.onAppsRemoved(matcher);
3844 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003845 }
3846 }
3847
3848 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003849 * Update the state of a package, typically related to install state.
3850 *
3851 * Implementation of the method from LauncherModel.Callbacks.
3852 */
Sunny Goyale755d462014-07-22 13:48:29 -07003853 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003854 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3855 Runnable r = new Runnable() {
3856 public void run() {
3857 bindRestoreItemsChange(updates);
3858 }
3859 };
3860 if (waitUntilResume(r)) {
3861 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003862 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003863
Sunny Goyal756adbc2015-04-16 15:20:51 -07003864 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003865 }
3866
3867 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003868 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003869 * in addition to specific applications to remove, the reason being that
3870 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003871 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003872 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003874 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003875 public void bindWorkspaceComponentsRemoved(
3876 final HashSet<String> packageNames, final HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003877 final UserHandle user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003878 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003879 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003880 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003881 }
Winson Chungd64d1762013-08-20 14:37:16 -07003882 };
3883 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003884 return;
3885 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003886 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003887 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3888 mWorkspace.removeItemsByMatcher(matcher);
3889 mDragController.onAppsRemoved(matcher);
3890
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003891 }
3892 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003893 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3894 mWorkspace.removeItemsByMatcher(matcher);
3895 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003896 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003897 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003898
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003899 @Override
3900 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3901 Runnable r = new Runnable() {
3902 public void run() {
3903 bindAppInfosRemoved(appInfos);
3904 }
3905 };
3906 if (waitUntilResume(r)) {
3907 return;
Joe Onorato36115782010-06-17 13:28:48 -04003908 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003909
Winson Chungdf95eb12013-10-16 14:57:07 -07003910 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003911 if (mAppsView != null) {
3912 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003913 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914 }
Joe Onoratobe386092009-11-17 17:32:16 -08003915
Sunny Goyald164b7f2016-10-12 20:49:31 -07003916 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003917 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003918 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003919 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003920 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003921
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003922 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003923 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3924 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3925 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003926 return;
3927 }
3928
Sunny Goyald164b7f2016-10-12 20:49:31 -07003929 if (mWidgetsView != null && allWidgets != null) {
3930 mWidgetsView.setWidgets(allWidgets);
3931 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003932 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003933 }
3934
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003935 @Override
3936 public void notifyWidgetProvidersChanged() {
3937 if (mWorkspace.getState().shouldUpdateWidget) {
3938 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3939 }
3940 }
3941
Winson Chung4b919f82012-05-01 10:44:08 -07003942 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003943 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003944 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003945 }
3946 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003947
Winson Chung4b919f82012-05-01 10:44:08 -07003948 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003949 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003950 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003951 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003952 } else {
3953 mHandler.postDelayed(new Runnable() {
3954 public void run() {
3955 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3956 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003957 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003958 }
Winson Chung4b919f82012-05-01 10:44:08 -07003959 }
Winson Chung400438b2011-01-16 17:53:48 -08003960 }
3961
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003962 private void markAppsViewShown() {
3963 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3964 return;
3965 }
3966 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3967 }
3968
3969 private boolean shouldShowDiscoveryBounce() {
3970 if (mState != mState.WORKSPACE) {
3971 return false;
3972 }
3973 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3974 return true;
3975 }
3976 if (!mIsResumeFromActionScreenOff) {
3977 return false;
3978 }
3979 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3980 return false;
3981 }
3982 return true;
3983 }
3984
Winson Chung5ffd51d2015-06-25 11:36:50 -07003985 protected void moveWorkspaceToDefaultScreen() {
3986 mWorkspace.moveToDefaultScreen(false);
3987 }
3988
Winson Chung80baf5a2010-08-09 16:03:15 -07003989 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003990 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003991 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003992 @Override
3993 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3994 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003995
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003996 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3997 writer.println(prefix + "Workspace Items");
3998 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
3999 writer.println(prefix + " Homescreen " + i);
4000
4001 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4002 for (int j = 0; j < layout.getChildCount(); j++) {
4003 Object tag = layout.getChildAt(j).getTag();
4004 if (tag != null) {
4005 writer.println(prefix + " " + tag.toString());
4006 }
4007 }
4008 }
4009
4010 writer.println(prefix + " Hotseat");
4011 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07004012 for (int j = 0; j < layout.getChildCount(); j++) {
4013 Object tag = layout.getChildAt(j).getTag();
4014 if (tag != null) {
4015 writer.println(prefix + " " + tag.toString());
4016 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004017 }
Sunny Goyala1365452015-10-01 15:46:24 -07004018
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08004019 try {
4020 FileLog.flushAll(writer);
4021 } catch (Exception e) {
4022 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004023 }
4024 }
4025
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08004026 writer.println(prefix + "Misc:");
4027 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
4028 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
4029 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
4030
4031 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004032
Adam Cohen9211d422014-10-07 18:14:53 -07004033 if (mLauncherCallbacks != null) {
4034 mLauncherCallbacks.dump(prefix, fd, writer, args);
4035 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004036 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004037
Sunny Goyal66b24572016-09-21 15:57:55 -07004038 @Override
4039 @TargetApi(Build.VERSION_CODES.N)
4040 public void onProvideKeyboardShortcuts(
4041 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4042
4043 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4044 if (mState == State.WORKSPACE) {
4045 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4046 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4047 }
4048 View currentFocus = getCurrentFocus();
4049 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4050 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4051 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4052 }
4053 if (currentFocus instanceof BubbleTextView &&
4054 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4055 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4056 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4057 }
4058 if (!shortcutInfos.isEmpty()) {
4059 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4060 }
4061
4062 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4063 }
4064
4065 @Override
4066 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4067 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4068 switch (keyCode) {
4069 case KeyEvent.KEYCODE_A:
4070 if (mState == State.WORKSPACE) {
4071 showAppsView(true, true, false);
4072 return true;
4073 }
4074 break;
4075 case KeyEvent.KEYCODE_S: {
4076 View focusedView = getCurrentFocus();
4077 if (focusedView instanceof BubbleTextView
4078 && focusedView.getTag() instanceof ItemInfo
4079 && mAccessibilityDelegate.performAction(focusedView,
4080 (ItemInfo) focusedView.getTag(),
4081 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08004082 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004083 return true;
4084 }
4085 break;
4086 }
4087 case KeyEvent.KEYCODE_O:
4088 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4089 return true;
4090 }
4091 break;
4092 }
4093 }
4094 return super.onKeyShortcut(keyCode, event);
4095 }
4096
Adam Cohen59400422014-03-05 18:07:04 -08004097 public static CustomAppWidget getCustomAppWidget(String name) {
4098 return sCustomAppWidgets.get(name);
4099 }
4100
4101 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4102 return sCustomAppWidgets;
4103 }
4104
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004105 public static Launcher getLauncher(Context context) {
4106 if (context instanceof Launcher) {
4107 return (Launcher) context;
4108 }
4109 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4110 }
4111
Sunny Goyal745bad92016-05-02 10:54:12 -07004112 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4113
4114 @Override
4115 public void onSharedPreferenceChanged(
4116 SharedPreferences sharedPreferences, String key) {
4117 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4118 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4119 if (!waitUntilResume(this, true)) {
4120 run();
4121 }
4122 }
4123 }
4124
4125 @Override
4126 public void run() {
4127 setOrientation();
4128 }
4129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004130}