blob: 9a42513b49b7276c93f79a8bc4056913964900a5 [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 Goyalb5e65c82016-10-26 18:32:38 -070089import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyalffe83f12014-08-14 17:39:34 -070090import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010091import com.android.launcher3.compat.LauncherActivityInfoCompat;
92import com.android.launcher3.compat.LauncherAppsCompat;
93import com.android.launcher3.compat.UserHandleCompat;
94import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080095import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070096import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -070097import com.android.launcher3.dragndrop.DragController;
98import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070099import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700100import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700101import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000102import com.android.launcher3.folder.Folder;
103import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700104import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700105import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700106import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700107import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700108import com.android.launcher3.model.PackageItemInfo;
109import com.android.launcher3.model.WidgetItem;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700110import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700111import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700112import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700113import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700114import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700115import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700117import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700118import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700119import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700120import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700121import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700122import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700123import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700124import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700125import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700127
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700131import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800132import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700134import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700135import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137/**
138 * Default launcher application.
139 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100140public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700141 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
142 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700143 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700144 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700145 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700147 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700154 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700157 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Sunny Goyal28c6b962015-10-12 11:42:05 -0700160 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
161
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700162 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
163
Mathew Inwood876a8462013-06-14 14:12:41 +0100164 /**
165 * IntentStarter uses request codes starting with this. This must be greater than all activity
166 * request codes used internally.
167 */
168 protected static final int REQUEST_LAST = 100;
169
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700171 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700182 // Type: PendingRequestArgs
183 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
184 // Type: ActivityResultInfo
185 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700187 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800188
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700190 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
191 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700192
Adam Cohen091440a2015-03-18 14:16:05 -0700193 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700194 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700195
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700196 private boolean mIsSafeModeEnabled;
197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800199 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700200 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700201 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chunga2413752012-04-03 14:22:34 -0700203 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700204 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
205 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700207
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700209 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800211 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700212 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700213
214 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Sunny Goyalffe83f12014-08-14 17:39:34 -0700216 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700217 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700218
Michael Jurka0280c3b2010-09-17 15:00:07 -0700219 private int[] mTmpAddItemCellCoordinates = new int[2];
220
Sunny Goyal316490e2015-06-02 09:38:28 -0700221 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800222 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700223
Michael Jurka838a4ca2011-02-07 13:33:06 -0800224 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700225 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700226
Sunny Goyal47328fd2016-05-25 18:56:41 -0700227 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700228
229 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700230 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700231 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700232
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700233 // Main container view and the model for the widget tray screen.
234 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700235 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700236
Winson Chung4a2afa32012-07-19 14:53:05 -0700237 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
238 // scroll issues (because the workspace may not have been measured yet) and extra work.
239 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
240 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241
242 private SpannableStringBuilder mDefaultKeySsb = null;
243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800246 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700247 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
Michael Jurka1e2f4652013-07-08 18:03:46 -0700249 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700250 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700251 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700252
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700255 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700256 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700257 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700258 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700259 private boolean mHasFocus = false;
260 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700262 /** Maps launcher activity components to their list of shortcut ids. */
263 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
264
Adam Cohen61f560d2013-09-30 15:58:20 -0700265 private View.OnTouchListener mHapticFeedbackTouchListener;
266
Winson Chung400438b2011-01-16 17:53:48 -0800267 // Determines how long to wait after a rotation before restoring the screen orientation to
268 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700269 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800270
Adam Cohen1462de32012-07-24 22:34:36 -0700271 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
272
Winson Chung46353de2012-02-16 14:05:10 -0800273 // We only want to get the SharedPreferences once since it does an FS stat each time we get
274 // it from the context.
275 private SharedPreferences mSharedPrefs;
276
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700277 private DeviceProfile mDeviceProfile;
278
Adam Cohen4b66bf32015-09-18 12:15:19 -0700279 private boolean mMoveToDefaultScreenFromNewIntent;
280
Winson Chung13eb5272015-05-11 16:30:13 -0700281 // This is set to the view that launched the activity that navigated the user away from
282 // launcher. Since there is no callback for when the activity has finished launching, enable
283 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800284 private BubbleTextView mWaitingForResume;
285
Adam Cohen59400422014-03-05 18:07:04 -0800286 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
287 new HashMap<String, CustomAppWidget>();
288
Adam Cohen59400422014-03-05 18:07:04 -0800289 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700290 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
291 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800292 }
293 }
294
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700295 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
296 // state transition. If another state transition happened during this delay,
297 // simply unregister this runnable.
298 private Runnable mExitSpringLoadedModeRunnable;
299
Adam Cohen091440a2015-03-18 14:16:05 -0700300 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700301 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700302 if (mWorkspace != null) {
303 mWorkspace.buildPageHardwareLayers();
304 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700305 }
306 };
307
Sunny Goyal2100c782016-08-22 16:00:03 -0700308 // Activity result which needs to be processed after workspace has loaded.
309 private ActivityResultInfo mPendingActivityResult;
310 /**
311 * Holds extra information required to handle a result from an external call, like
312 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
313 */
314 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800315
Hyunyoung Songaa953652016-04-19 18:30:24 -0700316 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800317
Jon Miranda379198e2016-09-23 08:54:40 -0700318 private float mLastDispatchTouchEventX = 0.0f;
319 private float mEdgeOfScreenThresholdPx = 0.0f;
320
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700321 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700322 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400323
324 @Thunk void setOrientation() {
325 if (mRotationEnabled) {
326 unlockScreenOrientation(true);
327 } else {
328 setRequestedOrientation(
329 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700330 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400331 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700332
Sunny Goyal745bad92016-05-02 10:54:12 -0700333 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 @Override
336 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700337 if (DEBUG_STRICT_MODE) {
338 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
339 .detectDiskReads()
340 .detectDiskWrites()
341 .detectNetwork() // or .detectAll() for all detectable problems
342 .penaltyLog()
343 .build());
344 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
345 .detectLeakedSqlLiteObjects()
346 .detectLeakedClosableObjects()
347 .penaltyLog()
348 .penaltyDeath()
349 .build());
350 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700351 if (LauncherAppState.PROFILE_STARTUP) {
352 Trace.beginSection("Launcher-onCreate");
353 }
Winson Chunga2413752012-04-03 14:22:34 -0700354
Adam Cohen9211d422014-10-07 18:14:53 -0700355 if (mLauncherCallbacks != null) {
356 mLauncherCallbacks.preOnCreate();
357 }
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400360
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400361 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700362
Adam Cohen2e6da152015-05-06 11:42:25 -0700363 // Load configuration-specific DeviceProfile
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700364 mDeviceProfile =
365 getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
366 ? app.getInvariantDeviceProfile().landscapeProfile
367 : app.getInvariantDeviceProfile().portraitProfile;
368
Sunny Goyalf5e37442016-11-02 10:31:24 -0700369 if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700370 Display display = getWindowManager().getDefaultDisplay();
371 Point mwSize = new Point();
372 display.getSize(mwSize);
373 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
374 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700375
Sunny Goyalf7258242015-10-19 16:59:07 -0700376 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700377 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800378 mModel = app.setLauncher(this);
379 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700380 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700381
Joe Onorato41a12d22009-10-31 18:30:00 -0400382 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700383 mAllAppsController = new AllAppsTransitionController(this);
384 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700385
Sunny Goyalffe83f12014-08-14 17:39:34 -0700386 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700387
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
389 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700390
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700391 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
392 // this also ensures that any synchronous binding below doesn't re-trigger another
393 // LauncherModel load.
394 mPaused = false;
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700397
Jon Miranda379198e2016-09-23 08:54:40 -0700398 mEdgeOfScreenThresholdPx = getResources()
399 .getDimensionPixelSize(R.dimen.edge_of_screen_threshold);
400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 setupViews();
Winson1f064272016-07-18 17:18:02 -0700402 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700403 mExtractedColors = new ExtractedColors();
404 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405
Tony Wickhame2217252016-03-22 16:34:23 -0700406 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
407 .addAccessibilityStateChangeListener(this);
408
Joe Onorato7c312c12009-08-13 21:36:53 -0700409 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700410
Sunny Goyalfe770c92016-09-27 14:38:58 -0700411 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Sunny Goyale26d1002016-06-20 14:52:14 -0700413 if (LauncherAppState.PROFILE_STARTUP) {
414 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
416
Sunny Goyal2100c782016-08-22 16:00:03 -0700417 // We only load the page synchronously if the user rotates (or triggers a
418 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700419 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
420 if (savedInstanceState != null) {
421 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
422 }
423 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700424 // If we are not binding synchronously, show a fade in animation when
425 // the first page bind completes.
426 mDragLayer.setAlpha(0);
427 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700428 // Pages bound synchronously.
429 mWorkspace.setCurrentPage(currentScreen);
430
Sunny Goyal2100c782016-08-22 16:00:03 -0700431 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 }
433
434 // For handling default keys
435 mDefaultKeySsb = new SpannableStringBuilder();
436 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800437
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800438 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700439 // In case we are on a device with locked rotation, we should look at preferences to check
440 // if the user has specifically allowed rotation.
441 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700442 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700443 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
444 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700445 }
446
447 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
448 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400449 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700450
Adam Cohen9211d422014-10-07 18:14:53 -0700451 if (mLauncherCallbacks != null) {
452 mLauncherCallbacks.onCreate(savedInstanceState);
453 }
454 }
455
Sunny Goyal7779d622015-06-11 16:18:39 -0700456 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700457 public void onExtractedColorsChanged() {
458 loadExtractedColorsAndColorItems();
459 }
460
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700461 @Override
462 public void onAppWidgetHostReset() {
463 if (mAppWidgetHost != null) {
464 mAppWidgetHost.startListening();
465 }
466 }
467
Tony Wickham827cef22016-03-17 15:39:39 -0700468 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700469 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700470 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700471 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700472 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700473 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700474 // It's possible that All Apps is visible when this is run,
475 // so always use light status bar in that case.
476 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700477 }
478 }
479
Tony Wickham345bff32016-09-28 15:34:51 -0700480 /**
481 * Sets the status bar to be light or not. Light status bar means dark icons.
482 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
483 */
484 public void activateLightStatusBar(boolean activate) {
Tony Wickham0fed55b2016-10-12 14:55:46 -0700485 boolean lightStatusBar = activate || (FeatureFlags.LIGHT_STATUS_BAR
486 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
487 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700488 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
489 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700490 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700491 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700492 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700493 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700494 }
Tony Wickham345bff32016-09-28 15:34:51 -0700495 if (newSystemUiFlags != oldSystemUiFlags) {
496 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
497 }
Tony Wickham93113872016-09-14 16:46:32 -0700498 }
499
Adam Cohen9211d422014-10-07 18:14:53 -0700500 private LauncherCallbacks mLauncherCallbacks;
501
502 public void onPostCreate(Bundle savedInstanceState) {
503 super.onPostCreate(savedInstanceState);
504 if (mLauncherCallbacks != null) {
505 mLauncherCallbacks.onPostCreate(savedInstanceState);
506 }
507 }
508
Winson1f064272016-07-18 17:18:02 -0700509 public void onInsetsChanged(Rect insets) {
510 mDeviceProfile.updateInsets(insets);
511 mDeviceProfile.layout(this, true /* notifyListeners */);
512 }
513
Sunny Goyal32554d12015-12-03 15:31:25 -0800514 /**
515 * Call this after onCreate to set or clear overlay.
516 */
517 public void setLauncherOverlay(LauncherOverlay overlay) {
518 if (overlay != null) {
519 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
520 }
521 mWorkspace.setLauncherOverlay(overlay);
522 }
523
Adam Cohen9211d422014-10-07 18:14:53 -0700524 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
525 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700526 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700527 private boolean mWorkspaceImportanceStored = false;
528 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700529 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800530 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
532
533 @Override
534 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700535 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 return;
537 }
538 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700539 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 if (mWorkspace != null) {
541 mWorkspaceImportanceForAccessibility =
542 mWorkspace.getImportantForAccessibility();
543 mWorkspace.setImportantForAccessibility(
544 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
545 mWorkspaceImportanceStored = true;
546 }
547 if (mHotseat != null) {
548 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
549 mHotseat.setImportantForAccessibility(
550 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
551 mHotseatImportanceStored = true;
552 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 }
554
555 @Override
556 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700557 if (mWorkspaceImportanceStored && mWorkspace != null) {
558 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
559 }
560 if (mHotseatImportanceStored && mHotseat != null) {
561 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
562 }
563 mWorkspaceImportanceStored = false;
564 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 }
566 });
Adam Cohen9211d422014-10-07 18:14:53 -0700567 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700568 }
569
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700570 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700571 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700572 if (mLauncherCallbacks != null) {
573 mLauncherCallbacks.onLauncherProviderChange();
574 }
575 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700576
Adam Cohen9211d422014-10-07 18:14:53 -0700577 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700578 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700579 if (mLauncherCallbacks != null) {
580 return mLauncherCallbacks.hasCustomContentToLeft();
581 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700582 return false;
583 }
584
Dave Hawkeya8881582013-09-17 15:55:33 -0600585 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500586 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600587 * {@link #addToCustomContentPage}. This will only be invoked if
588 * {@link #hasCustomContentToLeft()} is {@code true}.
589 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500590 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700591 if (mLauncherCallbacks != null) {
592 mLauncherCallbacks.populateCustomContentContainer();
593 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 }
595
596 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700597 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * ensure the custom content page is added or removed if necessary.
599 */
600 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600601 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 // Not bound yet, wait for bindScreens to be called.
603 return;
604 }
605
606 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
607 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500608 mWorkspace.createCustomContentContainer();
609 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
611 mWorkspace.removeCustomContentPage();
612 }
613 }
614
Hyunyoung Songaa953652016-04-19 18:30:24 -0700615 public UserEventDispatcher getUserEventDispatcher() {
616 if (mLauncherCallbacks != null) {
617 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
618 if (dispatcher != null) {
619 return dispatcher;
620 }
621 }
622
Sunny Goyal64976d52016-06-20 14:56:28 -0700623 // Logger object is a singleton and does not have to be coupled with the foreground
624 // activity. Since most user event logging is done on the UI, the object is retrieved
625 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700626 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700627 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700628 }
629 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800630 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100631
Hyunyoung Song3f471442015-04-08 19:01:34 -0700632 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700633 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
634 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700635 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700636 }
637
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000638 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700639 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
640 // This cast is safe as long as the id < 0x00FFFFFF
641 // Since we jail all the dynamically generated views, there should be no clashes
642 // with any other views.
643 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000644 }
645
646 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700647 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
648 * a configuration step, this allows the proper animations to run after other transitions.
649 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 private long completeAdd(
651 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
652 long screenId = info.screenId;
653 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700654 // When the screen id represents an actual screen (as opposed to a rank) we make sure
655 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700656 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700657 }
Adam Cohendb364c32014-05-20 14:23:40 -0700658
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700661 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700662 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700665 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700666 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700668 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700669 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 int widgetId = appWidgetId;
671 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700672 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700674 // Since the view was just bound, also launch the configure activity if needed
675 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
676 .getLauncherAppWidgetInfo(widgetId);
677 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700678 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700679 }
680 }
681 break;
682 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700684
Adam Cohendb364c32014-05-20 14:23:40 -0700685 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800686 }
687
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800688 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700689 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 if (isWorkspaceLoading()) {
691 // process the result once the workspace has loaded.
692 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
693 return;
694 }
695 mPendingActivityResult = null;
696
Winson Chunge341d302013-08-16 14:31:00 -0700697 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 final PendingRequestArgs requestArgs = mPendingRequestArgs;
699 setWaitingForResult(null);
700 if (requestArgs == null) {
701 return;
702 }
703
704 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700705
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 Runnable exitSpringLoaded = new Runnable() {
707 @Override
708 public void run() {
709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
710 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
711 }
712 };
713
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700715 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700716 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
718 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700720 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700723 addAppWidgetImpl(
724 appWidgetId, requestArgs, null,
725 requestArgs.getWidgetProvider(),
726 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 }
728 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200729 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
730 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700731 // User could have free-scrolled between pages before picking a wallpaper; make sure
732 // we move to the closest one now.
733 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700734 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200735 }
736 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200738
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700740 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700741
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 // We have special handling for widgets
743 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800744 final int appWidgetId;
745 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
746 : -1;
747 if (widgetId < 0) {
748 appWidgetId = pendingAddWidgetId;
749 } else {
750 appWidgetId = widgetId;
751 }
752
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800754 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700755 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
756 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700758 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700759 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 @Override
761 public void run() {
762 exitSpringLoadedDragModeDelayed(false, 0, null);
763 }
764 };
Adam Cohendb364c32014-05-20 14:23:40 -0700765
Sunny Goyal2100c782016-08-22 16:00:03 -0700766 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
767 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
768 } else {
769 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
770 // When the screen id represents an actual screen (as opposed to a rank)
771 // we make sure that the drop page actually exists.
772 requestArgs.screenId =
773 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700774 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 final CellLayout dropLayout =
776 mWorkspace.getScreenWithId(requestArgs.screenId);
777
778 dropLayout.setDropPending(true);
779 final Runnable onComplete = new Runnable() {
780 @Override
781 public void run() {
782 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
783 dropLayout.setDropPending(false);
784 }
785 };
786 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
787 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700788 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800789 return;
790 }
791
Sunny Goyald478c832016-04-01 12:04:16 -0700792 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
793 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700794 if (resultCode == RESULT_OK) {
795 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700796 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700797 }
798 // Leave the widget in the pending state if the user canceled the configure.
799 return;
800 }
801
Sunny Goyalaad90582015-07-09 14:22:50 -0700802 if (requestCode == REQUEST_CREATE_SHORTCUT) {
803 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700804 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
805 completeAdd(requestCode, data, -1, requestArgs);
806 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
808
Sunny Goyalaad90582015-07-09 14:22:50 -0700809 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
811 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800814 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800815 }
816
817 @Override
818 protected void onActivityResult(
819 final int requestCode, final int resultCode, final Intent data) {
820 handleActivityResult(requestCode, resultCode, data);
821 if (mLauncherCallbacks != null) {
822 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
823 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800824 }
825
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600826 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700827 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600828 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700829 PendingRequestArgs pendingArgs = mPendingRequestArgs;
830 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
831 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
832 setWaitingForResult(null);
833
Sunny Goyal28c6b962015-10-12 11:42:05 -0700834 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700835 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700836 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700837 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700838 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700839 Intent intent = pendingArgs.getPendingIntent();
840
Sunny Goyal28c6b962015-10-12 11:42:05 -0700841 if (grantResults.length > 0
842 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700843 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700844 } else {
845 // TODO: Show a snack bar with link to settings
846 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700847 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700848 }
849 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
852 grantResults);
853 }
854 }
855
Adam Cohendb364c32014-05-20 14:23:40 -0700856 /**
857 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
858 *
859 * @param screenId the screen id to check
860 * @return the new screen, or screenId if it exists
861 */
862 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800863 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700864 if (dropLayout == null) {
865 // it's possible that the add screen was removed because it was
866 // empty and a re-bind occurred
867 mWorkspace.addExtraEmptyScreen();
868 return mWorkspace.commitExtraEmptyScreen();
869 } else {
870 return screenId;
871 }
872 }
873
Sunny Goyal2100c782016-08-22 16:00:03 -0700874 @Thunk void completeTwoStageWidgetDrop(
875 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
876 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 Runnable onCompleteRunnable = null;
878 int animationType = 0;
879
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700880 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 if (resultCode == RESULT_OK) {
882 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
883 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700884 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700885 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 onCompleteRunnable = new Runnable() {
887 @Override
888 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700889 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700890 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
891 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 }
893 };
894 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800895 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700899 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700900 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
901 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700902 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903 // The animated view may be null in the case of a rotation during widget configuration
904 onCompleteRunnable.run();
905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
908 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700909 protected void onStop() {
910 super.onStop();
911 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700912
913 if (mLauncherCallbacks != null) {
914 mLauncherCallbacks.onStop();
915 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700916
Sunny Goyalf5e37442016-11-02 10:31:24 -0700917 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700918 mAppWidgetHost.stopListening();
919 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700920 }
921
922 @Override
923 protected void onStart() {
924 super.onStart();
925 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700926
927 if (mLauncherCallbacks != null) {
928 mLauncherCallbacks.onStart();
929 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700930
Sunny Goyalf5e37442016-11-02 10:31:24 -0700931 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700932 mAppWidgetHost.startListening();
933 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700934 }
935
936 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200938 long startTime = 0;
939 if (DEBUG_RESUME_TIME) {
940 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400941 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200942 }
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.preOnResume();
946 }
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700949 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700950
Winson Chung4a2afa32012-07-19 14:53:05 -0700951 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700952 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700953 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800954 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700955 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700956 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700957 // view after launching an app, as they may be depending on the UI to be static to
958 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700959 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700960 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800961 } else if (mOnResumeState == State.WIDGETS) {
962 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700963 }
964 mOnResumeState = State.NONE;
965
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800966 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700967 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700968 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700969 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700970 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700972 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200973 // We might have postponed some bind calls until onResume (see waitUntilResume) --
974 // execute them here
975 long startTimeCallbacks = 0;
976 if (DEBUG_RESUME_TIME) {
977 startTimeCallbacks = System.currentTimeMillis();
978 }
979
Michael Jurka1e2f4652013-07-08 18:03:46 -0700980 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
981 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200982 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700983 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200984 if (DEBUG_RESUME_TIME) {
985 Log.d(TAG, "Time spent processing callbacks in onResume: " +
986 (System.currentTimeMillis() - startTimeCallbacks));
987 }
Michael Jurka447bf842013-05-15 14:52:15 +0200988 }
Michael Jurka54554252013-08-01 12:52:23 +0200989 if (mOnResumeCallbacks.size() > 0) {
990 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
991 mOnResumeCallbacks.get(i).run();
992 }
993 mOnResumeCallbacks.clear();
994 }
Winson Chunge4e50662012-01-23 14:45:13 -0800995
996 // Reset the pressed state of icons that were locked in the press state while activities
997 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800998 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800999 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001000 mWaitingForResume.setStayPressed(false);
1001 }
Winson Chung82963d52013-10-09 11:20:57 -07001002
Adam Cohen06dff352012-06-01 17:17:08 -07001003 // It is possible that widgets can receive updates while launcher is not in the foreground.
1004 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1005 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1006 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001007 if (!isWorkspaceLoading()) {
1008 getWorkspace().reinflateWidgetsIfNecessary();
1009 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001010
Michael Jurka447bf842013-05-15 14:52:15 +02001011 if (DEBUG_RESUME_TIME) {
1012 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1013 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001014
Adam Cohen4b66bf32015-09-18 12:15:19 -07001015 // We want to suppress callbacks about CustomContent being shown if we have just received
1016 // onNewIntent while the user was present within launcher. In that case, we post a call
1017 // to move the user to the main screen (which will occur after onResume). We don't want to
1018 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1019 // suppress here.
1020 if (mWorkspace.getCustomContentCallbacks() != null
1021 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001022 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001023 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001024 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1025 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001026 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001027 }
1028 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001029 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001030 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001031 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001032
Sunny Goyal756adbc2015-04-16 15:20:51 -07001033 if (!isWorkspaceLoading()) {
1034 // Process any items that were added while Launcher was away.
1035 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001036
1037 // Refresh shortcuts if the permission changed.
1038 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001039 }
Adam Cohen9211d422014-10-07 18:14:53 -07001040
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001041 if (shouldShowDiscoveryBounce()) {
1042 mAllAppsController.showDiscoveryBounce();
1043 }
1044 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001045 if (mLauncherCallbacks != null) {
1046 mLauncherCallbacks.onResume();
1047 }
Adam Cohen06dff352012-06-01 17:17:08 -07001048 }
1049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001052 // Ensure that items added to Launcher are queued until Launcher returns
1053 InstallShortcutReceiver.enableInstallQueue();
1054
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001056 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001057 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001058 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001059
1060 // We call onHide() aggressively. The custom content callbacks should be able to
1061 // debounce excess onHide calls.
1062 if (mWorkspace.getCustomContentCallbacks() != null) {
1063 mWorkspace.getCustomContentCallbacks().onHide();
1064 }
Romain Guycbb89e42009-06-08 15:52:54 -07001065
Adam Cohen9211d422014-10-07 18:14:53 -07001066 if (mLauncherCallbacks != null) {
1067 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001068 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001069 }
1070
1071 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001072 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1073 // by a onResume or by scrolling otherwise.
1074 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001075
1076 // Custom content is completely hidden
1077 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001078
1079 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1080 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001081
1082 // Indicates whether the user is allowed to scroll away from the custom content.
1083 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001084 }
1085
Adam Cohenc2d6e892014-10-16 09:49:24 -07001086 public interface LauncherOverlay {
1087
1088 /**
1089 * Touch interaction leading to overscroll has begun
1090 */
1091 public void onScrollInteractionBegin();
1092
1093 /**
1094 * Touch interaction related to overscroll has ended
1095 */
1096 public void onScrollInteractionEnd();
1097
1098 /**
1099 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1100 * screen (or in the case of RTL, the rightmost screen).
1101 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001102 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103
1104 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001105 * Called when the launcher is ready to use the overlay
1106 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001107 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001108 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001109 }
1110
Jun Mukai8af0cd82015-05-12 12:32:12 -07001111 public interface LauncherSearchCallbacks {
1112 /**
1113 * Called when the search overlay is shown.
1114 */
1115 public void onSearchOverlayOpened();
1116
1117 /**
1118 * Called when the search overlay is dismissed.
1119 */
1120 public void onSearchOverlayClosed();
1121 }
1122
Adam Cohenc2d6e892014-10-16 09:49:24 -07001123 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001124 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001125 }
1126
1127 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1128
Sunny Goyalc86df472016-02-25 09:19:38 -08001129 public void onScrollChanged(float progress) {
1130 if (mWorkspace != null) {
1131 mWorkspace.onOverlayScrollChanged(progress);
1132 }
1133 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 }
1135
Jorim Jaggid017f882014-01-14 17:08:48 -08001136 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001137 if (mLauncherCallbacks != null) {
1138 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001139 } else {
1140 // On devices with a locked orientation, we will at least have the allow rotation
1141 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001142 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001143 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001144 }
1145
Adam Cohen9211d422014-10-07 18:14:53 -07001146 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001147 CustomContentCallbacks callbacks, String description) {
1148 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001149 }
1150
Adam Cohenedb40762013-07-18 16:45:45 -07001151 // The custom content needs to offset its content to account for the QSB
1152 public int getTopOffsetForCustomContent() {
1153 return mWorkspace.getPaddingTop();
1154 }
1155
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001156 @Override
1157 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001158 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001159 if (mModel.isCurrentCallbacks(this)) {
1160 mModel.stopLoader();
1161 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001162 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1163
Romain Guy13c2e7b2010-03-10 19:45:00 -08001164 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001165 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001166
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001167 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001168 @Override
1169 public void onWindowFocusChanged(boolean hasFocus) {
1170 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001171 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001172
1173 if (mLauncherCallbacks != null) {
1174 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1175 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001176 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 private boolean acceptFilter() {
1179 final InputMethodManager inputManager = (InputMethodManager)
1180 getSystemService(Context.INPUT_METHOD_SERVICE);
1181 return !inputManager.isFullscreenMode();
1182 }
1183
1184 @Override
1185 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001186 final int uniChar = event.getUnicodeChar();
1187 final boolean handled = super.onKeyDown(keyCode, event);
1188 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1189 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1191 keyCode, event);
1192 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001193 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001194 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001195 // showSearchDialog()
1196 // If there are multiple keystrokes before the search dialog takes focus,
1197 // onSearchRequested() will be called for every keystroke,
1198 // but it is idempotent, so it's fine.
1199 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
1201 }
1202
Joe Onorato8a9625e2010-01-28 15:55:35 -08001203 // Eat the long press event so the keyboard doesn't come up.
1204 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1205 return true;
1206 }
1207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 return handled;
1209 }
1210
Winson46163472015-09-22 17:31:56 -07001211 @Override
1212 public boolean onKeyUp(int keyCode, KeyEvent event) {
1213 if (keyCode == KeyEvent.KEYCODE_MENU) {
1214 // Ignore the menu key if we are currently dragging or are on the custom content screen
1215 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001216 // Close any open floating view
1217 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001218
Winson46163472015-09-22 17:31:56 -07001219 // Stop resizing any widgets
1220 mWorkspace.exitWidgetResizeMode();
1221
1222 // Show the overview mode if we are on the workspace
1223 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1224 !mWorkspace.isSwitchingState()) {
1225 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001226 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001227 }
1228 }
1229 return true;
1230 }
1231 return super.onKeyUp(keyCode, event);
1232 }
1233
Karl Rosaen138a0412009-04-23 19:00:21 -07001234 private String getTypedText() {
1235 return mDefaultKeySsb.toString();
1236 }
1237
Sunny Goyal6f28e712016-09-13 14:19:29 -07001238 @Override
1239 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 mDefaultKeySsb.clear();
1241 mDefaultKeySsb.clearSpans();
1242 Selection.setSelection(mDefaultKeySsb, 0);
1243 }
1244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 /**
1246 * Restores the previous state, if it exists.
1247 *
1248 * @param savedState The previous state.
1249 */
1250 private void restoreState(Bundle savedState) {
1251 if (savedState == null) {
1252 return;
1253 }
1254
Sunny Goyalfe770c92016-09-27 14:38:58 -07001255 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1256 State[] stateValues = State.values();
1257 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1258 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001259 if (state == State.APPS || state == State.WIDGETS) {
1260 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001261 }
1262
Sunny Goyal2100c782016-08-22 16:00:03 -07001263 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1264 if (requestArgs != null) {
1265 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001267
1268 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
1271 /**
1272 * Finds all the views we need and configure them properly.
1273 */
1274 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001275 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001276 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001277 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001278 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001279 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1280 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001281 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001282
Sunny Goyal784f9c32016-03-23 16:56:54 -07001283 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1284 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1285 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286
Winson Chung4c98d922011-05-31 16:50:48 -07001287 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001288 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001289
Winson Chung3d503fb2011-07-13 17:25:49 -07001290 // Setup the hotseat
1291 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1292 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001293 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001294 }
1295
Winsone9f27272015-10-13 10:47:51 -07001296 // Setup the overview panel
1297 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001298
Winson Chung4c98d922011-05-31 16:50:48 -07001299 // Setup the workspace
1300 mWorkspace.setHapticFeedbackEnabled(false);
1301 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001302 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001303 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1304 // default state, otherwise we will update to the wrong offsets in RTL
1305 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001306 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001307 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001308
Tony Wickham34d2c912015-09-11 09:27:58 -07001309 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001310 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001311
Winson Chungef7f8742015-06-04 17:18:17 -07001312 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001313 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001314 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001315 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1316 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1317 } else {
1318 mAppsView.setSearchBarController(new DefaultAppSearchController());
1319 }
Craig Mautner360310b2012-10-26 15:13:08 -07001320
Winson Chung3d503fb2011-07-13 17:25:49 -07001321 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song0de01172016-10-05 16:27:48 -07001322 mDragController.setDragScroller(mWorkspace);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001323 mDragController.setScrollView(mDragLayer);
1324 mDragController.setMoveTarget(mWorkspace);
1325 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001326 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001327
Peter Schiller310a9882016-06-30 13:52:36 -07001328 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1329 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1330 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001331
Sunny Goyal322d5562015-06-25 19:35:49 -07001332 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1333 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336
Winsone9f27272015-10-13 10:47:51 -07001337 private void setupOverviewPanel() {
1338 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1339
Winsone9f27272015-10-13 10:47:51 -07001340 // Bind wallpaper button actions
1341 View wallpaperButton = findViewById(R.id.wallpaper_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001342 new OverviewButtonClickListener(LauncherLogProto.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001343 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001344 public void handleViewClick(View view) {
1345 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001346 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001347 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001348 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1349
1350 // Bind widget button actions
1351 mWidgetsButton = findViewById(R.id.widget_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001352 new OverviewButtonClickListener(LauncherLogProto.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001353 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001354 public void handleViewClick(View view) {
1355 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001356 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001357 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001358 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1359
1360 // Bind settings actions
1361 View settingsButton = findViewById(R.id.settings_button);
1362 boolean hasSettings = hasSettings();
1363 if (hasSettings) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001364 new OverviewButtonClickListener(LauncherLogProto.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001365 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001366 public void handleViewClick(View view) {
1367 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001368 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001369 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001370 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1371 } else {
1372 settingsButton.setVisibility(View.GONE);
1373 }
1374
1375 mOverviewPanel.setAlpha(0f);
1376 }
1377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001379 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001380 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001381 */
1382 public void setAllAppsButton(View allAppsButton) {
1383 mAllAppsButton = allAppsButton;
1384 }
1385
Sunny Goyalbb011da2016-06-15 15:42:29 -07001386 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001387 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001388 }
1389
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001390 public View getWidgetsButton() {
1391 return mWidgetsButton;
1392 }
1393
Anjali Koppal5ad44842014-03-10 20:34:39 -07001394 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 * Creates a view representing a shortcut.
1396 *
1397 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001399 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001400 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402
1403 /**
1404 * Creates a view representing a shortcut inflated from the specified resource.
1405 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 * @param parent The group the shortcut belongs to.
1407 * @param info The data structure describing the shortcut.
1408 *
1409 * @return A View inflated from layoutResId.
1410 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001411 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001412 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001413 parent, false);
1414 favorite.applyFromShortcutInfo(info, mIconCache);
1415 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001417 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 return favorite;
1419 }
1420
1421 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 * Add a shortcut to the workspace.
1423 *
1424 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001426 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001427 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001428 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001429 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001430
Sunny Goyal5c97f512015-05-19 16:03:28 -07001431 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001432 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1433 args.getPendingIntent().getComponent() == null) {
1434 return;
1435 }
1436 if (!PackageManagerHelper.hasPermissionForActivity(
1437 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1438 // The app is trying to add a shortcut without sufficient permissions
1439 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001440 return;
1441 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001442 final View view = createShortcut(info);
1443
Sunny Goyal5c97f512015-05-19 16:03:28 -07001444 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001445 // First we check if we already know the exact location where we want to add this item.
1446 if (cellX >= 0 && cellY >= 0) {
1447 cellXY[0] = cellX;
1448 cellXY[1] = cellY;
1449 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001450
1451 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001452 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001453 true, null,null)) {
1454 return;
1455 }
1456 DragObject dragObject = new DragObject();
1457 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001458 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1459 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001460 return;
1461 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001462 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001463 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001464 }
1465
1466 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001467 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001468 return;
1469 }
1470
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001471 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
Sunny Goyal2100c782016-08-22 16:00:03 -07001473 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1474 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001478 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001480 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001482 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001483 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1484
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001486 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001487 }
Romain Guycbb89e42009-06-08 15:52:54 -07001488
Adam Cohen59400422014-03-05 18:07:04 -08001489 if (appWidgetInfo.isCustomWidget) {
1490 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001491 }
1492
Adam Cohen59400422014-03-05 18:07:04 -08001493 LauncherAppWidgetInfo launcherInfo;
1494 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001495 launcherInfo.spanX = itemInfo.spanX;
1496 launcherInfo.spanY = itemInfo.spanY;
1497 launcherInfo.minSpanX = itemInfo.minSpanX;
1498 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001499 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001502 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503
Sunny Goyal2100c782016-08-22 16:00:03 -07001504 if (hostView == null) {
1505 // Perform actual inflation because we're live
1506 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001508 hostView.setVisibility(View.VISIBLE);
1509 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
Romain Guycbb89e42009-06-08 15:52:54 -07001511
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001512 private void addAppWidgetToWorkspace(
1513 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001514 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001515 hostView.setTag(item);
1516 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001517
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001518 hostView.setFocusable(true);
1519 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001520
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001521 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001522 item.cellX, item.cellY, item.spanX, item.spanY, insert);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001523 }
1524
Adam Cohended9f8d2010-11-03 13:25:16 -07001525 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1526 @Override
1527 public void onReceive(Context context, Intent intent) {
1528 final String action = intent.getAction();
1529 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001530 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001531
Winson Chungc100e8e2011-08-09 16:02:43 -07001532 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001533 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001534 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001535 if (!showWorkspace(false)) {
1536 // If we are already on the workspace, then manually reset all apps
1537 mAppsView.reset();
1538 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001539 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001540 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001541 }
1542 }
1543 };
1544
1545 @Override
1546 public void onAttachedToWindow() {
1547 super.onAttachedToWindow();
1548
1549 // Listen for broadcasts related to user-presence
1550 final IntentFilter filter = new IntentFilter();
1551 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001552 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001553 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001554 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001555
1556 if (mLauncherCallbacks != null) {
1557 mLauncherCallbacks.onAttachedToWindow();
1558 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001559 }
1560
1561 @Override
1562 public void onDetachedFromWindow() {
1563 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001564 if (mAttached) {
1565 unregisterReceiver(mReceiver);
1566 mAttached = false;
1567 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001568
1569 if (mLauncherCallbacks != null) {
1570 mLauncherCallbacks.onDetachedFromWindow();
1571 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001572 }
1573
1574 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001575 // The following code used to be in onResume, but it turns out onResume is called when
1576 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1577 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001578 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001579 if (!mWorkspaceLoading) {
1580 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001581 // We want to let Launcher draw itself at least once before we force it to build
1582 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001583 // apps is nice and speedy.
1584 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001585 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001586 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001587 if (mStarted) return;
1588 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001589 // We delay the layer building a bit in order to give
1590 // other message processing a time to run. In particular
1591 // this avoids a delay in hiding the IME if it was
1592 // currently shown, because doing that may involve
1593 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001594 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001595 final ViewTreeObserver.OnDrawListener listener = this;
1596 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001597 public void run() {
1598 if (mWorkspace != null &&
1599 mWorkspace.getViewTreeObserver() != null) {
1600 mWorkspace.getViewTreeObserver().
1601 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001602 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001603 }
1604 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001605 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001606 }
1607 });
1608 }
1609 clearTypedText();
1610 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001611 }
1612
Winson Chunga6945242014-01-08 14:04:34 -08001613 public DragLayer getDragLayer() {
1614 return mDragLayer;
1615 }
1616
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001617 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001618 return mAppsView;
1619 }
1620
Hyunyoung Song3f471442015-04-08 19:01:34 -07001621 public WidgetsContainerView getWidgetsView() {
1622 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001623 }
1624
Winson Chunga6945242014-01-08 14:04:34 -08001625 public Workspace getWorkspace() {
1626 return mWorkspace;
1627 }
1628
Sunny Goyal6178f132016-07-11 17:30:03 -07001629 public View getQsbContainer() {
1630 return mQsbContainer;
1631 }
1632
Winson Chunga6945242014-01-08 14:04:34 -08001633 public Hotseat getHotseat() {
1634 return mHotseat;
1635 }
1636
Jorim Jaggid017f882014-01-14 17:08:48 -08001637 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001638 return mOverviewPanel;
1639 }
1640
Sunny Goyal47328fd2016-05-25 18:56:41 -07001641 public DropTargetBar getDropTargetBar() {
1642 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001643 }
1644
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001645 public LauncherAppWidgetHost getAppWidgetHost() {
1646 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Winson Chunga9abd0e2010-10-27 17:18:37 -07001649 public LauncherModel getModel() {
1650 return mModel;
1651 }
1652
Adam Cohen79d90c52016-04-22 13:29:20 -07001653 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001654 return mSharedPrefs;
1655 }
1656
Adam Cohen2e6da152015-05-06 11:42:25 -07001657 public DeviceProfile getDeviceProfile() {
1658 return mDeviceProfile;
1659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 @Override
1662 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001663 long startTime = 0;
1664 if (DEBUG_RESUME_TIME) {
1665 startTime = System.currentTimeMillis();
1666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 super.onNewIntent(intent);
1668
Winson15f8b172015-08-19 11:02:39 -07001669 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1670 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1671 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001672
1673 // Check this condition before handling isActionMain, as this will get reset.
1674 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001675 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001676
Winson15f8b172015-08-19 11:02:39 -07001677 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1678 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001679 if (mWorkspace == null) {
1680 // Can be cases where mWorkspace is null, this prevents a NPE
1681 return;
1682 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001683 // In all these cases, only animate if we're already on home
1684 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001685
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001686 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001687 exitSpringLoadedDragMode();
1688
1689 // If we are already on home, then just animate back to the workspace,
1690 // otherwise, just wait until onResume to set the state back to Workspace
1691 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001692 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001693 } else {
1694 mOnResumeState = State.WORKSPACE;
1695 }
1696
1697 final View v = getWindow().peekDecorView();
1698 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001699 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001700 INPUT_METHOD_SERVICE);
1701 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1702 }
1703
Winson Chungb745afb2015-03-02 11:51:23 -08001704 // Reset the apps view
1705 if (!alreadyOnHome && mAppsView != null) {
1706 mAppsView.scrollToTop();
1707 }
1708
Hyunyoung Song3f471442015-04-08 19:01:34 -07001709 // Reset the widgets view
1710 if (!alreadyOnHome && mWidgetsView != null) {
1711 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001712 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001713
Adam Cohen9211d422014-10-07 18:14:53 -07001714 if (mLauncherCallbacks != null) {
1715 mLauncherCallbacks.onHomeIntent();
1716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
Adam Cohened307df2013-10-02 09:37:31 -07001718
Adam Cohen9211d422014-10-07 18:14:53 -07001719 if (mLauncherCallbacks != null) {
1720 mLauncherCallbacks.onNewIntent(intent);
1721 }
Winson15f8b172015-08-19 11:02:39 -07001722
1723 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1724 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1725 // animation.
1726 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001727 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001728 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001729 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1730 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001731
1732 // We use this flag to suppress noisy callbacks above custom content state
1733 // from onResume.
1734 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001735 mWorkspace.post(new Runnable() {
1736 @Override
1737 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001738 if (mWorkspace != null) {
1739 mWorkspace.moveToDefaultScreen(true);
1740 }
Winson15f8b172015-08-19 11:02:39 -07001741 }
1742 });
1743 }
1744 }
1745
1746 if (DEBUG_RESUME_TIME) {
1747 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1748 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001749 }
1750
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001752 public void onRestoreInstanceState(Bundle state) {
1753 super.onRestoreInstanceState(state);
1754 for (int page: mSynchronouslyBoundPages) {
1755 mWorkspace.restoreInstanceStateForChild(page);
1756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
1758
1759 @Override
1760 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001761 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001762 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1763 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001764
Adam Cohen21cd0022013-10-09 18:57:02 -07001765 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001766 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767
Michael Jurka883f55b2010-10-21 15:47:14 -07001768 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001769 // We close any open folders and shortcut containers since they will not be re-opened,
1770 // and we need to make sure this state is reflected.
1771 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001772
Sunny Goyal2100c782016-08-22 16:00:03 -07001773 if (mPendingRequestArgs != null) {
1774 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1775 }
1776 if (mPendingActivityResult != null) {
1777 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
1779
Adam Cohen9211d422014-10-07 18:14:53 -07001780 if (mLauncherCallbacks != null) {
1781 mLauncherCallbacks.onSaveInstanceState(outState);
1782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
1784
1785 @Override
1786 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001788
Michael Jurka9d906c72011-10-14 06:25:36 -07001789 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001790 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001791
Winson Chungcd2b0142011-06-08 16:02:26 -07001792 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001793 // It's possible to receive onDestroy after a new Launcher activity has
1794 // been created. In this case, don't interfere with the new Launcher.
1795 if (mModel.isCurrentCallbacks(this)) {
1796 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001797 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001798 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001799
Sunny Goyal745bad92016-05-02 10:54:12 -07001800 if (mRotationPrefChangeHandler != null) {
1801 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1802 }
1803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001805 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001807 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001809 mAppWidgetHost = null;
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 TextKeyListener.getInstance().release();
1812
Tony Wickhame2217252016-03-22 16:34:23 -07001813 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1814 .removeAccessibilityStateChangeListener(this);
1815
Michael Jurka2ecf9952012-06-18 12:52:28 -07001816 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001817
1818 if (mLauncherCallbacks != null) {
1819 mLauncherCallbacks.onDestroy();
1820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
1822
Sunny Goyalae502842016-06-17 08:43:56 -07001823 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1824 return mAccessibilityDelegate;
1825 }
1826
Adam Cohena9cf38f2011-05-02 15:36:58 -07001827 public DragController getDragController() {
1828 return mDragController;
1829 }
1830
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001832 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001833 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001834 }
1835
1836 @Override
1837 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1838 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001839 try {
1840 super.startIntentSenderForResult(intent, requestCode,
1841 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1842 } catch (IntentSender.SendIntentException e) {
1843 throw new ActivityNotFoundException();
1844 }
1845 }
1846
Winson Chung70d72102011-08-12 11:24:35 -07001847 /**
1848 * Indicates that we want global search for this activity by setting the globalSearch
1849 * argument for {@link #startSearch} to true.
1850 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001852 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001854
Karl Rosaen138a0412009-04-23 19:00:21 -07001855 if (initialQuery == null) {
1856 // Use any text typed in the launcher as the initial query
1857 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 if (appSearchData == null) {
1860 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001861 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001863
Sunny Goyal6f28e712016-09-13 14:19:29 -07001864 if (mLauncherCallbacks == null ||
1865 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1866 // Starting search from the callbacks failed. Start the default global search.
1867 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001868 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001869
1870 // We need to show the workspace after starting the search
1871 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001872 }
1873
Ian Parkinson047036e2014-09-01 15:40:53 +01001874 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001875 * Starts the global search activity. This code is a copied from SearchManager
1876 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001877 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001878 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001879 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001880 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1881 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1882 if (globalSearchActivity == null) {
1883 Log.w(TAG, "No global search activity found.");
1884 return;
1885 }
1886 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1887 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1888 intent.setComponent(globalSearchActivity);
1889 // Make sure that we have a Bundle to put source in
1890 if (appSearchData == null) {
1891 appSearchData = new Bundle();
1892 } else {
1893 appSearchData = new Bundle(appSearchData);
1894 }
Adam Cohen9211d422014-10-07 18:14:53 -07001895 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001896 if (!appSearchData.containsKey("source")) {
1897 appSearchData.putString("source", getPackageName());
1898 }
1899 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1900 if (!TextUtils.isEmpty(initialQuery)) {
1901 intent.putExtra(SearchManager.QUERY, initialQuery);
1902 }
1903 if (selectInitialQuery) {
1904 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1905 }
1906 intent.setSourceBounds(sourceBounds);
1907 try {
1908 startActivity(intent);
1909 } catch (ActivityNotFoundException ex) {
1910 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
1913
Yura4f93ec62014-02-04 14:15:21 +00001914 public boolean isOnCustomContent() {
1915 return mWorkspace.isOnOrMovingToCustomContent();
1916 }
1917
Winson Chung70d72102011-08-12 11:24:35 -07001918 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001919 public boolean onPrepareOptionsMenu(Menu menu) {
1920 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001921 if (mLauncherCallbacks != null) {
1922 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1923 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001924 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001927 @Override
1928 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001929 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001930 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001931 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001932 }
1933
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001935 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 }
1937
Adam Cohen517a7f52014-03-01 12:12:59 -08001938 public boolean isWorkspaceLoading() {
1939 return mWorkspaceLoading;
1940 }
1941
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001942 private void setWorkspaceLoading(boolean value) {
1943 boolean isLocked = isWorkspaceLocked();
1944 mWorkspaceLoading = value;
1945 if (isLocked != isWorkspaceLocked()) {
1946 onWorkspaceLockedChanged();
1947 }
1948 }
1949
Sunny Goyal2100c782016-08-22 16:00:03 -07001950 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001951 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001952 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001953 if (isLocked != isWorkspaceLocked()) {
1954 onWorkspaceLockedChanged();
1955 }
1956 }
1957
Adam Cohen9211d422014-10-07 18:14:53 -07001958 protected void onWorkspaceLockedChanged() {
1959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onWorkspaceLockedChanged();
1961 }
1962 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001963
Sunny Goyal2100c782016-08-22 16:00:03 -07001964 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1965 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001966 if (LOGD) {
1967 Log.d(TAG, "Adding widget from drop");
1968 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07001969 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1970 }
1971
Sunny Goyal2100c782016-08-22 16:00:03 -07001972 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
1973 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001974 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001975 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001976 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07001977
Bjorn Bringert7984c942009-12-09 15:38:25 +00001978 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07001979 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
1980 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001982 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001983 Runnable onComplete = new Runnable() {
1984 @Override
1985 public void run() {
1986 // Exit spring loaded mode if necessary after adding the widget
1987 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1988 null);
1989 }
1990 };
Sunny Goyal2100c782016-08-22 16:00:03 -07001991 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07001992 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 }
1994 }
Romain Guycbb89e42009-06-08 15:52:54 -07001995
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001996 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001997 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001998 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001999 mWorkspace.moveToCustomContentScreen(animate);
2000 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002001
2002 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2003 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002004 info.container = container;
2005 info.screenId = screenId;
2006 if (cell != null) {
2007 info.cellX = cell[0];
2008 info.cellY = cell[1];
2009 }
2010 info.spanX = spanX;
2011 info.spanY = spanY;
2012
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002013 switch (info.itemType) {
2014 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2015 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002016 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002017 break;
2018 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002019 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002020 break;
2021 default:
2022 throw new IllegalStateException("Unknown item type: " + info.itemType);
2023 }
2024 }
2025
Adam Cohenfbba09b2011-07-18 21:43:05 -07002026 /**
2027 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002028 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002029 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002030 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2031 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2032 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002033 }
2034
Adam Cohenfbba09b2011-07-18 21:43:05 -07002035 /**
2036 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002037 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002038 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002039 AppWidgetHostView hostView = info.boundWidget;
2040 int appWidgetId;
2041 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002042 // In the case where we've prebound the widget, we remove it from the DragLayer
2043 if (LOGD) {
2044 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2045 }
2046 getDragLayer().removeView(hostView);
2047
Adam Cohened66b2b2012-01-23 17:28:51 -08002048 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002049 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002050
2051 // Clear the boundWidget so that it doesn't get destroyed.
2052 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002053 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002054 // In this case, we either need to start an activity to get permission to bind
2055 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002056 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002057 Bundle options = info.bindOptions;
2058
Sunny Goyalffe83f12014-08-14 17:39:34 -07002059 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2060 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002061 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002062 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002063 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002064 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002065 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2066 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2067 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002068 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002069 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002070 // TODO: we need to make sure that this accounts for the options bundle.
2071 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002072 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2073 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002074 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002075 }
2076
Adam Cohendcd297f2013-06-18 13:13:40 -07002077 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002078 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002079 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 folderInfo.title = getText(R.string.folder_name);
2081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002083 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2084 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085
2086 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002087 FolderIcon newFolder =
2088 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002089 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002091 // Force measure the new folder icon
2092 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2093 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002094 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 }
Romain Guycbb89e42009-06-08 15:52:54 -07002096
Winsonc0b52fe2015-09-09 16:38:15 -07002097 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002098 * Unbinds the view for the specified item, and removes the item and all its children.
2099 *
2100 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002101 * @param itemInfo the {@link ItemInfo} for this view.
2102 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002103 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002104 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002105 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002106 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002107 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2108 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002109 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002110 } else {
2111 mWorkspace.removeWorkspaceItem(v);
2112 }
Winsonc0b52fe2015-09-09 16:38:15 -07002113 if (deleteFromDb) {
2114 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2115 }
2116 } else if (itemInfo instanceof FolderInfo) {
2117 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002118 if (v instanceof FolderIcon) {
2119 ((FolderIcon) v).removeListeners();
2120 }
Winsonc0b52fe2015-09-09 16:38:15 -07002121 mWorkspace.removeWorkspaceItem(v);
2122 if (deleteFromDb) {
2123 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2124 }
2125 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2126 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002127 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002128 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002129 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002130 }
2131 } else {
2132 return false;
2133 }
2134 return true;
2135 }
2136
2137 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002138 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002139 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002140 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002141 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002142 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002143 // Deleting an app widget ID is a void call but writes to disk before returning
2144 // to the caller...
2145 new AsyncTask<Void, Void, Void>() {
2146 public Void doInBackground(Void ... args) {
2147 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2148 return null;
2149 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002150 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002151 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002152 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002153 }
2154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 @Override
2156 public boolean dispatchKeyEvent(KeyEvent event) {
2157 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2158 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002160 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002161 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002162 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002163 dumpState();
2164 return true;
2165 }
2166 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002167 }
2168 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2169 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002170 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 return true;
2172 }
2173 }
2174
2175 return super.dispatchKeyEvent(event);
2176 }
2177
Joe Onorato88ec0992009-11-19 13:16:06 -08002178 @Override
2179 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002180 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2181 return;
2182 }
2183
Sunny Goyal45478022015-06-08 16:52:41 -07002184 if (mDragController.isDragging()) {
2185 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002186 return;
2187 }
2188
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002189 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2190 if (topView != null) {
2191 if (topView.getActiveTextView() != null) {
2192 topView.getActiveTextView().dispatchBackKey();
2193 } else {
2194 topView.close(true);
2195 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002196 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002197 showWorkspace(true);
2198 } else if (isWidgetsViewVisible()) {
2199 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002200 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002201 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002202 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002203 mWorkspace.exitWidgetResizeMode();
2204
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002205 // Back button is a no-op here, but give at least some feedback for the button press
2206 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002207 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002208 }
2209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 * Launches the intent referred by the clicked shortcut.
2212 *
2213 * @param v The view representing the clicked shortcut.
2214 */
2215 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002216 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2217 // view has detached (it's possible for this to happen if the view is removed mid touch).
2218 if (v.getWindowToken() == null) {
2219 return;
2220 }
2221
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002222 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002223 return;
2224 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002225
Adam Cohen1697b792013-09-17 19:08:21 -07002226 if (v instanceof Workspace) {
2227 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002228 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002229 }
2230 return;
2231 }
2232
2233 if (v instanceof CellLayout) {
2234 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002235 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2236 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002237 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002238 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002239 }
2240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002242 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002243 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002245 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002246 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002247 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002248 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2249 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002250 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002251 } else if (tag instanceof AppInfo) {
2252 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002253 } else if (tag instanceof LauncherAppWidgetInfo) {
2254 if (v instanceof PendingAppWidgetHostView) {
2255 onClickPendingWidget((PendingAppWidgetHostView) v);
2256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 }
2258 }
2259
Sunny Goyal70660032015-05-14 00:07:08 -07002260 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002261 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002262 return false;
2263 }
2264
Michael Jurkaaf442092010-06-10 17:01:57 -07002265 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002266 * Event handler for the app widget view which has not fully restored.
2267 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002268 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002269 if (mIsSafeModeEnabled) {
2270 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2271 return;
2272 }
2273
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002274 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002275 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002276 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2277 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2278 // This should not happen, as we make sure that an Id is allocated during bind.
2279 return;
2280 }
2281 LauncherAppWidgetProviderInfo appWidgetInfo =
2282 mAppWidgetManager.findProvider(info.providerName, info.user);
2283 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002284 setWaitingForResult(PendingRequestArgs
2285 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002286
Sunny Goyald478c832016-04-01 12:04:16 -07002287 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002288 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002289 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002290 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002291 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2292 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2293 }
2294 } else {
2295 LauncherAppWidgetProviderInfo appWidgetInfo =
2296 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2297 if (appWidgetInfo != null) {
2298 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2299 }
Sunny Goyalff572272014-07-23 13:58:07 -07002300 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002301 } else if (info.installProgress < 0) {
2302 // The install has not been queued
2303 final String packageName = info.providerName.getPackageName();
2304 showBrokenAppInstallDialog(packageName,
2305 new DialogInterface.OnClickListener() {
2306 public void onClick(DialogInterface dialog, int id) {
2307 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2308 }
2309 });
2310 } else {
2311 // Download has started.
2312 final String packageName = info.providerName.getPackageName();
2313 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002314 }
2315 }
2316
Sunny Goyald478c832016-04-01 12:04:16 -07002317 private void startRestoredWidgetReconfigActivity(
2318 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002319 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002320 mAppWidgetManager.startConfigActivity(provider,
2321 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2322 }
2323
Sunny Goyalff572272014-07-23 13:58:07 -07002324 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002325 * Event handler for the "grid" button that appears on the home screen, which
2326 * enters all apps mode.
2327 *
2328 * @param v The view that was clicked.
2329 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002330 protected void onClickAllAppsButton(View v) {
2331 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002332 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002333 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002334 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002335 showAppsView(true /* animated */, true /* updatePredictedApps */,
2336 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002337 }
2338 }
2339
2340 protected void onLongClickAllAppsButton(View v) {
2341 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2342 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002343 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002344 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002345 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002346 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002347 }
2348 }
2349
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002350 private void showBrokenAppInstallDialog(final String packageName,
2351 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002352 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002353 .setTitle(R.string.abandoned_promises_title)
2354 .setMessage(R.string.abandoned_promise_explanation)
2355 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2356 .setNeutralButton(R.string.abandoned_clean_this,
2357 new DialogInterface.OnClickListener() {
2358 public void onClick(DialogInterface dialog, int id) {
2359 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2360 mWorkspace.removeAbandonedPromise(packageName, user);
2361 }
2362 })
2363 .create().show();
2364 return;
2365 }
2366
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002367 /**
2368 * Event handler for an app shortcut click.
2369 *
2370 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2371 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002372 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002373 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2374 Object tag = v.getTag();
2375 if (!(tag instanceof ShortcutInfo)) {
2376 throw new IllegalArgumentException("Input must be a Shortcut");
2377 }
2378
2379 // Open shortcut
2380 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002381
2382 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002383 if ((shortcut.isDisabled &
2384 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2385 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2386 // If the app is only disabled because of the above flags, launch activity anyway.
2387 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002388 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002389 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2390 // Use a message specific to this shortcut, if it has one.
2391 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2392 return;
2393 }
2394 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002395 int error = R.string.activity_not_available;
2396 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2397 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002398 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2399 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002400 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002401 }
2402 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2403 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002404 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002405 }
2406
Chris Wren40c5ed32014-06-24 18:24:23 -04002407 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002408 if ((v instanceof BubbleTextView)
2409 && shortcut.isPromise()
2410 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002411 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002412 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002413 new DialogInterface.OnClickListener() {
2414 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002415 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002416 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002417 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002418 return;
2419 }
2420
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002421 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002422 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002423 }
2424
Sunny Goyala7ce1662016-05-31 15:01:35 -07002425 private void startAppShortcutOrInfoActivity(View v) {
2426 ItemInfo item = (ItemInfo) v.getTag();
2427 Intent intent = item.getIntent();
2428 if (intent == null) {
2429 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002430 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002431 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002432 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002433
2434 if (success && v instanceof BubbleTextView) {
2435 mWaitingForResume = (BubbleTextView) v;
2436 mWaitingForResume.setStayPressed(true);
2437 }
2438 }
2439
2440 /**
2441 * Event handler for a folder icon click.
2442 *
2443 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2444 */
2445 protected void onClickFolderIcon(View v) {
2446 if (LOGD) Log.d(TAG, "onClickFolder");
2447 if (!(v instanceof FolderIcon)){
2448 throw new IllegalArgumentException("Input must be a FolderIcon");
2449 }
2450
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002451 Folder folder = ((FolderIcon) v).getFolder();
2452 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002453 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002454 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002455 }
Michael Jurka5130e402011-10-13 04:55:35 -07002456 }
2457
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002458 /**
2459 * Event handler for the (Add) Widgets button that appears after a long press
2460 * on the home screen.
2461 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002462 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002463 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002464 if (mIsSafeModeEnabled) {
2465 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2466 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002467 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002468 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002469 }
2470
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002471 /**
2472 * Event handler for the wallpaper picker button that appears after a long press
2473 * on the home screen.
2474 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002475 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002476 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002477 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2478 return;
2479 }
2480
Tony Wickham785f7a52015-08-31 17:28:32 -07002481 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2482 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002483 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002484 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002485 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002486
2487 String pickerPackage = getString(R.string.wallpaper_picker_package);
2488 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2489 if (!hasTargetPackage) {
2490 intent.setPackage(pickerPackage);
2491 }
2492
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002493 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002494 try {
2495 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2496 // If there is no target package, use the default intent chooser animation
2497 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2498 } catch (ActivityNotFoundException e) {
2499 setWaitingForResult(null);
2500 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2501 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 }
2503
2504 /**
2505 * Event handler for a click on the settings button that appears after a long press
2506 * on the home screen.
2507 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002508 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002509 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002510 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2511 .setPackage(getPackageName());
2512 intent.setSourceBounds(getViewBounds(v));
2513 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002514 }
2515
Adam Cohen61f560d2013-09-30 15:58:20 -07002516 public View.OnTouchListener getHapticFeedbackTouchListener() {
2517 if (mHapticFeedbackTouchListener == null) {
2518 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002519 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002520 @Override
2521 public boolean onTouch(View v, MotionEvent event) {
2522 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2523 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2524 }
2525 return false;
2526 }
2527 };
2528 }
2529 return mHapticFeedbackTouchListener;
2530 }
2531
Tony Wickhame2217252016-03-22 16:34:23 -07002532 @Override
2533 public void onAccessibilityStateChanged(boolean enabled) {
2534 mDragLayer.onAccessibilityStateChanged(enabled);
2535 }
2536
Sunny Goyal06e21a22016-08-11 16:02:02 -07002537 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002538 if (isOnCustomContent()) {
2539 // Custom content screen doesn't participate in drag and drop. If on custom
2540 // content screen, move to default.
2541 moveWorkspaceToDefaultScreen();
2542 }
Adam Cohen9211d422014-10-07 18:14:53 -07002543 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002544
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002545 /**
2546 * Called when the user stops interacting with the launcher.
2547 * This implies that the user is now on the homescreen and is not doing housekeeping.
2548 */
Adam Cohen9211d422014-10-07 18:14:53 -07002549 protected void onInteractionEnd() {
2550 if (mLauncherCallbacks != null) {
2551 mLauncherCallbacks.onInteractionEnd();
2552 }
2553 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002554
2555 /**
2556 * Called when the user starts interacting with the launcher.
2557 * The possible interactions are:
2558 * - open all apps
2559 * - reorder an app shortcut, or a widget
2560 * - open the overview mode.
2561 * This is a good time to stop doing things that only make sense
2562 * when the user is on the homescreen and not doing housekeeping.
2563 */
Adam Cohen9211d422014-10-07 18:14:53 -07002564 protected void onInteractionBegin() {
2565 if (mLauncherCallbacks != null) {
2566 mLauncherCallbacks.onInteractionBegin();
2567 }
2568 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002569
Winson Chungcd99cd32015-04-29 11:03:24 -07002570 /** Updates the interaction state. */
2571 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002572 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002573 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002574 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2575 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002576 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002577 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002578 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002579 onInteractionEnd();
2580 }
2581 }
2582
Sunny Goyala7ce1662016-05-31 15:01:35 -07002583 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002585 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2586 try {
2587 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2588 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2589 // is enabled by default on NYC.
2590 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2591 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002592
2593 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2594 String id = ((ShortcutInfo) info).getDeepShortcutId();
2595 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002596 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2597 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002598 } else {
2599 // Could be launching some bookkeeping activity
2600 startActivity(intent, optsBundle);
2601 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002602 } finally {
2603 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002606 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2607 // corresponding permission. Show the appropriate permission prompt if that
2608 // is the case.
2609 if (intent.getComponent() == null
2610 && Intent.ACTION_CALL.equals(intent.getAction())
2611 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2612 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002613
2614 setWaitingForResult(PendingRequestArgs
2615 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002616 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2617 REQUEST_PERMISSION_CALL_PHONE);
2618 } else {
2619 // No idea why this was thrown.
2620 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 }
2623 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002624
Sunny Goyalfe770c92016-09-27 14:38:58 -07002625 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002626 private Bundle getActivityLaunchOptions(View v) {
2627 if (Utilities.ATLEAST_MARSHMALLOW) {
2628 int left = 0, top = 0;
2629 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2630 if (v instanceof TextView) {
2631 // Launch from center of icon, not entire view
2632 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2633 if (icon != null) {
2634 Rect bounds = icon.getBounds();
2635 left = (width - bounds.width()) / 2;
2636 top = v.getPaddingTop();
2637 width = bounds.width();
2638 height = bounds.height();
2639 }
2640 }
2641 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2642 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2643 // On L devices, we use the device default slide-up transition.
2644 // On L MR1 devices, we use a custom version of the slide-up transition which
2645 // doesn't have the delay present in the device default.
2646 return ActivityOptions.makeCustomAnimation(
2647 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2648 }
2649 return null;
2650 }
2651
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002652 private Rect getViewBounds(View v) {
2653 int[] pos = new int[2];
2654 v.getLocationOnScreen(pos);
2655 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2656 }
2657
Sunny Goyala7ce1662016-05-31 15:01:35 -07002658 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002659 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2660 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2661 return false;
2662 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002663 // Only launch using the new animation if the shortcut has not opted out (this is a
2664 // private contract between launcher and may be ignored in the future).
2665 boolean useLaunchAnimation = (v != null) &&
2666 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2667 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2668
2669 UserHandleCompat user = null;
2670 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2671 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2672 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002673 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002674
2675 // Prepare intent
2676 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2677 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002678 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002679 }
2680 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002681 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2682 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002683 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2684 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002685 // Shortcuts need some special checks due to legacy reasons.
2686 startShortcutIntentSafely(intent, optsBundle, item);
2687 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2688 // Could be launching some bookkeeping activity
2689 startActivity(intent, optsBundle);
2690 } else {
2691 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2692 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2693 }
2694 return true;
2695 } catch (ActivityNotFoundException|SecurityException e) {
2696 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2697 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2698 }
2699 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002700 }
2701
Tony Wickhamdadb3042016-02-24 11:07:00 -08002702 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002703 public boolean dispatchTouchEvent(MotionEvent ev) {
2704 mLastDispatchTouchEventX = ev.getX();
2705 return super.dispatchTouchEvent(ev);
2706 }
2707
2708 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002710 if (!isDraggingEnabled()) return false;
2711 if (isWorkspaceLocked()) return false;
2712 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002714 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2715 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002716 onLongClickAllAppsButton(v);
2717 return true;
2718 }
2719
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002720
2721 boolean ignoreLongPressToOverview = mDeviceProfile.shouldIgnoreLongPressToOverview(
2722 mLastDispatchTouchEventX, mEdgeOfScreenThresholdPx);
Jon Miranda379198e2016-09-23 08:54:40 -07002723
Adam Cohen1697b792013-09-17 19:08:21 -07002724 if (v instanceof Workspace) {
2725 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002726 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002727 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2728 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2729 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002730 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002731 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2732 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2733 return true;
2734 } else {
2735 return false;
2736 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002737 } else {
2738 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002739 }
Adam Cohen1697b792013-09-17 19:08:21 -07002740 }
2741
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002742 CellLayout.CellInfo longClickCellInfo = null;
2743 View itemUnderLongClick = null;
2744 if (v.getTag() instanceof ItemInfo) {
2745 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002746 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002747 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002748 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 }
2750
Winson Chung3d503fb2011-07-13 17:25:49 -07002751 // The hotseat touch handling does not go through Workspace, and we always allow long press
2752 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002753 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002754 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002755 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002756 if (mWorkspace.isInOverviewMode()) {
2757 mWorkspace.startReordering(v);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002758 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2759 LauncherLogProto.Action.NONE, LauncherLogProto.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002760 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002761 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002762 return false;
2763 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002764 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2765 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2766 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002767 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002768 }
Jon Miranda379198e2016-09-23 08:54:40 -07002769 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2770 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002772 final boolean isAllAppsButton =
2773 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2774 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2775 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002776 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002777 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002778 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 }
2780 }
2781 }
2782 return true;
2783 }
2784
Winson Chung3d503fb2011-07-13 17:25:49 -07002785 boolean isHotseatLayout(View layout) {
2786 return mHotseat != null && layout != null &&
2787 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2788 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002789
Winson Chung3d503fb2011-07-13 17:25:49 -07002790 /**
2791 * Returns the CellLayout of the specified container at the specified screen.
2792 */
Sunny Goyal92820592015-03-02 11:31:35 -08002793 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002794 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2795 if (mHotseat != null) {
2796 return mHotseat.getLayout();
2797 } else {
2798 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002799 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002800 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002801 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002802 }
2803 }
2804
Winson Chungb745afb2015-03-02 11:51:23 -08002805 /**
2806 * For overridden classes.
2807 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002808 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002809 return isAppsViewVisible();
2810 }
2811
2812 public boolean isAppsViewVisible() {
2813 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2814 }
2815
2816 public boolean isWidgetsViewVisible() {
2817 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002818 }
2819
Michael Jurkae326f182011-11-21 14:05:46 -08002820 @Override
2821 public void onTrimMemory(int level) {
2822 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002823 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2824 // The widget preview db can result in holding onto over
2825 // 3MB of memory for caching which isn't necessary.
2826 SQLiteDatabase.releaseMemory();
2827
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002828 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002829 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002830 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002831 if (mLauncherCallbacks != null) {
2832 mLauncherCallbacks.onTrimMemory(level);
2833 }
Michael Jurkae326f182011-11-21 14:05:46 -08002834 }
2835
Winson5c6bdbb2015-09-03 11:36:19 -07002836 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002837 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002838 }
2839
Winson5c6bdbb2015-09-03 11:36:19 -07002840 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002841 boolean changed = mState != State.WORKSPACE ||
2842 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002843 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002844 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002845 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002846 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002847
Michael Jurkab3e22d92011-10-31 15:58:33 -07002848 // Set focus to the AppsCustomize button
2849 if (mAllAppsButton != null) {
2850 mAllAppsButton.requestFocus();
2851 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002852 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002853
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002854 // Change the state *after* we've called all the transition code
2855 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002856
Winson Chung0f785722015-04-08 10:27:49 -07002857 if (changed) {
2858 // Send an accessibility event to announce the context change
2859 getWindow().getDecorView()
2860 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002861 }
Winson5c6bdbb2015-09-03 11:36:19 -07002862 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002863 }
2864
Winsone9f27272015-10-13 10:47:51 -07002865 /**
2866 * Shows the overview button.
2867 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002868 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002869 showOverviewMode(animated, false);
2870 }
2871
2872 /**
2873 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2874 * onto one of the overview panel buttons.
2875 */
2876 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2877 Runnable postAnimRunnable = null;
2878 if (requestButtonFocus) {
2879 postAnimRunnable = new Runnable() {
2880 @Override
2881 public void run() {
2882 // Hitting the menu button when in touch mode does not trigger touch mode to
2883 // be disabled, so if requested, force focus on one of the overview panel
2884 // buttons.
2885 mOverviewPanel.requestFocusFromTouch();
2886 }
2887 };
2888 }
Adam Cohened307df2013-10-02 09:37:31 -07002889 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002890 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002891 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002892 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002893 // If animated from long press, then don't allow any of the controller in the drag
2894 // layer to intercept any remaining touch.
2895 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002896 }
2897
Winson Chungb745afb2015-03-02 11:51:23 -08002898 /**
2899 * Shows the apps view.
2900 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002901 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002902 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002903 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002904 if (updatePredictedApps) {
2905 tryAndUpdatePredictedApps();
2906 }
Winson Chung76648c52015-07-10 14:33:23 -07002907 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002908 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002909
Winson Chungb745afb2015-03-02 11:51:23 -08002910 /**
2911 * Shows the widgets view.
2912 */
2913 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002914 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002915 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002916 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002917 }
Winson Chung76648c52015-07-10 14:33:23 -07002918 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002919
2920 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002921 @Override
2922 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002923 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002924 }
2925 });
Winson Chungb745afb2015-03-02 11:51:23 -08002926 }
2927
2928 /**
2929 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002930 *
2931 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002932 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002933 // TODO: calling method should use the return value so that when {@code false} is returned
2934 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002935 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002936 if (!(mState == State.WORKSPACE ||
2937 mState == State.APPS_SPRING_LOADED ||
2938 mState == State.WIDGETS_SPRING_LOADED ||
2939 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002940 return false;
2941 }
2942 if (toState != State.APPS && toState != State.WIDGETS) {
2943 return false;
2944 }
Winson Chungb745afb2015-03-02 11:51:23 -08002945
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002946 // This is a safe and supported transition to bypass spring_loaded mode.
2947 if (mExitSpringLoadedModeRunnable != null) {
2948 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2949 mExitSpringLoadedModeRunnable = null;
2950 }
2951
Winson Chungb745afb2015-03-02 11:51:23 -08002952 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07002953 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
2954 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002955 } else {
Winson Chung006ee262015-08-03 14:40:11 -07002956 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002957 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002958
Michael Jurkab3e22d92011-10-31 15:58:33 -07002959 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002960 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002961 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002962
2963 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002964 getWindow().getDecorView()
2965 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002966 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002967 }
2968
Winson Chungcd99cd32015-04-29 11:03:24 -07002969 /**
2970 * Updates the workspace and interaction state on state change, and return the animation to this
2971 * new state.
2972 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002973 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002974 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002975 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002976 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002977 updateInteraction(fromState, toState);
2978 return anim;
2979 }
2980
Hyunyoung Song3f471442015-04-08 19:01:34 -07002981 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002982 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002983 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002984 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002985 }
Winson Chungb745afb2015-03-02 11:51:23 -08002986
Winson Chung006ee262015-08-03 14:40:11 -07002987 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002988 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002989 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07002990
2991 if (isAppsViewVisible()) {
2992 mState = State.APPS_SPRING_LOADED;
2993 } else if (isWidgetsViewVisible()) {
2994 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08002995 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002996 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08002997 } else {
2998 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07002999 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003000 }
Adam Cohen7777d962011-08-18 18:58:38 -07003001
Hyunyoung Song3f471442015-04-08 19:01:34 -07003002 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003003 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003004 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003005
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003006 if (mExitSpringLoadedModeRunnable != null) {
3007 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3008 }
3009 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003010 @Override
3011 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003012 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003013 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3014 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003015 // Before we show workspace, hide all apps again because
3016 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3017 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003018 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003019 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003020 } else {
3021 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003022 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003023 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003024 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003025 };
3026 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003027 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003028
Tony Wickhame0c33232016-02-08 11:37:04 -08003029 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003030 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3031 || mState == State.WIDGETS_SPRING_LOADED;
3032 }
3033
Michael Jurkad3ef3062010-11-23 16:23:58 -08003034 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003035 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003036 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003037 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003038 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003039 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003040 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3041 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003042 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 }
3044
Winson Chung4ac30062015-05-08 17:34:17 -07003045 /**
3046 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3047 * resumed.
3048 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003049 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003050 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003051 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003052 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003053 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003054 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003055 }
3056 }
3057 }
3058
Michael Jurkab3e22d92011-10-31 15:58:33 -07003059 void lockAllApps() {
3060 // TODO
3061 }
3062
3063 void unlockAllApps() {
3064 // TODO
3065 }
3066
Michael Jurkad7c28052012-04-27 15:43:36 -07003067 @Override
3068 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003069 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003070 final List<CharSequence> text = event.getText();
3071 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003072 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003073 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003074 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003075 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003076 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003077 } else if (mWorkspace != null) {
3078 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003079 } else {
3080 text.add(getString(R.string.all_apps_home_button_label));
3081 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003082 return result;
3083 }
3084
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003085 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003086 * If the activity is currently paused, signal that we need to run the passed Runnable
3087 * in onResume.
3088 *
3089 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003090 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3091 * wrong when we're not running, and if the activity comes back to what the configuration was
3092 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003093 *
3094 * Implementation of the method from LauncherModel.Callbacks.
3095 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003096 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003097 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003098 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003099 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003100 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003101 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003102 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003103 }
3104 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003105 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003106 return true;
3107 } else {
3108 return false;
3109 }
3110 }
3111
Michael Jurkac402cd92013-05-20 15:49:32 +02003112 private boolean waitUntilResume(Runnable run) {
3113 return waitUntilResume(run, false);
3114 }
3115
Michael Jurka1e2f4652013-07-08 18:03:46 -07003116 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003117 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003118 }
3119
Michael Jurka7607c2f2013-04-03 14:33:19 -07003120 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003121 * If the activity is currently paused, signal that we need to re-run the loader
3122 * in onResume.
3123 *
3124 * This needs to be called from incoming places where resources might have been loaded
3125 * while we are paused. That is becaues the Configuration might be wrong
3126 * when we're not running, and if it comes back to what it was when we
3127 * were paused, we are not restarted.
3128 *
3129 * Implementation of the method from LauncherModel.Callbacks.
3130 *
3131 * @return true if we are currently paused. The caller might be able to
3132 * skip some work in that case since we will come back again.
3133 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003134 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003135 public boolean setLoadOnResume() {
3136 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003137 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003138 mOnResumeNeedsLoad = true;
3139 return true;
3140 } else {
3141 return false;
3142 }
3143 }
3144
3145 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003148 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003149 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003150 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003151 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003152 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003153 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003156
Joe Onorato9c1289c2009-08-17 11:03:03 -04003157 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003158 * Clear any pending bind callbacks. This is called when is loader is planning to
3159 * perform a full rebind from scratch.
3160 */
3161 @Override
3162 public void clearPendingBinds() {
3163 mBindOnResumeCallbacks.clear();
3164 if (mPendingExecutor != null) {
3165 mPendingExecutor.markCompleted();
3166 mPendingExecutor = null;
3167 }
3168 }
3169
3170 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 * Refreshes the shortcuts shown on the workspace.
3172 *
3173 * Implementation of the method from LauncherModel.Callbacks.
3174 */
3175 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003176 if (LauncherAppState.PROFILE_STARTUP) {
3177 Trace.beginSection("Starting page bind");
3178 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003179 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003180
Winson Chungd64d1762013-08-20 14:37:16 -07003181 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003182 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003183 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003184
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 if (mHotseat != null) {
3186 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003188 if (LauncherAppState.PROFILE_STARTUP) {
3189 Trace.endSection();
3190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 }
3192
Adam Cohendcd297f2013-06-18 13:13:40 -07003193 @Override
3194 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003195 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003196 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3197 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003198 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3199 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3200 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003201 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3202 // If there are no screens, we need to have an empty screen
3203 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003204 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003205 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003206
3207 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003208 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003209 if (hasCustomContentToLeft()) {
3210 mWorkspace.createCustomContentContainer();
3211 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003212 }
Winsonc7d2e832016-07-28 12:24:55 -07003213
3214 // After we have added all the screens, if the wallpaper was locked to the default state,
3215 // then notify to indicate that it can be released and a proper wallpaper offset can be
3216 // computed before the next layout
3217 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003218 }
3219
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003220 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003221 int count = orderedScreenIds.size();
3222 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003223 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003224 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003225 // No need to bind the first screen, as its always bound.
3226 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3227 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003228 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003229 }
3230
Winson Chungd64d1762013-08-20 14:37:16 -07003231 public void bindAppsAdded(final ArrayList<Long> newScreens,
3232 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003233 final ArrayList<ItemInfo> addAnimated,
3234 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003235 Runnable r = new Runnable() {
3236 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003237 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003238 }
3239 };
3240 if (waitUntilResume(r)) {
3241 return;
3242 }
3243
Winson Chungd64d1762013-08-20 14:37:16 -07003244 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003245 if (newScreens != null) {
3246 bindAddScreens(newScreens);
3247 }
Winson Chungd64d1762013-08-20 14:37:16 -07003248
3249 // We add the items without animation on non-visible pages, and with
3250 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003251 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003252 bindItems(addNotAnimated, 0,
3253 addNotAnimated.size(), false);
3254 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003255 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003256 bindItems(addAnimated, 0,
3257 addAnimated.size(), true);
3258 }
Winson Chungc58497e2013-09-03 17:48:37 -07003259
Winson Chung87412982013-10-03 18:34:14 -07003260 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003261 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003262
Winson Chungb745afb2015-03-02 11:51:23 -08003263 if (addedApps != null && mAppsView != null) {
3264 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003265 }
Winson Chungd64d1762013-08-20 14:37:16 -07003266 }
3267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 * Bind the items start-end from the list.
3270 *
3271 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003272 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003273 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003274 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3275 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003276 Runnable r = new Runnable() {
3277 public void run() {
3278 bindItems(shortcuts, start, end, forceAnimateIcons);
3279 }
3280 };
3281 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003282 return;
3283 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003284
Winson Chungf0c6ae02012-03-21 16:10:31 -07003285 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003286 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3287 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003288 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003289 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003290 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003291 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003292 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003293
3294 // Short circuit if we are loading dock items for a configuration which has no dock
3295 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3296 mHotseat == null) {
3297 continue;
3298 }
3299
Sunny Goyal639e9062015-08-19 19:17:06 -07003300 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 switch (item.itemType) {
3302 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3303 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003304 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003305 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003306 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003308 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003309 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003310 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003311 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003313 default:
3314 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003316
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003317 /*
3318 * Remove colliding items.
3319 */
3320 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3321 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3322 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3323 View v = cl.getChildAt(item.cellX, item.cellY);
3324 Object tag = v.getTag();
3325 String desc = "Collision while binding workspace item: " + item
3326 + ". Collides with " + tag;
3327 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3328 throw (new RuntimeException(desc));
3329 } else {
3330 Log.d(TAG, desc);
3331 LauncherModel.deleteItemFromDatabase(this, item);
3332 continue;
3333 }
3334 }
3335 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003336 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3337 item.cellY, 1, 1);
3338 if (animateIcons) {
3339 // Animate all the applications up now
3340 view.setAlpha(0f);
3341 view.setScaleX(0f);
3342 view.setScaleY(0f);
3343 bounceAnims.add(createNewAppBounceAnimation(view, i));
3344 newShortcutsScreenId = item.screenId;
3345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003346 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003347
Winson Chung997a9232013-07-24 15:33:46 -07003348 if (animateIcons) {
3349 // Animate to the correct page
3350 if (newShortcutsScreenId > -1) {
3351 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003352 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003353 final Runnable startBounceAnimRunnable = new Runnable() {
3354 public void run() {
3355 anim.playTogether(bounceAnims);
3356 anim.start();
3357 }
3358 };
Winson Chung997a9232013-07-24 15:33:46 -07003359 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003360 // We post the animation slightly delayed to prevent slowdowns
3361 // when we are loading right after we return to launcher.
3362 mWorkspace.postDelayed(new Runnable() {
3363 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003364 if (mWorkspace != null) {
3365 mWorkspace.snapToPage(newScreenIndex);
3366 mWorkspace.postDelayed(startBounceAnimRunnable,
3367 NEW_APPS_ANIMATION_DELAY);
3368 }
Winson Chung94d67682013-09-25 16:29:40 -07003369 }
3370 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003371 } else {
3372 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003373 }
3374 }
Winson Chung64359a52013-07-08 17:17:08 -07003375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003376 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003377 }
3378
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003379 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3380 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3381 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003382 view.updateAppWidget(null);
3383 view.setOnClickListener(this);
3384 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003385 mWorkspace.requestLayout();
3386 }
3387
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 /**
3389 * Add the views for a widget to the workspace.
3390 *
3391 * Implementation of the method from LauncherModel.Callbacks.
3392 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003393 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003394 Runnable r = new Runnable() {
3395 public void run() {
3396 bindAppWidget(item);
3397 }
3398 };
3399 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003400 return;
3401 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003402
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003403 if (mIsSafeModeEnabled) {
3404 bindSafeModeWidget(item);
3405 return;
3406 }
3407
Daniel Sandler843e8602010-06-07 14:59:01 -04003408 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3409 if (DEBUG_WIDGETS) {
3410 Log.d(TAG, "bindAppWidget: " + item);
3411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003413 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003414
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003415 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3416 // If the provider is not ready, bind as a pending widget.
3417 appWidgetInfo = null;
3418 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3419 // The widget id is not valid. Try to find the widget based on the provider info.
3420 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3421 } else {
3422 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3423 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003424
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003425 // If the provider is ready, but the width is not yet restored, try to restore it.
3426 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3427 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003428 if (appWidgetInfo == null) {
3429 if (DEBUG_WIDGETS) {
3430 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3431 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003432 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003433 }
3434 LauncherModel.deleteItemFromDatabase(this, item);
3435 return;
3436 }
Sunny Goyalff572272014-07-23 13:58:07 -07003437
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003438 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003439 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003440 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3441 // Id has not been allocated yet. Allocate a new id.
3442 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3443 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003444
Sunny Goyald478c832016-04-01 12:04:16 -07003445 // Also try to bind the widget. If the bind fails, the user will be shown
3446 // a click to setup UI, which will ask for the bind permission.
3447 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3448 pendingInfo.spanX = item.spanX;
3449 pendingInfo.spanY = item.spanY;
3450 pendingInfo.minSpanX = item.minSpanX;
3451 pendingInfo.minSpanY = item.minSpanY;
3452 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003453
3454 boolean isDirectConfig =
3455 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3456 if (isDirectConfig && item.bindOptions != null) {
3457 Bundle newOptions = item.bindOptions.getExtras();
3458 if (options != null) {
3459 newOptions.putAll(options);
3460 }
3461 options = newOptions;
3462 }
Sunny Goyald478c832016-04-01 12:04:16 -07003463 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3464 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003465
Sunny Goyal86df1382016-08-10 15:03:22 -07003466 // We tried to bind once. If we were not able to bind, we would need to
3467 // go through the permission dialog, which means we cannot skip the config
3468 // activity.
3469 item.bindOptions = null;
3470 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3471
Sunny Goyald478c832016-04-01 12:04:16 -07003472 // Bind succeeded
3473 if (success) {
3474 // If the widget has a configure activity, it is still needs to set it up,
3475 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003476 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003477 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3478 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003479 }
Sunny Goyald478c832016-04-01 12:04:16 -07003480
3481 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003482 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003483 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003484 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003485 // The widget was marked as UI not ready, but there is no configure activity to
3486 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003487 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3488 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003489 }
Sunny Goyalff572272014-07-23 13:58:07 -07003490 }
3491
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003492 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003493 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003494 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3495 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003496 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003497
Sunny Goyal56c73602015-09-25 12:55:01 -07003498 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003499 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003500 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003501 deleteWidgetInfo(item);
3502 return;
3503 }
3504
Sunny Goyal233ee962015-08-03 13:05:01 -07003505 item.minSpanX = appWidgetInfo.minSpanX;
3506 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003507 addAppWidgetToWorkspace(
3508 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3509 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003510 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003511 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003512 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003513 view.updateAppWidget(null);
3514 view.setOnClickListener(this);
3515 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003516 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003517 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518
Daniel Sandler843e8602010-06-07 14:59:01 -04003519 if (DEBUG_WIDGETS) {
3520 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3521 + (SystemClock.uptimeMillis()-start) + "ms");
3522 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 }
3524
Sunny Goyalff572272014-07-23 13:58:07 -07003525 /**
3526 * Restores a pending widget.
3527 *
3528 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003529 */
Sunny Goyald478c832016-04-01 12:04:16 -07003530 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003531 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3532 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3533 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003534 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003535 }
3536
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003537 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003538 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003539
3540 mWorkspace.reinflateWidgetsIfNecessary();
3541 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003542 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003543 }
3544
Adam Cohen1462de32012-07-24 22:34:36 -07003545 public void onPageBoundSynchronously(int page) {
3546 mSynchronouslyBoundPages.add(page);
3547 }
3548
Sunny Goyal527c7d32015-08-28 15:19:36 -07003549 @Override
3550 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3551 if (mPendingExecutor != null) {
3552 mPendingExecutor.markCompleted();
3553 }
3554 mPendingExecutor = executor;
3555 executor.attachTo(this);
3556 }
3557
3558 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3559 if (mPendingExecutor == executor) {
3560 mPendingExecutor = null;
3561 }
3562 }
3563
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003564 @Override
3565 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3566 Runnable r = new Runnable() {
3567 public void run() {
3568 finishFirstPageBind(executor);
3569 }
3570 };
3571 if (waitUntilResume(r)) {
3572 return;
3573 }
3574
3575 Runnable onComplete = new Runnable() {
3576 @Override
3577 public void run() {
3578 if (executor != null) {
3579 executor.onLoadAnimationCompleted();
3580 }
3581 }
3582 };
3583 if (mDragLayer.getAlpha() < 1) {
3584 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3585 } else {
3586 onComplete.run();
3587 }
3588 }
3589
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 /**
3591 * Callback saying that there aren't any more items to bind.
3592 *
3593 * Implementation of the method from LauncherModel.Callbacks.
3594 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003595 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003596 Runnable r = new Runnable() {
3597 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003598 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003599 }
3600 };
3601 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003602 return;
3603 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003604 if (LauncherAppState.PROFILE_STARTUP) {
3605 Trace.beginSection("Page bind completed");
3606 }
Adam Cohen1462de32012-07-24 22:34:36 -07003607 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003609 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003610
Sunny Goyal2100c782016-08-22 16:00:03 -07003611 if (mPendingActivityResult != null) {
3612 handleActivityResult(mPendingActivityResult.requestCode,
3613 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3614 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003615 }
3616
Sunny Goyal756adbc2015-04-16 15:20:51 -07003617 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003618
3619 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003620 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003621 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003622 if (LauncherAppState.PROFILE_STARTUP) {
3623 Trace.endSection();
3624 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003625 }
3626
Winson Chunga2413752012-04-03 14:22:34 -07003627 private boolean canRunNewAppsAnimation() {
3628 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003629 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003630 }
3631
Winson Chungc9168342013-06-26 14:54:55 -07003632 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003633 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003634 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3635 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003636 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003637 return bounceAnim;
3638 }
3639
Adam Cohen27772732013-11-11 14:00:56 +00003640 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003641 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003642 }
3643
Tony Wickham55616cd2015-09-23 14:55:17 -07003644 public int getSearchBarHeight() {
3645 if (mLauncherCallbacks != null) {
3646 return mLauncherCallbacks.getSearchBarHeight();
3647 }
3648 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3649 }
3650
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003651 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003652 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3653 * multiple calls to bind the same list.)
3654 */
3655 @Thunk ArrayList<AppInfo> mTmpAppsList;
3656 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3657 public void run() {
3658 bindAllApplications(mTmpAppsList);
3659 mTmpAppsList = null;
3660 }
3661 };
3662
3663 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 * Add the icons for all apps.
3665 *
3666 * Implementation of the method from LauncherModel.Callbacks.
3667 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003668 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003669 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3670 mTmpAppsList = apps;
3671 return;
3672 }
3673
Winson Chungb745afb2015-03-02 11:51:23 -08003674 if (mAppsView != null) {
3675 mAppsView.setApps(apps);
3676 }
Adam Cohen9211d422014-10-07 18:14:53 -07003677 if (mLauncherCallbacks != null) {
3678 mLauncherCallbacks.bindAllApplications(apps);
3679 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 }
3681
3682 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003683 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3684 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3685 */
3686 @Override
3687 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
3688 mDeepShortcutMap = deepShortcutMapCopy;
3689 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
3690 }
3691
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003692 public List<String> getShortcutIdsForItem(ItemInfo info) {
3693 if (!DeepShortcutManager.supportsShortcuts(info)) {
3694 return Collections.EMPTY_LIST;
3695 }
3696 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07003697 if (component == null) {
3698 return Collections.EMPTY_LIST;
3699 }
3700
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003701 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
3702 return ids == null ? Collections.EMPTY_LIST : ids;
3703 }
3704
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003705 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 * A package was updated.
3707 *
3708 * Implementation of the method from LauncherModel.Callbacks.
3709 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003710 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003711 Runnable r = new Runnable() {
3712 public void run() {
3713 bindAppsUpdated(apps);
3714 }
3715 };
3716 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003717 return;
3718 }
3719
Winson Chungb745afb2015-03-02 11:51:23 -08003720 if (mAppsView != null) {
3721 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003722 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 }
3724
Sunny Goyal4390ace2014-10-13 11:33:11 -07003725 @Override
3726 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3727 Runnable r = new Runnable() {
3728 public void run() {
3729 bindWidgetsRestored(widgets);
3730 }
3731 };
3732 if (waitUntilResume(r)) {
3733 return;
3734 }
3735 mWorkspace.widgetsRestored(widgets);
3736 }
3737
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003739 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003740 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003741 *
3742 * @param updated list of shortcuts which have changed.
3743 * @param removed list of shortcuts which were deleted in the background. This can happen when
3744 * an app gets removed from the system or some of its components are no longer
3745 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003746 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003747 @Override
3748 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
3749 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003750 Runnable r = new Runnable() {
3751 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003752 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003753 }
3754 };
3755 if (waitUntilResume(r)) {
3756 return;
3757 }
3758
Sunny Goyal4390ace2014-10-13 11:33:11 -07003759 if (!updated.isEmpty()) {
3760 mWorkspace.updateShortcuts(updated);
3761 }
3762
3763 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003764 HashSet<ComponentName> removedComponents = new HashSet<>();
3765 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3766
Sunny Goyal4390ace2014-10-13 11:33:11 -07003767 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003768 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003769 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003770 } else {
3771 removedComponents.add(si.getTargetComponent());
3772 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003773 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003774
3775 if (!removedComponents.isEmpty()) {
3776 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3777 mWorkspace.removeItemsByMatcher(matcher);
3778 mDragController.onAppsRemoved(matcher);
3779 }
3780
3781 if (!removedDeepShortcuts.isEmpty()) {
3782 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3783 mWorkspace.removeItemsByMatcher(matcher);
3784 mDragController.onAppsRemoved(matcher);
3785 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003786 }
3787 }
3788
3789 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003790 * Update the state of a package, typically related to install state.
3791 *
3792 * Implementation of the method from LauncherModel.Callbacks.
3793 */
Sunny Goyale755d462014-07-22 13:48:29 -07003794 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003795 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3796 Runnable r = new Runnable() {
3797 public void run() {
3798 bindRestoreItemsChange(updates);
3799 }
3800 };
3801 if (waitUntilResume(r)) {
3802 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003803 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003804
Sunny Goyal756adbc2015-04-16 15:20:51 -07003805 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003806 }
3807
3808 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003809 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003810 * in addition to specific applications to remove, the reason being that
3811 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003812 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003813 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003815 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003816 public void bindWorkspaceComponentsRemoved(
3817 final HashSet<String> packageNames, final HashSet<ComponentName> components,
3818 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003819 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003820 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003821 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003822 }
Winson Chungd64d1762013-08-20 14:37:16 -07003823 };
3824 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003825 return;
3826 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003827 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003828 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3829 mWorkspace.removeItemsByMatcher(matcher);
3830 mDragController.onAppsRemoved(matcher);
3831
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003832 }
3833 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003834 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3835 mWorkspace.removeItemsByMatcher(matcher);
3836 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003837 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003838 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003839
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003840 @Override
3841 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3842 Runnable r = new Runnable() {
3843 public void run() {
3844 bindAppInfosRemoved(appInfos);
3845 }
3846 };
3847 if (waitUntilResume(r)) {
3848 return;
Joe Onorato36115782010-06-17 13:28:48 -04003849 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003850
Winson Chungdf95eb12013-10-16 14:57:07 -07003851 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003852 if (mAppsView != null) {
3853 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855 }
Joe Onoratobe386092009-11-17 17:32:16 -08003856
Sunny Goyald164b7f2016-10-12 20:49:31 -07003857 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003858 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003859 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003860 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003861 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003862
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003863 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003864 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3865 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3866 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003867 return;
3868 }
3869
Sunny Goyald164b7f2016-10-12 20:49:31 -07003870 if (mWidgetsView != null && allWidgets != null) {
3871 mWidgetsView.setWidgets(allWidgets);
3872 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003873 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003874 }
3875
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003876 @Override
3877 public void notifyWidgetProvidersChanged() {
3878 if (mWorkspace.getState().shouldUpdateWidget) {
3879 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3880 }
3881 }
3882
Winson Chung400438b2011-01-16 17:53:48 -08003883 private int mapConfigurationOriActivityInfoOri(int configOri) {
3884 final Display d = getWindowManager().getDefaultDisplay();
3885 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3886 switch (d.getRotation()) {
3887 case Surface.ROTATION_0:
3888 case Surface.ROTATION_180:
3889 // We are currently in the same basic orientation as the natural orientation
3890 naturalOri = configOri;
3891 break;
3892 case Surface.ROTATION_90:
3893 case Surface.ROTATION_270:
3894 // We are currently in the other basic orientation to the natural orientation
3895 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3896 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3897 break;
3898 }
3899
3900 int[] oriMap = {
3901 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3902 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3903 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3904 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3905 };
3906 // Since the map starts at portrait, we need to offset if this device's natural orientation
3907 // is landscape.
3908 int indexOffset = 0;
3909 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3910 indexOffset = 1;
3911 }
3912 return oriMap[(d.getRotation() + indexOffset) % 4];
3913 }
Adam Cohen446e9402011-09-15 18:21:21 -07003914
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003915 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07003916 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003917 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003918 if (Utilities.ATLEAST_JB_MR2) {
3919 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
3920 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003921 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3922 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003923 }
Winson Chung4b919f82012-05-01 10:44:08 -07003924 }
3925 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003926
Winson Chung4b919f82012-05-01 10:44:08 -07003927 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003928 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003929 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003930 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003931 } else {
3932 mHandler.postDelayed(new Runnable() {
3933 public void run() {
3934 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3935 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003936 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003937 }
Winson Chung4b919f82012-05-01 10:44:08 -07003938 }
Winson Chung400438b2011-01-16 17:53:48 -08003939 }
3940
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003941 private void markAppsViewShown() {
3942 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3943 return;
3944 }
3945 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3946 }
3947
3948 private boolean shouldShowDiscoveryBounce() {
3949 if (mState != mState.WORKSPACE) {
3950 return false;
3951 }
3952 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3953 return true;
3954 }
3955 if (!mIsResumeFromActionScreenOff) {
3956 return false;
3957 }
3958 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3959 return false;
3960 }
3961 return true;
3962 }
3963
Winson Chung5ffd51d2015-06-25 11:36:50 -07003964 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003965 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00003966 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003967 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003968 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003969 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003970 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
3971 if (userHandle != null) {
3972 user = UserHandleCompat.fromUser(userHandle);
3973 }
3974 }
3975 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07003976 }
3977
3978 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003979 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003980 if (user == null) {
3981 user = UserHandleCompat.myUserHandle();
3982 }
3983
3984 // Called from search suggestion, add the profile extra to the intent to ensure that we
3985 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01003986 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003987 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01003988 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00003989 return null;
3990 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07003991 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00003992 }
3993
Winson Chung5ffd51d2015-06-25 11:36:50 -07003994 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00003995 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
3996 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003997 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01003998 UserHandleCompat.myUserHandle());
3999 }
4000
Winson Chung5ffd51d2015-06-25 11:36:50 -07004001 protected void moveWorkspaceToDefaultScreen() {
4002 mWorkspace.moveToDefaultScreen(false);
4003 }
4004
Winson Chung80baf5a2010-08-09 16:03:15 -07004005 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004006 * Returns a FastBitmapDrawable with the icon, accurately sized.
4007 */
4008 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4009 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4010 d.setFilterBitmap(true);
4011 resizeIconDrawable(d);
4012 return d;
4013 }
4014
4015 /**
4016 * Resizes an icon drawable to the correct icon size.
4017 */
Winson5fbe0742015-09-30 15:33:00 -07004018 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004019 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004020 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004021 }
4022
4023 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004024 * Prints out out state for debugging.
4025 */
4026 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004027 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004028 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004029 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4030 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004031 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004032 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004033
Daniel Sandler325dc232013-06-05 22:57:57 -04004034 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004035 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004036
4037 @Override
4038 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4039 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004040 // Dump workspace
4041 writer.println(prefix + "Workspace Items");
4042 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4043 writer.println(prefix + " Homescreen " + i);
4044
4045 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4046 for (int j = 0; j < layout.getChildCount(); j++) {
4047 Object tag = layout.getChildAt(j).getTag();
4048 if (tag != null) {
4049 writer.println(prefix + " " + tag.toString());
4050 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004051 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004052 }
Sunny Goyala1365452015-10-01 15:46:24 -07004053
4054 writer.println(prefix + " Hotseat");
4055 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4056 for (int j = 0; j < layout.getChildCount(); j++) {
4057 Object tag = layout.getChildAt(j).getTag();
4058 if (tag != null) {
4059 writer.println(prefix + " " + tag.toString());
4060 }
4061 }
4062
Sunny Goyal713edfc2016-05-06 09:58:34 -07004063 try {
4064 FileLog.flushAll(writer);
4065 } catch (Exception e) {
4066 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004067 }
4068
Adam Cohen9211d422014-10-07 18:14:53 -07004069 if (mLauncherCallbacks != null) {
4070 mLauncherCallbacks.dump(prefix, fd, writer, args);
4071 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004072 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004073
Sunny Goyal66b24572016-09-21 15:57:55 -07004074 @Override
4075 @TargetApi(Build.VERSION_CODES.N)
4076 public void onProvideKeyboardShortcuts(
4077 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4078
4079 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4080 if (mState == State.WORKSPACE) {
4081 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4082 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4083 }
4084 View currentFocus = getCurrentFocus();
4085 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4086 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4087 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4088 }
4089 if (currentFocus instanceof BubbleTextView &&
4090 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4091 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4092 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4093 }
4094 if (!shortcutInfos.isEmpty()) {
4095 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4096 }
4097
4098 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4099 }
4100
4101 @Override
4102 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4103 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4104 switch (keyCode) {
4105 case KeyEvent.KEYCODE_A:
4106 if (mState == State.WORKSPACE) {
4107 showAppsView(true, true, false);
4108 return true;
4109 }
4110 break;
4111 case KeyEvent.KEYCODE_S: {
4112 View focusedView = getCurrentFocus();
4113 if (focusedView instanceof BubbleTextView
4114 && focusedView.getTag() instanceof ItemInfo
4115 && mAccessibilityDelegate.performAction(focusedView,
4116 (ItemInfo) focusedView.getTag(),
4117 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07004118 DeepShortcutsContainer.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004119 return true;
4120 }
4121 break;
4122 }
4123 case KeyEvent.KEYCODE_O:
4124 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4125 return true;
4126 }
4127 break;
4128 }
4129 }
4130 return super.onKeyShortcut(keyCode, event);
4131 }
4132
Adam Cohen59400422014-03-05 18:07:04 -08004133 public static CustomAppWidget getCustomAppWidget(String name) {
4134 return sCustomAppWidgets.get(name);
4135 }
4136
4137 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4138 return sCustomAppWidgets;
4139 }
4140
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004141 public static Launcher getLauncher(Context context) {
4142 if (context instanceof Launcher) {
4143 return (Launcher) context;
4144 }
4145 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4146 }
4147
Sunny Goyal745bad92016-05-02 10:54:12 -07004148 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4149
4150 @Override
4151 public void onSharedPreferenceChanged(
4152 SharedPreferences sharedPreferences, String key) {
4153 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4154 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4155 if (!waitUntilResume(this, true)) {
4156 run();
4157 }
4158 }
4159 }
4160
4161 @Override
4162 public void run() {
4163 setOrientation();
4164 }
4165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004166}