blob: c5cefa678041ad4f764b5b0d85901f653e0a193b [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;
Sunny Goyal9b29ca52017-02-17 10:39:44 -080068import android.view.LayoutInflater;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.MotionEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080072import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.ViewGroup;
74import android.view.ViewTreeObserver;
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
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700187 // Type: PendingRequestArgs
188 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
189 // Type: ActivityResultInfo
190 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700192 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700195 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
196 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700197
Adam Cohen091440a2015-03-18 14:16:05 -0700198 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700199 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700200
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700201 private boolean mIsSafeModeEnabled;
202
Sunny Goyal27835952017-01-13 12:15:53 -0800203 public static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800204 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700205 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700206 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chunga2413752012-04-03 14:22:34 -0700208 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700209 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700214 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700215 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800216 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700217 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700218
219 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700220
Sunny Goyalffe83f12014-08-14 17:39:34 -0700221 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700222 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700223
Michael Jurka0280c3b2010-09-17 15:00:07 -0700224 private int[] mTmpAddItemCellCoordinates = new int[2];
225
Sunny Goyal316490e2015-06-02 09:38:28 -0700226 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800227 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700228
Michael Jurka838a4ca2011-02-07 13:33:06 -0800229 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700230 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700231
Sunny Goyal47328fd2016-05-25 18:56:41 -0700232 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700233
234 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700235 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700236 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700237
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700238 // Main container view and the model for the widget tray screen.
239 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700240 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700241
Winson Chung4a2afa32012-07-19 14:53:05 -0700242 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
243 // scroll issues (because the workspace may not have been measured yet) and extra work.
244 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
245 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246
247 private SpannableStringBuilder mDefaultKeySsb = null;
248
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800251 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700252 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
Michael Jurka1e2f4652013-07-08 18:03:46 -0700254 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700255 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700256 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700257
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258 private LauncherModel mModel;
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800259 private ModelWriter mModelWriter;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800260 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700261 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700262 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700263 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700264 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700265 private boolean mHasFocus = false;
266 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267
Tony Wickham010d2552017-01-20 08:15:28 -0800268 private PopupDataProvider mPopupDataProvider;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700269
Adam Cohen61f560d2013-09-30 15:58:20 -0700270 private View.OnTouchListener mHapticFeedbackTouchListener;
271
Winson Chung400438b2011-01-16 17:53:48 -0800272 // Determines how long to wait after a rotation before restoring the screen orientation to
273 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700274 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800275
Adam Cohen1462de32012-07-24 22:34:36 -0700276 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
277
Winson Chung46353de2012-02-16 14:05:10 -0800278 // We only want to get the SharedPreferences once since it does an FS stat each time we get
279 // it from the context.
280 private SharedPreferences mSharedPrefs;
281
Adam Cohen4b66bf32015-09-18 12:15:19 -0700282 private boolean mMoveToDefaultScreenFromNewIntent;
283
Winson Chung13eb5272015-05-11 16:30:13 -0700284 // This is set to the view that launched the activity that navigated the user away from
285 // launcher. Since there is no callback for when the activity has finished launching, enable
286 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800287 private BubbleTextView mWaitingForResume;
288
Adam Cohen59400422014-03-05 18:07:04 -0800289 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
290 new HashMap<String, CustomAppWidget>();
291
Adam Cohen59400422014-03-05 18:07:04 -0800292 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700293 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
294 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800295 }
296 }
297
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700298 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
299 // state transition. If another state transition happened during this delay,
300 // simply unregister this runnable.
301 private Runnable mExitSpringLoadedModeRunnable;
302
Adam Cohen091440a2015-03-18 14:16:05 -0700303 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700304 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700305 if (mWorkspace != null) {
306 mWorkspace.buildPageHardwareLayers();
307 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700308 }
309 };
310
Sunny Goyal2100c782016-08-22 16:00:03 -0700311 // Activity result which needs to be processed after workspace has loaded.
312 private ActivityResultInfo mPendingActivityResult;
313 /**
314 * Holds extra information required to handle a result from an external call, like
315 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
316 */
317 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800318
Hyunyoung Songaa953652016-04-19 18:30:24 -0700319 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800320
Jon Miranda379198e2016-09-23 08:54:40 -0700321 private float mLastDispatchTouchEventX = 0.0f;
Jon Miranda379198e2016-09-23 08:54:40 -0700322
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700323 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700324 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400325
326 @Thunk void setOrientation() {
327 if (mRotationEnabled) {
328 unlockScreenOrientation(true);
329 } else {
330 setRequestedOrientation(
331 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700332 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400333 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700334
Sunny Goyal745bad92016-05-02 10:54:12 -0700335 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 @Override
338 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700339 if (DEBUG_STRICT_MODE) {
340 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
341 .detectDiskReads()
342 .detectDiskWrites()
343 .detectNetwork() // or .detectAll() for all detectable problems
344 .penaltyLog()
345 .build());
346 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
347 .detectLeakedSqlLiteObjects()
348 .detectLeakedClosableObjects()
349 .penaltyLog()
350 .penaltyDeath()
351 .build());
352 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700353 if (LauncherAppState.PROFILE_STARTUP) {
354 Trace.beginSection("Launcher-onCreate");
355 }
Winson Chunga2413752012-04-03 14:22:34 -0700356
Adam Cohen9211d422014-10-07 18:14:53 -0700357 if (mLauncherCallbacks != null) {
358 mLauncherCallbacks.preOnCreate();
359 }
360
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400362
Sunny Goyal87f784c2017-01-11 10:48:34 -0800363 LauncherAppState app = LauncherAppState.getInstance(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700364
Adam Cohen2e6da152015-05-06 11:42:25 -0700365 // Load configuration-specific DeviceProfile
Sunny Goyal27835952017-01-13 12:15:53 -0800366 mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
Sunny Goyalf5e37442016-11-02 10:31:24 -0700367 if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700368 Display display = getWindowManager().getDefaultDisplay();
369 Point mwSize = new Point();
370 display.getSize(mwSize);
371 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
372 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700373
Sunny Goyalf7258242015-10-19 16:59:07 -0700374 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700375 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 mModel = app.setLauncher(this);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800377 mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800378 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700379 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700380
Joe Onorato41a12d22009-10-31 18:30:00 -0400381 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700382 mAllAppsController = new AllAppsTransitionController(this);
383 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Sunny Goyalffe83f12014-08-14 17:39:34 -0700385 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700386
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700387 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
388 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700389
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700390 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
391 // this also ensures that any synchronous binding below doesn't re-trigger another
392 // LauncherModel load.
393 mPaused = false;
394
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800395 mLauncherView = getLayoutInflater().inflate(R.layout.launcher, null);
Michael Jurka7267fa52013-09-26 15:29:57 -0700396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 setupViews();
Winson1f064272016-07-18 17:18:02 -0700398 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700399 mExtractedColors = new ExtractedColors();
400 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Tony Wickham010d2552017-01-20 08:15:28 -0800402 mPopupDataProvider = new PopupDataProvider(this);
403
Tony Wickhame2217252016-03-22 16:34:23 -0700404 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
405 .addAccessibilityStateChangeListener(this);
406
Joe Onorato7c312c12009-08-13 21:36:53 -0700407 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700408
Sunny Goyalfe770c92016-09-27 14:38:58 -0700409 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Sunny Goyale26d1002016-06-20 14:52:14 -0700411 if (LauncherAppState.PROFILE_STARTUP) {
412 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 }
414
Sunny Goyal2100c782016-08-22 16:00:03 -0700415 // We only load the page synchronously if the user rotates (or triggers a
416 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700417 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
418 if (savedInstanceState != null) {
419 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
420 }
421 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700422 // If we are not binding synchronously, show a fade in animation when
423 // the first page bind completes.
424 mDragLayer.setAlpha(0);
425 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700426 // Pages bound synchronously.
427 mWorkspace.setCurrentPage(currentScreen);
428
Sunny Goyal2100c782016-08-22 16:00:03 -0700429 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
431
432 // For handling default keys
433 mDefaultKeySsb = new SpannableStringBuilder();
434 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800435
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800436 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700437 // In case we are on a device with locked rotation, we should look at preferences to check
438 // if the user has specifically allowed rotation.
439 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700440 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700441 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
442 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700443 }
444
445 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
446 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400447 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700448
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800449 setContentView(mLauncherView);
Adam Cohen9211d422014-10-07 18:14:53 -0700450 if (mLauncherCallbacks != null) {
451 mLauncherCallbacks.onCreate(savedInstanceState);
452 }
453 }
454
Sunny Goyal7779d622015-06-11 16:18:39 -0700455 @Override
Sunny Goyal0bbd5542016-11-11 10:41:26 -0800456 public View findViewById(int id) {
457 return mLauncherView.findViewById(id);
458 }
459
460 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700461 public void onExtractedColorsChanged() {
462 loadExtractedColorsAndColorItems();
463 }
464
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700465 @Override
466 public void onAppWidgetHostReset() {
467 if (mAppWidgetHost != null) {
468 mAppWidgetHost.startListening();
469 }
470 }
471
Tony Wickham827cef22016-03-17 15:39:39 -0700472 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700473 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700474 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700475 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700476 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700477 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800478 boolean lightStatusBar = (FeatureFlags.LIGHT_STATUS_BAR
479 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
480 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700481 // It's possible that All Apps is visible when this is run,
Hyunyoung Song3285da32016-12-19 14:46:21 -0800482 // so always use light status bar in that case. Only change nav bar color to status bar
483 // color when All Apps is visible.
484 activateLightStatusBar(lightStatusBar || isAllAppsVisible(), isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700485 }
486 }
487
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800488 // TODO: use platform flag on API >= 26
489 private static final int SYSTEM_UI_FLAG_LIGHT_NAV_BAR = 0x10;
490
Tony Wickham345bff32016-09-28 15:34:51 -0700491 /**
492 * Sets the status bar to be light or not. Light status bar means dark icons.
Hyunyoung Song3285da32016-12-19 14:46:21 -0800493 * @param lightStatusBar make sure the status bar is light
494 * @param changeNavBar if true, make the nav bar theme in sync with status bar.
Tony Wickham345bff32016-09-28 15:34:51 -0700495 */
Hyunyoung Song3285da32016-12-19 14:46:21 -0800496 public void activateLightStatusBar(boolean lightStatusBar, boolean changeNavBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700497 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
498 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700499 if (lightStatusBar) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800500 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR ;
Hyunyoung Song3285da32016-12-19 14:46:21 -0800501 if (changeNavBar && Utilities.isAtLeastO()) {
502 newSystemUiFlags |= SYSTEM_UI_FLAG_LIGHT_NAV_BAR;
503 }
Tony Wickham93113872016-09-14 16:46:32 -0700504 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700505 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Hyunyoung Song3285da32016-12-19 14:46:21 -0800506 if (changeNavBar && Utilities.isAtLeastO()) {
Hyunyoung Songdf9f1472016-12-09 13:56:15 -0800507 newSystemUiFlags &= ~(SYSTEM_UI_FLAG_LIGHT_NAV_BAR);
508 }
509 }
Hyunyoung Song3285da32016-12-19 14:46:21 -0800510
Tony Wickham345bff32016-09-28 15:34:51 -0700511 if (newSystemUiFlags != oldSystemUiFlags) {
512 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
513 }
Tony Wickham93113872016-09-14 16:46:32 -0700514 }
515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 private LauncherCallbacks mLauncherCallbacks;
517
518 public void onPostCreate(Bundle savedInstanceState) {
519 super.onPostCreate(savedInstanceState);
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onPostCreate(savedInstanceState);
522 }
523 }
524
Winson1f064272016-07-18 17:18:02 -0700525 public void onInsetsChanged(Rect insets) {
526 mDeviceProfile.updateInsets(insets);
527 mDeviceProfile.layout(this, true /* notifyListeners */);
528 }
529
Sunny Goyal32554d12015-12-03 15:31:25 -0800530 /**
531 * Call this after onCreate to set or clear overlay.
532 */
533 public void setLauncherOverlay(LauncherOverlay overlay) {
534 if (overlay != null) {
535 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
536 }
537 mWorkspace.setLauncherOverlay(overlay);
538 }
539
Adam Cohen9211d422014-10-07 18:14:53 -0700540 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
541 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700543 private boolean mWorkspaceImportanceStored = false;
544 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800546 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
548
549 @Override
550 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700552 return;
553 }
554 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700555 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700556 if (mWorkspace != null) {
557 mWorkspaceImportanceForAccessibility =
558 mWorkspace.getImportantForAccessibility();
559 mWorkspace.setImportantForAccessibility(
560 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
561 mWorkspaceImportanceStored = true;
562 }
563 if (mHotseat != null) {
564 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
565 mHotseat.setImportantForAccessibility(
566 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
567 mHotseatImportanceStored = true;
568 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700569 }
570
571 @Override
572 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700573 if (mWorkspaceImportanceStored && mWorkspace != null) {
574 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
575 }
576 if (mHotseatImportanceStored && mHotseat != null) {
577 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
578 }
579 mWorkspaceImportanceStored = false;
580 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700581 }
582 });
Adam Cohen9211d422014-10-07 18:14:53 -0700583 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700584 }
585
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700586 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700587 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700588 if (mLauncherCallbacks != null) {
589 mLauncherCallbacks.onLauncherProviderChange();
590 }
591 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700592
Adam Cohen9211d422014-10-07 18:14:53 -0700593 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700595 if (mLauncherCallbacks != null) {
596 return mLauncherCallbacks.hasCustomContentToLeft();
597 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700598 return false;
599 }
600
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 * {@link #addToCustomContentPage}. This will only be invoked if
604 * {@link #hasCustomContentToLeft()} is {@code true}.
605 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500606 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700607 if (mLauncherCallbacks != null) {
608 mLauncherCallbacks.populateCustomContentContainer();
609 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 }
611
612 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700613 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 * ensure the custom content page is added or removed if necessary.
615 */
616 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600617 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 // Not bound yet, wait for bindScreens to be called.
619 return;
620 }
621
622 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
623 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500624 mWorkspace.createCustomContentContainer();
625 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
627 mWorkspace.removeCustomContentPage();
628 }
629 }
630
Hyunyoung Songaa953652016-04-19 18:30:24 -0700631 public UserEventDispatcher getUserEventDispatcher() {
632 if (mLauncherCallbacks != null) {
633 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
634 if (dispatcher != null) {
635 return dispatcher;
636 }
637 }
638
Sunny Goyal64976d52016-06-20 14:56:28 -0700639 // Logger object is a singleton and does not have to be coupled with the foreground
640 // activity. Since most user event logging is done on the UI, the object is retrieved
641 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700642 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700643 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700644 }
645 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800646 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100647
Hyunyoung Song3f471442015-04-08 19:01:34 -0700648 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700649 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
650 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700651 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700652 }
653
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000654 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700655 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
656 // This cast is safe as long as the id < 0x00FFFFFF
657 // Since we jail all the dynamically generated views, there should be no clashes
658 // with any other views.
659 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000660 }
661
Tony Wickham010d2552017-01-20 08:15:28 -0800662 public PopupDataProvider getPopupDataProvider() {
663 return mPopupDataProvider;
664 }
665
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000666 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
668 * a configuration step, this allows the proper animations to run after other transitions.
669 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 private long completeAdd(
671 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
672 long screenId = info.screenId;
673 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700674 // When the screen id represents an actual screen (as opposed to a rank) we make sure
675 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700677 }
Adam Cohendb364c32014-05-20 14:23:40 -0700678
Sunny Goyal2100c782016-08-22 16:00:03 -0700679 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700681 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700682 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700685 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700686 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700688 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700689 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 int widgetId = appWidgetId;
691 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700692 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700693 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700694 // Since the view was just bound, also launch the configure activity if needed
695 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
696 .getLauncherAppWidgetInfo(widgetId);
Sunny Goyal04a324a2017-01-20 21:08:59 -0800697 if (provider != null) {
698 new WidgetAddFlowHandler(provider)
699 .startConfigActivity(this, widgetInfo, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -0700700 }
701 }
702 break;
703 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700705
Adam Cohendb364c32014-05-20 14:23:40 -0700706 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800707 }
708
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800709 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700710 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700711 if (isWorkspaceLoading()) {
712 // process the result once the workspace has loaded.
713 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
714 return;
715 }
716 mPendingActivityResult = null;
717
Winson Chunge341d302013-08-16 14:31:00 -0700718 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 final PendingRequestArgs requestArgs = mPendingRequestArgs;
720 setWaitingForResult(null);
721 if (requestArgs == null) {
722 return;
723 }
724
725 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700726
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 Runnable exitSpringLoaded = new Runnable() {
728 @Override
729 public void run() {
730 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
731 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
732 }
733 };
734
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700736 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
739 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700740 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700741 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700742 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700743 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700744 addAppWidgetImpl(
745 appWidgetId, requestArgs, null,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800746 requestArgs.getWidgetHandler(),
Sunny Goyal2100c782016-08-22 16:00:03 -0700747 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 }
749 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200750 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
751 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700752 // User could have free-scrolled between pages before picking a wallpaper; make sure
753 // we move to the closest one now.
754 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700755 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200756 }
757 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200759
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700761 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700762
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 // We have special handling for widgets
764 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800765 final int appWidgetId;
766 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
767 : -1;
768 if (widgetId < 0) {
769 appWidgetId = pendingAddWidgetId;
770 } else {
771 appWidgetId = widgetId;
772 }
773
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800775 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700776 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
777 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700779 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700780 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 @Override
782 public void run() {
783 exitSpringLoadedDragModeDelayed(false, 0, null);
784 }
785 };
Adam Cohendb364c32014-05-20 14:23:40 -0700786
Sunny Goyal2100c782016-08-22 16:00:03 -0700787 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
788 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
789 } else {
790 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
791 // When the screen id represents an actual screen (as opposed to a rank)
792 // we make sure that the drop page actually exists.
793 requestArgs.screenId =
794 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700795 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700796 final CellLayout dropLayout =
797 mWorkspace.getScreenWithId(requestArgs.screenId);
798
799 dropLayout.setDropPending(true);
800 final Runnable onComplete = new Runnable() {
801 @Override
802 public void run() {
803 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
804 dropLayout.setDropPending(false);
805 }
806 };
807 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
808 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700809 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 return;
811 }
812
Sunny Goyald478c832016-04-01 12:04:16 -0700813 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
814 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700815 if (resultCode == RESULT_OK) {
816 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700817 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700818 }
819 // Leave the widget in the pending state if the user canceled the configure.
820 return;
821 }
822
Sunny Goyalaad90582015-07-09 14:22:50 -0700823 if (requestCode == REQUEST_CREATE_SHORTCUT) {
824 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700825 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
826 completeAdd(requestCode, data, -1, requestArgs);
827 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
829
Sunny Goyalaad90582015-07-09 14:22:50 -0700830 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700831 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800836 }
837
838 @Override
Sunny Goyaldec3a902017-01-25 18:23:36 -0800839 protected void onActivityResult(
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800840 final int requestCode, final int resultCode, final Intent data) {
841 handleActivityResult(requestCode, resultCode, data);
842 if (mLauncherCallbacks != null) {
843 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
844 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800845 }
846
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600847 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700848 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600849 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700850 PendingRequestArgs pendingArgs = mPendingRequestArgs;
851 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
852 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
853 setWaitingForResult(null);
854
Sunny Goyal28c6b962015-10-12 11:42:05 -0700855 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700856 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700857 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700858 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700859 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700860 Intent intent = pendingArgs.getPendingIntent();
861
Sunny Goyal28c6b962015-10-12 11:42:05 -0700862 if (grantResults.length > 0
863 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700864 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700865 } else {
866 // TODO: Show a snack bar with link to settings
867 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700868 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700869 }
870 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
873 grantResults);
874 }
875 }
876
Adam Cohendb364c32014-05-20 14:23:40 -0700877 /**
878 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
879 *
880 * @param screenId the screen id to check
881 * @return the new screen, or screenId if it exists
882 */
883 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800884 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700885 if (dropLayout == null) {
886 // it's possible that the add screen was removed because it was
887 // empty and a re-bind occurred
888 mWorkspace.addExtraEmptyScreen();
889 return mWorkspace.commitExtraEmptyScreen();
890 } else {
891 return screenId;
892 }
893 }
894
Sunny Goyal2100c782016-08-22 16:00:03 -0700895 @Thunk void completeTwoStageWidgetDrop(
896 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
897 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 Runnable onCompleteRunnable = null;
899 int animationType = 0;
900
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700901 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 if (resultCode == RESULT_OK) {
903 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
904 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal04a324a2017-01-20 21:08:59 -0800905 requestArgs.getWidgetHandler().getProviderInfo(this));
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700906 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 onCompleteRunnable = new Runnable() {
908 @Override
909 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700910 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700911 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
912 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 }
914 };
915 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800916 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700919 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700920 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700921 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
922 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700923 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 // The animated view may be null in the case of a rotation during widget configuration
925 onCompleteRunnable.run();
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
928
929 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700930 protected void onStop() {
931 super.onStop();
932 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700933
934 if (mLauncherCallbacks != null) {
935 mLauncherCallbacks.onStop();
936 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700937
Sunny Goyalf5e37442016-11-02 10:31:24 -0700938 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700939 mAppWidgetHost.stopListening();
940 }
Tony Wickham010d2552017-01-20 08:15:28 -0800941
942 NotificationListener.removeNotificationsChangedListener();
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 }
944
945 @Override
946 protected void onStart() {
947 super.onStart();
948 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStart();
952 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700953
Sunny Goyalf5e37442016-11-02 10:31:24 -0700954 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700955 mAppWidgetHost.startListening();
956 }
Tony Wickham010d2552017-01-20 08:15:28 -0800957
958 if (!isWorkspaceLoading()) {
959 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
960 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 }
962
963 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200965 long startTime = 0;
966 if (DEBUG_RESUME_TIME) {
967 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400968 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200969 }
Adam Cohen9211d422014-10-07 18:14:53 -0700970
971 if (mLauncherCallbacks != null) {
972 mLauncherCallbacks.preOnResume();
973 }
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700976 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700977
Winson Chung4a2afa32012-07-19 14:53:05 -0700978 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700979 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800981 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700982 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700983 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700984 // view after launching an app, as they may be depending on the UI to be static to
985 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700986 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700987 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800988 } else if (mOnResumeState == State.WIDGETS) {
989 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700990 }
991 mOnResumeState = State.NONE;
992
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800993 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700994 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700995 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700996 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700997 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700999 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001000 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1001 // execute them here
1002 long startTimeCallbacks = 0;
1003 if (DEBUG_RESUME_TIME) {
1004 startTimeCallbacks = System.currentTimeMillis();
1005 }
1006
Michael Jurka1e2f4652013-07-08 18:03:46 -07001007 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1008 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001009 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001010 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001011 if (DEBUG_RESUME_TIME) {
1012 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1013 (System.currentTimeMillis() - startTimeCallbacks));
1014 }
Michael Jurka447bf842013-05-15 14:52:15 +02001015 }
Michael Jurka54554252013-08-01 12:52:23 +02001016 if (mOnResumeCallbacks.size() > 0) {
1017 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1018 mOnResumeCallbacks.get(i).run();
1019 }
1020 mOnResumeCallbacks.clear();
1021 }
Winson Chunge4e50662012-01-23 14:45:13 -08001022
1023 // Reset the pressed state of icons that were locked in the press state while activities
1024 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001025 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001026 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001027 mWaitingForResume.setStayPressed(false);
1028 }
Winson Chung82963d52013-10-09 11:20:57 -07001029
Adam Cohen06dff352012-06-01 17:17:08 -07001030 // It is possible that widgets can receive updates while launcher is not in the foreground.
1031 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1032 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1033 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001034 if (!isWorkspaceLoading()) {
1035 getWorkspace().reinflateWidgetsIfNecessary();
1036 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001037
Michael Jurka447bf842013-05-15 14:52:15 +02001038 if (DEBUG_RESUME_TIME) {
1039 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1040 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001041
Adam Cohen4b66bf32015-09-18 12:15:19 -07001042 // We want to suppress callbacks about CustomContent being shown if we have just received
1043 // onNewIntent while the user was present within launcher. In that case, we post a call
1044 // to move the user to the main screen (which will occur after onResume). We don't want to
1045 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1046 // suppress here.
1047 if (mWorkspace.getCustomContentCallbacks() != null
1048 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001049 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001050 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001051 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1052 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001053 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001054 }
1055 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001056 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001057 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001058 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001059
Sunny Goyal756adbc2015-04-16 15:20:51 -07001060 if (!isWorkspaceLoading()) {
1061 // Process any items that were added while Launcher was away.
1062 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001063
1064 // Refresh shortcuts if the permission changed.
1065 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001066 }
Adam Cohen9211d422014-10-07 18:14:53 -07001067
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001068 if (shouldShowDiscoveryBounce()) {
1069 mAllAppsController.showDiscoveryBounce();
1070 }
1071 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001072 if (mLauncherCallbacks != null) {
1073 mLauncherCallbacks.onResume();
1074 }
Adam Cohen06dff352012-06-01 17:17:08 -07001075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001079 // Ensure that items added to Launcher are queued until Launcher returns
1080 InstallShortcutReceiver.enableInstallQueue();
1081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001083 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001084 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001085 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001086
1087 // We call onHide() aggressively. The custom content callbacks should be able to
1088 // debounce excess onHide calls.
1089 if (mWorkspace.getCustomContentCallbacks() != null) {
1090 mWorkspace.getCustomContentCallbacks().onHide();
1091 }
Romain Guycbb89e42009-06-08 15:52:54 -07001092
Adam Cohen9211d422014-10-07 18:14:53 -07001093 if (mLauncherCallbacks != null) {
1094 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
1097
1098 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001099 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1100 // by a onResume or by scrolling otherwise.
1101 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001102
1103 // Custom content is completely hidden
1104 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001105
1106 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1107 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001108
1109 // Indicates whether the user is allowed to scroll away from the custom content.
1110 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
1112
Adam Cohenc2d6e892014-10-16 09:49:24 -07001113 public interface LauncherOverlay {
1114
1115 /**
1116 * Touch interaction leading to overscroll has begun
1117 */
1118 public void onScrollInteractionBegin();
1119
1120 /**
1121 * Touch interaction related to overscroll has ended
1122 */
1123 public void onScrollInteractionEnd();
1124
1125 /**
1126 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1127 * screen (or in the case of RTL, the rightmost screen).
1128 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001129 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130
1131 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001132 * Called when the launcher is ready to use the overlay
1133 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001135 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001136 }
1137
Jun Mukai8af0cd82015-05-12 12:32:12 -07001138 public interface LauncherSearchCallbacks {
1139 /**
1140 * Called when the search overlay is shown.
1141 */
1142 public void onSearchOverlayOpened();
1143
1144 /**
1145 * Called when the search overlay is dismissed.
1146 */
1147 public void onSearchOverlayClosed();
1148 }
1149
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001151 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
1154 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1155
Sunny Goyalc86df472016-02-25 09:19:38 -08001156 public void onScrollChanged(float progress) {
1157 if (mWorkspace != null) {
1158 mWorkspace.onOverlayScrollChanged(progress);
1159 }
1160 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
Jorim Jaggid017f882014-01-14 17:08:48 -08001163 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001164 if (mLauncherCallbacks != null) {
1165 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001166 } else {
1167 // On devices with a locked orientation, we will at least have the allow rotation
1168 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001169 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001170 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001171 }
1172
Adam Cohen9211d422014-10-07 18:14:53 -07001173 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001174 CustomContentCallbacks callbacks, String description) {
1175 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001176 }
1177
Adam Cohenedb40762013-07-18 16:45:45 -07001178 // The custom content needs to offset its content to account for the QSB
1179 public int getTopOffsetForCustomContent() {
1180 return mWorkspace.getPaddingTop();
1181 }
1182
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001183 @Override
1184 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001186 if (mModel.isCurrentCallbacks(this)) {
1187 mModel.stopLoader();
1188 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001189 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1190
Romain Guy13c2e7b2010-03-10 19:45:00 -08001191 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001192 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 @Override
1196 public void onWindowFocusChanged(boolean hasFocus) {
1197 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001198 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001199
1200 if (mLauncherCallbacks != null) {
1201 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1202 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 private boolean acceptFilter() {
1206 final InputMethodManager inputManager = (InputMethodManager)
1207 getSystemService(Context.INPUT_METHOD_SERVICE);
1208 return !inputManager.isFullscreenMode();
1209 }
1210
1211 @Override
1212 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001213 final int uniChar = event.getUnicodeChar();
1214 final boolean handled = super.onKeyDown(keyCode, event);
1215 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1216 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1218 keyCode, event);
1219 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001221 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 // showSearchDialog()
1223 // If there are multiple keystrokes before the search dialog takes focus,
1224 // onSearchRequested() will be called for every keystroke,
1225 // but it is idempotent, so it's fine.
1226 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 }
1229
Joe Onorato8a9625e2010-01-28 15:55:35 -08001230 // Eat the long press event so the keyboard doesn't come up.
1231 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1232 return true;
1233 }
1234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 return handled;
1236 }
1237
Winson46163472015-09-22 17:31:56 -07001238 @Override
1239 public boolean onKeyUp(int keyCode, KeyEvent event) {
1240 if (keyCode == KeyEvent.KEYCODE_MENU) {
1241 // Ignore the menu key if we are currently dragging or are on the custom content screen
1242 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001243 // Close any open floating view
1244 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001245
Winson46163472015-09-22 17:31:56 -07001246 // Stop resizing any widgets
1247 mWorkspace.exitWidgetResizeMode();
1248
1249 // Show the overview mode if we are on the workspace
1250 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1251 !mWorkspace.isSwitchingState()) {
1252 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001253 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001254 }
1255 }
1256 return true;
1257 }
1258 return super.onKeyUp(keyCode, event);
1259 }
1260
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 private String getTypedText() {
1262 return mDefaultKeySsb.toString();
1263 }
1264
Sunny Goyal6f28e712016-09-13 14:19:29 -07001265 @Override
1266 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 mDefaultKeySsb.clear();
1268 mDefaultKeySsb.clearSpans();
1269 Selection.setSelection(mDefaultKeySsb, 0);
1270 }
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 /**
1273 * Restores the previous state, if it exists.
1274 *
1275 * @param savedState The previous state.
1276 */
1277 private void restoreState(Bundle savedState) {
1278 if (savedState == null) {
1279 return;
1280 }
1281
Sunny Goyalfe770c92016-09-27 14:38:58 -07001282 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1283 State[] stateValues = State.values();
1284 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1285 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001286 if (state == State.APPS || state == State.WIDGETS) {
1287 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001288 }
1289
Sunny Goyal2100c782016-08-22 16:00:03 -07001290 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1291 if (requestArgs != null) {
1292 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001294
1295 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
1297
1298 /**
1299 * Finds all the views we need and configure them properly.
1300 */
1301 private void setupViews() {
Winson Chung4c98d922011-05-31 16:50:48 -07001302 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001303 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001304 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001305 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1306 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001307 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001308
Sunny Goyal784f9c32016-03-23 16:56:54 -07001309 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1310 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1311 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312
Winson Chung4c98d922011-05-31 16:50:48 -07001313 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001314 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001315
Winson Chung3d503fb2011-07-13 17:25:49 -07001316 // Setup the hotseat
1317 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1318 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001319 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001320 }
1321
Winsone9f27272015-10-13 10:47:51 -07001322 // Setup the overview panel
1323 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001324
Winson Chung4c98d922011-05-31 16:50:48 -07001325 // Setup the workspace
1326 mWorkspace.setHapticFeedbackEnabled(false);
1327 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001328 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001329 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1330 // default state, otherwise we will update to the wrong offsets in RTL
1331 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001332 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001333 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001334
Tony Wickham34d2c912015-09-11 09:27:58 -07001335 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001336 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001337
Winson Chungef7f8742015-06-04 17:18:17 -07001338 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001339 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001340 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001341 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1342 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1343 } else {
1344 mAppsView.setSearchBarController(new DefaultAppSearchController());
1345 }
Craig Mautner360310b2012-10-26 15:13:08 -07001346
Winson Chung3d503fb2011-07-13 17:25:49 -07001347 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001348 mDragController.setMoveTarget(mWorkspace);
1349 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001350 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001351
Peter Schiller310a9882016-06-30 13:52:36 -07001352 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1353 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1354 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001355
Sunny Goyal322d5562015-06-25 19:35:49 -07001356 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1357 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360
Winsone9f27272015-10-13 10:47:51 -07001361 private void setupOverviewPanel() {
1362 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1363
Winsone9f27272015-10-13 10:47:51 -07001364 // Bind wallpaper button actions
1365 View wallpaperButton = findViewById(R.id.wallpaper_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301366 new OverviewButtonClickListener(ControlType.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001367 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001368 public void handleViewClick(View view) {
1369 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001370 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001371 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001372 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1373
1374 // Bind widget button actions
1375 mWidgetsButton = findViewById(R.id.widget_button);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301376 new OverviewButtonClickListener(ControlType.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001377 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001378 public void handleViewClick(View view) {
1379 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001380 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001381 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001382 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1383
1384 // Bind settings actions
1385 View settingsButton = findViewById(R.id.settings_button);
1386 boolean hasSettings = hasSettings();
1387 if (hasSettings) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301388 new OverviewButtonClickListener(ControlType.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001389 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001390 public void handleViewClick(View view) {
1391 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001392 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001393 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001394 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1395 } else {
1396 settingsButton.setVisibility(View.GONE);
1397 }
1398
1399 mOverviewPanel.setAlpha(0f);
1400 }
1401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001403 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001404 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001405 */
1406 public void setAllAppsButton(View allAppsButton) {
1407 mAllAppsButton = allAppsButton;
1408 }
1409
Sunny Goyalbb011da2016-06-15 15:42:29 -07001410 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001411 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001412 }
1413
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001414 public View getWidgetsButton() {
1415 return mWidgetsButton;
1416 }
1417
Anjali Koppal5ad44842014-03-10 20:34:39 -07001418 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 * Creates a view representing a shortcut.
1420 *
1421 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001423 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001424 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
1427 /**
1428 * Creates a view representing a shortcut inflated from the specified resource.
1429 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 * @param parent The group the shortcut belongs to.
1431 * @param info The data structure describing the shortcut.
1432 *
1433 * @return A View inflated from layoutResId.
1434 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001435 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal9b29ca52017-02-17 10:39:44 -08001436 BubbleTextView favorite = (BubbleTextView) LayoutInflater.from(parent.getContext())
1437 .inflate(R.layout.app_icon, parent, false);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001438 favorite.applyFromShortcutInfo(info);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001439 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001441 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 return favorite;
1443 }
1444
1445 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 * Add a shortcut to the workspace.
1447 *
1448 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001450 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001451 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001452 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001453 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001454
Sunny Goyal782f0c92017-01-19 10:27:54 -08001455 if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001456 args.getPendingIntent().getComponent() == null) {
1457 return;
1458 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001459
Sunny Goyal782f0c92017-01-19 10:27:54 -08001460 ShortcutInfo info = null;
1461 if (Utilities.isAtLeastO()) {
Sunny Goyaldec3a902017-01-25 18:23:36 -08001462 info = LauncherAppsCompat.createShortcutInfoFromPinItemRequest(
1463 this, PinItemRequestCompat.getPinItemRequest(data));
Sunny Goyal782f0c92017-01-19 10:27:54 -08001464 }
1465
1466 if (info == null) {
1467 info = InstallShortcutReceiver.fromShortcutIntent(this, data);
1468
Sunny Goyal342e4662017-02-02 15:21:08 -08001469 if (info == null || !new PackageManagerHelper(this).hasPermissionForActivity(
1470 info.intent, args.getPendingIntent().getComponent().getPackageName())) {
Sunny Goyal782f0c92017-01-19 10:27:54 -08001471 // The app is trying to add a shortcut without sufficient permissions
1472 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
1473 return;
1474 }
1475 }
1476
1477 final View view = createShortcut(info);
Sunny Goyal5c97f512015-05-19 16:03:28 -07001478 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001479 // First we check if we already know the exact location where we want to add this item.
1480 if (cellX >= 0 && cellY >= 0) {
1481 cellXY[0] = cellX;
1482 cellXY[1] = cellY;
1483 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001484
1485 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001486 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001487 true, null,null)) {
1488 return;
1489 }
1490 DragObject dragObject = new DragObject();
1491 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001492 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1493 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001494 return;
1495 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001496 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001497 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001498 }
1499
1500 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001501 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 return;
1503 }
1504
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001505 getModelWriter().addItemToDatabase(info, container, screenId, cellXY[0], cellXY[1]);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301506 mWorkspace.addInScreen(view, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001510 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001512 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001514 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001515 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1516
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001518 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 }
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Adam Cohen59400422014-03-05 18:07:04 -08001521 if (appWidgetInfo.isCustomWidget) {
1522 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001523 }
1524
Adam Cohen59400422014-03-05 18:07:04 -08001525 LauncherAppWidgetInfo launcherInfo;
1526 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001527 launcherInfo.spanX = itemInfo.spanX;
1528 launcherInfo.spanY = itemInfo.spanY;
1529 launcherInfo.minSpanX = itemInfo.minSpanX;
1530 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyala52ecb02016-12-16 15:04:51 -08001531 launcherInfo.user = appWidgetInfo.getUser();
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001533 getModelWriter().addItemToDatabase(launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001534 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
Sunny Goyal2100c782016-08-22 16:00:03 -07001536 if (hostView == null) {
1537 // Perform actual inflation because we're live
1538 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001540 hostView.setVisibility(View.VISIBLE);
Sunny Goyald5462aa2016-11-22 16:52:39 +05301541 prepareAppWidget(hostView, launcherInfo);
1542 mWorkspace.addInScreen(hostView, launcherInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Sunny Goyald5462aa2016-11-22 16:52:39 +05301545 private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001546 hostView.setTag(item);
1547 item.onBindAppWidget(this, hostView);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001548 hostView.setFocusable(true);
1549 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001550 }
1551
Adam Cohended9f8d2010-11-03 13:25:16 -07001552 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1553 @Override
1554 public void onReceive(Context context, Intent intent) {
1555 final String action = intent.getAction();
1556 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001557 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001558
Winson Chungc100e8e2011-08-09 16:02:43 -07001559 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001560 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001561 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001562 if (!showWorkspace(false)) {
1563 // If we are already on the workspace, then manually reset all apps
1564 mAppsView.reset();
1565 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001566 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001567 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001568 }
1569 }
1570 };
1571
Tony Wickham010d2552017-01-20 08:15:28 -08001572 public void updateIconBadges(final Set<PackageUserKey> updatedBadges) {
1573 Runnable r = new Runnable() {
1574 @Override
1575 public void run() {
1576 mWorkspace.updateIconBadges(updatedBadges);
1577 mAppsView.updateIconBadges(updatedBadges);
1578 }
1579 };
1580 if (!waitUntilResume(r)) {
1581 r.run();
1582 }
1583 }
1584
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 @Override
1586 public void onAttachedToWindow() {
1587 super.onAttachedToWindow();
1588
1589 // Listen for broadcasts related to user-presence
1590 final IntentFilter filter = new IntentFilter();
1591 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001592 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001593 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001594 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001595
1596 if (mLauncherCallbacks != null) {
1597 mLauncherCallbacks.onAttachedToWindow();
1598 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 }
1600
1601 @Override
1602 public void onDetachedFromWindow() {
1603 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001604 if (mAttached) {
1605 unregisterReceiver(mReceiver);
1606 mAttached = false;
1607 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001608
1609 if (mLauncherCallbacks != null) {
1610 mLauncherCallbacks.onDetachedFromWindow();
1611 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001612 }
1613
1614 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001615 // The following code used to be in onResume, but it turns out onResume is called when
1616 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1617 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001618 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001619 if (!mWorkspaceLoading) {
1620 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001621 // We want to let Launcher draw itself at least once before we force it to build
1622 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001623 // apps is nice and speedy.
1624 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001625 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001626 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001627 if (mStarted) return;
1628 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001629 // We delay the layer building a bit in order to give
1630 // other message processing a time to run. In particular
1631 // this avoids a delay in hiding the IME if it was
1632 // currently shown, because doing that may involve
1633 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001634 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001635 final ViewTreeObserver.OnDrawListener listener = this;
1636 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001637 public void run() {
1638 if (mWorkspace != null &&
1639 mWorkspace.getViewTreeObserver() != null) {
1640 mWorkspace.getViewTreeObserver().
1641 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001642 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001643 }
1644 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001645 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001646 }
1647 });
1648 }
1649 clearTypedText();
1650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 }
1652
Winson Chunga6945242014-01-08 14:04:34 -08001653 public DragLayer getDragLayer() {
1654 return mDragLayer;
1655 }
1656
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001657 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001658 return mAppsView;
1659 }
1660
Hyunyoung Song3f471442015-04-08 19:01:34 -07001661 public WidgetsContainerView getWidgetsView() {
1662 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001663 }
1664
Winson Chunga6945242014-01-08 14:04:34 -08001665 public Workspace getWorkspace() {
1666 return mWorkspace;
1667 }
1668
Sunny Goyal6178f132016-07-11 17:30:03 -07001669 public View getQsbContainer() {
1670 return mQsbContainer;
1671 }
1672
Winson Chunga6945242014-01-08 14:04:34 -08001673 public Hotseat getHotseat() {
1674 return mHotseat;
1675 }
1676
Jorim Jaggid017f882014-01-14 17:08:48 -08001677 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001678 return mOverviewPanel;
1679 }
1680
Sunny Goyal47328fd2016-05-25 18:56:41 -07001681 public DropTargetBar getDropTargetBar() {
1682 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001683 }
1684
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001685 public LauncherAppWidgetHost getAppWidgetHost() {
1686 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 }
Romain Guycbb89e42009-06-08 15:52:54 -07001688
Winson Chunga9abd0e2010-10-27 17:18:37 -07001689 public LauncherModel getModel() {
1690 return mModel;
1691 }
1692
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001693 public ModelWriter getModelWriter() {
1694 return mModelWriter;
1695 }
1696
Adam Cohen79d90c52016-04-22 13:29:20 -07001697 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001698 return mSharedPrefs;
1699 }
1700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 @Override
1702 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001703 long startTime = 0;
1704 if (DEBUG_RESUME_TIME) {
1705 startTime = System.currentTimeMillis();
1706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 super.onNewIntent(intent);
1708
Winson15f8b172015-08-19 11:02:39 -07001709 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1710 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1711 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001712
1713 // Check this condition before handling isActionMain, as this will get reset.
1714 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001715 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001716
Winson15f8b172015-08-19 11:02:39 -07001717 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1718 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001719 if (mWorkspace == null) {
1720 // Can be cases where mWorkspace is null, this prevents a NPE
1721 return;
1722 }
Jon Mirandafeba90f2016-10-06 10:53:29 -07001723
1724 // Note: There should be at most one log per method call. This is enforced implicitly
1725 // by using if-else statements.
1726 UserEventDispatcher ued = getUserEventDispatcher();
1727
1728 // TODO: Log this case.
Adam Cohen6fecd412013-10-02 17:41:50 -07001729 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001730
Jon Mirandafeba90f2016-10-06 10:53:29 -07001731 AbstractFloatingView topOpenView = AbstractFloatingView.getTopOpenView(this);
Tony Wickham540913e2017-01-23 11:47:51 -08001732 if (topOpenView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301733 ued.logActionCommand(Action.Command.HOME_INTENT,
1734 topOpenView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001735 } else if (topOpenView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301736 ued.logActionCommand(Action.Command.HOME_INTENT,
1737 ((Folder) topOpenView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07001738 } else if (alreadyOnHome) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05301739 ued.logActionCommand(Action.Command.HOME_INTENT,
Jon Mirandafeba90f2016-10-06 10:53:29 -07001740 mWorkspace.getState().containerType, mWorkspace.getCurrentPage());
1741 }
1742
1743 // In all these cases, only animate if we're already on home
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001744 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001745 exitSpringLoadedDragMode();
1746
1747 // If we are already on home, then just animate back to the workspace,
1748 // otherwise, just wait until onResume to set the state back to Workspace
1749 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001750 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001751 } else {
1752 mOnResumeState = State.WORKSPACE;
1753 }
1754
1755 final View v = getWindow().peekDecorView();
1756 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001757 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001758 INPUT_METHOD_SERVICE);
1759 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1760 }
1761
Winson Chungb745afb2015-03-02 11:51:23 -08001762 // Reset the apps view
1763 if (!alreadyOnHome && mAppsView != null) {
1764 mAppsView.scrollToTop();
1765 }
1766
Hyunyoung Song3f471442015-04-08 19:01:34 -07001767 // Reset the widgets view
1768 if (!alreadyOnHome && mWidgetsView != null) {
1769 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001770 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001771
Adam Cohen9211d422014-10-07 18:14:53 -07001772 if (mLauncherCallbacks != null) {
1773 mLauncherCallbacks.onHomeIntent();
1774 }
Sunny Goyalb38fab72017-01-20 19:32:31 -08001775
1776 Parcelable dragExtra = intent
1777 .getParcelableExtra(PinItemDragListener.EXTRA_PIN_ITEM_DRAG_LISTENER);
1778 if (dragExtra instanceof PinItemDragListener) {
1779 PinItemDragListener dragListener = (PinItemDragListener) dragExtra;
1780 dragListener.setLauncher(this);
1781 mDragLayer.setOnDragListener(dragListener);
1782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
Adam Cohened307df2013-10-02 09:37:31 -07001784
Adam Cohen9211d422014-10-07 18:14:53 -07001785 if (mLauncherCallbacks != null) {
1786 mLauncherCallbacks.onNewIntent(intent);
1787 }
Winson15f8b172015-08-19 11:02:39 -07001788
1789 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1790 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1791 // animation.
1792 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001793 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001794 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001795 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1796 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001797
1798 // We use this flag to suppress noisy callbacks above custom content state
1799 // from onResume.
1800 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001801 mWorkspace.post(new Runnable() {
1802 @Override
1803 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001804 if (mWorkspace != null) {
1805 mWorkspace.moveToDefaultScreen(true);
1806 }
Winson15f8b172015-08-19 11:02:39 -07001807 }
1808 });
1809 }
1810 }
1811
1812 if (DEBUG_RESUME_TIME) {
1813 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1814 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001815 }
1816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001818 public void onRestoreInstanceState(Bundle state) {
1819 super.onRestoreInstanceState(state);
1820 for (int page: mSynchronouslyBoundPages) {
1821 mWorkspace.restoreInstanceStateForChild(page);
1822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
1824
1825 @Override
1826 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001827 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001828 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1829 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001830
Adam Cohen21cd0022013-10-09 18:57:02 -07001831 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001832 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
Michael Jurka883f55b2010-10-21 15:47:14 -07001834 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001835 // We close any open folders and shortcut containers since they will not be re-opened,
1836 // and we need to make sure this state is reflected.
1837 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001838
Sunny Goyal2100c782016-08-22 16:00:03 -07001839 if (mPendingRequestArgs != null) {
1840 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1841 }
1842 if (mPendingActivityResult != null) {
1843 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845
Adam Cohen9211d422014-10-07 18:14:53 -07001846 if (mLauncherCallbacks != null) {
1847 mLauncherCallbacks.onSaveInstanceState(outState);
1848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
1850
1851 @Override
1852 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001854
Michael Jurka9d906c72011-10-14 06:25:36 -07001855 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001856 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001857
Winson Chungcd2b0142011-06-08 16:02:26 -07001858 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001859 // It's possible to receive onDestroy after a new Launcher activity has
1860 // been created. In this case, don't interfere with the new Launcher.
1861 if (mModel.isCurrentCallbacks(this)) {
1862 mModel.stopLoader();
Sunny Goyal87f784c2017-01-11 10:48:34 -08001863 LauncherAppState.getInstance(this).setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001864 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001865
Sunny Goyal745bad92016-05-02 10:54:12 -07001866 if (mRotationPrefChangeHandler != null) {
1867 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1868 }
1869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001871 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001873 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001875 mAppWidgetHost = null;
1876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 TextKeyListener.getInstance().release();
1878
Tony Wickhame2217252016-03-22 16:34:23 -07001879 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1880 .removeAccessibilityStateChangeListener(this);
1881
Michael Jurka2ecf9952012-06-18 12:52:28 -07001882 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001883
1884 if (mLauncherCallbacks != null) {
1885 mLauncherCallbacks.onDestroy();
1886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888
Sunny Goyalae502842016-06-17 08:43:56 -07001889 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1890 return mAccessibilityDelegate;
1891 }
1892
Adam Cohena9cf38f2011-05-02 15:36:58 -07001893 public DragController getDragController() {
1894 return mDragController;
1895 }
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001898 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001899 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001900 }
1901
1902 @Override
1903 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1904 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001905 try {
1906 super.startIntentSenderForResult(intent, requestCode,
1907 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1908 } catch (IntentSender.SendIntentException e) {
1909 throw new ActivityNotFoundException();
1910 }
1911 }
1912
Winson Chung70d72102011-08-12 11:24:35 -07001913 /**
1914 * Indicates that we want global search for this activity by setting the globalSearch
1915 * argument for {@link #startSearch} to true.
1916 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001918 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001920
Karl Rosaen138a0412009-04-23 19:00:21 -07001921 if (initialQuery == null) {
1922 // Use any text typed in the launcher as the initial query
1923 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 if (appSearchData == null) {
1926 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001927 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001929
Sunny Goyal6f28e712016-09-13 14:19:29 -07001930 if (mLauncherCallbacks == null ||
1931 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1932 // Starting search from the callbacks failed. Start the default global search.
1933 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001934 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001935
1936 // We need to show the workspace after starting the search
1937 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001938 }
1939
Ian Parkinson047036e2014-09-01 15:40:53 +01001940 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001941 * Starts the global search activity. This code is a copied from SearchManager
1942 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001943 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001944 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001945 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001946 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1947 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1948 if (globalSearchActivity == null) {
1949 Log.w(TAG, "No global search activity found.");
1950 return;
1951 }
1952 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1953 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1954 intent.setComponent(globalSearchActivity);
1955 // Make sure that we have a Bundle to put source in
1956 if (appSearchData == null) {
1957 appSearchData = new Bundle();
1958 } else {
1959 appSearchData = new Bundle(appSearchData);
1960 }
Adam Cohen9211d422014-10-07 18:14:53 -07001961 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001962 if (!appSearchData.containsKey("source")) {
1963 appSearchData.putString("source", getPackageName());
1964 }
1965 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1966 if (!TextUtils.isEmpty(initialQuery)) {
1967 intent.putExtra(SearchManager.QUERY, initialQuery);
1968 }
1969 if (selectInitialQuery) {
1970 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1971 }
1972 intent.setSourceBounds(sourceBounds);
1973 try {
1974 startActivity(intent);
1975 } catch (ActivityNotFoundException ex) {
1976 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
1979
Yura4f93ec62014-02-04 14:15:21 +00001980 public boolean isOnCustomContent() {
1981 return mWorkspace.isOnOrMovingToCustomContent();
1982 }
1983
Winson Chung70d72102011-08-12 11:24:35 -07001984 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001985 public boolean onPrepareOptionsMenu(Menu menu) {
1986 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001987 if (mLauncherCallbacks != null) {
1988 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1989 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001990 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001991 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001993 @Override
1994 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001995 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001996 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001997 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001998 }
1999
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002001 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002 }
2003
Adam Cohen517a7f52014-03-01 12:12:59 -08002004 public boolean isWorkspaceLoading() {
2005 return mWorkspaceLoading;
2006 }
2007
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002008 private void setWorkspaceLoading(boolean value) {
2009 boolean isLocked = isWorkspaceLocked();
2010 mWorkspaceLoading = value;
2011 if (isLocked != isWorkspaceLocked()) {
2012 onWorkspaceLockedChanged();
2013 }
2014 }
2015
Sunny Goyal04a324a2017-01-20 21:08:59 -08002016 public void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002017 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002018 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002019 if (isLocked != isWorkspaceLocked()) {
2020 onWorkspaceLockedChanged();
2021 }
2022 }
2023
Adam Cohen9211d422014-10-07 18:14:53 -07002024 protected void onWorkspaceLockedChanged() {
2025 if (mLauncherCallbacks != null) {
2026 mLauncherCallbacks.onWorkspaceLockedChanged();
2027 }
2028 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002029
Sunny Goyal2100c782016-08-22 16:00:03 -07002030 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002031 WidgetAddFlowHandler addFlowHandler) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002032 if (LOGD) {
2033 Log.d(TAG, "Adding widget from drop");
2034 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002035 addAppWidgetImpl(appWidgetId, info, boundWidget, addFlowHandler, 0);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002036 }
2037
Sunny Goyal2100c782016-08-22 16:00:03 -07002038 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
Sunny Goyal04a324a2017-01-20 21:08:59 -08002039 AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) {
2040 if (!addFlowHandler.startConfigActivity(this, appWidgetId, info, REQUEST_CREATE_APPWIDGET)) {
2041 // If the configuration flow was not started, add the widget
Michael Jurkaaf442092010-06-10 17:01:57 -07002042
Adam Cohenad4e15c2013-10-17 16:21:35 -07002043 Runnable onComplete = new Runnable() {
2044 @Override
2045 public void run() {
2046 // Exit spring loaded mode if necessary after adding the widget
2047 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2048 null);
2049 }
2050 };
Sunny Goyal04a324a2017-01-20 21:08:59 -08002051 completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this));
Adam Cohen689ff162014-05-08 17:27:56 -07002052 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
2054 }
Romain Guycbb89e42009-06-08 15:52:54 -07002055
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002056 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002057 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002058 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002059 mWorkspace.moveToCustomContentScreen(animate);
2060 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002061
2062 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2063 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002064 info.container = container;
2065 info.screenId = screenId;
2066 if (cell != null) {
2067 info.cellX = cell[0];
2068 info.cellY = cell[1];
2069 }
2070 info.spanX = spanX;
2071 info.spanY = spanY;
2072
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002073 switch (info.itemType) {
2074 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2075 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002076 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002077 break;
2078 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal782f0c92017-01-19 10:27:54 -08002079 processShortcutFromDrop((PendingAddShortcutInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002080 break;
2081 default:
2082 throw new IllegalStateException("Unknown item type: " + info.itemType);
2083 }
2084 }
2085
Adam Cohenfbba09b2011-07-18 21:43:05 -07002086 /**
2087 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002088 */
Sunny Goyal782f0c92017-01-19 10:27:54 -08002089 private void processShortcutFromDrop(PendingAddShortcutInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002090 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2091 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
Sunny Goyal782f0c92017-01-19 10:27:54 -08002092 if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) {
2093 handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null);
2094 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002095 }
2096
Adam Cohenfbba09b2011-07-18 21:43:05 -07002097 /**
2098 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002099 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002100 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002101 AppWidgetHostView hostView = info.boundWidget;
2102 int appWidgetId;
Sunny Goyal8a0dc382017-01-25 11:30:06 -08002103 WidgetAddFlowHandler addFlowHandler = info.getHandler();
Adam Cohened66b2b2012-01-23 17:28:51 -08002104 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002105 // In the case where we've prebound the widget, we remove it from the DragLayer
2106 if (LOGD) {
2107 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2108 }
2109 getDragLayer().removeView(hostView);
2110
Adam Cohened66b2b2012-01-23 17:28:51 -08002111 appWidgetId = hostView.getAppWidgetId();
Sunny Goyal04a324a2017-01-20 21:08:59 -08002112 addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002113
2114 // Clear the boundWidget so that it doesn't get destroyed.
2115 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002116 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002117 // In this case, we either need to start an activity to get permission to bind
2118 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002119 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002120 Bundle options = info.bindOptions;
2121
Sunny Goyalffe83f12014-08-14 17:39:34 -07002122 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2123 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002124 if (success) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002125 addAppWidgetFromDropImpl(appWidgetId, info, null, addFlowHandler);
Michael Jurka8b805b12012-04-18 14:23:14 -07002126 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002127 addFlowHandler.startBindFlow(this, appWidgetId, info, REQUEST_BIND_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07002128 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002129 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002130 }
2131
Adam Cohendcd297f2013-06-18 13:13:40 -07002132 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002133 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002134 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 folderInfo.title = getText(R.string.folder_name);
2136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 // Update the model
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002138 getModelWriter().addItemToDatabase(folderInfo, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139
2140 // Create the view
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002141 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo);
Sunny Goyald5462aa2016-11-22 16:52:39 +05302142 mWorkspace.addInScreen(newFolder, folderInfo);
Winson Chung5f8afe62013-08-12 16:19:28 -07002143 // Force measure the new folder icon
2144 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2145 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002146 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 }
Romain Guycbb89e42009-06-08 15:52:54 -07002148
Winsonc0b52fe2015-09-09 16:38:15 -07002149 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002150 * Unbinds the view for the specified item, and removes the item and all its children.
2151 *
2152 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002153 * @param itemInfo the {@link ItemInfo} for this view.
2154 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002155 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002156 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002157 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002158 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002159 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2160 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002161 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002162 } else {
2163 mWorkspace.removeWorkspaceItem(v);
2164 }
Winsonc0b52fe2015-09-09 16:38:15 -07002165 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002166 getModelWriter().deleteItemFromDatabase(itemInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002167 }
2168 } else if (itemInfo instanceof FolderInfo) {
2169 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002170 if (v instanceof FolderIcon) {
2171 ((FolderIcon) v).removeListeners();
2172 }
Winsonc0b52fe2015-09-09 16:38:15 -07002173 mWorkspace.removeWorkspaceItem(v);
2174 if (deleteFromDb) {
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002175 getModelWriter().deleteFolderAndContentsFromDatabase(folderInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002176 }
2177 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2178 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002179 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002180 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002181 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002182 }
2183 } else {
2184 return false;
2185 }
2186 return true;
2187 }
2188
2189 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002190 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002191 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002192 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002193 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002194 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002195 // Deleting an app widget ID is a void call but writes to disk before returning
2196 // to the caller...
2197 new AsyncTask<Void, Void, Void>() {
2198 public Void doInBackground(Void ... args) {
2199 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2200 return null;
2201 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002202 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002203 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08002204 getModelWriter().deleteItemFromDatabase(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002205 }
2206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 @Override
2208 public boolean dispatchKeyEvent(KeyEvent event) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002209 return (event.getKeyCode() == KeyEvent.KEYCODE_HOME) || super.dispatchKeyEvent(event);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
2211
Joe Onorato88ec0992009-11-19 13:16:06 -08002212 @Override
2213 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002214 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2215 return;
2216 }
2217
Sunny Goyal45478022015-06-08 16:52:41 -07002218 if (mDragController.isDragging()) {
2219 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002220 return;
2221 }
2222
Jon Mirandafeba90f2016-10-06 10:53:29 -07002223 // Note: There should be at most one log per method call. This is enforced implicitly
2224 // by using if-else statements.
2225 UserEventDispatcher ued = getUserEventDispatcher();
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002226 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2227 if (topView != null) {
2228 if (topView.getActiveTextView() != null) {
2229 topView.getActiveTextView().dispatchBackKey();
2230 } else {
Tony Wickham540913e2017-01-23 11:47:51 -08002231 if (topView instanceof PopupContainerWithArrow) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302232 ued.logActionCommand(Action.Command.BACK,
2233 topView.getExtendedTouchView(), ContainerType.DEEPSHORTCUTS);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002234 } else if (topView instanceof Folder) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302235 ued.logActionCommand(Action.Command.BACK,
2236 ((Folder) topView).getFolderIcon(), ContainerType.FOLDER);
Jon Mirandafeba90f2016-10-06 10:53:29 -07002237 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002238 topView.close(true);
2239 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002240 } else if (isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302241 ued.logActionCommand(Action.Command.BACK, ContainerType.ALLAPPS);
Winson Chungb745afb2015-03-02 11:51:23 -08002242 showWorkspace(true);
2243 } else if (isWidgetsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302244 ued.logActionCommand(Action.Command.BACK, ContainerType.WIDGETS);
Winson Chungb745afb2015-03-02 11:51:23 -08002245 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002246 } else if (mWorkspace.isInOverviewMode()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302247 ued.logActionCommand(Action.Command.BACK, ContainerType.OVERVIEW);
Winson Chungdc61c4d2015-04-20 18:26:57 -07002248 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002249 } else {
Jon Mirandafeba90f2016-10-06 10:53:29 -07002250 // TODO: Log this case.
Patrick Dubroy758a9232011-03-03 19:54:56 -08002251 mWorkspace.exitWidgetResizeMode();
2252
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002253 // Back button is a no-op here, but give at least some feedback for the button press
2254 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002255 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002256 }
2257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 * Launches the intent referred by the clicked shortcut.
2260 *
2261 * @param v The view representing the clicked shortcut.
2262 */
2263 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002264 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2265 // view has detached (it's possible for this to happen if the view is removed mid touch).
2266 if (v.getWindowToken() == null) {
2267 return;
2268 }
2269
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002270 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002271 return;
2272 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002273
Adam Cohen1697b792013-09-17 19:08:21 -07002274 if (v instanceof Workspace) {
2275 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002276 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002277 }
2278 return;
2279 }
2280
2281 if (v instanceof CellLayout) {
2282 if (mWorkspace.isInOverviewMode()) {
Jon Mirandac6cf4932017-02-07 17:12:36 -08002283 int page = mWorkspace.indexOfChild(v);
2284 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
2285 LauncherLogProto.Action.Direction.NONE,
2286 LauncherLogProto.ContainerType.OVERVIEW, page);
2287 mWorkspace.snapToPageFromOverView(page);
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002288 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002289 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002290 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002291 }
2292
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002294 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002295 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002297 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002298 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002299 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002300 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2301 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002302 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002303 } else if (tag instanceof AppInfo) {
2304 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002305 } else if (tag instanceof LauncherAppWidgetInfo) {
2306 if (v instanceof PendingAppWidgetHostView) {
2307 onClickPendingWidget((PendingAppWidgetHostView) v);
2308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
2310 }
2311
Sunny Goyal70660032015-05-14 00:07:08 -07002312 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002313 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002314 return false;
2315 }
2316
Michael Jurkaaf442092010-06-10 17:01:57 -07002317 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002318 * Event handler for the app widget view which has not fully restored.
2319 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002320 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002321 if (mIsSafeModeEnabled) {
2322 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2323 return;
2324 }
2325
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002326 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002327 if (v.isReadyForClickSetup()) {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002328 LauncherAppWidgetProviderInfo appWidgetInfo =
2329 mAppWidgetManager.findProvider(info.providerName, info.user);
2330 if (appWidgetInfo == null) {
2331 return;
2332 }
2333 WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
2334
Sunny Goyald478c832016-04-01 12:04:16 -07002335 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2336 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2337 // This should not happen, as we make sure that an Id is allocated during bind.
2338 return;
2339 }
Sunny Goyal04a324a2017-01-20 21:08:59 -08002340 addFlowHandler.startBindFlow(this, info.appWidgetId, info,
2341 REQUEST_BIND_PENDING_APPWIDGET);
Sunny Goyald478c832016-04-01 12:04:16 -07002342 } else {
Sunny Goyal04a324a2017-01-20 21:08:59 -08002343 addFlowHandler.startConfigActivity(this, info, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002344 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002345 } else {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002346 final String packageName = info.providerName.getPackageName();
Sunny Goyale6e72002017-01-12 16:55:36 -08002347 onClickPendingAppItem(v, packageName, info.installProgress >= 0);
Sunny Goyalff572272014-07-23 13:58:07 -07002348 }
2349 }
2350
2351 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002352 * Event handler for the "grid" button that appears on the home screen, which
2353 * enters all apps mode.
2354 *
2355 * @param v The view that was clicked.
2356 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002357 protected void onClickAllAppsButton(View v) {
2358 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002359 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302360 getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
2361 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002362 showAppsView(true /* animated */, true /* updatePredictedApps */,
2363 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002364 }
2365 }
2366
2367 protected void onLongClickAllAppsButton(View v) {
2368 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2369 if (!isAppsViewVisible()) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302370 getUserEventDispatcher().logActionOnControl(Action.Touch.LONGPRESS,
2371 ControlType.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002372 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002373 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002374 }
2375 }
2376
Sunny Goyale6e72002017-01-12 16:55:36 -08002377 private void onClickPendingAppItem(final View v, final String packageName,
2378 boolean downloadStarted) {
2379 if (downloadStarted) {
2380 // If the download has started, simply direct to the market app.
2381 startMarketIntentForPackage(v, packageName);
2382 return;
2383 }
Sunny Goyale03b8122014-10-08 09:55:24 -07002384 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002385 .setTitle(R.string.abandoned_promises_title)
2386 .setMessage(R.string.abandoned_promise_explanation)
Sunny Goyale6e72002017-01-12 16:55:36 -08002387 .setPositiveButton(R.string.abandoned_search, new DialogInterface.OnClickListener() {
2388 @Override
2389 public void onClick(DialogInterface dialogInterface, int i) {
2390 startMarketIntentForPackage(v, packageName);
2391 }
2392 })
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002393 .setNeutralButton(R.string.abandoned_clean_this,
2394 new DialogInterface.OnClickListener() {
2395 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002396 final UserHandle user = Process.myUserHandle();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002397 mWorkspace.removeAbandonedPromise(packageName, user);
2398 }
2399 })
2400 .create().show();
Sunny Goyale6e72002017-01-12 16:55:36 -08002401 }
2402
2403 private void startMarketIntentForPackage(View v, String packageName) {
2404 ItemInfo item = (ItemInfo) v.getTag();
2405 Intent intent = PackageManagerHelper.getMarketIntent(packageName);
2406 boolean success = startActivitySafely(v, intent, item);
2407 if (success && v instanceof BubbleTextView) {
2408 mWaitingForResume = (BubbleTextView) v;
2409 mWaitingForResume.setStayPressed(true);
2410 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002411 }
2412
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002413 /**
2414 * Event handler for an app shortcut click.
2415 *
2416 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2417 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002418 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002419 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2420 Object tag = v.getTag();
2421 if (!(tag instanceof ShortcutInfo)) {
2422 throw new IllegalArgumentException("Input must be a Shortcut");
2423 }
2424
2425 // Open shortcut
2426 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002427
2428 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002429 if ((shortcut.isDisabled &
2430 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2431 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2432 // If the app is only disabled because of the above flags, launch activity anyway.
2433 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002434 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002435 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2436 // Use a message specific to this shortcut, if it has one.
2437 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2438 return;
2439 }
2440 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002441 int error = R.string.activity_not_available;
2442 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2443 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002444 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2445 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002446 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002447 }
2448 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2449 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002450 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002451 }
2452
Chris Wren40c5ed32014-06-24 18:24:23 -04002453 // Check for abandoned promise
Sunny Goyale6e72002017-01-12 16:55:36 -08002454 if ((v instanceof BubbleTextView) && shortcut.isPromise()) {
2455 String packageName = shortcut.intent.getComponent() != null ?
2456 shortcut.intent.getComponent().getPackageName() : shortcut.intent.getPackage();
2457 if (!TextUtils.isEmpty(packageName)) {
2458 onClickPendingAppItem(v, packageName,
2459 shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE));
2460 return;
2461 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002462 }
2463
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002464 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002465 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002466 }
2467
Sunny Goyala7ce1662016-05-31 15:01:35 -07002468 private void startAppShortcutOrInfoActivity(View v) {
2469 ItemInfo item = (ItemInfo) v.getTag();
2470 Intent intent = item.getIntent();
2471 if (intent == null) {
2472 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002473 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002474 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002475 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002476
2477 if (success && v instanceof BubbleTextView) {
2478 mWaitingForResume = (BubbleTextView) v;
2479 mWaitingForResume.setStayPressed(true);
2480 }
2481 }
2482
2483 /**
2484 * Event handler for a folder icon click.
2485 *
2486 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2487 */
2488 protected void onClickFolderIcon(View v) {
2489 if (LOGD) Log.d(TAG, "onClickFolder");
2490 if (!(v instanceof FolderIcon)){
2491 throw new IllegalArgumentException("Input must be a FolderIcon");
2492 }
2493
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002494 Folder folder = ((FolderIcon) v).getFolder();
2495 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002496 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002497 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002498 }
Michael Jurka5130e402011-10-13 04:55:35 -07002499 }
2500
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002501 /**
2502 * Event handler for the (Add) Widgets button that appears after a long press
2503 * on the home screen.
2504 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002505 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002506 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002507 if (mIsSafeModeEnabled) {
2508 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2509 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002510 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002511 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002512 }
2513
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002514 /**
2515 * Event handler for the wallpaper picker button that appears after a long press
2516 * on the home screen.
2517 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002518 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002519 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002520 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2521 return;
2522 }
2523
Tony Wickham785f7a52015-08-31 17:28:32 -07002524 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2525 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002526 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002527 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002528 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002529
2530 String pickerPackage = getString(R.string.wallpaper_picker_package);
2531 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2532 if (!hasTargetPackage) {
2533 intent.setPackage(pickerPackage);
2534 }
2535
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002536 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002537 try {
2538 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2539 // If there is no target package, use the default intent chooser animation
2540 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2541 } catch (ActivityNotFoundException e) {
2542 setWaitingForResult(null);
2543 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2544 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 }
2546
2547 /**
2548 * Event handler for a click on the settings button that appears after a long press
2549 * on the home screen.
2550 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002551 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002552 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002553 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2554 .setPackage(getPackageName());
2555 intent.setSourceBounds(getViewBounds(v));
2556 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002557 }
2558
Adam Cohen61f560d2013-09-30 15:58:20 -07002559 public View.OnTouchListener getHapticFeedbackTouchListener() {
2560 if (mHapticFeedbackTouchListener == null) {
2561 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002562 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002563 @Override
2564 public boolean onTouch(View v, MotionEvent event) {
2565 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2566 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2567 }
2568 return false;
2569 }
2570 };
2571 }
2572 return mHapticFeedbackTouchListener;
2573 }
2574
Tony Wickhame2217252016-03-22 16:34:23 -07002575 @Override
2576 public void onAccessibilityStateChanged(boolean enabled) {
2577 mDragLayer.onAccessibilityStateChanged(enabled);
2578 }
2579
Sunny Goyal06e21a22016-08-11 16:02:02 -07002580 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002581 if (isOnCustomContent()) {
2582 // Custom content screen doesn't participate in drag and drop. If on custom
2583 // content screen, move to default.
2584 moveWorkspaceToDefaultScreen();
2585 }
Adam Cohen9211d422014-10-07 18:14:53 -07002586 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002587
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002588 /**
2589 * Called when the user stops interacting with the launcher.
2590 * This implies that the user is now on the homescreen and is not doing housekeeping.
2591 */
Adam Cohen9211d422014-10-07 18:14:53 -07002592 protected void onInteractionEnd() {
2593 if (mLauncherCallbacks != null) {
2594 mLauncherCallbacks.onInteractionEnd();
2595 }
2596 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002597
2598 /**
2599 * Called when the user starts interacting with the launcher.
2600 * The possible interactions are:
2601 * - open all apps
2602 * - reorder an app shortcut, or a widget
2603 * - open the overview mode.
2604 * This is a good time to stop doing things that only make sense
2605 * when the user is on the homescreen and not doing housekeeping.
2606 */
Adam Cohen9211d422014-10-07 18:14:53 -07002607 protected void onInteractionBegin() {
2608 if (mLauncherCallbacks != null) {
2609 mLauncherCallbacks.onInteractionBegin();
2610 }
2611 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002612
Winson Chungcd99cd32015-04-29 11:03:24 -07002613 /** Updates the interaction state. */
2614 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002615 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002616 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002617 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2618 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002619 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002620 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002621 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002622 onInteractionEnd();
2623 }
2624 }
2625
Sunny Goyala7ce1662016-05-31 15:01:35 -07002626 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002628 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2629 try {
2630 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2631 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2632 // is enabled by default on NYC.
2633 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2634 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002635
2636 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2637 String id = ((ShortcutInfo) info).getDeepShortcutId();
2638 String packageName = intent.getPackage();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302639 DeepShortcutManager.getInstance(this).startShortcut(
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002640 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002641 } else {
2642 // Could be launching some bookkeeping activity
2643 startActivity(intent, optsBundle);
2644 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002645 } finally {
2646 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002649 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2650 // corresponding permission. Show the appropriate permission prompt if that
2651 // is the case.
2652 if (intent.getComponent() == null
2653 && Intent.ACTION_CALL.equals(intent.getAction())
2654 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2655 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002656
2657 setWaitingForResult(PendingRequestArgs
2658 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002659 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2660 REQUEST_PERMISSION_CALL_PHONE);
2661 } else {
2662 // No idea why this was thrown.
2663 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 }
2666 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002667
Sunny Goyalfe770c92016-09-27 14:38:58 -07002668 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002669 private Bundle getActivityLaunchOptions(View v) {
2670 if (Utilities.ATLEAST_MARSHMALLOW) {
2671 int left = 0, top = 0;
2672 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2673 if (v instanceof TextView) {
2674 // Launch from center of icon, not entire view
2675 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2676 if (icon != null) {
2677 Rect bounds = icon.getBounds();
2678 left = (width - bounds.width()) / 2;
2679 top = v.getPaddingTop();
2680 width = bounds.width();
2681 height = bounds.height();
2682 }
2683 }
2684 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2685 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2686 // On L devices, we use the device default slide-up transition.
2687 // On L MR1 devices, we use a custom version of the slide-up transition which
2688 // doesn't have the delay present in the device default.
2689 return ActivityOptions.makeCustomAnimation(
2690 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2691 }
2692 return null;
2693 }
2694
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002695 private Rect getViewBounds(View v) {
2696 int[] pos = new int[2];
2697 v.getLocationOnScreen(pos);
2698 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2699 }
2700
Sunny Goyala7ce1662016-05-31 15:01:35 -07002701 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002702 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2703 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2704 return false;
2705 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002706 // Only launch using the new animation if the shortcut has not opted out (this is a
2707 // private contract between launcher and may be ignored in the future).
2708 boolean useLaunchAnimation = (v != null) &&
2709 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2710 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2711
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002712 UserHandle user = null;
Sunny Goyala7ce1662016-05-31 15:01:35 -07002713 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2714 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2715 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002716 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002717
2718 // Prepare intent
2719 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2720 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002721 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002722 }
2723 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002724 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2725 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002726 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyale6e72002017-01-12 16:55:36 -08002727 && !((ShortcutInfo) item).isPromise()) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002728 // Shortcuts need some special checks due to legacy reasons.
2729 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002730 } else if (user == null || user.equals(Process.myUserHandle())) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002731 // Could be launching some bookkeeping activity
2732 startActivity(intent, optsBundle);
2733 } else {
2734 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2735 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2736 }
2737 return true;
2738 } catch (ActivityNotFoundException|SecurityException e) {
2739 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2740 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2741 }
2742 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002743 }
2744
Tony Wickhamdadb3042016-02-24 11:07:00 -08002745 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002746 public boolean dispatchTouchEvent(MotionEvent ev) {
2747 mLastDispatchTouchEventX = ev.getX();
2748 return super.dispatchTouchEvent(ev);
2749 }
2750
2751 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002753 if (!isDraggingEnabled()) return false;
2754 if (isWorkspaceLocked()) return false;
2755 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002757 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2758 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002759 onLongClickAllAppsButton(v);
2760 return true;
2761 }
2762
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002763
Jon Miranda51f037d2016-11-07 08:37:20 -08002764 boolean ignoreLongPressToOverview =
2765 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002766
Adam Cohen1697b792013-09-17 19:08:21 -07002767 if (v instanceof Workspace) {
2768 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002769 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302770 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2771 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002772 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002773 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002774 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2775 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2776 return true;
2777 } else {
2778 return false;
2779 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002780 } else {
2781 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002782 }
Adam Cohen1697b792013-09-17 19:08:21 -07002783 }
2784
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002785 CellLayout.CellInfo longClickCellInfo = null;
2786 View itemUnderLongClick = null;
2787 if (v.getTag() instanceof ItemInfo) {
2788 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002789 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002790 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002791 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 }
2793
Winson Chung3d503fb2011-07-13 17:25:49 -07002794 // The hotseat touch handling does not go through Workspace, and we always allow long press
2795 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002796 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002797 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002798 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002799 if (mWorkspace.isInOverviewMode()) {
2800 mWorkspace.startReordering(v);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302801 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2802 Action.Direction.NONE, ContainerType.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002803 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002804 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002805 return false;
2806 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +05302807 getUserEventDispatcher().logActionOnContainer(Action.Touch.LONGPRESS,
2808 Action.Direction.NONE, ContainerType.WORKSPACE,
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002809 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002810 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002811 }
Jon Miranda379198e2016-09-23 08:54:40 -07002812 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2813 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002815 final boolean isAllAppsButton =
2816 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2817 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2818 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002819 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002821 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822 }
2823 }
2824 }
2825 return true;
2826 }
2827
Winson Chung3d503fb2011-07-13 17:25:49 -07002828 boolean isHotseatLayout(View layout) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08002829 // TODO: Remove this method
Winson Chung3d503fb2011-07-13 17:25:49 -07002830 return mHotseat != null && layout != null &&
2831 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2832 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002833
Winson Chung3d503fb2011-07-13 17:25:49 -07002834 /**
2835 * Returns the CellLayout of the specified container at the specified screen.
2836 */
Sunny Goyal92820592015-03-02 11:31:35 -08002837 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002838 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2839 if (mHotseat != null) {
2840 return mHotseat.getLayout();
2841 } else {
2842 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002843 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002844 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002845 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002846 }
2847 }
2848
Winson Chungb745afb2015-03-02 11:51:23 -08002849 /**
2850 * For overridden classes.
2851 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002852 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002853 return isAppsViewVisible();
2854 }
2855
2856 public boolean isAppsViewVisible() {
2857 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2858 }
2859
2860 public boolean isWidgetsViewVisible() {
2861 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002862 }
2863
Michael Jurkae326f182011-11-21 14:05:46 -08002864 @Override
2865 public void onTrimMemory(int level) {
2866 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002867 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2868 // The widget preview db can result in holding onto over
2869 // 3MB of memory for caching which isn't necessary.
2870 SQLiteDatabase.releaseMemory();
2871
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002872 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002873 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002874 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002875 if (mLauncherCallbacks != null) {
2876 mLauncherCallbacks.onTrimMemory(level);
2877 }
Michael Jurkae326f182011-11-21 14:05:46 -08002878 }
2879
Winson5c6bdbb2015-09-03 11:36:19 -07002880 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002881 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002882 }
2883
Winson5c6bdbb2015-09-03 11:36:19 -07002884 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002885 boolean changed = mState != State.WORKSPACE ||
2886 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002887 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002888 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002889 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002890 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002891
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892 // Set focus to the AppsCustomize button
2893 if (mAllAppsButton != null) {
2894 mAllAppsButton.requestFocus();
2895 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002896 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002897
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002898 // Change the state *after* we've called all the transition code
2899 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002900
Winson Chung0f785722015-04-08 10:27:49 -07002901 if (changed) {
2902 // Send an accessibility event to announce the context change
2903 getWindow().getDecorView()
2904 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002905 }
Winson5c6bdbb2015-09-03 11:36:19 -07002906 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002907 }
2908
Winsone9f27272015-10-13 10:47:51 -07002909 /**
2910 * Shows the overview button.
2911 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002912 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002913 showOverviewMode(animated, false);
2914 }
2915
2916 /**
2917 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2918 * onto one of the overview panel buttons.
2919 */
2920 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2921 Runnable postAnimRunnable = null;
2922 if (requestButtonFocus) {
2923 postAnimRunnable = new Runnable() {
2924 @Override
2925 public void run() {
2926 // Hitting the menu button when in touch mode does not trigger touch mode to
2927 // be disabled, so if requested, force focus on one of the overview panel
2928 // buttons.
2929 mOverviewPanel.requestFocusFromTouch();
2930 }
2931 };
2932 }
Adam Cohened307df2013-10-02 09:37:31 -07002933 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002934 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002935 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002936 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002937 // If animated from long press, then don't allow any of the controller in the drag
2938 // layer to intercept any remaining touch.
2939 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002940 }
2941
Winson Chungb745afb2015-03-02 11:51:23 -08002942 /**
2943 * Shows the apps view.
2944 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002945 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002946 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002947 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002948 if (updatePredictedApps) {
2949 tryAndUpdatePredictedApps();
2950 }
Winson Chung76648c52015-07-10 14:33:23 -07002951 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002952 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002953
Winson Chungb745afb2015-03-02 11:51:23 -08002954 /**
2955 * Shows the widgets view.
2956 */
2957 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002958 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002959 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002960 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002961 }
Winson Chung76648c52015-07-10 14:33:23 -07002962 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002963
2964 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002965 @Override
2966 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002967 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002968 }
2969 });
Winson Chungb745afb2015-03-02 11:51:23 -08002970 }
2971
2972 /**
2973 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002974 *
2975 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002976 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002977 // TODO: calling method should use the return value so that when {@code false} is returned
2978 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002979 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002980 if (!(mState == State.WORKSPACE ||
2981 mState == State.APPS_SPRING_LOADED ||
2982 mState == State.WIDGETS_SPRING_LOADED ||
2983 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002984 return false;
2985 }
2986 if (toState != State.APPS && toState != State.WIDGETS) {
2987 return false;
2988 }
Winson Chungb745afb2015-03-02 11:51:23 -08002989
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002990 // This is a safe and supported transition to bypass spring_loaded mode.
2991 if (mExitSpringLoadedModeRunnable != null) {
2992 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2993 mExitSpringLoadedModeRunnable = null;
2994 }
2995
Winson Chungb745afb2015-03-02 11:51:23 -08002996 if (toState == State.APPS) {
Sunny Goyaldb364372016-10-26 19:12:47 -07002997 mStateTransitionAnimation.startAnimationToAllApps(animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002998 } else {
Sunny Goyaldb364372016-10-26 19:12:47 -07002999 mStateTransitionAnimation.startAnimationToWidgets(animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003000 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003001
Michael Jurkab3e22d92011-10-31 15:58:33 -07003002 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003003 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07003004 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003005
3006 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003007 getWindow().getDecorView()
3008 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003009 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003010 }
3011
Winson Chungcd99cd32015-04-29 11:03:24 -07003012 /**
3013 * Updates the workspace and interaction state on state change, and return the animation to this
3014 * new state.
3015 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003016 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07003017 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003018 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003019 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003020 updateInteraction(fromState, toState);
3021 return anim;
3022 }
3023
Hyunyoung Song3f471442015-04-08 19:01:34 -07003024 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003025 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003026 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003027 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003028 }
Winson Chungb745afb2015-03-02 11:51:23 -08003029
Winson Chung006ee262015-08-03 14:40:11 -07003030 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003031 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003032 null /* onCompleteRunnable */);
Sunny Goyalf5440cb2016-12-14 15:13:00 -08003033 mState = State.WORKSPACE_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003034 }
Adam Cohen7777d962011-08-18 18:58:38 -07003035
Hyunyoung Song3f471442015-04-08 19:01:34 -07003036 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003037 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003038 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003039
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003040 if (mExitSpringLoadedModeRunnable != null) {
3041 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3042 }
3043 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003044 @Override
3045 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003046 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003047 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3048 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003049 // Before we show workspace, hide all apps again because
3050 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3051 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003052 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003053 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003054 } else {
3055 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003056 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003057 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003058 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003059 };
3060 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003061 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003062
Tony Wickhame0c33232016-02-08 11:37:04 -08003063 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003064 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3065 || mState == State.WIDGETS_SPRING_LOADED;
3066 }
3067
Sunny Goyal0f76b562016-12-13 19:37:10 -08003068 public void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003069 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003070 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003071 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003072 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003073 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003074 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3075 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003076 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003077 }
3078
Winson Chung4ac30062015-05-08 17:34:17 -07003079 /**
3080 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3081 * resumed.
3082 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003083 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003084 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003085 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003086 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003087 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003088 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003089 }
3090 }
3091 }
3092
Michael Jurkab3e22d92011-10-31 15:58:33 -07003093 void lockAllApps() {
3094 // TODO
3095 }
3096
3097 void unlockAllApps() {
3098 // TODO
3099 }
3100
Michael Jurkad7c28052012-04-27 15:43:36 -07003101 @Override
3102 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003103 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003104 final List<CharSequence> text = event.getText();
3105 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003106 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003107 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003108 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003109 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003110 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003111 } else if (mWorkspace != null) {
3112 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003113 } else {
3114 text.add(getString(R.string.all_apps_home_button_label));
3115 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003116 return result;
3117 }
3118
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003119 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003120 * If the activity is currently paused, signal that we need to run the passed Runnable
3121 * in onResume.
3122 *
3123 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003124 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3125 * wrong when we're not running, and if the activity comes back to what the configuration was
3126 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003127 *
3128 * Implementation of the method from LauncherModel.Callbacks.
3129 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003130 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003131 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003132 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003133 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003134 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003135 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003136 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003137 }
3138 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003139 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003140 return true;
3141 } else {
3142 return false;
3143 }
3144 }
3145
Michael Jurkac402cd92013-05-20 15:49:32 +02003146 private boolean waitUntilResume(Runnable run) {
3147 return waitUntilResume(run, false);
3148 }
3149
Michael Jurka1e2f4652013-07-08 18:03:46 -07003150 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003151 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003152 }
3153
Michael Jurka7607c2f2013-04-03 14:33:19 -07003154 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003155 * If the activity is currently paused, signal that we need to re-run the loader
3156 * in onResume.
3157 *
3158 * This needs to be called from incoming places where resources might have been loaded
3159 * while we are paused. That is becaues the Configuration might be wrong
3160 * when we're not running, and if it comes back to what it was when we
3161 * were paused, we are not restarted.
3162 *
3163 * Implementation of the method from LauncherModel.Callbacks.
3164 *
3165 * @return true if we are currently paused. The caller might be able to
3166 * skip some work in that case since we will come back again.
3167 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003168 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003169 public boolean setLoadOnResume() {
3170 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003171 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003172 mOnResumeNeedsLoad = true;
3173 return true;
3174 } else {
3175 return false;
3176 }
3177 }
3178
3179 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003180 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003182 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003184 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003185 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003186 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003187 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003188 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003189 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003190
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003192 * Clear any pending bind callbacks. This is called when is loader is planning to
3193 * perform a full rebind from scratch.
3194 */
3195 @Override
3196 public void clearPendingBinds() {
3197 mBindOnResumeCallbacks.clear();
3198 if (mPendingExecutor != null) {
3199 mPendingExecutor.markCompleted();
3200 mPendingExecutor = null;
3201 }
3202 }
3203
3204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205 * Refreshes the shortcuts shown on the workspace.
3206 *
3207 * Implementation of the method from LauncherModel.Callbacks.
3208 */
3209 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003210 if (LauncherAppState.PROFILE_STARTUP) {
3211 Trace.beginSection("Starting page bind");
3212 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003213 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003214
Winson Chungd64d1762013-08-20 14:37:16 -07003215 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003216 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003217 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003218
Winson Chung3d503fb2011-07-13 17:25:49 -07003219 if (mHotseat != null) {
3220 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003222 if (LauncherAppState.PROFILE_STARTUP) {
3223 Trace.endSection();
3224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 }
3226
Adam Cohendcd297f2013-06-18 13:13:40 -07003227 @Override
3228 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003229 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003230 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3231 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003232 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3233 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3234 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003235 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3236 // If there are no screens, we need to have an empty screen
3237 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003238 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003239 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003240
3241 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003242 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003243 if (hasCustomContentToLeft()) {
3244 mWorkspace.createCustomContentContainer();
3245 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003246 }
Winsonc7d2e832016-07-28 12:24:55 -07003247
3248 // After we have added all the screens, if the wallpaper was locked to the default state,
3249 // then notify to indicate that it can be released and a proper wallpaper offset can be
3250 // computed before the next layout
3251 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003252 }
3253
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003254 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003255 int count = orderedScreenIds.size();
3256 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003257 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003258 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003259 // No need to bind the first screen, as its always bound.
3260 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3261 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003262 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003263 }
3264
Winson Chungd64d1762013-08-20 14:37:16 -07003265 public void bindAppsAdded(final ArrayList<Long> newScreens,
3266 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003267 final ArrayList<ItemInfo> addAnimated,
3268 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003269 Runnable r = new Runnable() {
3270 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003271 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003272 }
3273 };
3274 if (waitUntilResume(r)) {
3275 return;
3276 }
3277
Winson Chungd64d1762013-08-20 14:37:16 -07003278 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003279 if (newScreens != null) {
3280 bindAddScreens(newScreens);
3281 }
Winson Chungd64d1762013-08-20 14:37:16 -07003282
3283 // We add the items without animation on non-visible pages, and with
3284 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003285 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003286 bindItems(addNotAnimated, 0,
3287 addNotAnimated.size(), false);
3288 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003289 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003290 bindItems(addAnimated, 0,
3291 addAnimated.size(), true);
3292 }
Winson Chungc58497e2013-09-03 17:48:37 -07003293
Winson Chung87412982013-10-03 18:34:14 -07003294 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003295 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003296
Winson Chungb745afb2015-03-02 11:51:23 -08003297 if (addedApps != null && mAppsView != null) {
3298 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003299 }
Winson Chungd64d1762013-08-20 14:37:16 -07003300 }
3301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003302 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 * Bind the items start-end from the list.
3304 *
3305 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003306 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003307 @Override
Sunny Goyal3be633b2016-12-08 09:59:25 -08003308 public void bindItems(final ArrayList<ItemInfo> items, final int start, final int end,
Winson Chung64359a52013-07-08 17:17:08 -07003309 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003310 Runnable r = new Runnable() {
3311 public void run() {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003312 bindItems(items, start, end, forceAnimateIcons);
Winson Chungd64d1762013-08-20 14:37:16 -07003313 }
3314 };
3315 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003316 return;
3317 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003318
Sunny Goyal3be633b2016-12-08 09:59:25 -08003319 // Get the list of added items and intersect them with the set of items here
Winson Chung64359a52013-07-08 17:17:08 -07003320 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3321 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003322 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003323 Workspace workspace = mWorkspace;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003324 long newItemsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003325 for (int i = start; i < end; i++) {
Sunny Goyal3be633b2016-12-08 09:59:25 -08003326 final ItemInfo item = items.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003327
3328 // Short circuit if we are loading dock items for a configuration which has no dock
3329 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3330 mHotseat == null) {
3331 continue;
3332 }
3333
Sunny Goyal639e9062015-08-19 19:17:06 -07003334 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 switch (item.itemType) {
3336 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3337 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal3be633b2016-12-08 09:59:25 -08003338 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003339 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003340 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003342 }
3343 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
Sunny Goyal639e9062015-08-19 19:17:06 -07003344 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003345 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Sunny Goyal1cd01b02016-11-09 10:43:58 -08003346 (FolderInfo) item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 break;
Sunny Goyal3be633b2016-12-08 09:59:25 -08003348 }
3349 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
3350 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
3351 if (mIsSafeModeEnabled) {
3352 view = new PendingAppWidgetHostView(this, info, mIconCache, true);
3353 } else {
3354 LauncherAppWidgetProviderInfo providerInfo =
3355 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
3356 if (providerInfo == null) {
3357 deleteWidgetInfo(info);
3358 continue;
3359 }
3360 view = mAppWidgetHost.createView(this, info.appWidgetId, providerInfo);
3361 }
3362 prepareAppWidget((AppWidgetHostView) view, info);
3363 break;
3364 }
Winson Chungc763c4e2013-07-19 13:49:06 -07003365 default:
3366 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003368
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003369 /*
3370 * Remove colliding items.
3371 */
3372 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3373 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3374 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3375 View v = cl.getChildAt(item.cellX, item.cellY);
3376 Object tag = v.getTag();
3377 String desc = "Collision while binding workspace item: " + item
3378 + ". Collides with " + tag;
3379 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3380 throw (new RuntimeException(desc));
3381 } else {
3382 Log.d(TAG, desc);
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003383 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003384 continue;
3385 }
3386 }
3387 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303388 workspace.addInScreenFromBind(view, item);
Sunny Goyal639e9062015-08-19 19:17:06 -07003389 if (animateIcons) {
3390 // Animate all the applications up now
3391 view.setAlpha(0f);
3392 view.setScaleX(0f);
3393 view.setScaleY(0f);
3394 bounceAnims.add(createNewAppBounceAnimation(view, i));
Sunny Goyal3be633b2016-12-08 09:59:25 -08003395 newItemsScreenId = item.screenId;
Sunny Goyal639e9062015-08-19 19:17:06 -07003396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003397 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003398
Winson Chung997a9232013-07-24 15:33:46 -07003399 if (animateIcons) {
3400 // Animate to the correct page
Sunny Goyal3be633b2016-12-08 09:59:25 -08003401 if (newItemsScreenId > -1) {
Winson Chung997a9232013-07-24 15:33:46 -07003402 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Sunny Goyal3be633b2016-12-08 09:59:25 -08003403 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newItemsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003404 final Runnable startBounceAnimRunnable = new Runnable() {
3405 public void run() {
3406 anim.playTogether(bounceAnims);
3407 anim.start();
3408 }
3409 };
Sunny Goyal3be633b2016-12-08 09:59:25 -08003410 if (newItemsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003411 // We post the animation slightly delayed to prevent slowdowns
3412 // when we are loading right after we return to launcher.
3413 mWorkspace.postDelayed(new Runnable() {
3414 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003415 if (mWorkspace != null) {
3416 mWorkspace.snapToPage(newScreenIndex);
3417 mWorkspace.postDelayed(startBounceAnimRunnable,
3418 NEW_APPS_ANIMATION_DELAY);
3419 }
Winson Chung94d67682013-09-25 16:29:40 -07003420 }
3421 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003422 } else {
3423 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003424 }
3425 }
Winson Chung64359a52013-07-08 17:17:08 -07003426 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003428 }
3429
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 /**
3431 * Add the views for a widget to the workspace.
3432 *
3433 * Implementation of the method from LauncherModel.Callbacks.
3434 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003435 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003436 Runnable r = new Runnable() {
3437 public void run() {
3438 bindAppWidget(item);
3439 }
3440 };
3441 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003442 return;
3443 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003444
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003445 if (mIsSafeModeEnabled) {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303446 PendingAppWidgetHostView view =
3447 new PendingAppWidgetHostView(this, item, mIconCache, true);
3448 prepareAppWidget(view, item);
3449 mWorkspace.addInScreen(view, item);
3450 mWorkspace.requestLayout();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003451 return;
3452 }
3453
Daniel Sandler843e8602010-06-07 14:59:01 -04003454 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3455 if (DEBUG_WIDGETS) {
3456 Log.d(TAG, "bindAppWidget: " + item);
3457 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003459 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003460
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003461 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3462 // If the provider is not ready, bind as a pending widget.
3463 appWidgetInfo = null;
3464 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3465 // The widget id is not valid. Try to find the widget based on the provider info.
3466 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3467 } else {
3468 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3469 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003470
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003471 // If the provider is ready, but the width is not yet restored, try to restore it.
3472 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3473 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003474 if (appWidgetInfo == null) {
3475 if (DEBUG_WIDGETS) {
3476 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3477 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003478 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003479 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003480 getModelWriter().deleteItemFromDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003481 return;
3482 }
Sunny Goyalff572272014-07-23 13:58:07 -07003483
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003484 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003485 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003486 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3487 // Id has not been allocated yet. Allocate a new id.
3488 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3489 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003490
Sunny Goyald478c832016-04-01 12:04:16 -07003491 // Also try to bind the widget. If the bind fails, the user will be shown
3492 // a click to setup UI, which will ask for the bind permission.
Sunny Goyala52ecb02016-12-16 15:04:51 -08003493 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -07003494 pendingInfo.spanX = item.spanX;
3495 pendingInfo.spanY = item.spanY;
3496 pendingInfo.minSpanX = item.minSpanX;
3497 pendingInfo.minSpanY = item.minSpanY;
3498 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003499
3500 boolean isDirectConfig =
3501 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3502 if (isDirectConfig && item.bindOptions != null) {
3503 Bundle newOptions = item.bindOptions.getExtras();
3504 if (options != null) {
3505 newOptions.putAll(options);
3506 }
3507 options = newOptions;
3508 }
Sunny Goyald478c832016-04-01 12:04:16 -07003509 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3510 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003511
Sunny Goyal86df1382016-08-10 15:03:22 -07003512 // We tried to bind once. If we were not able to bind, we would need to
3513 // go through the permission dialog, which means we cannot skip the config
3514 // activity.
3515 item.bindOptions = null;
3516 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3517
Sunny Goyald478c832016-04-01 12:04:16 -07003518 // Bind succeeded
3519 if (success) {
3520 // If the widget has a configure activity, it is still needs to set it up,
3521 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003522 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003523 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3524 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003525 }
Sunny Goyald478c832016-04-01 12:04:16 -07003526
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003527 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003528 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003529 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003530 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003531 // The widget was marked as UI not ready, but there is no configure activity to
3532 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003533 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003534 getModelWriter().updateItemInDatabase(item);
Sunny Goyalff572272014-07-23 13:58:07 -07003535 }
Sunny Goyalff572272014-07-23 13:58:07 -07003536 }
3537
Sunny Goyald5462aa2016-11-22 16:52:39 +05303538 final AppWidgetHostView view;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003539 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003540 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003541 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3542 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003543 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003544
Sunny Goyal56c73602015-09-25 12:55:01 -07003545 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003546 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003547 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003548 deleteWidgetInfo(item);
3549 return;
3550 }
3551
Sunny Goyal233ee962015-08-03 13:05:01 -07003552 item.minSpanX = appWidgetInfo.minSpanX;
3553 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyald5462aa2016-11-22 16:52:39 +05303554 view = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal651077b2014-06-30 14:15:31 -07003555 } else {
Sunny Goyald5462aa2016-11-22 16:52:39 +05303556 view = new PendingAppWidgetHostView(this, item, mIconCache, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003557 }
Sunny Goyald5462aa2016-11-22 16:52:39 +05303558 prepareAppWidget(view, item);
3559 mWorkspace.addInScreen(view, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003560 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561
Daniel Sandler843e8602010-06-07 14:59:01 -04003562 if (DEBUG_WIDGETS) {
3563 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3564 + (SystemClock.uptimeMillis()-start) + "ms");
3565 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 }
3567
Sunny Goyalff572272014-07-23 13:58:07 -07003568 /**
3569 * Restores a pending widget.
3570 *
3571 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003572 */
Sunny Goyald478c832016-04-01 12:04:16 -07003573 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003574 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3575 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3576 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003577 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003578 }
3579
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003580 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003581 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003582
3583 mWorkspace.reinflateWidgetsIfNecessary();
Sunny Goyal43bf11d2017-02-02 13:52:53 -08003584 getModelWriter().updateItemInDatabase(info);
Sunny Goyald478c832016-04-01 12:04:16 -07003585 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003586 }
3587
Adam Cohen1462de32012-07-24 22:34:36 -07003588 public void onPageBoundSynchronously(int page) {
3589 mSynchronouslyBoundPages.add(page);
3590 }
3591
Sunny Goyal527c7d32015-08-28 15:19:36 -07003592 @Override
3593 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3594 if (mPendingExecutor != null) {
3595 mPendingExecutor.markCompleted();
3596 }
3597 mPendingExecutor = executor;
3598 executor.attachTo(this);
3599 }
3600
3601 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3602 if (mPendingExecutor == executor) {
3603 mPendingExecutor = null;
3604 }
3605 }
3606
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003607 @Override
3608 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3609 Runnable r = new Runnable() {
3610 public void run() {
3611 finishFirstPageBind(executor);
3612 }
3613 };
3614 if (waitUntilResume(r)) {
3615 return;
3616 }
3617
3618 Runnable onComplete = new Runnable() {
3619 @Override
3620 public void run() {
3621 if (executor != null) {
3622 executor.onLoadAnimationCompleted();
3623 }
3624 }
3625 };
3626 if (mDragLayer.getAlpha() < 1) {
3627 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3628 } else {
3629 onComplete.run();
3630 }
3631 }
3632
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 /**
3634 * Callback saying that there aren't any more items to bind.
3635 *
3636 * Implementation of the method from LauncherModel.Callbacks.
3637 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003638 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003639 Runnable r = new Runnable() {
3640 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003641 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003642 }
3643 };
3644 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003645 return;
3646 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003647 if (LauncherAppState.PROFILE_STARTUP) {
3648 Trace.beginSection("Page bind completed");
3649 }
Adam Cohen1462de32012-07-24 22:34:36 -07003650 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003652 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003653
Sunny Goyal2100c782016-08-22 16:00:03 -07003654 if (mPendingActivityResult != null) {
3655 handleActivityResult(mPendingActivityResult.requestCode,
3656 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3657 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003658 }
3659
Sunny Goyal756adbc2015-04-16 15:20:51 -07003660 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003661
Tony Wickham010d2552017-01-20 08:15:28 -08003662 NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
3663
Adam Cohen9211d422014-10-07 18:14:53 -07003664 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003665 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003666 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003667 if (LauncherAppState.PROFILE_STARTUP) {
3668 Trace.endSection();
3669 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003670 }
3671
Winson Chunga2413752012-04-03 14:22:34 -07003672 private boolean canRunNewAppsAnimation() {
3673 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003674 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003675 }
3676
Winson Chungc9168342013-06-26 14:54:55 -07003677 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003678 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003679 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3680 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003681 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003682 return bounceAnim;
3683 }
3684
Adam Cohen27772732013-11-11 14:00:56 +00003685 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003686 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003687 }
3688
Tony Wickham55616cd2015-09-23 14:55:17 -07003689 public int getSearchBarHeight() {
3690 if (mLauncherCallbacks != null) {
3691 return mLauncherCallbacks.getSearchBarHeight();
3692 }
3693 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3694 }
3695
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003696 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003697 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3698 * multiple calls to bind the same list.)
3699 */
3700 @Thunk ArrayList<AppInfo> mTmpAppsList;
3701 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3702 public void run() {
3703 bindAllApplications(mTmpAppsList);
3704 mTmpAppsList = null;
3705 }
3706 };
3707
3708 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 * Add the icons for all apps.
3710 *
3711 * Implementation of the method from LauncherModel.Callbacks.
3712 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003713 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003714 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3715 mTmpAppsList = apps;
3716 return;
3717 }
3718
Winson Chungb745afb2015-03-02 11:51:23 -08003719 if (mAppsView != null) {
3720 mAppsView.setApps(apps);
3721 }
Adam Cohen9211d422014-10-07 18:14:53 -07003722 if (mLauncherCallbacks != null) {
3723 mLauncherCallbacks.bindAllApplications(apps);
3724 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 }
3726
3727 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003728 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3729 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3730 */
3731 @Override
3732 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
Tony Wickham010d2552017-01-20 08:15:28 -08003733 mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003734 }
3735
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 * A package was updated.
3738 *
3739 * Implementation of the method from LauncherModel.Callbacks.
3740 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003741 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003742 Runnable r = new Runnable() {
3743 public void run() {
3744 bindAppsUpdated(apps);
3745 }
3746 };
3747 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003748 return;
3749 }
3750
Winson Chungb745afb2015-03-02 11:51:23 -08003751 if (mAppsView != null) {
3752 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 }
3755
Sunny Goyal4390ace2014-10-13 11:33:11 -07003756 @Override
3757 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3758 Runnable r = new Runnable() {
3759 public void run() {
3760 bindWidgetsRestored(widgets);
3761 }
3762 };
3763 if (waitUntilResume(r)) {
3764 return;
3765 }
3766 mWorkspace.widgetsRestored(widgets);
3767 }
3768
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003770 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003771 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003772 *
3773 * @param updated list of shortcuts which have changed.
3774 * @param removed list of shortcuts which were deleted in the background. This can happen when
3775 * an app gets removed from the system or some of its components are no longer
3776 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003777 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003778 @Override
3779 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003780 final ArrayList<ShortcutInfo> removed, final UserHandle user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003781 Runnable r = new Runnable() {
3782 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003783 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003784 }
3785 };
3786 if (waitUntilResume(r)) {
3787 return;
3788 }
3789
Sunny Goyal4390ace2014-10-13 11:33:11 -07003790 if (!updated.isEmpty()) {
3791 mWorkspace.updateShortcuts(updated);
3792 }
3793
3794 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003795 HashSet<ComponentName> removedComponents = new HashSet<>();
3796 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3797
Sunny Goyal4390ace2014-10-13 11:33:11 -07003798 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003799 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08003800 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003801 } else {
3802 removedComponents.add(si.getTargetComponent());
3803 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003804 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003805
3806 if (!removedComponents.isEmpty()) {
3807 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3808 mWorkspace.removeItemsByMatcher(matcher);
3809 mDragController.onAppsRemoved(matcher);
3810 }
3811
3812 if (!removedDeepShortcuts.isEmpty()) {
3813 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3814 mWorkspace.removeItemsByMatcher(matcher);
3815 mDragController.onAppsRemoved(matcher);
3816 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003817 }
3818 }
3819
3820 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003821 * Update the state of a package, typically related to install state.
3822 *
3823 * Implementation of the method from LauncherModel.Callbacks.
3824 */
Sunny Goyale755d462014-07-22 13:48:29 -07003825 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003826 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3827 Runnable r = new Runnable() {
3828 public void run() {
3829 bindRestoreItemsChange(updates);
3830 }
3831 };
3832 if (waitUntilResume(r)) {
3833 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003834 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003835
Sunny Goyal756adbc2015-04-16 15:20:51 -07003836 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003837 }
3838
3839 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003840 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003841 * in addition to specific applications to remove, the reason being that
3842 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003843 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003844 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003845 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003846 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003847 public void bindWorkspaceComponentsRemoved(
3848 final HashSet<String> packageNames, final HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08003849 final UserHandle user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003850 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003851 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003852 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003853 }
Winson Chungd64d1762013-08-20 14:37:16 -07003854 };
3855 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003856 return;
3857 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003858 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003859 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3860 mWorkspace.removeItemsByMatcher(matcher);
3861 mDragController.onAppsRemoved(matcher);
3862
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003863 }
3864 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003865 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3866 mWorkspace.removeItemsByMatcher(matcher);
3867 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003868 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003869 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003870
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003871 @Override
3872 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3873 Runnable r = new Runnable() {
3874 public void run() {
3875 bindAppInfosRemoved(appInfos);
3876 }
3877 };
3878 if (waitUntilResume(r)) {
3879 return;
Joe Onorato36115782010-06-17 13:28:48 -04003880 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003881
Winson Chungdf95eb12013-10-16 14:57:07 -07003882 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003883 if (mAppsView != null) {
3884 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003885 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003886 }
Joe Onoratobe386092009-11-17 17:32:16 -08003887
Sunny Goyald164b7f2016-10-12 20:49:31 -07003888 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003889 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003890 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003891 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003892 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003893
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003894 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003895 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3896 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3897 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003898 return;
3899 }
3900
Sunny Goyald164b7f2016-10-12 20:49:31 -07003901 if (mWidgetsView != null && allWidgets != null) {
3902 mWidgetsView.setWidgets(allWidgets);
3903 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003904 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003905 }
3906
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003907 @Override
3908 public void notifyWidgetProvidersChanged() {
3909 if (mWorkspace.getState().shouldUpdateWidget) {
3910 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3911 }
3912 }
3913
Winson Chung4b919f82012-05-01 10:44:08 -07003914 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003915 if (mRotationEnabled) {
Sunny Goyala52ecb02016-12-16 15:04:51 -08003916 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
Winson Chung4b919f82012-05-01 10:44:08 -07003917 }
3918 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003919
Winson Chung4b919f82012-05-01 10:44:08 -07003920 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003921 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003922 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003923 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003924 } else {
3925 mHandler.postDelayed(new Runnable() {
3926 public void run() {
3927 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3928 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003929 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003930 }
Winson Chung4b919f82012-05-01 10:44:08 -07003931 }
Winson Chung400438b2011-01-16 17:53:48 -08003932 }
3933
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003934 private void markAppsViewShown() {
3935 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3936 return;
3937 }
3938 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3939 }
3940
3941 private boolean shouldShowDiscoveryBounce() {
3942 if (mState != mState.WORKSPACE) {
3943 return false;
3944 }
3945 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3946 return true;
3947 }
3948 if (!mIsResumeFromActionScreenOff) {
3949 return false;
3950 }
3951 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3952 return false;
3953 }
3954 return true;
3955 }
3956
Winson Chung5ffd51d2015-06-25 11:36:50 -07003957 protected void moveWorkspaceToDefaultScreen() {
3958 mWorkspace.moveToDefaultScreen(false);
3959 }
3960
Winson Chung80baf5a2010-08-09 16:03:15 -07003961 /**
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003962 * $ adb shell dumpsys activity com.android.launcher3.Launcher [--all]
Joe Onoratobe386092009-11-17 17:32:16 -08003963 */
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003964 @Override
3965 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3966 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07003967
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003968 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
3969 writer.println(prefix + "Workspace Items");
3970 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
3971 writer.println(prefix + " Homescreen " + i);
3972
3973 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
3974 for (int j = 0; j < layout.getChildCount(); j++) {
3975 Object tag = layout.getChildAt(j).getTag();
3976 if (tag != null) {
3977 writer.println(prefix + " " + tag.toString());
3978 }
3979 }
3980 }
3981
3982 writer.println(prefix + " Hotseat");
3983 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
Sunny Goyala1365452015-10-01 15:46:24 -07003984 for (int j = 0; j < layout.getChildCount(); j++) {
3985 Object tag = layout.getChildAt(j).getTag();
3986 if (tag != null) {
3987 writer.println(prefix + " " + tag.toString());
3988 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003989 }
Sunny Goyala1365452015-10-01 15:46:24 -07003990
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003991 try {
3992 FileLog.flushAll(writer);
3993 } catch (Exception e) {
3994 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07003995 }
3996 }
3997
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08003998 writer.println(prefix + "Misc:");
3999 writer.print(prefix + "\tmWorkspaceLoading=" + mWorkspaceLoading);
4000 writer.print(" mPendingRequestArgs=" + mPendingRequestArgs);
4001 writer.println(" mPendingActivityResult=" + mPendingActivityResult);
4002
4003 mModel.dumpState(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004004
Adam Cohen9211d422014-10-07 18:14:53 -07004005 if (mLauncherCallbacks != null) {
4006 mLauncherCallbacks.dump(prefix, fd, writer, args);
4007 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004008 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004009
Sunny Goyal66b24572016-09-21 15:57:55 -07004010 @Override
4011 @TargetApi(Build.VERSION_CODES.N)
4012 public void onProvideKeyboardShortcuts(
4013 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4014
4015 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4016 if (mState == State.WORKSPACE) {
4017 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4018 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4019 }
4020 View currentFocus = getCurrentFocus();
4021 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4022 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4023 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4024 }
4025 if (currentFocus instanceof BubbleTextView &&
4026 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4027 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4028 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4029 }
4030 if (!shortcutInfos.isEmpty()) {
4031 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4032 }
4033
4034 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4035 }
4036
4037 @Override
4038 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4039 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4040 switch (keyCode) {
4041 case KeyEvent.KEYCODE_A:
4042 if (mState == State.WORKSPACE) {
4043 showAppsView(true, true, false);
4044 return true;
4045 }
4046 break;
4047 case KeyEvent.KEYCODE_S: {
4048 View focusedView = getCurrentFocus();
4049 if (focusedView instanceof BubbleTextView
4050 && focusedView.getTag() instanceof ItemInfo
4051 && mAccessibilityDelegate.performAction(focusedView,
4052 (ItemInfo) focusedView.getTag(),
4053 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Tony Wickham540913e2017-01-23 11:47:51 -08004054 PopupContainerWithArrow.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004055 return true;
4056 }
4057 break;
4058 }
4059 case KeyEvent.KEYCODE_O:
4060 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4061 return true;
4062 }
4063 break;
4064 }
4065 }
4066 return super.onKeyShortcut(keyCode, event);
4067 }
4068
Adam Cohen59400422014-03-05 18:07:04 -08004069 public static CustomAppWidget getCustomAppWidget(String name) {
4070 return sCustomAppWidgets.get(name);
4071 }
4072
4073 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4074 return sCustomAppWidgets;
4075 }
4076
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004077 public static Launcher getLauncher(Context context) {
4078 if (context instanceof Launcher) {
4079 return (Launcher) context;
4080 }
4081 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4082 }
4083
Sunny Goyal745bad92016-05-02 10:54:12 -07004084 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4085
4086 @Override
4087 public void onSharedPreferenceChanged(
4088 SharedPreferences sharedPreferences, String key) {
4089 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4090 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4091 if (!waitUntilResume(this, true)) {
4092 run();
4093 }
4094 }
4095 }
4096
4097 @Override
4098 public void run() {
4099 setOrientation();
4100 }
4101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004102}