blob: c7fac87e33cfe191620ffa028301ebffc4822e25 [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;
Michael Jurka946ad472010-07-09 18:05:18 -070025import android.app.Activity;
Winson Chungc7450e32012-04-17 17:34:08 -070026import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080029import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070030import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080032import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080033import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040034import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070036import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040037import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070039import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070040import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070041import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070042import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070046import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070047import android.graphics.Bitmap;
Jon Mirandacc42c5b2016-10-27 17:15:27 -070048import android.graphics.Point;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070051import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020052import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080054import android.os.Handler;
Winson Chunga2413752012-04-03 14:22:34 -070055import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040056import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070057import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070058import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.text.Selection;
60import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070061import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070063import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070065import android.view.HapticFeedbackConstants;
66import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070067import android.view.KeyboardShortcutGroup;
68import android.view.KeyboardShortcutInfo;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.MotionEvent;
71import android.view.Surface;
72import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080073import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.ViewGroup;
75import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080076import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070077import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070078import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.inputmethod.InputMethodManager;
Chet Haasea8f996d2015-02-17 12:56:04 -080080import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070082
Sunny Goyal651077b2014-06-30 14:15:31 -070083import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070084import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070085import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070086import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070087import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080088import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070089import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010090import com.android.launcher3.compat.LauncherActivityInfoCompat;
91import com.android.launcher3.compat.LauncherAppsCompat;
92import com.android.launcher3.compat.UserHandleCompat;
93import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080094import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070095import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -070096import com.android.launcher3.dragndrop.DragController;
97import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070098import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070099import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700100import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000101import com.android.launcher3.folder.Folder;
102import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700103import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700104import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700105import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700106import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700107import com.android.launcher3.model.PackageItemInfo;
108import com.android.launcher3.model.WidgetItem;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700109import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700110import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700111import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700112import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700113import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700114import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700116import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700117import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700118import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700119import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700120import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700121import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700122import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700124import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700126
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700130import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800131import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700134import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100139public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700140 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
141 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700142 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700146 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700156 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700157 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700158
Sunny Goyal28c6b962015-10-12 11:42:05 -0700159 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
160
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700161 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
162
Mathew Inwood876a8462013-06-14 14:12:41 +0100163 /**
164 * IntentStarter uses request codes starting with this. This must be greater than all activity
165 * request codes used internally.
166 */
167 protected static final int REQUEST_LAST = 100;
168
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700170 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chung2672ff92012-05-04 16:22:30 -0700173 // The Intent extra that defines whether to ignore the launch animation
174 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400175 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
178 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700179 // Type: int
180 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700181 // Type: PendingRequestArgs
182 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
183 // Type: ActivityResultInfo
184 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700186 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800187
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700189 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
190 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700191
Adam Cohen091440a2015-03-18 14:16:05 -0700192 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700193 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700195 private boolean mIsSafeModeEnabled;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800198 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700199 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700200 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Winson Chunga2413752012-04-03 14:22:34 -0700202 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700203 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
204 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700208 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800210 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700211 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700212
213 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Sunny Goyalffe83f12014-08-14 17:39:34 -0700215 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Michael Jurka0280c3b2010-09-17 15:00:07 -0700218 private int[] mTmpAddItemCellCoordinates = new int[2];
219
Sunny Goyal316490e2015-06-02 09:38:28 -0700220 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800221 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700222
Michael Jurka838a4ca2011-02-07 13:33:06 -0800223 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700224 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700225
Sunny Goyal47328fd2016-05-25 18:56:41 -0700226 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700227
228 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700229 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700230 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700231
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700232 // Main container view and the model for the widget tray screen.
233 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700234 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700235
Winson Chung4a2afa32012-07-19 14:53:05 -0700236 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
237 // scroll issues (because the workspace may not have been measured yet) and extra work.
238 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
239 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240
241 private SpannableStringBuilder mDefaultKeySsb = null;
242
Adam Cohen091440a2015-03-18 14:16:05 -0700243 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800245 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700246 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
Michael Jurka1e2f4652013-07-08 18:03:46 -0700248 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700249 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700250 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700251
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700254 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700255 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700256 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700257 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700258 private boolean mHasFocus = false;
259 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700261 /** Maps launcher activity components to their list of shortcut ids. */
262 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
263
Adam Cohen61f560d2013-09-30 15:58:20 -0700264 private View.OnTouchListener mHapticFeedbackTouchListener;
265
Winson Chung400438b2011-01-16 17:53:48 -0800266 // Determines how long to wait after a rotation before restoring the screen orientation to
267 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700268 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800269
Adam Cohen1462de32012-07-24 22:34:36 -0700270 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
271
Winson Chung46353de2012-02-16 14:05:10 -0800272 // We only want to get the SharedPreferences once since it does an FS stat each time we get
273 // it from the context.
274 private SharedPreferences mSharedPrefs;
275
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700276 private DeviceProfile mDeviceProfile;
277
Adam Cohen4b66bf32015-09-18 12:15:19 -0700278 private boolean mMoveToDefaultScreenFromNewIntent;
279
Winson Chung13eb5272015-05-11 16:30:13 -0700280 // This is set to the view that launched the activity that navigated the user away from
281 // launcher. Since there is no callback for when the activity has finished launching, enable
282 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800283 private BubbleTextView mWaitingForResume;
284
Adam Cohen59400422014-03-05 18:07:04 -0800285 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
286 new HashMap<String, CustomAppWidget>();
287
Adam Cohen59400422014-03-05 18:07:04 -0800288 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700289 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
290 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800291 }
292 }
293
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700294 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
295 // state transition. If another state transition happened during this delay,
296 // simply unregister this runnable.
297 private Runnable mExitSpringLoadedModeRunnable;
298
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700300 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700301 if (mWorkspace != null) {
302 mWorkspace.buildPageHardwareLayers();
303 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700304 }
305 };
306
Sunny Goyal2100c782016-08-22 16:00:03 -0700307 // Activity result which needs to be processed after workspace has loaded.
308 private ActivityResultInfo mPendingActivityResult;
309 /**
310 * Holds extra information required to handle a result from an external call, like
311 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
312 */
313 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800314
Hyunyoung Songaa953652016-04-19 18:30:24 -0700315 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800316
Jon Miranda379198e2016-09-23 08:54:40 -0700317 private float mLastDispatchTouchEventX = 0.0f;
318 private float mEdgeOfScreenThresholdPx = 0.0f;
319
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700320 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700321 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400322
323 @Thunk void setOrientation() {
324 if (mRotationEnabled) {
325 unlockScreenOrientation(true);
326 } else {
327 setRequestedOrientation(
328 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700329 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400330 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700331
Sunny Goyal745bad92016-05-02 10:54:12 -0700332 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700336 if (DEBUG_STRICT_MODE) {
337 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
338 .detectDiskReads()
339 .detectDiskWrites()
340 .detectNetwork() // or .detectAll() for all detectable problems
341 .penaltyLog()
342 .build());
343 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
344 .detectLeakedSqlLiteObjects()
345 .detectLeakedClosableObjects()
346 .penaltyLog()
347 .penaltyDeath()
348 .build());
349 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700350 if (LauncherAppState.PROFILE_STARTUP) {
351 Trace.beginSection("Launcher-onCreate");
352 }
Winson Chunga2413752012-04-03 14:22:34 -0700353
Adam Cohen9211d422014-10-07 18:14:53 -0700354 if (mLauncherCallbacks != null) {
355 mLauncherCallbacks.preOnCreate();
356 }
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400359
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400360 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700361
Adam Cohen2e6da152015-05-06 11:42:25 -0700362 // Load configuration-specific DeviceProfile
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700363 mDeviceProfile =
364 getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
365 ? app.getInvariantDeviceProfile().landscapeProfile
366 : app.getInvariantDeviceProfile().portraitProfile;
367
368 if (Utilities.isNycOrAbove() && isInMultiWindowMode()) {
369 Display display = getWindowManager().getDefaultDisplay();
370 Point mwSize = new Point();
371 display.getSize(mwSize);
372 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
373 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700374
Sunny Goyalf7258242015-10-19 16:59:07 -0700375 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700376 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800377 mModel = app.setLauncher(this);
378 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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700396
Jon Miranda379198e2016-09-23 08:54:40 -0700397 mEdgeOfScreenThresholdPx = getResources()
398 .getDimensionPixelSize(R.dimen.edge_of_screen_threshold);
399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 setupViews();
Winson1f064272016-07-18 17:18:02 -0700401 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700402 mExtractedColors = new ExtractedColors();
403 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404
Tony Wickhame2217252016-03-22 16:34:23 -0700405 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
406 .addAccessibilityStateChangeListener(this);
407
Joe Onorato7c312c12009-08-13 21:36:53 -0700408 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700409
Sunny Goyalfe770c92016-09-27 14:38:58 -0700410 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411
Sunny Goyale26d1002016-06-20 14:52:14 -0700412 if (LauncherAppState.PROFILE_STARTUP) {
413 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
415
Sunny Goyal2100c782016-08-22 16:00:03 -0700416 // We only load the page synchronously if the user rotates (or triggers a
417 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700418 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
419 if (savedInstanceState != null) {
420 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
421 }
422 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700423 // If we are not binding synchronously, show a fade in animation when
424 // the first page bind completes.
425 mDragLayer.setAlpha(0);
426 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700427 // Pages bound synchronously.
428 mWorkspace.setCurrentPage(currentScreen);
429
Sunny Goyal2100c782016-08-22 16:00:03 -0700430 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432
433 // For handling default keys
434 mDefaultKeySsb = new SpannableStringBuilder();
435 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800436
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800437 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700438 // In case we are on a device with locked rotation, we should look at preferences to check
439 // if the user has specifically allowed rotation.
440 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700441 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700442 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
443 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700444 }
445
446 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
447 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400448 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700449
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
Tony Wickham827cef22016-03-17 15:39:39 -0700456 public void onExtractedColorsChanged() {
457 loadExtractedColorsAndColorItems();
458 }
459
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700460 @Override
461 public void onAppWidgetHostReset() {
462 if (mAppWidgetHost != null) {
463 mAppWidgetHost.startListening();
464 }
465 }
466
Tony Wickham827cef22016-03-17 15:39:39 -0700467 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700468 // TODO: do this in pre-N as well, once the extraction part is complete.
Tony Wickham345bff32016-09-28 15:34:51 -0700469 if (Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700470 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700471 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700472 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700473 // It's possible that All Apps is visible when this is run,
474 // so always use light status bar in that case.
475 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700476 }
477 }
478
Tony Wickham345bff32016-09-28 15:34:51 -0700479 /**
480 * Sets the status bar to be light or not. Light status bar means dark icons.
481 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
482 */
483 public void activateLightStatusBar(boolean activate) {
Tony Wickham0fed55b2016-10-12 14:55:46 -0700484 boolean lightStatusBar = activate || (FeatureFlags.LIGHT_STATUS_BAR
485 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
486 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700487 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
488 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700489 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700490 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700491 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700492 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700493 }
Tony Wickham345bff32016-09-28 15:34:51 -0700494 if (newSystemUiFlags != oldSystemUiFlags) {
495 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
496 }
Tony Wickham93113872016-09-14 16:46:32 -0700497 }
498
Adam Cohen9211d422014-10-07 18:14:53 -0700499 private LauncherCallbacks mLauncherCallbacks;
500
501 public void onPostCreate(Bundle savedInstanceState) {
502 super.onPostCreate(savedInstanceState);
503 if (mLauncherCallbacks != null) {
504 mLauncherCallbacks.onPostCreate(savedInstanceState);
505 }
506 }
507
Winson1f064272016-07-18 17:18:02 -0700508 public void onInsetsChanged(Rect insets) {
509 mDeviceProfile.updateInsets(insets);
510 mDeviceProfile.layout(this, true /* notifyListeners */);
511 }
512
Sunny Goyal32554d12015-12-03 15:31:25 -0800513 /**
514 * Call this after onCreate to set or clear overlay.
515 */
516 public void setLauncherOverlay(LauncherOverlay overlay) {
517 if (overlay != null) {
518 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
519 }
520 mWorkspace.setLauncherOverlay(overlay);
521 }
522
Adam Cohen9211d422014-10-07 18:14:53 -0700523 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
524 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700525 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700526 private boolean mWorkspaceImportanceStored = false;
527 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700528 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800529 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700530 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
531
532 @Override
533 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700534 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 return;
536 }
537 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700538 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 if (mWorkspace != null) {
540 mWorkspaceImportanceForAccessibility =
541 mWorkspace.getImportantForAccessibility();
542 mWorkspace.setImportantForAccessibility(
543 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
544 mWorkspaceImportanceStored = true;
545 }
546 if (mHotseat != null) {
547 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
548 mHotseat.setImportantForAccessibility(
549 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
550 mHotseatImportanceStored = true;
551 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700552 }
553
554 @Override
555 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700556 if (mWorkspaceImportanceStored && mWorkspace != null) {
557 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
558 }
559 if (mHotseatImportanceStored && mHotseat != null) {
560 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
561 }
562 mWorkspaceImportanceStored = false;
563 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565 });
Adam Cohen9211d422014-10-07 18:14:53 -0700566 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700567 }
568
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700569 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700570 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700571 if (mLauncherCallbacks != null) {
572 mLauncherCallbacks.onLauncherProviderChange();
573 }
574 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700575
Adam Cohen9211d422014-10-07 18:14:53 -0700576 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700577 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700578 if (mLauncherCallbacks != null) {
579 return mLauncherCallbacks.hasCustomContentToLeft();
580 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700581 return false;
582 }
583
Dave Hawkeya8881582013-09-17 15:55:33 -0600584 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500585 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600586 * {@link #addToCustomContentPage}. This will only be invoked if
587 * {@link #hasCustomContentToLeft()} is {@code true}.
588 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500589 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 mLauncherCallbacks.populateCustomContentContainer();
592 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600593 }
594
595 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700596 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 * ensure the custom content page is added or removed if necessary.
598 */
599 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600600 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 // Not bound yet, wait for bindScreens to be called.
602 return;
603 }
604
605 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
606 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 mWorkspace.createCustomContentContainer();
608 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
610 mWorkspace.removeCustomContentPage();
611 }
612 }
613
Hyunyoung Songaa953652016-04-19 18:30:24 -0700614 public UserEventDispatcher getUserEventDispatcher() {
615 if (mLauncherCallbacks != null) {
616 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
617 if (dispatcher != null) {
618 return dispatcher;
619 }
620 }
621
Sunny Goyal64976d52016-06-20 14:56:28 -0700622 // Logger object is a singleton and does not have to be coupled with the foreground
623 // activity. Since most user event logging is done on the UI, the object is retrieved
624 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700625 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700626 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700627 }
628 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800629 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100630
Hyunyoung Song3f471442015-04-08 19:01:34 -0700631 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700632 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
633 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700634 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700635 }
636
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000637 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700638 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
639 // This cast is safe as long as the id < 0x00FFFFFF
640 // Since we jail all the dynamically generated views, there should be no clashes
641 // with any other views.
642 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000643 }
644
645 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700646 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
647 * a configuration step, this allows the proper animations to run after other transitions.
648 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 private long completeAdd(
650 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
651 long screenId = info.screenId;
652 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700653 // When the screen id represents an actual screen (as opposed to a rank) we make sure
654 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700656 }
Adam Cohendb364c32014-05-20 14:23:40 -0700657
Sunny Goyal2100c782016-08-22 16:00:03 -0700658 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700660 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700664 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700665 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700667 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700668 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 int widgetId = appWidgetId;
670 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700671 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700673 // Since the view was just bound, also launch the configure activity if needed
674 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
675 .getLauncherAppWidgetInfo(widgetId);
676 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700678 }
679 }
680 break;
681 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800682 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700683
Adam Cohendb364c32014-05-20 14:23:40 -0700684 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 }
686
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800687 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700688 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 if (isWorkspaceLoading()) {
690 // process the result once the workspace has loaded.
691 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
692 return;
693 }
694 mPendingActivityResult = null;
695
Winson Chunge341d302013-08-16 14:31:00 -0700696 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700697 final PendingRequestArgs requestArgs = mPendingRequestArgs;
698 setWaitingForResult(null);
699 if (requestArgs == null) {
700 return;
701 }
702
703 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700704
Adam Cohenad4e15c2013-10-17 16:21:35 -0700705 Runnable exitSpringLoaded = new Runnable() {
706 @Override
707 public void run() {
708 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
709 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
710 }
711 };
712
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700714 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700715 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
717 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700718 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700719 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700720 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700722 addAppWidgetImpl(
723 appWidgetId, requestArgs, null,
724 requestArgs.getWidgetProvider(),
725 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700726 }
727 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200728 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
729 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700730 // User could have free-scrolled between pages before picking a wallpaper; make sure
731 // we move to the closest one now.
732 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700733 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 }
735 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700739 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700740
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 // We have special handling for widgets
742 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800743 final int appWidgetId;
744 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
745 : -1;
746 if (widgetId < 0) {
747 appWidgetId = pendingAddWidgetId;
748 } else {
749 appWidgetId = widgetId;
750 }
751
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800753 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700754 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
755 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700757 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700758 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 @Override
760 public void run() {
761 exitSpringLoadedDragModeDelayed(false, 0, null);
762 }
763 };
Adam Cohendb364c32014-05-20 14:23:40 -0700764
Sunny Goyal2100c782016-08-22 16:00:03 -0700765 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
766 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
767 } else {
768 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
769 // When the screen id represents an actual screen (as opposed to a rank)
770 // we make sure that the drop page actually exists.
771 requestArgs.screenId =
772 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700773 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700774 final CellLayout dropLayout =
775 mWorkspace.getScreenWithId(requestArgs.screenId);
776
777 dropLayout.setDropPending(true);
778 final Runnable onComplete = new Runnable() {
779 @Override
780 public void run() {
781 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
782 dropLayout.setDropPending(false);
783 }
784 };
785 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
786 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700787 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800788 return;
789 }
790
Sunny Goyald478c832016-04-01 12:04:16 -0700791 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
792 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700793 if (resultCode == RESULT_OK) {
794 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700795 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700796 }
797 // Leave the widget in the pending state if the user canceled the configure.
798 return;
799 }
800
Sunny Goyalaad90582015-07-09 14:22:50 -0700801 if (requestCode == REQUEST_CREATE_SHORTCUT) {
802 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700803 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
804 completeAdd(requestCode, data, -1, requestArgs);
805 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
806 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
807
Sunny Goyalaad90582015-07-09 14:22:50 -0700808 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700809 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800813 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800814 }
815
816 @Override
817 protected void onActivityResult(
818 final int requestCode, final int resultCode, final Intent data) {
819 handleActivityResult(requestCode, resultCode, data);
820 if (mLauncherCallbacks != null) {
821 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
822 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800823 }
824
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600825 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700826 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600827 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700828 PendingRequestArgs pendingArgs = mPendingRequestArgs;
829 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
830 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
831 setWaitingForResult(null);
832
Sunny Goyal28c6b962015-10-12 11:42:05 -0700833 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700834 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700835 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700836 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700837 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700838 Intent intent = pendingArgs.getPendingIntent();
839
Sunny Goyal28c6b962015-10-12 11:42:05 -0700840 if (grantResults.length > 0
841 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700842 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700843 } else {
844 // TODO: Show a snack bar with link to settings
845 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700846 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700847 }
848 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600849 if (mLauncherCallbacks != null) {
850 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
851 grantResults);
852 }
853 }
854
Adam Cohendb364c32014-05-20 14:23:40 -0700855 /**
856 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
857 *
858 * @param screenId the screen id to check
859 * @return the new screen, or screenId if it exists
860 */
861 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800862 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700863 if (dropLayout == null) {
864 // it's possible that the add screen was removed because it was
865 // empty and a re-bind occurred
866 mWorkspace.addExtraEmptyScreen();
867 return mWorkspace.commitExtraEmptyScreen();
868 } else {
869 return screenId;
870 }
871 }
872
Sunny Goyal2100c782016-08-22 16:00:03 -0700873 @Thunk void completeTwoStageWidgetDrop(
874 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
875 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 Runnable onCompleteRunnable = null;
877 int animationType = 0;
878
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700879 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 if (resultCode == RESULT_OK) {
881 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
882 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700883 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700884 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 onCompleteRunnable = new Runnable() {
886 @Override
887 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700888 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700889 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
890 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 }
892 };
893 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800894 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700898 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
900 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 // The animated view may be null in the case of a rotation during widget configuration
903 onCompleteRunnable.run();
904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700908 protected void onStop() {
909 super.onStop();
910 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700911
912 if (mLauncherCallbacks != null) {
913 mLauncherCallbacks.onStop();
914 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700915
916 if (Utilities.isNycMR1OrAbove()) {
917 mAppWidgetHost.stopListening();
918 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700919 }
920
921 @Override
922 protected void onStart() {
923 super.onStart();
924 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700925
926 if (mLauncherCallbacks != null) {
927 mLauncherCallbacks.onStart();
928 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700929
930 if (Utilities.isNycMR1OrAbove()) {
931 mAppWidgetHost.startListening();
932 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700933 }
934
935 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200937 long startTime = 0;
938 if (DEBUG_RESUME_TIME) {
939 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400940 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200941 }
Adam Cohen9211d422014-10-07 18:14:53 -0700942
943 if (mLauncherCallbacks != null) {
944 mLauncherCallbacks.preOnResume();
945 }
946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700948 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700949
Winson Chung4a2afa32012-07-19 14:53:05 -0700950 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700951 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700952 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800953 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700954 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700955 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700956 // view after launching an app, as they may be depending on the UI to be static to
957 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700958 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700959 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800960 } else if (mOnResumeState == State.WIDGETS) {
961 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700962 }
963 mOnResumeState = State.NONE;
964
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800965 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700966 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700967 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700968 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700969 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700971 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200972 // We might have postponed some bind calls until onResume (see waitUntilResume) --
973 // execute them here
974 long startTimeCallbacks = 0;
975 if (DEBUG_RESUME_TIME) {
976 startTimeCallbacks = System.currentTimeMillis();
977 }
978
Michael Jurka1e2f4652013-07-08 18:03:46 -0700979 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
980 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200981 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700982 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200983 if (DEBUG_RESUME_TIME) {
984 Log.d(TAG, "Time spent processing callbacks in onResume: " +
985 (System.currentTimeMillis() - startTimeCallbacks));
986 }
Michael Jurka447bf842013-05-15 14:52:15 +0200987 }
Michael Jurka54554252013-08-01 12:52:23 +0200988 if (mOnResumeCallbacks.size() > 0) {
989 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
990 mOnResumeCallbacks.get(i).run();
991 }
992 mOnResumeCallbacks.clear();
993 }
Winson Chunge4e50662012-01-23 14:45:13 -0800994
995 // Reset the pressed state of icons that were locked in the press state while activities
996 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800997 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800998 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800999 mWaitingForResume.setStayPressed(false);
1000 }
Winson Chung82963d52013-10-09 11:20:57 -07001001
Adam Cohen06dff352012-06-01 17:17:08 -07001002 // It is possible that widgets can receive updates while launcher is not in the foreground.
1003 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1004 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1005 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001006 if (!isWorkspaceLoading()) {
1007 getWorkspace().reinflateWidgetsIfNecessary();
1008 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001009
Michael Jurka447bf842013-05-15 14:52:15 +02001010 if (DEBUG_RESUME_TIME) {
1011 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1012 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001013
Adam Cohen4b66bf32015-09-18 12:15:19 -07001014 // We want to suppress callbacks about CustomContent being shown if we have just received
1015 // onNewIntent while the user was present within launcher. In that case, we post a call
1016 // to move the user to the main screen (which will occur after onResume). We don't want to
1017 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1018 // suppress here.
1019 if (mWorkspace.getCustomContentCallbacks() != null
1020 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001021 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001022 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001023 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1024 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001025 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001026 }
1027 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001028 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001029 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001030 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001031
Sunny Goyal756adbc2015-04-16 15:20:51 -07001032 if (!isWorkspaceLoading()) {
1033 // Process any items that were added while Launcher was away.
1034 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001035
1036 // Refresh shortcuts if the permission changed.
1037 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001038 }
Adam Cohen9211d422014-10-07 18:14:53 -07001039
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001040 if (shouldShowDiscoveryBounce()) {
1041 mAllAppsController.showDiscoveryBounce();
1042 }
1043 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001044 if (mLauncherCallbacks != null) {
1045 mLauncherCallbacks.onResume();
1046 }
Adam Cohen06dff352012-06-01 17:17:08 -07001047 }
1048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001050 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001051 // Ensure that items added to Launcher are queued until Launcher returns
1052 InstallShortcutReceiver.enableInstallQueue();
1053
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001055 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001056 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001057 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001058
1059 // We call onHide() aggressively. The custom content callbacks should be able to
1060 // debounce excess onHide calls.
1061 if (mWorkspace.getCustomContentCallbacks() != null) {
1062 mWorkspace.getCustomContentCallbacks().onHide();
1063 }
Romain Guycbb89e42009-06-08 15:52:54 -07001064
Adam Cohen9211d422014-10-07 18:14:53 -07001065 if (mLauncherCallbacks != null) {
1066 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001068 }
1069
1070 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001071 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1072 // by a onResume or by scrolling otherwise.
1073 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001074
1075 // Custom content is completely hidden
1076 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001077
1078 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1079 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001080
1081 // Indicates whether the user is allowed to scroll away from the custom content.
1082 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001083 }
1084
Adam Cohenc2d6e892014-10-16 09:49:24 -07001085 public interface LauncherOverlay {
1086
1087 /**
1088 * Touch interaction leading to overscroll has begun
1089 */
1090 public void onScrollInteractionBegin();
1091
1092 /**
1093 * Touch interaction related to overscroll has ended
1094 */
1095 public void onScrollInteractionEnd();
1096
1097 /**
1098 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1099 * screen (or in the case of RTL, the rightmost screen).
1100 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001101 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001102
1103 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001104 * Called when the launcher is ready to use the overlay
1105 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001106 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001107 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001108 }
1109
Jun Mukai8af0cd82015-05-12 12:32:12 -07001110 public interface LauncherSearchCallbacks {
1111 /**
1112 * Called when the search overlay is shown.
1113 */
1114 public void onSearchOverlayOpened();
1115
1116 /**
1117 * Called when the search overlay is dismissed.
1118 */
1119 public void onSearchOverlayClosed();
1120 }
1121
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001123 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001124 }
1125
1126 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1127
Sunny Goyalc86df472016-02-25 09:19:38 -08001128 public void onScrollChanged(float progress) {
1129 if (mWorkspace != null) {
1130 mWorkspace.onOverlayScrollChanged(progress);
1131 }
1132 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 }
1134
Jorim Jaggid017f882014-01-14 17:08:48 -08001135 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001136 if (mLauncherCallbacks != null) {
1137 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001138 } else {
1139 // On devices with a locked orientation, we will at least have the allow rotation
1140 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001141 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001142 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001143 }
1144
Adam Cohen9211d422014-10-07 18:14:53 -07001145 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001146 CustomContentCallbacks callbacks, String description) {
1147 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001148 }
1149
Adam Cohenedb40762013-07-18 16:45:45 -07001150 // The custom content needs to offset its content to account for the QSB
1151 public int getTopOffsetForCustomContent() {
1152 return mWorkspace.getPaddingTop();
1153 }
1154
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001155 @Override
1156 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001157 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001158 if (mModel.isCurrentCallbacks(this)) {
1159 mModel.stopLoader();
1160 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001161 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1162
Romain Guy13c2e7b2010-03-10 19:45:00 -08001163 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001164 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001165
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001166 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001167 @Override
1168 public void onWindowFocusChanged(boolean hasFocus) {
1169 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001170 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001171
1172 if (mLauncherCallbacks != null) {
1173 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1174 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001175 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 private boolean acceptFilter() {
1178 final InputMethodManager inputManager = (InputMethodManager)
1179 getSystemService(Context.INPUT_METHOD_SERVICE);
1180 return !inputManager.isFullscreenMode();
1181 }
1182
1183 @Override
1184 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001185 final int uniChar = event.getUnicodeChar();
1186 final boolean handled = super.onKeyDown(keyCode, event);
1187 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1188 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1190 keyCode, event);
1191 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001192 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001193 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001194 // showSearchDialog()
1195 // If there are multiple keystrokes before the search dialog takes focus,
1196 // onSearchRequested() will be called for every keystroke,
1197 // but it is idempotent, so it's fine.
1198 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
1200 }
1201
Joe Onorato8a9625e2010-01-28 15:55:35 -08001202 // Eat the long press event so the keyboard doesn't come up.
1203 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1204 return true;
1205 }
1206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 return handled;
1208 }
1209
Winson46163472015-09-22 17:31:56 -07001210 @Override
1211 public boolean onKeyUp(int keyCode, KeyEvent event) {
1212 if (keyCode == KeyEvent.KEYCODE_MENU) {
1213 // Ignore the menu key if we are currently dragging or are on the custom content screen
1214 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001215 // Close any open floating view
1216 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001217
Winson46163472015-09-22 17:31:56 -07001218 // Stop resizing any widgets
1219 mWorkspace.exitWidgetResizeMode();
1220
1221 // Show the overview mode if we are on the workspace
1222 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1223 !mWorkspace.isSwitchingState()) {
1224 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001225 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001226 }
1227 }
1228 return true;
1229 }
1230 return super.onKeyUp(keyCode, event);
1231 }
1232
Karl Rosaen138a0412009-04-23 19:00:21 -07001233 private String getTypedText() {
1234 return mDefaultKeySsb.toString();
1235 }
1236
Sunny Goyal6f28e712016-09-13 14:19:29 -07001237 @Override
1238 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 mDefaultKeySsb.clear();
1240 mDefaultKeySsb.clearSpans();
1241 Selection.setSelection(mDefaultKeySsb, 0);
1242 }
1243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 /**
1245 * Restores the previous state, if it exists.
1246 *
1247 * @param savedState The previous state.
1248 */
1249 private void restoreState(Bundle savedState) {
1250 if (savedState == null) {
1251 return;
1252 }
1253
Sunny Goyalfe770c92016-09-27 14:38:58 -07001254 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1255 State[] stateValues = State.values();
1256 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1257 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001258 if (state == State.APPS || state == State.WIDGETS) {
1259 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001260 }
1261
Sunny Goyal2100c782016-08-22 16:00:03 -07001262 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1263 if (requestArgs != null) {
1264 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001266
1267 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
1269
1270 /**
1271 * Finds all the views we need and configure them properly.
1272 */
1273 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001274 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001275 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001276 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001277 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001278 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1279 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001280 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001281
Sunny Goyal784f9c32016-03-23 16:56:54 -07001282 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1283 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1284 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285
Winson Chung4c98d922011-05-31 16:50:48 -07001286 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001287 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001288
Winson Chung3d503fb2011-07-13 17:25:49 -07001289 // Setup the hotseat
1290 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1291 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001292 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001293 }
1294
Winsone9f27272015-10-13 10:47:51 -07001295 // Setup the overview panel
1296 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001297
Winson Chung4c98d922011-05-31 16:50:48 -07001298 // Setup the workspace
1299 mWorkspace.setHapticFeedbackEnabled(false);
1300 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001301 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001302 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1303 // default state, otherwise we will update to the wrong offsets in RTL
1304 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001305 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001306 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001307
Tony Wickham34d2c912015-09-11 09:27:58 -07001308 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001309 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001310
Winson Chungef7f8742015-06-04 17:18:17 -07001311 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001312 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001313 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001314 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1315 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1316 } else {
1317 mAppsView.setSearchBarController(new DefaultAppSearchController());
1318 }
Craig Mautner360310b2012-10-26 15:13:08 -07001319
Winson Chung3d503fb2011-07-13 17:25:49 -07001320 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song0de01172016-10-05 16:27:48 -07001321 mDragController.setDragScroller(mWorkspace);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001322 mDragController.setScrollView(mDragLayer);
1323 mDragController.setMoveTarget(mWorkspace);
1324 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001325 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001326
Peter Schiller310a9882016-06-30 13:52:36 -07001327 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1328 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1329 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001330
Sunny Goyal322d5562015-06-25 19:35:49 -07001331 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1332 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
Winsone9f27272015-10-13 10:47:51 -07001336 private void setupOverviewPanel() {
1337 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1338
Winsone9f27272015-10-13 10:47:51 -07001339 // Bind wallpaper button actions
1340 View wallpaperButton = findViewById(R.id.wallpaper_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001341 new OverviewButtonClickListener(LauncherLogProto.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001342 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001343 public void handleViewClick(View view) {
1344 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001345 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001346 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001347 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1348
1349 // Bind widget button actions
1350 mWidgetsButton = findViewById(R.id.widget_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001351 new OverviewButtonClickListener(LauncherLogProto.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001352 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001353 public void handleViewClick(View view) {
1354 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001355 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001356 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001357 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1358
1359 // Bind settings actions
1360 View settingsButton = findViewById(R.id.settings_button);
1361 boolean hasSettings = hasSettings();
1362 if (hasSettings) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001363 new OverviewButtonClickListener(LauncherLogProto.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001364 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001365 public void handleViewClick(View view) {
1366 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001367 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001368 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001369 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1370 } else {
1371 settingsButton.setVisibility(View.GONE);
1372 }
1373
1374 mOverviewPanel.setAlpha(0f);
1375 }
1376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001378 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001379 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001380 */
1381 public void setAllAppsButton(View allAppsButton) {
1382 mAllAppsButton = allAppsButton;
1383 }
1384
Sunny Goyalbb011da2016-06-15 15:42:29 -07001385 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001386 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001387 }
1388
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001389 public View getWidgetsButton() {
1390 return mWidgetsButton;
1391 }
1392
Anjali Koppal5ad44842014-03-10 20:34:39 -07001393 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 * Creates a view representing a shortcut.
1395 *
1396 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001398 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001399 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401
1402 /**
1403 * Creates a view representing a shortcut inflated from the specified resource.
1404 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 * @param parent The group the shortcut belongs to.
1406 * @param info The data structure describing the shortcut.
1407 *
1408 * @return A View inflated from layoutResId.
1409 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001410 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001411 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001412 parent, false);
1413 favorite.applyFromShortcutInfo(info, mIconCache);
1414 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001416 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 return favorite;
1418 }
1419
1420 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 * Add a shortcut to the workspace.
1422 *
1423 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001425 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001426 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001427 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001428 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Sunny Goyal5c97f512015-05-19 16:03:28 -07001430 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001431 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1432 args.getPendingIntent().getComponent() == null) {
1433 return;
1434 }
1435 if (!PackageManagerHelper.hasPermissionForActivity(
1436 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1437 // The app is trying to add a shortcut without sufficient permissions
1438 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001439 return;
1440 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001441 final View view = createShortcut(info);
1442
Sunny Goyal5c97f512015-05-19 16:03:28 -07001443 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001444 // First we check if we already know the exact location where we want to add this item.
1445 if (cellX >= 0 && cellY >= 0) {
1446 cellXY[0] = cellX;
1447 cellXY[1] = cellY;
1448 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001449
1450 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001451 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001452 true, null,null)) {
1453 return;
1454 }
1455 DragObject dragObject = new DragObject();
1456 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001457 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1458 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001459 return;
1460 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001461 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001462 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001463 }
1464
1465 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001466 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001467 return;
1468 }
1469
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001470 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
Sunny Goyal2100c782016-08-22 16:00:03 -07001472 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1473 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001477 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001479 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001481 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001482 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1483
Adam Cohened66b2b2012-01-23 17:28:51 -08001484 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001485 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001486 }
Romain Guycbb89e42009-06-08 15:52:54 -07001487
Adam Cohen59400422014-03-05 18:07:04 -08001488 if (appWidgetInfo.isCustomWidget) {
1489 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001490 }
1491
Adam Cohen59400422014-03-05 18:07:04 -08001492 LauncherAppWidgetInfo launcherInfo;
1493 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001494 launcherInfo.spanX = itemInfo.spanX;
1495 launcherInfo.spanY = itemInfo.spanY;
1496 launcherInfo.minSpanX = itemInfo.minSpanX;
1497 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001498 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001501 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502
Sunny Goyal2100c782016-08-22 16:00:03 -07001503 if (hostView == null) {
1504 // Perform actual inflation because we're live
1505 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001507 hostView.setVisibility(View.VISIBLE);
1508 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001511 private void addAppWidgetToWorkspace(
1512 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001513 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001514 hostView.setTag(item);
1515 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001516
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001517 hostView.setFocusable(true);
1518 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001519
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001520 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001521 item.cellX, item.cellY, item.spanX, item.spanY, insert);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001522 }
1523
Adam Cohended9f8d2010-11-03 13:25:16 -07001524 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1525 @Override
1526 public void onReceive(Context context, Intent intent) {
1527 final String action = intent.getAction();
1528 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001529 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001530
Winson Chungc100e8e2011-08-09 16:02:43 -07001531 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001532 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001533 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001534 if (!showWorkspace(false)) {
1535 // If we are already on the workspace, then manually reset all apps
1536 mAppsView.reset();
1537 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001538 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001539 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001540 }
1541 }
1542 };
1543
1544 @Override
1545 public void onAttachedToWindow() {
1546 super.onAttachedToWindow();
1547
1548 // Listen for broadcasts related to user-presence
1549 final IntentFilter filter = new IntentFilter();
1550 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001551 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001552 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001553 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001554
1555 if (mLauncherCallbacks != null) {
1556 mLauncherCallbacks.onAttachedToWindow();
1557 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001558 }
1559
1560 @Override
1561 public void onDetachedFromWindow() {
1562 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001563 if (mAttached) {
1564 unregisterReceiver(mReceiver);
1565 mAttached = false;
1566 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001567
1568 if (mLauncherCallbacks != null) {
1569 mLauncherCallbacks.onDetachedFromWindow();
1570 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 }
1572
1573 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001574 // The following code used to be in onResume, but it turns out onResume is called when
1575 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1576 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001577 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001578 if (!mWorkspaceLoading) {
1579 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001580 // We want to let Launcher draw itself at least once before we force it to build
1581 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001582 // apps is nice and speedy.
1583 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001584 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001585 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001586 if (mStarted) return;
1587 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001588 // We delay the layer building a bit in order to give
1589 // other message processing a time to run. In particular
1590 // this avoids a delay in hiding the IME if it was
1591 // currently shown, because doing that may involve
1592 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001593 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001594 final ViewTreeObserver.OnDrawListener listener = this;
1595 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001596 public void run() {
1597 if (mWorkspace != null &&
1598 mWorkspace.getViewTreeObserver() != null) {
1599 mWorkspace.getViewTreeObserver().
1600 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001601 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001602 }
1603 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001604 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001605 }
1606 });
1607 }
1608 clearTypedText();
1609 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 }
1611
Winson Chunga6945242014-01-08 14:04:34 -08001612 public DragLayer getDragLayer() {
1613 return mDragLayer;
1614 }
1615
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001616 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001617 return mAppsView;
1618 }
1619
Hyunyoung Song3f471442015-04-08 19:01:34 -07001620 public WidgetsContainerView getWidgetsView() {
1621 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001622 }
1623
Winson Chunga6945242014-01-08 14:04:34 -08001624 public Workspace getWorkspace() {
1625 return mWorkspace;
1626 }
1627
Sunny Goyal6178f132016-07-11 17:30:03 -07001628 public View getQsbContainer() {
1629 return mQsbContainer;
1630 }
1631
Winson Chunga6945242014-01-08 14:04:34 -08001632 public Hotseat getHotseat() {
1633 return mHotseat;
1634 }
1635
Jorim Jaggid017f882014-01-14 17:08:48 -08001636 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001637 return mOverviewPanel;
1638 }
1639
Sunny Goyal47328fd2016-05-25 18:56:41 -07001640 public DropTargetBar getDropTargetBar() {
1641 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001642 }
1643
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001644 public LauncherAppWidgetHost getAppWidgetHost() {
1645 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
Romain Guycbb89e42009-06-08 15:52:54 -07001647
Winson Chunga9abd0e2010-10-27 17:18:37 -07001648 public LauncherModel getModel() {
1649 return mModel;
1650 }
1651
Adam Cohen79d90c52016-04-22 13:29:20 -07001652 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001653 return mSharedPrefs;
1654 }
1655
Adam Cohen2e6da152015-05-06 11:42:25 -07001656 public DeviceProfile getDeviceProfile() {
1657 return mDeviceProfile;
1658 }
1659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 @Override
1661 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001662 long startTime = 0;
1663 if (DEBUG_RESUME_TIME) {
1664 startTime = System.currentTimeMillis();
1665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 super.onNewIntent(intent);
1667
Winson15f8b172015-08-19 11:02:39 -07001668 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1669 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1670 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001671
1672 // Check this condition before handling isActionMain, as this will get reset.
1673 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001674 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001675
Winson15f8b172015-08-19 11:02:39 -07001676 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1677 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001678 if (mWorkspace == null) {
1679 // Can be cases where mWorkspace is null, this prevents a NPE
1680 return;
1681 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001682 // In all these cases, only animate if we're already on home
1683 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001684
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001685 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001686 exitSpringLoadedDragMode();
1687
1688 // If we are already on home, then just animate back to the workspace,
1689 // otherwise, just wait until onResume to set the state back to Workspace
1690 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001691 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001692 } else {
1693 mOnResumeState = State.WORKSPACE;
1694 }
1695
1696 final View v = getWindow().peekDecorView();
1697 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001698 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001699 INPUT_METHOD_SERVICE);
1700 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1701 }
1702
Winson Chungb745afb2015-03-02 11:51:23 -08001703 // Reset the apps view
1704 if (!alreadyOnHome && mAppsView != null) {
1705 mAppsView.scrollToTop();
1706 }
1707
Hyunyoung Song3f471442015-04-08 19:01:34 -07001708 // Reset the widgets view
1709 if (!alreadyOnHome && mWidgetsView != null) {
1710 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001711 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001712
Adam Cohen9211d422014-10-07 18:14:53 -07001713 if (mLauncherCallbacks != null) {
1714 mLauncherCallbacks.onHomeIntent();
1715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 }
Adam Cohened307df2013-10-02 09:37:31 -07001717
Adam Cohen9211d422014-10-07 18:14:53 -07001718 if (mLauncherCallbacks != null) {
1719 mLauncherCallbacks.onNewIntent(intent);
1720 }
Winson15f8b172015-08-19 11:02:39 -07001721
1722 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1723 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1724 // animation.
1725 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001726 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001727 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001728 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1729 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001730
1731 // We use this flag to suppress noisy callbacks above custom content state
1732 // from onResume.
1733 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001734 mWorkspace.post(new Runnable() {
1735 @Override
1736 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001737 if (mWorkspace != null) {
1738 mWorkspace.moveToDefaultScreen(true);
1739 }
Winson15f8b172015-08-19 11:02:39 -07001740 }
1741 });
1742 }
1743 }
1744
1745 if (DEBUG_RESUME_TIME) {
1746 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1747 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001748 }
1749
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001751 public void onRestoreInstanceState(Bundle state) {
1752 super.onRestoreInstanceState(state);
1753 for (int page: mSynchronouslyBoundPages) {
1754 mWorkspace.restoreInstanceStateForChild(page);
1755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757
1758 @Override
1759 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001760 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001761 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1762 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001763
Adam Cohen21cd0022013-10-09 18:57:02 -07001764 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001765 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766
Michael Jurka883f55b2010-10-21 15:47:14 -07001767 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001768 // We close any open folders and shortcut containers since they will not be re-opened,
1769 // and we need to make sure this state is reflected.
1770 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001771
Sunny Goyal2100c782016-08-22 16:00:03 -07001772 if (mPendingRequestArgs != null) {
1773 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1774 }
1775 if (mPendingActivityResult != null) {
1776 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
1778
Adam Cohen9211d422014-10-07 18:14:53 -07001779 if (mLauncherCallbacks != null) {
1780 mLauncherCallbacks.onSaveInstanceState(outState);
1781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783
1784 @Override
1785 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001787
Michael Jurka9d906c72011-10-14 06:25:36 -07001788 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001789 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001790
Winson Chungcd2b0142011-06-08 16:02:26 -07001791 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001792 // It's possible to receive onDestroy after a new Launcher activity has
1793 // been created. In this case, don't interfere with the new Launcher.
1794 if (mModel.isCurrentCallbacks(this)) {
1795 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001796 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001797 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001798
Sunny Goyal745bad92016-05-02 10:54:12 -07001799 if (mRotationPrefChangeHandler != null) {
1800 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001804 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001806 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001808 mAppWidgetHost = null;
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 TextKeyListener.getInstance().release();
1811
Tony Wickhame2217252016-03-22 16:34:23 -07001812 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1813 .removeAccessibilityStateChangeListener(this);
1814
Michael Jurka2ecf9952012-06-18 12:52:28 -07001815 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001816
1817 if (mLauncherCallbacks != null) {
1818 mLauncherCallbacks.onDestroy();
1819 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 }
1821
Sunny Goyalae502842016-06-17 08:43:56 -07001822 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1823 return mAccessibilityDelegate;
1824 }
1825
Adam Cohena9cf38f2011-05-02 15:36:58 -07001826 public DragController getDragController() {
1827 return mDragController;
1828 }
1829
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001831 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001832 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001833 }
1834
1835 @Override
1836 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1837 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001838 try {
1839 super.startIntentSenderForResult(intent, requestCode,
1840 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1841 } catch (IntentSender.SendIntentException e) {
1842 throw new ActivityNotFoundException();
1843 }
1844 }
1845
Winson Chung70d72102011-08-12 11:24:35 -07001846 /**
1847 * Indicates that we want global search for this activity by setting the globalSearch
1848 * argument for {@link #startSearch} to true.
1849 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001851 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001853
Karl Rosaen138a0412009-04-23 19:00:21 -07001854 if (initialQuery == null) {
1855 // Use any text typed in the launcher as the initial query
1856 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 if (appSearchData == null) {
1859 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001860 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001862
Sunny Goyal6f28e712016-09-13 14:19:29 -07001863 if (mLauncherCallbacks == null ||
1864 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1865 // Starting search from the callbacks failed. Start the default global search.
1866 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001867 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001868
1869 // We need to show the workspace after starting the search
1870 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001871 }
1872
Ian Parkinson047036e2014-09-01 15:40:53 +01001873 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001874 * Starts the global search activity. This code is a copied from SearchManager
1875 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001876 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001877 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001878 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001879 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1880 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1881 if (globalSearchActivity == null) {
1882 Log.w(TAG, "No global search activity found.");
1883 return;
1884 }
1885 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1886 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1887 intent.setComponent(globalSearchActivity);
1888 // Make sure that we have a Bundle to put source in
1889 if (appSearchData == null) {
1890 appSearchData = new Bundle();
1891 } else {
1892 appSearchData = new Bundle(appSearchData);
1893 }
Adam Cohen9211d422014-10-07 18:14:53 -07001894 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001895 if (!appSearchData.containsKey("source")) {
1896 appSearchData.putString("source", getPackageName());
1897 }
1898 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1899 if (!TextUtils.isEmpty(initialQuery)) {
1900 intent.putExtra(SearchManager.QUERY, initialQuery);
1901 }
1902 if (selectInitialQuery) {
1903 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1904 }
1905 intent.setSourceBounds(sourceBounds);
1906 try {
1907 startActivity(intent);
1908 } catch (ActivityNotFoundException ex) {
1909 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912
Yura4f93ec62014-02-04 14:15:21 +00001913 public boolean isOnCustomContent() {
1914 return mWorkspace.isOnOrMovingToCustomContent();
1915 }
1916
Winson Chung70d72102011-08-12 11:24:35 -07001917 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001918 public boolean onPrepareOptionsMenu(Menu menu) {
1919 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001920 if (mLauncherCallbacks != null) {
1921 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1922 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001923 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001926 @Override
1927 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001928 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001929 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001930 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001931 }
1932
Joe Onorato9c1289c2009-08-17 11:03:03 -04001933 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001934 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001935 }
1936
Adam Cohen517a7f52014-03-01 12:12:59 -08001937 public boolean isWorkspaceLoading() {
1938 return mWorkspaceLoading;
1939 }
1940
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001941 private void setWorkspaceLoading(boolean value) {
1942 boolean isLocked = isWorkspaceLocked();
1943 mWorkspaceLoading = value;
1944 if (isLocked != isWorkspaceLocked()) {
1945 onWorkspaceLockedChanged();
1946 }
1947 }
1948
Sunny Goyal2100c782016-08-22 16:00:03 -07001949 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001950 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001951 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001952 if (isLocked != isWorkspaceLocked()) {
1953 onWorkspaceLockedChanged();
1954 }
1955 }
1956
Adam Cohen9211d422014-10-07 18:14:53 -07001957 protected void onWorkspaceLockedChanged() {
1958 if (mLauncherCallbacks != null) {
1959 mLauncherCallbacks.onWorkspaceLockedChanged();
1960 }
1961 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001962
Sunny Goyal2100c782016-08-22 16:00:03 -07001963 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1964 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001965 if (LOGD) {
1966 Log.d(TAG, "Adding widget from drop");
1967 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07001968 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1969 }
1970
Sunny Goyal2100c782016-08-22 16:00:03 -07001971 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
1972 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001973 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001974 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001975 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07001976
Bjorn Bringert7984c942009-12-09 15:38:25 +00001977 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07001978 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
1979 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001981 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001982 Runnable onComplete = new Runnable() {
1983 @Override
1984 public void run() {
1985 // Exit spring loaded mode if necessary after adding the widget
1986 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1987 null);
1988 }
1989 };
Sunny Goyal2100c782016-08-22 16:00:03 -07001990 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07001991 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
1993 }
Romain Guycbb89e42009-06-08 15:52:54 -07001994
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001995 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001996 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001997 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001998 mWorkspace.moveToCustomContentScreen(animate);
1999 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002000
2001 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2002 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002003 info.container = container;
2004 info.screenId = screenId;
2005 if (cell != null) {
2006 info.cellX = cell[0];
2007 info.cellY = cell[1];
2008 }
2009 info.spanX = spanX;
2010 info.spanY = spanY;
2011
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002012 switch (info.itemType) {
2013 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2014 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002015 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002016 break;
2017 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002018 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002019 break;
2020 default:
2021 throw new IllegalStateException("Unknown item type: " + info.itemType);
2022 }
2023 }
2024
Adam Cohenfbba09b2011-07-18 21:43:05 -07002025 /**
2026 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002027 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002028 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002029 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2030 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2031 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002032 }
2033
Adam Cohenfbba09b2011-07-18 21:43:05 -07002034 /**
2035 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002036 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002037 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002038 AppWidgetHostView hostView = info.boundWidget;
2039 int appWidgetId;
2040 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002041 // In the case where we've prebound the widget, we remove it from the DragLayer
2042 if (LOGD) {
2043 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2044 }
2045 getDragLayer().removeView(hostView);
2046
Adam Cohened66b2b2012-01-23 17:28:51 -08002047 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002048 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002049
2050 // Clear the boundWidget so that it doesn't get destroyed.
2051 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002052 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002053 // In this case, we either need to start an activity to get permission to bind
2054 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002055 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002056 Bundle options = info.bindOptions;
2057
Sunny Goyalffe83f12014-08-14 17:39:34 -07002058 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2059 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002060 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002061 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002062 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002063 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002064 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2065 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2066 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002067 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002068 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002069 // TODO: we need to make sure that this accounts for the options bundle.
2070 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002071 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2072 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002073 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002074 }
2075
Adam Cohendcd297f2013-06-18 13:13:40 -07002076 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002077 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002078 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 folderInfo.title = getText(R.string.folder_name);
2080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002082 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2083 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084
2085 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 FolderIcon newFolder =
2087 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002088 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002089 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002090 // Force measure the new folder icon
2091 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2092 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002093 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
Romain Guycbb89e42009-06-08 15:52:54 -07002095
Winsonc0b52fe2015-09-09 16:38:15 -07002096 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002097 * Unbinds the view for the specified item, and removes the item and all its children.
2098 *
2099 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002100 * @param itemInfo the {@link ItemInfo} for this view.
2101 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002102 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002103 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002104 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002105 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002106 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2107 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002108 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002109 } else {
2110 mWorkspace.removeWorkspaceItem(v);
2111 }
Winsonc0b52fe2015-09-09 16:38:15 -07002112 if (deleteFromDb) {
2113 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2114 }
2115 } else if (itemInfo instanceof FolderInfo) {
2116 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002117 if (v instanceof FolderIcon) {
2118 ((FolderIcon) v).removeListeners();
2119 }
Winsonc0b52fe2015-09-09 16:38:15 -07002120 mWorkspace.removeWorkspaceItem(v);
2121 if (deleteFromDb) {
2122 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2123 }
2124 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2125 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002126 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002127 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002128 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002129 }
2130 } else {
2131 return false;
2132 }
2133 return true;
2134 }
2135
2136 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002137 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002138 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002139 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002140 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002141 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002142 // Deleting an app widget ID is a void call but writes to disk before returning
2143 // to the caller...
2144 new AsyncTask<Void, Void, Void>() {
2145 public Void doInBackground(Void ... args) {
2146 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2147 return null;
2148 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002149 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002150 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002151 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002152 }
2153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 @Override
2155 public boolean dispatchKeyEvent(KeyEvent event) {
2156 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2157 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002159 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002160 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002161 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002162 dumpState();
2163 return true;
2164 }
2165 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002166 }
2167 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2168 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002169 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 return true;
2171 }
2172 }
2173
2174 return super.dispatchKeyEvent(event);
2175 }
2176
Joe Onorato88ec0992009-11-19 13:16:06 -08002177 @Override
2178 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002179 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2180 return;
2181 }
2182
Sunny Goyal45478022015-06-08 16:52:41 -07002183 if (mDragController.isDragging()) {
2184 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002185 return;
2186 }
2187
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002188 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2189 if (topView != null) {
2190 if (topView.getActiveTextView() != null) {
2191 topView.getActiveTextView().dispatchBackKey();
2192 } else {
2193 topView.close(true);
2194 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002195 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002196 showWorkspace(true);
2197 } else if (isWidgetsViewVisible()) {
2198 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002199 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002200 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002201 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002202 mWorkspace.exitWidgetResizeMode();
2203
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002204 // Back button is a no-op here, but give at least some feedback for the button press
2205 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002206 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002207 }
2208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 * Launches the intent referred by the clicked shortcut.
2211 *
2212 * @param v The view representing the clicked shortcut.
2213 */
2214 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002215 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2216 // view has detached (it's possible for this to happen if the view is removed mid touch).
2217 if (v.getWindowToken() == null) {
2218 return;
2219 }
2220
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002221 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002222 return;
2223 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002224
Adam Cohen1697b792013-09-17 19:08:21 -07002225 if (v instanceof Workspace) {
2226 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002227 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002228 }
2229 return;
2230 }
2231
2232 if (v instanceof CellLayout) {
2233 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002234 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2235 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002236 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002237 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002238 }
2239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002241 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002242 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002244 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002245 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002246 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002247 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2248 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002249 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002250 } else if (tag instanceof AppInfo) {
2251 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002252 } else if (tag instanceof LauncherAppWidgetInfo) {
2253 if (v instanceof PendingAppWidgetHostView) {
2254 onClickPendingWidget((PendingAppWidgetHostView) v);
2255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 }
2257 }
2258
Sunny Goyal70660032015-05-14 00:07:08 -07002259 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002260 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002261 return false;
2262 }
2263
Michael Jurkaaf442092010-06-10 17:01:57 -07002264 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002265 * Event handler for the app widget view which has not fully restored.
2266 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002267 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002268 if (mIsSafeModeEnabled) {
2269 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2270 return;
2271 }
2272
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002273 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002274 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002275 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2276 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2277 // This should not happen, as we make sure that an Id is allocated during bind.
2278 return;
2279 }
2280 LauncherAppWidgetProviderInfo appWidgetInfo =
2281 mAppWidgetManager.findProvider(info.providerName, info.user);
2282 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002283 setWaitingForResult(PendingRequestArgs
2284 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002285
Sunny Goyald478c832016-04-01 12:04:16 -07002286 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002287 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002288 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002289 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002290 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2291 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2292 }
2293 } else {
2294 LauncherAppWidgetProviderInfo appWidgetInfo =
2295 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2296 if (appWidgetInfo != null) {
2297 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2298 }
Sunny Goyalff572272014-07-23 13:58:07 -07002299 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002300 } else if (info.installProgress < 0) {
2301 // The install has not been queued
2302 final String packageName = info.providerName.getPackageName();
2303 showBrokenAppInstallDialog(packageName,
2304 new DialogInterface.OnClickListener() {
2305 public void onClick(DialogInterface dialog, int id) {
2306 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2307 }
2308 });
2309 } else {
2310 // Download has started.
2311 final String packageName = info.providerName.getPackageName();
2312 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002313 }
2314 }
2315
Sunny Goyald478c832016-04-01 12:04:16 -07002316 private void startRestoredWidgetReconfigActivity(
2317 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002318 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002319 mAppWidgetManager.startConfigActivity(provider,
2320 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2321 }
2322
Sunny Goyalff572272014-07-23 13:58:07 -07002323 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002324 * Event handler for the "grid" button that appears on the home screen, which
2325 * enters all apps mode.
2326 *
2327 * @param v The view that was clicked.
2328 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002329 protected void onClickAllAppsButton(View v) {
2330 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002331 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002332 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002333 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002334 showAppsView(true /* animated */, true /* updatePredictedApps */,
2335 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002336 }
2337 }
2338
2339 protected void onLongClickAllAppsButton(View v) {
2340 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2341 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002342 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002343 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002344 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002345 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002346 }
2347 }
2348
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002349 private void showBrokenAppInstallDialog(final String packageName,
2350 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002351 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002352 .setTitle(R.string.abandoned_promises_title)
2353 .setMessage(R.string.abandoned_promise_explanation)
2354 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2355 .setNeutralButton(R.string.abandoned_clean_this,
2356 new DialogInterface.OnClickListener() {
2357 public void onClick(DialogInterface dialog, int id) {
2358 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2359 mWorkspace.removeAbandonedPromise(packageName, user);
2360 }
2361 })
2362 .create().show();
2363 return;
2364 }
2365
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002366 /**
2367 * Event handler for an app shortcut click.
2368 *
2369 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2370 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002371 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002372 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2373 Object tag = v.getTag();
2374 if (!(tag instanceof ShortcutInfo)) {
2375 throw new IllegalArgumentException("Input must be a Shortcut");
2376 }
2377
2378 // Open shortcut
2379 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002380
2381 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002382 if ((shortcut.isDisabled &
2383 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2384 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2385 // If the app is only disabled because of the above flags, launch activity anyway.
2386 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002387 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002388 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2389 // Use a message specific to this shortcut, if it has one.
2390 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2391 return;
2392 }
2393 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002394 int error = R.string.activity_not_available;
2395 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2396 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002397 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2398 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002399 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002400 }
2401 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2402 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002403 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002404 }
2405
Chris Wren40c5ed32014-06-24 18:24:23 -04002406 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002407 if ((v instanceof BubbleTextView)
2408 && shortcut.isPromise()
2409 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002410 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002411 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002412 new DialogInterface.OnClickListener() {
2413 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002414 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002415 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002416 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002417 return;
2418 }
2419
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002420 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002421 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002422 }
2423
Sunny Goyala7ce1662016-05-31 15:01:35 -07002424 private void startAppShortcutOrInfoActivity(View v) {
2425 ItemInfo item = (ItemInfo) v.getTag();
2426 Intent intent = item.getIntent();
2427 if (intent == null) {
2428 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002429 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002430 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002431 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002432
2433 if (success && v instanceof BubbleTextView) {
2434 mWaitingForResume = (BubbleTextView) v;
2435 mWaitingForResume.setStayPressed(true);
2436 }
2437 }
2438
2439 /**
2440 * Event handler for a folder icon click.
2441 *
2442 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2443 */
2444 protected void onClickFolderIcon(View v) {
2445 if (LOGD) Log.d(TAG, "onClickFolder");
2446 if (!(v instanceof FolderIcon)){
2447 throw new IllegalArgumentException("Input must be a FolderIcon");
2448 }
2449
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002450 Folder folder = ((FolderIcon) v).getFolder();
2451 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002452 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002453 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002454 }
Michael Jurka5130e402011-10-13 04:55:35 -07002455 }
2456
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002457 /**
2458 * Event handler for the (Add) Widgets button that appears after a long press
2459 * on the home screen.
2460 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002461 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002462 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002463 if (mIsSafeModeEnabled) {
2464 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2465 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002466 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002467 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002468 }
2469
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002470 /**
2471 * Event handler for the wallpaper picker button that appears after a long press
2472 * on the home screen.
2473 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002474 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002475 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002476 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2477 return;
2478 }
2479
Tony Wickham785f7a52015-08-31 17:28:32 -07002480 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2481 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002482 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002483 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002484 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002485
2486 String pickerPackage = getString(R.string.wallpaper_picker_package);
2487 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2488 if (!hasTargetPackage) {
2489 intent.setPackage(pickerPackage);
2490 }
2491
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002492 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002493 try {
2494 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2495 // If there is no target package, use the default intent chooser animation
2496 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2497 } catch (ActivityNotFoundException e) {
2498 setWaitingForResult(null);
2499 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2500 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002501 }
2502
2503 /**
2504 * Event handler for a click on the settings button that appears after a long press
2505 * on the home screen.
2506 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002507 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002508 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002509 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2510 .setPackage(getPackageName());
2511 intent.setSourceBounds(getViewBounds(v));
2512 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 }
2514
Adam Cohen61f560d2013-09-30 15:58:20 -07002515 public View.OnTouchListener getHapticFeedbackTouchListener() {
2516 if (mHapticFeedbackTouchListener == null) {
2517 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002518 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002519 @Override
2520 public boolean onTouch(View v, MotionEvent event) {
2521 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2522 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2523 }
2524 return false;
2525 }
2526 };
2527 }
2528 return mHapticFeedbackTouchListener;
2529 }
2530
Tony Wickhame2217252016-03-22 16:34:23 -07002531 @Override
2532 public void onAccessibilityStateChanged(boolean enabled) {
2533 mDragLayer.onAccessibilityStateChanged(enabled);
2534 }
2535
Sunny Goyal06e21a22016-08-11 16:02:02 -07002536 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002537 if (isOnCustomContent()) {
2538 // Custom content screen doesn't participate in drag and drop. If on custom
2539 // content screen, move to default.
2540 moveWorkspaceToDefaultScreen();
2541 }
Adam Cohen9211d422014-10-07 18:14:53 -07002542 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002543
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002544 /**
2545 * Called when the user stops interacting with the launcher.
2546 * This implies that the user is now on the homescreen and is not doing housekeeping.
2547 */
Adam Cohen9211d422014-10-07 18:14:53 -07002548 protected void onInteractionEnd() {
2549 if (mLauncherCallbacks != null) {
2550 mLauncherCallbacks.onInteractionEnd();
2551 }
2552 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002553
2554 /**
2555 * Called when the user starts interacting with the launcher.
2556 * The possible interactions are:
2557 * - open all apps
2558 * - reorder an app shortcut, or a widget
2559 * - open the overview mode.
2560 * This is a good time to stop doing things that only make sense
2561 * when the user is on the homescreen and not doing housekeeping.
2562 */
Adam Cohen9211d422014-10-07 18:14:53 -07002563 protected void onInteractionBegin() {
2564 if (mLauncherCallbacks != null) {
2565 mLauncherCallbacks.onInteractionBegin();
2566 }
2567 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002568
Winson Chungcd99cd32015-04-29 11:03:24 -07002569 /** Updates the interaction state. */
2570 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002571 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002572 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002573 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2574 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002575 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002576 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002577 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002578 onInteractionEnd();
2579 }
2580 }
2581
Sunny Goyala7ce1662016-05-31 15:01:35 -07002582 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002584 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2585 try {
2586 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2587 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2588 // is enabled by default on NYC.
2589 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2590 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002591
2592 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2593 String id = ((ShortcutInfo) info).getDeepShortcutId();
2594 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002595 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2596 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002597 } else {
2598 // Could be launching some bookkeeping activity
2599 startActivity(intent, optsBundle);
2600 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002601 } finally {
2602 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002605 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2606 // corresponding permission. Show the appropriate permission prompt if that
2607 // is the case.
2608 if (intent.getComponent() == null
2609 && Intent.ACTION_CALL.equals(intent.getAction())
2610 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2611 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002612
2613 setWaitingForResult(PendingRequestArgs
2614 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002615 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2616 REQUEST_PERMISSION_CALL_PHONE);
2617 } else {
2618 // No idea why this was thrown.
2619 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002621 }
2622 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002623
Sunny Goyalfe770c92016-09-27 14:38:58 -07002624 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002625 private Bundle getActivityLaunchOptions(View v) {
2626 if (Utilities.ATLEAST_MARSHMALLOW) {
2627 int left = 0, top = 0;
2628 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2629 if (v instanceof TextView) {
2630 // Launch from center of icon, not entire view
2631 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2632 if (icon != null) {
2633 Rect bounds = icon.getBounds();
2634 left = (width - bounds.width()) / 2;
2635 top = v.getPaddingTop();
2636 width = bounds.width();
2637 height = bounds.height();
2638 }
2639 }
2640 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2641 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2642 // On L devices, we use the device default slide-up transition.
2643 // On L MR1 devices, we use a custom version of the slide-up transition which
2644 // doesn't have the delay present in the device default.
2645 return ActivityOptions.makeCustomAnimation(
2646 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2647 }
2648 return null;
2649 }
2650
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002651 private Rect getViewBounds(View v) {
2652 int[] pos = new int[2];
2653 v.getLocationOnScreen(pos);
2654 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2655 }
2656
Sunny Goyala7ce1662016-05-31 15:01:35 -07002657 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002658 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2659 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2660 return false;
2661 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002662 // Only launch using the new animation if the shortcut has not opted out (this is a
2663 // private contract between launcher and may be ignored in the future).
2664 boolean useLaunchAnimation = (v != null) &&
2665 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2666 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2667
2668 UserHandleCompat user = null;
2669 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2670 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2671 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002672 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002673
2674 // Prepare intent
2675 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2676 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002677 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002678 }
2679 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002680 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2681 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002682 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2683 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002684 // Shortcuts need some special checks due to legacy reasons.
2685 startShortcutIntentSafely(intent, optsBundle, item);
2686 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2687 // Could be launching some bookkeeping activity
2688 startActivity(intent, optsBundle);
2689 } else {
2690 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2691 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2692 }
2693 return true;
2694 } catch (ActivityNotFoundException|SecurityException e) {
2695 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2696 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2697 }
2698 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002699 }
2700
Tony Wickhamdadb3042016-02-24 11:07:00 -08002701 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002702 public boolean dispatchTouchEvent(MotionEvent ev) {
2703 mLastDispatchTouchEventX = ev.getX();
2704 return super.dispatchTouchEvent(ev);
2705 }
2706
2707 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002709 if (!isDraggingEnabled()) return false;
2710 if (isWorkspaceLocked()) return false;
2711 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002713 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2714 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002715 onLongClickAllAppsButton(v);
2716 return true;
2717 }
2718
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002719
2720 boolean ignoreLongPressToOverview = mDeviceProfile.shouldIgnoreLongPressToOverview(
2721 mLastDispatchTouchEventX, mEdgeOfScreenThresholdPx);
Jon Miranda379198e2016-09-23 08:54:40 -07002722
Adam Cohen1697b792013-09-17 19:08:21 -07002723 if (v instanceof Workspace) {
2724 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002725 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002726 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2727 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2728 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002729 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002730 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2731 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2732 return true;
2733 } else {
2734 return false;
2735 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002736 } else {
2737 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002738 }
Adam Cohen1697b792013-09-17 19:08:21 -07002739 }
2740
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002741 CellLayout.CellInfo longClickCellInfo = null;
2742 View itemUnderLongClick = null;
2743 if (v.getTag() instanceof ItemInfo) {
2744 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002745 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002746 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002747 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748 }
2749
Winson Chung3d503fb2011-07-13 17:25:49 -07002750 // The hotseat touch handling does not go through Workspace, and we always allow long press
2751 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002752 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002753 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002754 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002755 if (mWorkspace.isInOverviewMode()) {
2756 mWorkspace.startReordering(v);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002757 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2758 LauncherLogProto.Action.NONE, LauncherLogProto.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002759 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002760 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002761 return false;
2762 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002763 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2764 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2765 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002766 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002767 }
Jon Miranda379198e2016-09-23 08:54:40 -07002768 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2769 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002770 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002771 final boolean isAllAppsButton =
2772 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2773 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2774 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002775 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002777 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002778 }
2779 }
2780 }
2781 return true;
2782 }
2783
Winson Chung3d503fb2011-07-13 17:25:49 -07002784 boolean isHotseatLayout(View layout) {
2785 return mHotseat != null && layout != null &&
2786 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2787 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002788
Winson Chung3d503fb2011-07-13 17:25:49 -07002789 /**
2790 * Returns the CellLayout of the specified container at the specified screen.
2791 */
Sunny Goyal92820592015-03-02 11:31:35 -08002792 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002793 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2794 if (mHotseat != null) {
2795 return mHotseat.getLayout();
2796 } else {
2797 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002798 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002799 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002800 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002801 }
2802 }
2803
Winson Chungb745afb2015-03-02 11:51:23 -08002804 /**
2805 * For overridden classes.
2806 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002807 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002808 return isAppsViewVisible();
2809 }
2810
2811 public boolean isAppsViewVisible() {
2812 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2813 }
2814
2815 public boolean isWidgetsViewVisible() {
2816 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002817 }
2818
Michael Jurkae326f182011-11-21 14:05:46 -08002819 @Override
2820 public void onTrimMemory(int level) {
2821 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002822 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2823 // The widget preview db can result in holding onto over
2824 // 3MB of memory for caching which isn't necessary.
2825 SQLiteDatabase.releaseMemory();
2826
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002827 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002828 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002829 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002830 if (mLauncherCallbacks != null) {
2831 mLauncherCallbacks.onTrimMemory(level);
2832 }
Michael Jurkae326f182011-11-21 14:05:46 -08002833 }
2834
Winson5c6bdbb2015-09-03 11:36:19 -07002835 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002836 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002837 }
2838
Winson5c6bdbb2015-09-03 11:36:19 -07002839 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002840 boolean changed = mState != State.WORKSPACE ||
2841 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002842 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002843 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002844 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002845 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002846
Michael Jurkab3e22d92011-10-31 15:58:33 -07002847 // Set focus to the AppsCustomize button
2848 if (mAllAppsButton != null) {
2849 mAllAppsButton.requestFocus();
2850 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002851 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002852
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002853 // Change the state *after* we've called all the transition code
2854 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002855
Winson Chung0f785722015-04-08 10:27:49 -07002856 if (changed) {
2857 // Send an accessibility event to announce the context change
2858 getWindow().getDecorView()
2859 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002860 }
Winson5c6bdbb2015-09-03 11:36:19 -07002861 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002862 }
2863
Winsone9f27272015-10-13 10:47:51 -07002864 /**
2865 * Shows the overview button.
2866 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002867 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002868 showOverviewMode(animated, false);
2869 }
2870
2871 /**
2872 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2873 * onto one of the overview panel buttons.
2874 */
2875 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2876 Runnable postAnimRunnable = null;
2877 if (requestButtonFocus) {
2878 postAnimRunnable = new Runnable() {
2879 @Override
2880 public void run() {
2881 // Hitting the menu button when in touch mode does not trigger touch mode to
2882 // be disabled, so if requested, force focus on one of the overview panel
2883 // buttons.
2884 mOverviewPanel.requestFocusFromTouch();
2885 }
2886 };
2887 }
Adam Cohened307df2013-10-02 09:37:31 -07002888 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002889 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002890 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002891 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002892 // If animated from long press, then don't allow any of the controller in the drag
2893 // layer to intercept any remaining touch.
2894 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002895 }
2896
Winson Chungb745afb2015-03-02 11:51:23 -08002897 /**
2898 * Shows the apps view.
2899 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002900 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002901 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002902 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002903 if (updatePredictedApps) {
2904 tryAndUpdatePredictedApps();
2905 }
Winson Chung76648c52015-07-10 14:33:23 -07002906 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002907 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002908
Winson Chungb745afb2015-03-02 11:51:23 -08002909 /**
2910 * Shows the widgets view.
2911 */
2912 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002913 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002914 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002915 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002916 }
Winson Chung76648c52015-07-10 14:33:23 -07002917 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002918
2919 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002920 @Override
2921 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002922 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002923 }
2924 });
Winson Chungb745afb2015-03-02 11:51:23 -08002925 }
2926
2927 /**
2928 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002929 *
2930 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002931 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002932 // TODO: calling method should use the return value so that when {@code false} is returned
2933 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002934 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002935 if (!(mState == State.WORKSPACE ||
2936 mState == State.APPS_SPRING_LOADED ||
2937 mState == State.WIDGETS_SPRING_LOADED ||
2938 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002939 return false;
2940 }
2941 if (toState != State.APPS && toState != State.WIDGETS) {
2942 return false;
2943 }
Winson Chungb745afb2015-03-02 11:51:23 -08002944
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002945 // This is a safe and supported transition to bypass spring_loaded mode.
2946 if (mExitSpringLoadedModeRunnable != null) {
2947 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2948 mExitSpringLoadedModeRunnable = null;
2949 }
2950
Winson Chungb745afb2015-03-02 11:51:23 -08002951 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07002952 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
2953 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002954 } else {
Winson Chung006ee262015-08-03 14:40:11 -07002955 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002956 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002957
Michael Jurkab3e22d92011-10-31 15:58:33 -07002958 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002959 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002960 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002961
2962 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002963 getWindow().getDecorView()
2964 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002965 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002966 }
2967
Winson Chungcd99cd32015-04-29 11:03:24 -07002968 /**
2969 * Updates the workspace and interaction state on state change, and return the animation to this
2970 * new state.
2971 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002972 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07002973 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002974 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002975 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002976 updateInteraction(fromState, toState);
2977 return anim;
2978 }
2979
Hyunyoung Song3f471442015-04-08 19:01:34 -07002980 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002981 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002982 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002983 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002984 }
Winson Chungb745afb2015-03-02 11:51:23 -08002985
Winson Chung006ee262015-08-03 14:40:11 -07002986 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002987 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002988 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07002989
2990 if (isAppsViewVisible()) {
2991 mState = State.APPS_SPRING_LOADED;
2992 } else if (isWidgetsViewVisible()) {
2993 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08002994 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002995 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08002996 } else {
2997 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07002998 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002999 }
Adam Cohen7777d962011-08-18 18:58:38 -07003000
Hyunyoung Song3f471442015-04-08 19:01:34 -07003001 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003002 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003003 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003004
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003005 if (mExitSpringLoadedModeRunnable != null) {
3006 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3007 }
3008 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003009 @Override
3010 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003011 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003012 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3013 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003014 // Before we show workspace, hide all apps again because
3015 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3016 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003017 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003018 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003019 } else {
3020 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003021 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003022 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003023 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003024 };
3025 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003026 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003027
Tony Wickhame0c33232016-02-08 11:37:04 -08003028 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003029 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3030 || mState == State.WIDGETS_SPRING_LOADED;
3031 }
3032
Michael Jurkad3ef3062010-11-23 16:23:58 -08003033 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003034 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003035 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003036 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003037 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003038 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003039 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3040 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003041 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003042 }
3043
Winson Chung4ac30062015-05-08 17:34:17 -07003044 /**
3045 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3046 * resumed.
3047 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003048 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003049 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003050 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003051 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003052 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003053 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003054 }
3055 }
3056 }
3057
Michael Jurkab3e22d92011-10-31 15:58:33 -07003058 void lockAllApps() {
3059 // TODO
3060 }
3061
3062 void unlockAllApps() {
3063 // TODO
3064 }
3065
Michael Jurkad7c28052012-04-27 15:43:36 -07003066 @Override
3067 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003068 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003069 final List<CharSequence> text = event.getText();
3070 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003071 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003072 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003073 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003074 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003075 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003076 } else if (mWorkspace != null) {
3077 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003078 } else {
3079 text.add(getString(R.string.all_apps_home_button_label));
3080 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003081 return result;
3082 }
3083
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003084 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003085 * If the activity is currently paused, signal that we need to run the passed Runnable
3086 * in onResume.
3087 *
3088 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003089 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3090 * wrong when we're not running, and if the activity comes back to what the configuration was
3091 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003092 *
3093 * Implementation of the method from LauncherModel.Callbacks.
3094 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003095 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003096 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003097 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003098 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003099 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003100 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003101 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003102 }
3103 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003104 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003105 return true;
3106 } else {
3107 return false;
3108 }
3109 }
3110
Michael Jurkac402cd92013-05-20 15:49:32 +02003111 private boolean waitUntilResume(Runnable run) {
3112 return waitUntilResume(run, false);
3113 }
3114
Michael Jurka1e2f4652013-07-08 18:03:46 -07003115 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003116 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003117 }
3118
Michael Jurka7607c2f2013-04-03 14:33:19 -07003119 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003120 * If the activity is currently paused, signal that we need to re-run the loader
3121 * in onResume.
3122 *
3123 * This needs to be called from incoming places where resources might have been loaded
3124 * while we are paused. That is becaues the Configuration might be wrong
3125 * when we're not running, and if it comes back to what it was when we
3126 * were paused, we are not restarted.
3127 *
3128 * Implementation of the method from LauncherModel.Callbacks.
3129 *
3130 * @return true if we are currently paused. The caller might be able to
3131 * skip some work in that case since we will come back again.
3132 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003133 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003134 public boolean setLoadOnResume() {
3135 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003136 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003137 mOnResumeNeedsLoad = true;
3138 return true;
3139 } else {
3140 return false;
3141 }
3142 }
3143
3144 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003147 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003148 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003149 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003150 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003151 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003152 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003153 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003155
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003157 * Clear any pending bind callbacks. This is called when is loader is planning to
3158 * perform a full rebind from scratch.
3159 */
3160 @Override
3161 public void clearPendingBinds() {
3162 mBindOnResumeCallbacks.clear();
3163 if (mPendingExecutor != null) {
3164 mPendingExecutor.markCompleted();
3165 mPendingExecutor = null;
3166 }
3167 }
3168
3169 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003170 * Refreshes the shortcuts shown on the workspace.
3171 *
3172 * Implementation of the method from LauncherModel.Callbacks.
3173 */
3174 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003175 if (LauncherAppState.PROFILE_STARTUP) {
3176 Trace.beginSection("Starting page bind");
3177 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003178 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003179
Winson Chungd64d1762013-08-20 14:37:16 -07003180 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003181 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003182 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003183
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 if (mHotseat != null) {
3185 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003187 if (LauncherAppState.PROFILE_STARTUP) {
3188 Trace.endSection();
3189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 }
3191
Adam Cohendcd297f2013-06-18 13:13:40 -07003192 @Override
3193 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003194 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003195 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3196 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003197 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3198 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3199 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003200 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3201 // If there are no screens, we need to have an empty screen
3202 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003203 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003204 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003205
3206 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003207 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003208 if (hasCustomContentToLeft()) {
3209 mWorkspace.createCustomContentContainer();
3210 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003211 }
Winsonc7d2e832016-07-28 12:24:55 -07003212
3213 // After we have added all the screens, if the wallpaper was locked to the default state,
3214 // then notify to indicate that it can be released and a proper wallpaper offset can be
3215 // computed before the next layout
3216 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003217 }
3218
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003219 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003220 int count = orderedScreenIds.size();
3221 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003222 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003223 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003224 // No need to bind the first screen, as its always bound.
3225 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3226 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003227 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003228 }
3229
Winson Chungd64d1762013-08-20 14:37:16 -07003230 public void bindAppsAdded(final ArrayList<Long> newScreens,
3231 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003232 final ArrayList<ItemInfo> addAnimated,
3233 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003234 Runnable r = new Runnable() {
3235 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003236 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003237 }
3238 };
3239 if (waitUntilResume(r)) {
3240 return;
3241 }
3242
Winson Chungd64d1762013-08-20 14:37:16 -07003243 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003244 if (newScreens != null) {
3245 bindAddScreens(newScreens);
3246 }
Winson Chungd64d1762013-08-20 14:37:16 -07003247
3248 // We add the items without animation on non-visible pages, and with
3249 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003250 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003251 bindItems(addNotAnimated, 0,
3252 addNotAnimated.size(), false);
3253 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003254 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003255 bindItems(addAnimated, 0,
3256 addAnimated.size(), true);
3257 }
Winson Chungc58497e2013-09-03 17:48:37 -07003258
Winson Chung87412982013-10-03 18:34:14 -07003259 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003260 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003261
Winson Chungb745afb2015-03-02 11:51:23 -08003262 if (addedApps != null && mAppsView != null) {
3263 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003264 }
Winson Chungd64d1762013-08-20 14:37:16 -07003265 }
3266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 * Bind the items start-end from the list.
3269 *
3270 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003271 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003272 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003273 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3274 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003275 Runnable r = new Runnable() {
3276 public void run() {
3277 bindItems(shortcuts, start, end, forceAnimateIcons);
3278 }
3279 };
3280 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003281 return;
3282 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003283
Winson Chungf0c6ae02012-03-21 16:10:31 -07003284 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003285 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3286 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003287 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003288 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003289 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003290 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003292
3293 // Short circuit if we are loading dock items for a configuration which has no dock
3294 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3295 mHotseat == null) {
3296 continue;
3297 }
3298
Sunny Goyal639e9062015-08-19 19:17:06 -07003299 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003300 switch (item.itemType) {
3301 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3302 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003303 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003304 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003305 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003306 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003307 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003308 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003309 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003310 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003312 default:
3313 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003315
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003316 /*
3317 * Remove colliding items.
3318 */
3319 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3320 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3321 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3322 View v = cl.getChildAt(item.cellX, item.cellY);
3323 Object tag = v.getTag();
3324 String desc = "Collision while binding workspace item: " + item
3325 + ". Collides with " + tag;
3326 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3327 throw (new RuntimeException(desc));
3328 } else {
3329 Log.d(TAG, desc);
3330 LauncherModel.deleteItemFromDatabase(this, item);
3331 continue;
3332 }
3333 }
3334 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003335 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3336 item.cellY, 1, 1);
3337 if (animateIcons) {
3338 // Animate all the applications up now
3339 view.setAlpha(0f);
3340 view.setScaleX(0f);
3341 view.setScaleY(0f);
3342 bounceAnims.add(createNewAppBounceAnimation(view, i));
3343 newShortcutsScreenId = item.screenId;
3344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003345 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003346
Winson Chung997a9232013-07-24 15:33:46 -07003347 if (animateIcons) {
3348 // Animate to the correct page
3349 if (newShortcutsScreenId > -1) {
3350 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003351 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003352 final Runnable startBounceAnimRunnable = new Runnable() {
3353 public void run() {
3354 anim.playTogether(bounceAnims);
3355 anim.start();
3356 }
3357 };
Winson Chung997a9232013-07-24 15:33:46 -07003358 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003359 // We post the animation slightly delayed to prevent slowdowns
3360 // when we are loading right after we return to launcher.
3361 mWorkspace.postDelayed(new Runnable() {
3362 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003363 if (mWorkspace != null) {
3364 mWorkspace.snapToPage(newScreenIndex);
3365 mWorkspace.postDelayed(startBounceAnimRunnable,
3366 NEW_APPS_ANIMATION_DELAY);
3367 }
Winson Chung94d67682013-09-25 16:29:40 -07003368 }
3369 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003370 } else {
3371 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003372 }
3373 }
Winson Chung64359a52013-07-08 17:17:08 -07003374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003376 }
3377
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003378 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3379 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3380 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003381 view.updateAppWidget(null);
3382 view.setOnClickListener(this);
3383 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003384 mWorkspace.requestLayout();
3385 }
3386
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 /**
3388 * Add the views for a widget to the workspace.
3389 *
3390 * Implementation of the method from LauncherModel.Callbacks.
3391 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003392 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003393 Runnable r = new Runnable() {
3394 public void run() {
3395 bindAppWidget(item);
3396 }
3397 };
3398 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003399 return;
3400 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003401
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003402 if (mIsSafeModeEnabled) {
3403 bindSafeModeWidget(item);
3404 return;
3405 }
3406
Daniel Sandler843e8602010-06-07 14:59:01 -04003407 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3408 if (DEBUG_WIDGETS) {
3409 Log.d(TAG, "bindAppWidget: " + item);
3410 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003412 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003413
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003414 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3415 // If the provider is not ready, bind as a pending widget.
3416 appWidgetInfo = null;
3417 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3418 // The widget id is not valid. Try to find the widget based on the provider info.
3419 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3420 } else {
3421 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3422 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003423
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003424 // If the provider is ready, but the width is not yet restored, try to restore it.
3425 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3426 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003427 if (appWidgetInfo == null) {
3428 if (DEBUG_WIDGETS) {
3429 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3430 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003431 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003432 }
3433 LauncherModel.deleteItemFromDatabase(this, item);
3434 return;
3435 }
Sunny Goyalff572272014-07-23 13:58:07 -07003436
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003437 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003438 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003439 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3440 // Id has not been allocated yet. Allocate a new id.
3441 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3442 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003443
Sunny Goyald478c832016-04-01 12:04:16 -07003444 // Also try to bind the widget. If the bind fails, the user will be shown
3445 // a click to setup UI, which will ask for the bind permission.
3446 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3447 pendingInfo.spanX = item.spanX;
3448 pendingInfo.spanY = item.spanY;
3449 pendingInfo.minSpanX = item.minSpanX;
3450 pendingInfo.minSpanY = item.minSpanY;
3451 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003452
3453 boolean isDirectConfig =
3454 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3455 if (isDirectConfig && item.bindOptions != null) {
3456 Bundle newOptions = item.bindOptions.getExtras();
3457 if (options != null) {
3458 newOptions.putAll(options);
3459 }
3460 options = newOptions;
3461 }
Sunny Goyald478c832016-04-01 12:04:16 -07003462 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3463 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003464
Sunny Goyal86df1382016-08-10 15:03:22 -07003465 // We tried to bind once. If we were not able to bind, we would need to
3466 // go through the permission dialog, which means we cannot skip the config
3467 // activity.
3468 item.bindOptions = null;
3469 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3470
Sunny Goyald478c832016-04-01 12:04:16 -07003471 // Bind succeeded
3472 if (success) {
3473 // If the widget has a configure activity, it is still needs to set it up,
3474 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003475 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003476 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3477 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003478 }
Sunny Goyald478c832016-04-01 12:04:16 -07003479
3480 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003481 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003482 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003483 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003484 // The widget was marked as UI not ready, but there is no configure activity to
3485 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003486 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3487 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003488 }
Sunny Goyalff572272014-07-23 13:58:07 -07003489 }
3490
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003491 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003492 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003493 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3494 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003495 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003496
Sunny Goyal56c73602015-09-25 12:55:01 -07003497 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003498 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003499 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003500 deleteWidgetInfo(item);
3501 return;
3502 }
3503
Sunny Goyal233ee962015-08-03 13:05:01 -07003504 item.minSpanX = appWidgetInfo.minSpanX;
3505 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003506 addAppWidgetToWorkspace(
3507 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3508 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003509 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003510 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003511 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003512 view.updateAppWidget(null);
3513 view.setOnClickListener(this);
3514 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003515 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003516 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003517
Daniel Sandler843e8602010-06-07 14:59:01 -04003518 if (DEBUG_WIDGETS) {
3519 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3520 + (SystemClock.uptimeMillis()-start) + "ms");
3521 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 }
3523
Sunny Goyalff572272014-07-23 13:58:07 -07003524 /**
3525 * Restores a pending widget.
3526 *
3527 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003528 */
Sunny Goyald478c832016-04-01 12:04:16 -07003529 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003530 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3531 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3532 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003533 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003534 }
3535
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003536 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003537 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003538
3539 mWorkspace.reinflateWidgetsIfNecessary();
3540 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003541 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003542 }
3543
Adam Cohen1462de32012-07-24 22:34:36 -07003544 public void onPageBoundSynchronously(int page) {
3545 mSynchronouslyBoundPages.add(page);
3546 }
3547
Sunny Goyal527c7d32015-08-28 15:19:36 -07003548 @Override
3549 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3550 if (mPendingExecutor != null) {
3551 mPendingExecutor.markCompleted();
3552 }
3553 mPendingExecutor = executor;
3554 executor.attachTo(this);
3555 }
3556
3557 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3558 if (mPendingExecutor == executor) {
3559 mPendingExecutor = null;
3560 }
3561 }
3562
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003563 @Override
3564 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3565 Runnable r = new Runnable() {
3566 public void run() {
3567 finishFirstPageBind(executor);
3568 }
3569 };
3570 if (waitUntilResume(r)) {
3571 return;
3572 }
3573
3574 Runnable onComplete = new Runnable() {
3575 @Override
3576 public void run() {
3577 if (executor != null) {
3578 executor.onLoadAnimationCompleted();
3579 }
3580 }
3581 };
3582 if (mDragLayer.getAlpha() < 1) {
3583 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3584 } else {
3585 onComplete.run();
3586 }
3587 }
3588
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 /**
3590 * Callback saying that there aren't any more items to bind.
3591 *
3592 * Implementation of the method from LauncherModel.Callbacks.
3593 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003594 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003595 Runnable r = new Runnable() {
3596 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003597 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003598 }
3599 };
3600 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 return;
3602 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003603 if (LauncherAppState.PROFILE_STARTUP) {
3604 Trace.beginSection("Page bind completed");
3605 }
Adam Cohen1462de32012-07-24 22:34:36 -07003606 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003608 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003609
Sunny Goyal2100c782016-08-22 16:00:03 -07003610 if (mPendingActivityResult != null) {
3611 handleActivityResult(mPendingActivityResult.requestCode,
3612 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3613 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003614 }
3615
Sunny Goyal756adbc2015-04-16 15:20:51 -07003616 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003617
3618 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003619 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003620 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003621 if (LauncherAppState.PROFILE_STARTUP) {
3622 Trace.endSection();
3623 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003624 }
3625
Winson Chunga2413752012-04-03 14:22:34 -07003626 private boolean canRunNewAppsAnimation() {
3627 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003628 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003629 }
3630
Winson Chungc9168342013-06-26 14:54:55 -07003631 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003632 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003633 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3634 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003635 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003636 return bounceAnim;
3637 }
3638
Adam Cohen27772732013-11-11 14:00:56 +00003639 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003640 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003641 }
3642
Tony Wickham55616cd2015-09-23 14:55:17 -07003643 public int getSearchBarHeight() {
3644 if (mLauncherCallbacks != null) {
3645 return mLauncherCallbacks.getSearchBarHeight();
3646 }
3647 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3648 }
3649
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003650 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003651 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3652 * multiple calls to bind the same list.)
3653 */
3654 @Thunk ArrayList<AppInfo> mTmpAppsList;
3655 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3656 public void run() {
3657 bindAllApplications(mTmpAppsList);
3658 mTmpAppsList = null;
3659 }
3660 };
3661
3662 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 * Add the icons for all apps.
3664 *
3665 * Implementation of the method from LauncherModel.Callbacks.
3666 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003667 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003668 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3669 mTmpAppsList = apps;
3670 return;
3671 }
3672
Winson Chungb745afb2015-03-02 11:51:23 -08003673 if (mAppsView != null) {
3674 mAppsView.setApps(apps);
3675 }
Adam Cohen9211d422014-10-07 18:14:53 -07003676 if (mLauncherCallbacks != null) {
3677 mLauncherCallbacks.bindAllApplications(apps);
3678 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 }
3680
3681 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003682 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3683 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3684 */
3685 @Override
3686 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
3687 mDeepShortcutMap = deepShortcutMapCopy;
3688 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
3689 }
3690
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003691 public List<String> getShortcutIdsForItem(ItemInfo info) {
3692 if (!DeepShortcutManager.supportsShortcuts(info)) {
3693 return Collections.EMPTY_LIST;
3694 }
3695 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07003696 if (component == null) {
3697 return Collections.EMPTY_LIST;
3698 }
3699
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003700 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
3701 return ids == null ? Collections.EMPTY_LIST : ids;
3702 }
3703
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003704 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 * A package was updated.
3706 *
3707 * Implementation of the method from LauncherModel.Callbacks.
3708 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003709 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003710 Runnable r = new Runnable() {
3711 public void run() {
3712 bindAppsUpdated(apps);
3713 }
3714 };
3715 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003716 return;
3717 }
3718
Winson Chungb745afb2015-03-02 11:51:23 -08003719 if (mAppsView != null) {
3720 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 }
3723
Sunny Goyal4390ace2014-10-13 11:33:11 -07003724 @Override
3725 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3726 Runnable r = new Runnable() {
3727 public void run() {
3728 bindWidgetsRestored(widgets);
3729 }
3730 };
3731 if (waitUntilResume(r)) {
3732 return;
3733 }
3734 mWorkspace.widgetsRestored(widgets);
3735 }
3736
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003738 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003739 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003740 *
3741 * @param updated list of shortcuts which have changed.
3742 * @param removed list of shortcuts which were deleted in the background. This can happen when
3743 * an app gets removed from the system or some of its components are no longer
3744 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003745 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003746 @Override
3747 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
3748 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003749 Runnable r = new Runnable() {
3750 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003751 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003752 }
3753 };
3754 if (waitUntilResume(r)) {
3755 return;
3756 }
3757
Sunny Goyal4390ace2014-10-13 11:33:11 -07003758 if (!updated.isEmpty()) {
3759 mWorkspace.updateShortcuts(updated);
3760 }
3761
3762 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003763 HashSet<ComponentName> removedComponents = new HashSet<>();
3764 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3765
Sunny Goyal4390ace2014-10-13 11:33:11 -07003766 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003767 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003768 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003769 } else {
3770 removedComponents.add(si.getTargetComponent());
3771 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003772 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003773
3774 if (!removedComponents.isEmpty()) {
3775 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3776 mWorkspace.removeItemsByMatcher(matcher);
3777 mDragController.onAppsRemoved(matcher);
3778 }
3779
3780 if (!removedDeepShortcuts.isEmpty()) {
3781 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3782 mWorkspace.removeItemsByMatcher(matcher);
3783 mDragController.onAppsRemoved(matcher);
3784 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003785 }
3786 }
3787
3788 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003789 * Update the state of a package, typically related to install state.
3790 *
3791 * Implementation of the method from LauncherModel.Callbacks.
3792 */
Sunny Goyale755d462014-07-22 13:48:29 -07003793 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003794 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3795 Runnable r = new Runnable() {
3796 public void run() {
3797 bindRestoreItemsChange(updates);
3798 }
3799 };
3800 if (waitUntilResume(r)) {
3801 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003802 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003803
Sunny Goyal756adbc2015-04-16 15:20:51 -07003804 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003805 }
3806
3807 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003808 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003809 * in addition to specific applications to remove, the reason being that
3810 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003811 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003812 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003814 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003815 public void bindWorkspaceComponentsRemoved(
3816 final HashSet<String> packageNames, final HashSet<ComponentName> components,
3817 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003818 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003819 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003820 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003821 }
Winson Chungd64d1762013-08-20 14:37:16 -07003822 };
3823 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003824 return;
3825 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003826 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003827 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3828 mWorkspace.removeItemsByMatcher(matcher);
3829 mDragController.onAppsRemoved(matcher);
3830
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003831 }
3832 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003833 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3834 mWorkspace.removeItemsByMatcher(matcher);
3835 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003836 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003837 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003838
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003839 @Override
3840 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3841 Runnable r = new Runnable() {
3842 public void run() {
3843 bindAppInfosRemoved(appInfos);
3844 }
3845 };
3846 if (waitUntilResume(r)) {
3847 return;
Joe Onorato36115782010-06-17 13:28:48 -04003848 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003849
Winson Chungdf95eb12013-10-16 14:57:07 -07003850 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003851 if (mAppsView != null) {
3852 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 }
Joe Onoratobe386092009-11-17 17:32:16 -08003855
Sunny Goyald164b7f2016-10-12 20:49:31 -07003856 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003857 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003858 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003859 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003860 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003861
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003862 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003863 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3864 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3865 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003866 return;
3867 }
3868
Sunny Goyald164b7f2016-10-12 20:49:31 -07003869 if (mWidgetsView != null && allWidgets != null) {
3870 mWidgetsView.setWidgets(allWidgets);
3871 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003872 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003873 }
3874
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003875 @Override
3876 public void notifyWidgetProvidersChanged() {
3877 if (mWorkspace.getState().shouldUpdateWidget) {
3878 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3879 }
3880 }
3881
Winson Chung400438b2011-01-16 17:53:48 -08003882 private int mapConfigurationOriActivityInfoOri(int configOri) {
3883 final Display d = getWindowManager().getDefaultDisplay();
3884 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3885 switch (d.getRotation()) {
3886 case Surface.ROTATION_0:
3887 case Surface.ROTATION_180:
3888 // We are currently in the same basic orientation as the natural orientation
3889 naturalOri = configOri;
3890 break;
3891 case Surface.ROTATION_90:
3892 case Surface.ROTATION_270:
3893 // We are currently in the other basic orientation to the natural orientation
3894 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3895 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3896 break;
3897 }
3898
3899 int[] oriMap = {
3900 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3901 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3902 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3903 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3904 };
3905 // Since the map starts at portrait, we need to offset if this device's natural orientation
3906 // is landscape.
3907 int indexOffset = 0;
3908 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3909 indexOffset = 1;
3910 }
3911 return oriMap[(d.getRotation() + indexOffset) % 4];
3912 }
Adam Cohen446e9402011-09-15 18:21:21 -07003913
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003914 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07003915 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003916 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003917 if (Utilities.ATLEAST_JB_MR2) {
3918 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
3919 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003920 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3921 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003922 }
Winson Chung4b919f82012-05-01 10:44:08 -07003923 }
3924 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003925
Winson Chung4b919f82012-05-01 10:44:08 -07003926 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003927 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003928 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003929 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003930 } else {
3931 mHandler.postDelayed(new Runnable() {
3932 public void run() {
3933 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3934 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003935 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003936 }
Winson Chung4b919f82012-05-01 10:44:08 -07003937 }
Winson Chung400438b2011-01-16 17:53:48 -08003938 }
3939
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003940 private void markAppsViewShown() {
3941 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3942 return;
3943 }
3944 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3945 }
3946
3947 private boolean shouldShowDiscoveryBounce() {
3948 if (mState != mState.WORKSPACE) {
3949 return false;
3950 }
3951 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3952 return true;
3953 }
3954 if (!mIsResumeFromActionScreenOff) {
3955 return false;
3956 }
3957 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3958 return false;
3959 }
3960 return true;
3961 }
3962
Winson Chung5ffd51d2015-06-25 11:36:50 -07003963 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003964 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00003965 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003966 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003967 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003968 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003969 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
3970 if (userHandle != null) {
3971 user = UserHandleCompat.fromUser(userHandle);
3972 }
3973 }
3974 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07003975 }
3976
3977 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003978 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003979 if (user == null) {
3980 user = UserHandleCompat.myUserHandle();
3981 }
3982
3983 // Called from search suggestion, add the profile extra to the intent to ensure that we
3984 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01003985 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003986 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01003987 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00003988 return null;
3989 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07003990 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00003991 }
3992
Winson Chung5ffd51d2015-06-25 11:36:50 -07003993 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00003994 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
3995 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003996 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01003997 UserHandleCompat.myUserHandle());
3998 }
3999
Winson Chung5ffd51d2015-06-25 11:36:50 -07004000 protected void moveWorkspaceToDefaultScreen() {
4001 mWorkspace.moveToDefaultScreen(false);
4002 }
4003
Winson Chung80baf5a2010-08-09 16:03:15 -07004004 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004005 * Returns a FastBitmapDrawable with the icon, accurately sized.
4006 */
4007 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4008 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4009 d.setFilterBitmap(true);
4010 resizeIconDrawable(d);
4011 return d;
4012 }
4013
4014 /**
4015 * Resizes an icon drawable to the correct icon size.
4016 */
Winson5fbe0742015-09-30 15:33:00 -07004017 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004018 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004019 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004020 }
4021
4022 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004023 * Prints out out state for debugging.
4024 */
4025 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004026 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004027 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004028 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4029 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004030 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004031 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004032
Daniel Sandler325dc232013-06-05 22:57:57 -04004033 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004034 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004035
4036 @Override
4037 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4038 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004039 // Dump workspace
4040 writer.println(prefix + "Workspace Items");
4041 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4042 writer.println(prefix + " Homescreen " + i);
4043
4044 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4045 for (int j = 0; j < layout.getChildCount(); j++) {
4046 Object tag = layout.getChildAt(j).getTag();
4047 if (tag != null) {
4048 writer.println(prefix + " " + tag.toString());
4049 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004050 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004051 }
Sunny Goyala1365452015-10-01 15:46:24 -07004052
4053 writer.println(prefix + " Hotseat");
4054 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4055 for (int j = 0; j < layout.getChildCount(); j++) {
4056 Object tag = layout.getChildAt(j).getTag();
4057 if (tag != null) {
4058 writer.println(prefix + " " + tag.toString());
4059 }
4060 }
4061
Sunny Goyal713edfc2016-05-06 09:58:34 -07004062 try {
4063 FileLog.flushAll(writer);
4064 } catch (Exception e) {
4065 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004066 }
4067
Adam Cohen9211d422014-10-07 18:14:53 -07004068 if (mLauncherCallbacks != null) {
4069 mLauncherCallbacks.dump(prefix, fd, writer, args);
4070 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004071 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004072
Sunny Goyal66b24572016-09-21 15:57:55 -07004073 @Override
4074 @TargetApi(Build.VERSION_CODES.N)
4075 public void onProvideKeyboardShortcuts(
4076 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4077
4078 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4079 if (mState == State.WORKSPACE) {
4080 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4081 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4082 }
4083 View currentFocus = getCurrentFocus();
4084 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4085 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4086 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4087 }
4088 if (currentFocus instanceof BubbleTextView &&
4089 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4090 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4091 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4092 }
4093 if (!shortcutInfos.isEmpty()) {
4094 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4095 }
4096
4097 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4098 }
4099
4100 @Override
4101 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4102 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4103 switch (keyCode) {
4104 case KeyEvent.KEYCODE_A:
4105 if (mState == State.WORKSPACE) {
4106 showAppsView(true, true, false);
4107 return true;
4108 }
4109 break;
4110 case KeyEvent.KEYCODE_S: {
4111 View focusedView = getCurrentFocus();
4112 if (focusedView instanceof BubbleTextView
4113 && focusedView.getTag() instanceof ItemInfo
4114 && mAccessibilityDelegate.performAction(focusedView,
4115 (ItemInfo) focusedView.getTag(),
4116 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07004117 DeepShortcutsContainer.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004118 return true;
4119 }
4120 break;
4121 }
4122 case KeyEvent.KEYCODE_O:
4123 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4124 return true;
4125 }
4126 break;
4127 }
4128 }
4129 return super.onKeyShortcut(keyCode, event);
4130 }
4131
Adam Cohen59400422014-03-05 18:07:04 -08004132 public static CustomAppWidget getCustomAppWidget(String name) {
4133 return sCustomAppWidgets.get(name);
4134 }
4135
4136 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4137 return sCustomAppWidgets;
4138 }
4139
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004140 public static Launcher getLauncher(Context context) {
4141 if (context instanceof Launcher) {
4142 return (Launcher) context;
4143 }
4144 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4145 }
4146
Sunny Goyal745bad92016-05-02 10:54:12 -07004147 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4148
4149 @Override
4150 public void onSharedPreferenceChanged(
4151 SharedPreferences sharedPreferences, String key) {
4152 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4153 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4154 if (!waitUntilResume(this, true)) {
4155 run();
4156 }
4157 }
4158 }
4159
4160 @Override
4161 public void run() {
4162 setOrientation();
4163 }
4164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004165}