blob: 0540249b6954a99f616eb1186bb85d4c00de1b1f [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;
Jon Miranda379198e2016-09-23 08:54:40 -0700319
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700320 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700321 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400322
323 @Thunk void setOrientation() {
324 if (mRotationEnabled) {
325 unlockScreenOrientation(true);
326 } else {
327 setRequestedOrientation(
328 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700329 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400330 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700331
Sunny Goyal745bad92016-05-02 10:54:12 -0700332 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700336 if (DEBUG_STRICT_MODE) {
337 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
338 .detectDiskReads()
339 .detectDiskWrites()
340 .detectNetwork() // or .detectAll() for all detectable problems
341 .penaltyLog()
342 .build());
343 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
344 .detectLeakedSqlLiteObjects()
345 .detectLeakedClosableObjects()
346 .penaltyLog()
347 .penaltyDeath()
348 .build());
349 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700350 if (LauncherAppState.PROFILE_STARTUP) {
351 Trace.beginSection("Launcher-onCreate");
352 }
Winson Chunga2413752012-04-03 14:22:34 -0700353
Adam Cohen9211d422014-10-07 18:14:53 -0700354 if (mLauncherCallbacks != null) {
355 mLauncherCallbacks.preOnCreate();
356 }
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400359
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400360 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700361
Adam Cohen2e6da152015-05-06 11:42:25 -0700362 // Load configuration-specific DeviceProfile
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700363 mDeviceProfile =
364 getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
365 ? app.getInvariantDeviceProfile().landscapeProfile
366 : app.getInvariantDeviceProfile().portraitProfile;
367
Sunny Goyalf5e37442016-11-02 10:31:24 -0700368 if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700369 Display display = getWindowManager().getDefaultDisplay();
370 Point mwSize = new Point();
371 display.getSize(mwSize);
372 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
373 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700374
Sunny Goyalf7258242015-10-19 16:59:07 -0700375 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700376 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800377 mModel = app.setLauncher(this);
378 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700379 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700380
Joe Onorato41a12d22009-10-31 18:30:00 -0400381 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700382 mAllAppsController = new AllAppsTransitionController(this);
383 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Sunny Goyalffe83f12014-08-14 17:39:34 -0700385 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700386
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700387 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
388 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700389
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700390 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
391 // this also ensures that any synchronous binding below doesn't re-trigger another
392 // LauncherModel load.
393 mPaused = false;
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 setupViews();
Winson1f064272016-07-18 17:18:02 -0700398 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700399 mExtractedColors = new ExtractedColors();
400 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Tony Wickhame2217252016-03-22 16:34:23 -0700402 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
403 .addAccessibilityStateChangeListener(this);
404
Joe Onorato7c312c12009-08-13 21:36:53 -0700405 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700406
Sunny Goyalfe770c92016-09-27 14:38:58 -0700407 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408
Sunny Goyale26d1002016-06-20 14:52:14 -0700409 if (LauncherAppState.PROFILE_STARTUP) {
410 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
Sunny Goyal2100c782016-08-22 16:00:03 -0700413 // We only load the page synchronously if the user rotates (or triggers a
414 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700415 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
416 if (savedInstanceState != null) {
417 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
418 }
419 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700420 // If we are not binding synchronously, show a fade in animation when
421 // the first page bind completes.
422 mDragLayer.setAlpha(0);
423 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700424 // Pages bound synchronously.
425 mWorkspace.setCurrentPage(currentScreen);
426
Sunny Goyal2100c782016-08-22 16:00:03 -0700427 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
429
430 // For handling default keys
431 mDefaultKeySsb = new SpannableStringBuilder();
432 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800433
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800434 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700435 // In case we are on a device with locked rotation, we should look at preferences to check
436 // if the user has specifically allowed rotation.
437 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700438 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700439 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
440 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700441 }
442
443 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
444 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400445 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700446
Adam Cohen9211d422014-10-07 18:14:53 -0700447 if (mLauncherCallbacks != null) {
448 mLauncherCallbacks.onCreate(savedInstanceState);
449 }
450 }
451
Sunny Goyal7779d622015-06-11 16:18:39 -0700452 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700453 public void onExtractedColorsChanged() {
454 loadExtractedColorsAndColorItems();
455 }
456
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700457 @Override
458 public void onAppWidgetHostReset() {
459 if (mAppWidgetHost != null) {
460 mAppWidgetHost.startListening();
461 }
462 }
463
Tony Wickham827cef22016-03-17 15:39:39 -0700464 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700465 // TODO: do this in pre-N as well, once the extraction part is complete.
Sunny Goyalf5e37442016-11-02 10:31:24 -0700466 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700467 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700468 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700469 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700470 // It's possible that All Apps is visible when this is run,
471 // so always use light status bar in that case.
472 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700473 }
474 }
475
Tony Wickham345bff32016-09-28 15:34:51 -0700476 /**
477 * Sets the status bar to be light or not. Light status bar means dark icons.
478 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
479 */
480 public void activateLightStatusBar(boolean activate) {
Tony Wickham0fed55b2016-10-12 14:55:46 -0700481 boolean lightStatusBar = activate || (FeatureFlags.LIGHT_STATUS_BAR
482 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
483 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700484 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
485 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700486 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700487 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700488 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700489 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700490 }
Tony Wickham345bff32016-09-28 15:34:51 -0700491 if (newSystemUiFlags != oldSystemUiFlags) {
492 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
493 }
Tony Wickham93113872016-09-14 16:46:32 -0700494 }
495
Adam Cohen9211d422014-10-07 18:14:53 -0700496 private LauncherCallbacks mLauncherCallbacks;
497
498 public void onPostCreate(Bundle savedInstanceState) {
499 super.onPostCreate(savedInstanceState);
500 if (mLauncherCallbacks != null) {
501 mLauncherCallbacks.onPostCreate(savedInstanceState);
502 }
503 }
504
Winson1f064272016-07-18 17:18:02 -0700505 public void onInsetsChanged(Rect insets) {
506 mDeviceProfile.updateInsets(insets);
507 mDeviceProfile.layout(this, true /* notifyListeners */);
508 }
509
Sunny Goyal32554d12015-12-03 15:31:25 -0800510 /**
511 * Call this after onCreate to set or clear overlay.
512 */
513 public void setLauncherOverlay(LauncherOverlay overlay) {
514 if (overlay != null) {
515 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
516 }
517 mWorkspace.setLauncherOverlay(overlay);
518 }
519
Adam Cohen9211d422014-10-07 18:14:53 -0700520 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
521 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700522 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700523 private boolean mWorkspaceImportanceStored = false;
524 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700525 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800526 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700527 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
528
529 @Override
530 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700531 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700532 return;
533 }
534 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700535 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 if (mWorkspace != null) {
537 mWorkspaceImportanceForAccessibility =
538 mWorkspace.getImportantForAccessibility();
539 mWorkspace.setImportantForAccessibility(
540 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
541 mWorkspaceImportanceStored = true;
542 }
543 if (mHotseat != null) {
544 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
545 mHotseat.setImportantForAccessibility(
546 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
547 mHotseatImportanceStored = true;
548 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 }
550
551 @Override
552 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700553 if (mWorkspaceImportanceStored && mWorkspace != null) {
554 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
555 }
556 if (mHotseatImportanceStored && mHotseat != null) {
557 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
558 }
559 mWorkspaceImportanceStored = false;
560 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700561 }
562 });
Adam Cohen9211d422014-10-07 18:14:53 -0700563 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700564 }
565
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700566 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700567 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700568 if (mLauncherCallbacks != null) {
569 mLauncherCallbacks.onLauncherProviderChange();
570 }
571 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700572
Adam Cohen9211d422014-10-07 18:14:53 -0700573 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700574 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700575 if (mLauncherCallbacks != null) {
576 return mLauncherCallbacks.hasCustomContentToLeft();
577 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700578 return false;
579 }
580
Dave Hawkeya8881582013-09-17 15:55:33 -0600581 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500582 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600583 * {@link #addToCustomContentPage}. This will only be invoked if
584 * {@link #hasCustomContentToLeft()} is {@code true}.
585 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500586 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700587 if (mLauncherCallbacks != null) {
588 mLauncherCallbacks.populateCustomContentContainer();
589 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600590 }
591
592 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700593 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 * ensure the custom content page is added or removed if necessary.
595 */
596 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600597 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 // Not bound yet, wait for bindScreens to be called.
599 return;
600 }
601
602 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
603 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500604 mWorkspace.createCustomContentContainer();
605 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
607 mWorkspace.removeCustomContentPage();
608 }
609 }
610
Hyunyoung Songaa953652016-04-19 18:30:24 -0700611 public UserEventDispatcher getUserEventDispatcher() {
612 if (mLauncherCallbacks != null) {
613 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
614 if (dispatcher != null) {
615 return dispatcher;
616 }
617 }
618
Sunny Goyal64976d52016-06-20 14:56:28 -0700619 // Logger object is a singleton and does not have to be coupled with the foreground
620 // activity. Since most user event logging is done on the UI, the object is retrieved
621 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700622 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700623 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700624 }
625 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800626 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100627
Hyunyoung Song3f471442015-04-08 19:01:34 -0700628 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700629 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
630 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700631 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700632 }
633
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000634 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700635 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
636 // This cast is safe as long as the id < 0x00FFFFFF
637 // Since we jail all the dynamically generated views, there should be no clashes
638 // with any other views.
639 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000640 }
641
642 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700643 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
644 * a configuration step, this allows the proper animations to run after other transitions.
645 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700646 private long completeAdd(
647 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
648 long screenId = info.screenId;
649 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700650 // When the screen id represents an actual screen (as opposed to a rank) we make sure
651 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700652 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700653 }
Adam Cohendb364c32014-05-20 14:23:40 -0700654
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800656 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700657 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700658 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700660 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700662 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700664 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700665 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 int widgetId = appWidgetId;
667 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700668 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700670 // Since the view was just bound, also launch the configure activity if needed
671 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
672 .getLauncherAppWidgetInfo(widgetId);
673 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700675 }
676 }
677 break;
678 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700680
Adam Cohendb364c32014-05-20 14:23:40 -0700681 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800682 }
683
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800684 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700685 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700686 if (isWorkspaceLoading()) {
687 // process the result once the workspace has loaded.
688 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
689 return;
690 }
691 mPendingActivityResult = null;
692
Winson Chunge341d302013-08-16 14:31:00 -0700693 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700694 final PendingRequestArgs requestArgs = mPendingRequestArgs;
695 setWaitingForResult(null);
696 if (requestArgs == null) {
697 return;
698 }
699
700 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700701
Adam Cohenad4e15c2013-10-17 16:21:35 -0700702 Runnable exitSpringLoaded = new Runnable() {
703 @Override
704 public void run() {
705 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
706 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
707 }
708 };
709
Michael Jurka8b805b12012-04-18 14:23:14 -0700710 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700711 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
714 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700715 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700716 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700717 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700719 addAppWidgetImpl(
720 appWidgetId, requestArgs, null,
721 requestArgs.getWidgetProvider(),
722 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 }
724 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200725 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
726 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700727 // User could have free-scrolled between pages before picking a wallpaper; make sure
728 // we move to the closest one now.
729 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700730 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200731 }
732 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700736 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700737
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 // We have special handling for widgets
739 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800740 final int appWidgetId;
741 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
742 : -1;
743 if (widgetId < 0) {
744 appWidgetId = pendingAddWidgetId;
745 } else {
746 appWidgetId = widgetId;
747 }
748
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800750 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700751 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
752 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700754 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700755 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 @Override
757 public void run() {
758 exitSpringLoadedDragModeDelayed(false, 0, null);
759 }
760 };
Adam Cohendb364c32014-05-20 14:23:40 -0700761
Sunny Goyal2100c782016-08-22 16:00:03 -0700762 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
763 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
764 } else {
765 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
766 // When the screen id represents an actual screen (as opposed to a rank)
767 // we make sure that the drop page actually exists.
768 requestArgs.screenId =
769 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700770 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700771 final CellLayout dropLayout =
772 mWorkspace.getScreenWithId(requestArgs.screenId);
773
774 dropLayout.setDropPending(true);
775 final Runnable onComplete = new Runnable() {
776 @Override
777 public void run() {
778 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
779 dropLayout.setDropPending(false);
780 }
781 };
782 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700784 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 return;
786 }
787
Sunny Goyald478c832016-04-01 12:04:16 -0700788 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
789 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700790 if (resultCode == RESULT_OK) {
791 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700792 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700793 }
794 // Leave the widget in the pending state if the user canceled the configure.
795 return;
796 }
797
Sunny Goyalaad90582015-07-09 14:22:50 -0700798 if (requestCode == REQUEST_CREATE_SHORTCUT) {
799 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700800 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
801 completeAdd(requestCode, data, -1, requestArgs);
802 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
803 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
804
Sunny Goyalaad90582015-07-09 14:22:50 -0700805 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700806 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800811 }
812
813 @Override
814 protected void onActivityResult(
815 final int requestCode, final int resultCode, final Intent data) {
816 handleActivityResult(requestCode, resultCode, data);
817 if (mLauncherCallbacks != null) {
818 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
819 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 }
821
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600822 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700823 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600824 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700825 PendingRequestArgs pendingArgs = mPendingRequestArgs;
826 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
827 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
828 setWaitingForResult(null);
829
Sunny Goyal28c6b962015-10-12 11:42:05 -0700830 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700831 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700832 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700833 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700834 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700835 Intent intent = pendingArgs.getPendingIntent();
836
Sunny Goyal28c6b962015-10-12 11:42:05 -0700837 if (grantResults.length > 0
838 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700839 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700840 } else {
841 // TODO: Show a snack bar with link to settings
842 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700843 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700844 }
845 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600846 if (mLauncherCallbacks != null) {
847 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
848 grantResults);
849 }
850 }
851
Adam Cohendb364c32014-05-20 14:23:40 -0700852 /**
853 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
854 *
855 * @param screenId the screen id to check
856 * @return the new screen, or screenId if it exists
857 */
858 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800859 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700860 if (dropLayout == null) {
861 // it's possible that the add screen was removed because it was
862 // empty and a re-bind occurred
863 mWorkspace.addExtraEmptyScreen();
864 return mWorkspace.commitExtraEmptyScreen();
865 } else {
866 return screenId;
867 }
868 }
869
Sunny Goyal2100c782016-08-22 16:00:03 -0700870 @Thunk void completeTwoStageWidgetDrop(
871 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
872 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 Runnable onCompleteRunnable = null;
874 int animationType = 0;
875
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700876 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 if (resultCode == RESULT_OK) {
878 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
879 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700880 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700881 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800882 onCompleteRunnable = new Runnable() {
883 @Override
884 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700885 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700886 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
887 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 }
889 };
890 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800891 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700895 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
897 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700898 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899 // The animated view may be null in the case of a rotation during widget configuration
900 onCompleteRunnable.run();
901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
903
904 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700905 protected void onStop() {
906 super.onStop();
907 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700908
909 if (mLauncherCallbacks != null) {
910 mLauncherCallbacks.onStop();
911 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700912
Sunny Goyalf5e37442016-11-02 10:31:24 -0700913 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700914 mAppWidgetHost.stopListening();
915 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700916 }
917
918 @Override
919 protected void onStart() {
920 super.onStart();
921 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700922
923 if (mLauncherCallbacks != null) {
924 mLauncherCallbacks.onStart();
925 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700926
Sunny Goyalf5e37442016-11-02 10:31:24 -0700927 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal5da78f42016-06-17 14:00:22 -0700928 mAppWidgetHost.startListening();
929 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700930 }
931
932 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200934 long startTime = 0;
935 if (DEBUG_RESUME_TIME) {
936 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400937 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200938 }
Adam Cohen9211d422014-10-07 18:14:53 -0700939
940 if (mLauncherCallbacks != null) {
941 mLauncherCallbacks.preOnResume();
942 }
943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700945 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700946
Winson Chung4a2afa32012-07-19 14:53:05 -0700947 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700948 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700949 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800950 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700951 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700952 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700953 // view after launching an app, as they may be depending on the UI to be static to
954 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700955 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700956 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800957 } else if (mOnResumeState == State.WIDGETS) {
958 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700959 }
960 mOnResumeState = State.NONE;
961
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800962 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700963 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700964 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700965 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700966 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700968 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200969 // We might have postponed some bind calls until onResume (see waitUntilResume) --
970 // execute them here
971 long startTimeCallbacks = 0;
972 if (DEBUG_RESUME_TIME) {
973 startTimeCallbacks = System.currentTimeMillis();
974 }
975
Michael Jurka1e2f4652013-07-08 18:03:46 -0700976 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
977 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200978 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700979 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200980 if (DEBUG_RESUME_TIME) {
981 Log.d(TAG, "Time spent processing callbacks in onResume: " +
982 (System.currentTimeMillis() - startTimeCallbacks));
983 }
Michael Jurka447bf842013-05-15 14:52:15 +0200984 }
Michael Jurka54554252013-08-01 12:52:23 +0200985 if (mOnResumeCallbacks.size() > 0) {
986 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
987 mOnResumeCallbacks.get(i).run();
988 }
989 mOnResumeCallbacks.clear();
990 }
Winson Chunge4e50662012-01-23 14:45:13 -0800991
992 // Reset the pressed state of icons that were locked in the press state while activities
993 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800994 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800995 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800996 mWaitingForResume.setStayPressed(false);
997 }
Winson Chung82963d52013-10-09 11:20:57 -0700998
Adam Cohen06dff352012-06-01 17:17:08 -0700999 // It is possible that widgets can receive updates while launcher is not in the foreground.
1000 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1001 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1002 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001003 if (!isWorkspaceLoading()) {
1004 getWorkspace().reinflateWidgetsIfNecessary();
1005 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001006
Michael Jurka447bf842013-05-15 14:52:15 +02001007 if (DEBUG_RESUME_TIME) {
1008 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1009 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001010
Adam Cohen4b66bf32015-09-18 12:15:19 -07001011 // We want to suppress callbacks about CustomContent being shown if we have just received
1012 // onNewIntent while the user was present within launcher. In that case, we post a call
1013 // to move the user to the main screen (which will occur after onResume). We don't want to
1014 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1015 // suppress here.
1016 if (mWorkspace.getCustomContentCallbacks() != null
1017 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001018 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001019 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001020 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1021 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001022 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001023 }
1024 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001025 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001026 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001027 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001028
Sunny Goyal756adbc2015-04-16 15:20:51 -07001029 if (!isWorkspaceLoading()) {
1030 // Process any items that were added while Launcher was away.
1031 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001032
1033 // Refresh shortcuts if the permission changed.
1034 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001035 }
Adam Cohen9211d422014-10-07 18:14:53 -07001036
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001037 if (shouldShowDiscoveryBounce()) {
1038 mAllAppsController.showDiscoveryBounce();
1039 }
1040 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001041 if (mLauncherCallbacks != null) {
1042 mLauncherCallbacks.onResume();
1043 }
Adam Cohen06dff352012-06-01 17:17:08 -07001044 }
1045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001047 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001048 // Ensure that items added to Launcher are queued until Launcher returns
1049 InstallShortcutReceiver.enableInstallQueue();
1050
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001052 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001053 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001054 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001055
1056 // We call onHide() aggressively. The custom content callbacks should be able to
1057 // debounce excess onHide calls.
1058 if (mWorkspace.getCustomContentCallbacks() != null) {
1059 mWorkspace.getCustomContentCallbacks().onHide();
1060 }
Romain Guycbb89e42009-06-08 15:52:54 -07001061
Adam Cohen9211d422014-10-07 18:14:53 -07001062 if (mLauncherCallbacks != null) {
1063 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001064 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001065 }
1066
1067 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001068 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1069 // by a onResume or by scrolling otherwise.
1070 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001071
1072 // Custom content is completely hidden
1073 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001074
1075 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1076 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001077
1078 // Indicates whether the user is allowed to scroll away from the custom content.
1079 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 }
1081
Adam Cohenc2d6e892014-10-16 09:49:24 -07001082 public interface LauncherOverlay {
1083
1084 /**
1085 * Touch interaction leading to overscroll has begun
1086 */
1087 public void onScrollInteractionBegin();
1088
1089 /**
1090 * Touch interaction related to overscroll has ended
1091 */
1092 public void onScrollInteractionEnd();
1093
1094 /**
1095 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1096 * screen (or in the case of RTL, the rightmost screen).
1097 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001098 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001099
1100 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001101 * Called when the launcher is ready to use the overlay
1102 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001104 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001105 }
1106
Jun Mukai8af0cd82015-05-12 12:32:12 -07001107 public interface LauncherSearchCallbacks {
1108 /**
1109 * Called when the search overlay is shown.
1110 */
1111 public void onSearchOverlayOpened();
1112
1113 /**
1114 * Called when the search overlay is dismissed.
1115 */
1116 public void onSearchOverlayClosed();
1117 }
1118
Adam Cohenc2d6e892014-10-16 09:49:24 -07001119 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001120 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001121 }
1122
1123 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1124
Sunny Goyalc86df472016-02-25 09:19:38 -08001125 public void onScrollChanged(float progress) {
1126 if (mWorkspace != null) {
1127 mWorkspace.onOverlayScrollChanged(progress);
1128 }
1129 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 }
1131
Jorim Jaggid017f882014-01-14 17:08:48 -08001132 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001133 if (mLauncherCallbacks != null) {
1134 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001135 } else {
1136 // On devices with a locked orientation, we will at least have the allow rotation
1137 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001138 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001139 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001140 }
1141
Adam Cohen9211d422014-10-07 18:14:53 -07001142 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001143 CustomContentCallbacks callbacks, String description) {
1144 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001145 }
1146
Adam Cohenedb40762013-07-18 16:45:45 -07001147 // The custom content needs to offset its content to account for the QSB
1148 public int getTopOffsetForCustomContent() {
1149 return mWorkspace.getPaddingTop();
1150 }
1151
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001152 @Override
1153 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001154 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001155 if (mModel.isCurrentCallbacks(this)) {
1156 mModel.stopLoader();
1157 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001158 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1159
Romain Guy13c2e7b2010-03-10 19:45:00 -08001160 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001161 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001162
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001163 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001164 @Override
1165 public void onWindowFocusChanged(boolean hasFocus) {
1166 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001167 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001168
1169 if (mLauncherCallbacks != null) {
1170 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1171 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001172 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 private boolean acceptFilter() {
1175 final InputMethodManager inputManager = (InputMethodManager)
1176 getSystemService(Context.INPUT_METHOD_SERVICE);
1177 return !inputManager.isFullscreenMode();
1178 }
1179
1180 @Override
1181 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001182 final int uniChar = event.getUnicodeChar();
1183 final boolean handled = super.onKeyDown(keyCode, event);
1184 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1185 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1187 keyCode, event);
1188 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001189 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001190 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001191 // showSearchDialog()
1192 // If there are multiple keystrokes before the search dialog takes focus,
1193 // onSearchRequested() will be called for every keystroke,
1194 // but it is idempotent, so it's fine.
1195 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
1197 }
1198
Joe Onorato8a9625e2010-01-28 15:55:35 -08001199 // Eat the long press event so the keyboard doesn't come up.
1200 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1201 return true;
1202 }
1203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 return handled;
1205 }
1206
Winson46163472015-09-22 17:31:56 -07001207 @Override
1208 public boolean onKeyUp(int keyCode, KeyEvent event) {
1209 if (keyCode == KeyEvent.KEYCODE_MENU) {
1210 // Ignore the menu key if we are currently dragging or are on the custom content screen
1211 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001212 // Close any open floating view
1213 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001214
Winson46163472015-09-22 17:31:56 -07001215 // Stop resizing any widgets
1216 mWorkspace.exitWidgetResizeMode();
1217
1218 // Show the overview mode if we are on the workspace
1219 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1220 !mWorkspace.isSwitchingState()) {
1221 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001222 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001223 }
1224 }
1225 return true;
1226 }
1227 return super.onKeyUp(keyCode, event);
1228 }
1229
Karl Rosaen138a0412009-04-23 19:00:21 -07001230 private String getTypedText() {
1231 return mDefaultKeySsb.toString();
1232 }
1233
Sunny Goyal6f28e712016-09-13 14:19:29 -07001234 @Override
1235 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 mDefaultKeySsb.clear();
1237 mDefaultKeySsb.clearSpans();
1238 Selection.setSelection(mDefaultKeySsb, 0);
1239 }
1240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 /**
1242 * Restores the previous state, if it exists.
1243 *
1244 * @param savedState The previous state.
1245 */
1246 private void restoreState(Bundle savedState) {
1247 if (savedState == null) {
1248 return;
1249 }
1250
Sunny Goyalfe770c92016-09-27 14:38:58 -07001251 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1252 State[] stateValues = State.values();
1253 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1254 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001255 if (state == State.APPS || state == State.WIDGETS) {
1256 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001257 }
1258
Sunny Goyal2100c782016-08-22 16:00:03 -07001259 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1260 if (requestArgs != null) {
1261 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001263
1264 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
1266
1267 /**
1268 * Finds all the views we need and configure them properly.
1269 */
1270 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001271 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001272 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001273 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001274 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001275 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1276 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001277 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001278
Sunny Goyal784f9c32016-03-23 16:56:54 -07001279 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1280 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1281 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282
Winson Chung4c98d922011-05-31 16:50:48 -07001283 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001284 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001285
Winson Chung3d503fb2011-07-13 17:25:49 -07001286 // Setup the hotseat
1287 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1288 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001289 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001290 }
1291
Winsone9f27272015-10-13 10:47:51 -07001292 // Setup the overview panel
1293 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001294
Winson Chung4c98d922011-05-31 16:50:48 -07001295 // Setup the workspace
1296 mWorkspace.setHapticFeedbackEnabled(false);
1297 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001298 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001299 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1300 // default state, otherwise we will update to the wrong offsets in RTL
1301 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001302 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001303 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001304
Tony Wickham34d2c912015-09-11 09:27:58 -07001305 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001306 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001307
Winson Chungef7f8742015-06-04 17:18:17 -07001308 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001309 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001310 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001311 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1312 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1313 } else {
1314 mAppsView.setSearchBarController(new DefaultAppSearchController());
1315 }
Craig Mautner360310b2012-10-26 15:13:08 -07001316
Winson Chung3d503fb2011-07-13 17:25:49 -07001317 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song0de01172016-10-05 16:27:48 -07001318 mDragController.setDragScroller(mWorkspace);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001319 mDragController.setScrollView(mDragLayer);
1320 mDragController.setMoveTarget(mWorkspace);
1321 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001322 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001323
Peter Schiller310a9882016-06-30 13:52:36 -07001324 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1325 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1326 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001327
Sunny Goyal322d5562015-06-25 19:35:49 -07001328 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1329 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
Winsone9f27272015-10-13 10:47:51 -07001333 private void setupOverviewPanel() {
1334 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1335
Winsone9f27272015-10-13 10:47:51 -07001336 // Bind wallpaper button actions
1337 View wallpaperButton = findViewById(R.id.wallpaper_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001338 new OverviewButtonClickListener(LauncherLogProto.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001339 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001340 public void handleViewClick(View view) {
1341 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001342 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001343 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001344 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1345
1346 // Bind widget button actions
1347 mWidgetsButton = findViewById(R.id.widget_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001348 new OverviewButtonClickListener(LauncherLogProto.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001349 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001350 public void handleViewClick(View view) {
1351 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001352 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001353 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001354 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1355
1356 // Bind settings actions
1357 View settingsButton = findViewById(R.id.settings_button);
1358 boolean hasSettings = hasSettings();
1359 if (hasSettings) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001360 new OverviewButtonClickListener(LauncherLogProto.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001361 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001362 public void handleViewClick(View view) {
1363 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001364 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001365 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001366 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1367 } else {
1368 settingsButton.setVisibility(View.GONE);
1369 }
1370
1371 mOverviewPanel.setAlpha(0f);
1372 }
1373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001375 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001376 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001377 */
1378 public void setAllAppsButton(View allAppsButton) {
1379 mAllAppsButton = allAppsButton;
1380 }
1381
Sunny Goyalbb011da2016-06-15 15:42:29 -07001382 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001383 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001384 }
1385
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001386 public View getWidgetsButton() {
1387 return mWidgetsButton;
1388 }
1389
Anjali Koppal5ad44842014-03-10 20:34:39 -07001390 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 * Creates a view representing a shortcut.
1392 *
1393 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001395 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001396 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
1399 /**
1400 * Creates a view representing a shortcut inflated from the specified resource.
1401 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 * @param parent The group the shortcut belongs to.
1403 * @param info The data structure describing the shortcut.
1404 *
1405 * @return A View inflated from layoutResId.
1406 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001407 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001408 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001409 parent, false);
1410 favorite.applyFromShortcutInfo(info, mIconCache);
1411 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001413 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 return favorite;
1415 }
1416
1417 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 * Add a shortcut to the workspace.
1419 *
1420 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001422 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001423 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001424 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001425 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001426
Sunny Goyal5c97f512015-05-19 16:03:28 -07001427 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001428 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1429 args.getPendingIntent().getComponent() == null) {
1430 return;
1431 }
1432 if (!PackageManagerHelper.hasPermissionForActivity(
1433 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1434 // The app is trying to add a shortcut without sufficient permissions
1435 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001436 return;
1437 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001438 final View view = createShortcut(info);
1439
Sunny Goyal5c97f512015-05-19 16:03:28 -07001440 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001441 // First we check if we already know the exact location where we want to add this item.
1442 if (cellX >= 0 && cellY >= 0) {
1443 cellXY[0] = cellX;
1444 cellXY[1] = cellY;
1445 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001446
1447 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001448 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001449 true, null,null)) {
1450 return;
1451 }
1452 DragObject dragObject = new DragObject();
1453 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001454 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1455 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001456 return;
1457 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001458 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001459 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001460 }
1461
1462 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001463 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001464 return;
1465 }
1466
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001467 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468
Sunny Goyal2100c782016-08-22 16:00:03 -07001469 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1470 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001474 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001476 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001478 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001479 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1480
Adam Cohened66b2b2012-01-23 17:28:51 -08001481 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001482 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001483 }
Romain Guycbb89e42009-06-08 15:52:54 -07001484
Adam Cohen59400422014-03-05 18:07:04 -08001485 if (appWidgetInfo.isCustomWidget) {
1486 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001487 }
1488
Adam Cohen59400422014-03-05 18:07:04 -08001489 LauncherAppWidgetInfo launcherInfo;
1490 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001491 launcherInfo.spanX = itemInfo.spanX;
1492 launcherInfo.spanY = itemInfo.spanY;
1493 launcherInfo.minSpanX = itemInfo.minSpanX;
1494 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001495 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001498 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499
Sunny Goyal2100c782016-08-22 16:00:03 -07001500 if (hostView == null) {
1501 // Perform actual inflation because we're live
1502 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001504 hostView.setVisibility(View.VISIBLE);
1505 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 }
Romain Guycbb89e42009-06-08 15:52:54 -07001507
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001508 private void addAppWidgetToWorkspace(
1509 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001510 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001511 hostView.setTag(item);
1512 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001513
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001514 hostView.setFocusable(true);
1515 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001516
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001517 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001518 item.cellX, item.cellY, item.spanX, item.spanY, insert);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001519 }
1520
Adam Cohended9f8d2010-11-03 13:25:16 -07001521 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1522 @Override
1523 public void onReceive(Context context, Intent intent) {
1524 final String action = intent.getAction();
1525 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001526 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001527
Winson Chungc100e8e2011-08-09 16:02:43 -07001528 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001529 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001530 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001531 if (!showWorkspace(false)) {
1532 // If we are already on the workspace, then manually reset all apps
1533 mAppsView.reset();
1534 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001535 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001536 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001537 }
1538 }
1539 };
1540
1541 @Override
1542 public void onAttachedToWindow() {
1543 super.onAttachedToWindow();
1544
1545 // Listen for broadcasts related to user-presence
1546 final IntentFilter filter = new IntentFilter();
1547 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001548 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001549 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001550 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001551
1552 if (mLauncherCallbacks != null) {
1553 mLauncherCallbacks.onAttachedToWindow();
1554 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001555 }
1556
1557 @Override
1558 public void onDetachedFromWindow() {
1559 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001560 if (mAttached) {
1561 unregisterReceiver(mReceiver);
1562 mAttached = false;
1563 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001564
1565 if (mLauncherCallbacks != null) {
1566 mLauncherCallbacks.onDetachedFromWindow();
1567 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001568 }
1569
1570 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001571 // The following code used to be in onResume, but it turns out onResume is called when
1572 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1573 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001574 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001575 if (!mWorkspaceLoading) {
1576 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001577 // We want to let Launcher draw itself at least once before we force it to build
1578 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001579 // apps is nice and speedy.
1580 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001581 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001582 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001583 if (mStarted) return;
1584 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001585 // We delay the layer building a bit in order to give
1586 // other message processing a time to run. In particular
1587 // this avoids a delay in hiding the IME if it was
1588 // currently shown, because doing that may involve
1589 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001590 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001591 final ViewTreeObserver.OnDrawListener listener = this;
1592 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001593 public void run() {
1594 if (mWorkspace != null &&
1595 mWorkspace.getViewTreeObserver() != null) {
1596 mWorkspace.getViewTreeObserver().
1597 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001598 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001599 }
1600 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001601 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001602 }
1603 });
1604 }
1605 clearTypedText();
1606 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 }
1608
Winson Chunga6945242014-01-08 14:04:34 -08001609 public DragLayer getDragLayer() {
1610 return mDragLayer;
1611 }
1612
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001613 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001614 return mAppsView;
1615 }
1616
Hyunyoung Song3f471442015-04-08 19:01:34 -07001617 public WidgetsContainerView getWidgetsView() {
1618 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001619 }
1620
Winson Chunga6945242014-01-08 14:04:34 -08001621 public Workspace getWorkspace() {
1622 return mWorkspace;
1623 }
1624
Sunny Goyal6178f132016-07-11 17:30:03 -07001625 public View getQsbContainer() {
1626 return mQsbContainer;
1627 }
1628
Winson Chunga6945242014-01-08 14:04:34 -08001629 public Hotseat getHotseat() {
1630 return mHotseat;
1631 }
1632
Jorim Jaggid017f882014-01-14 17:08:48 -08001633 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001634 return mOverviewPanel;
1635 }
1636
Sunny Goyal47328fd2016-05-25 18:56:41 -07001637 public DropTargetBar getDropTargetBar() {
1638 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001639 }
1640
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001641 public LauncherAppWidgetHost getAppWidgetHost() {
1642 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
Romain Guycbb89e42009-06-08 15:52:54 -07001644
Winson Chunga9abd0e2010-10-27 17:18:37 -07001645 public LauncherModel getModel() {
1646 return mModel;
1647 }
1648
Adam Cohen79d90c52016-04-22 13:29:20 -07001649 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001650 return mSharedPrefs;
1651 }
1652
Adam Cohen2e6da152015-05-06 11:42:25 -07001653 public DeviceProfile getDeviceProfile() {
1654 return mDeviceProfile;
1655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 @Override
1658 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001659 long startTime = 0;
1660 if (DEBUG_RESUME_TIME) {
1661 startTime = System.currentTimeMillis();
1662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 super.onNewIntent(intent);
1664
Winson15f8b172015-08-19 11:02:39 -07001665 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1666 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1667 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001668
1669 // Check this condition before handling isActionMain, as this will get reset.
1670 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001671 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001672
Winson15f8b172015-08-19 11:02:39 -07001673 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1674 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001675 if (mWorkspace == null) {
1676 // Can be cases where mWorkspace is null, this prevents a NPE
1677 return;
1678 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001679 // In all these cases, only animate if we're already on home
1680 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001681
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001682 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001683 exitSpringLoadedDragMode();
1684
1685 // If we are already on home, then just animate back to the workspace,
1686 // otherwise, just wait until onResume to set the state back to Workspace
1687 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001688 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001689 } else {
1690 mOnResumeState = State.WORKSPACE;
1691 }
1692
1693 final View v = getWindow().peekDecorView();
1694 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001695 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001696 INPUT_METHOD_SERVICE);
1697 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1698 }
1699
Winson Chungb745afb2015-03-02 11:51:23 -08001700 // Reset the apps view
1701 if (!alreadyOnHome && mAppsView != null) {
1702 mAppsView.scrollToTop();
1703 }
1704
Hyunyoung Song3f471442015-04-08 19:01:34 -07001705 // Reset the widgets view
1706 if (!alreadyOnHome && mWidgetsView != null) {
1707 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001708 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001709
Adam Cohen9211d422014-10-07 18:14:53 -07001710 if (mLauncherCallbacks != null) {
1711 mLauncherCallbacks.onHomeIntent();
1712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
Adam Cohened307df2013-10-02 09:37:31 -07001714
Adam Cohen9211d422014-10-07 18:14:53 -07001715 if (mLauncherCallbacks != null) {
1716 mLauncherCallbacks.onNewIntent(intent);
1717 }
Winson15f8b172015-08-19 11:02:39 -07001718
1719 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1720 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1721 // animation.
1722 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001723 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001724 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001725 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1726 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001727
1728 // We use this flag to suppress noisy callbacks above custom content state
1729 // from onResume.
1730 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001731 mWorkspace.post(new Runnable() {
1732 @Override
1733 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001734 if (mWorkspace != null) {
1735 mWorkspace.moveToDefaultScreen(true);
1736 }
Winson15f8b172015-08-19 11:02:39 -07001737 }
1738 });
1739 }
1740 }
1741
1742 if (DEBUG_RESUME_TIME) {
1743 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1744 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001745 }
1746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001748 public void onRestoreInstanceState(Bundle state) {
1749 super.onRestoreInstanceState(state);
1750 for (int page: mSynchronouslyBoundPages) {
1751 mWorkspace.restoreInstanceStateForChild(page);
1752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754
1755 @Override
1756 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001757 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001758 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1759 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001760
Adam Cohen21cd0022013-10-09 18:57:02 -07001761 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001762 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763
Michael Jurka883f55b2010-10-21 15:47:14 -07001764 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001765 // We close any open folders and shortcut containers since they will not be re-opened,
1766 // and we need to make sure this state is reflected.
1767 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001768
Sunny Goyal2100c782016-08-22 16:00:03 -07001769 if (mPendingRequestArgs != null) {
1770 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1771 }
1772 if (mPendingActivityResult != null) {
1773 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
1775
Adam Cohen9211d422014-10-07 18:14:53 -07001776 if (mLauncherCallbacks != null) {
1777 mLauncherCallbacks.onSaveInstanceState(outState);
1778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 }
1780
1781 @Override
1782 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001784
Michael Jurka9d906c72011-10-14 06:25:36 -07001785 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001786 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001787
Winson Chungcd2b0142011-06-08 16:02:26 -07001788 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001789 // It's possible to receive onDestroy after a new Launcher activity has
1790 // been created. In this case, don't interfere with the new Launcher.
1791 if (mModel.isCurrentCallbacks(this)) {
1792 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001793 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001794 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001795
Sunny Goyal745bad92016-05-02 10:54:12 -07001796 if (mRotationPrefChangeHandler != null) {
1797 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1798 }
1799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001801 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001803 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001805 mAppWidgetHost = null;
1806
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 TextKeyListener.getInstance().release();
1808
Tony Wickhame2217252016-03-22 16:34:23 -07001809 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1810 .removeAccessibilityStateChangeListener(this);
1811
Michael Jurka2ecf9952012-06-18 12:52:28 -07001812 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001813
1814 if (mLauncherCallbacks != null) {
1815 mLauncherCallbacks.onDestroy();
1816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818
Sunny Goyalae502842016-06-17 08:43:56 -07001819 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1820 return mAccessibilityDelegate;
1821 }
1822
Adam Cohena9cf38f2011-05-02 15:36:58 -07001823 public DragController getDragController() {
1824 return mDragController;
1825 }
1826
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001828 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001829 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001830 }
1831
1832 @Override
1833 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1834 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001835 try {
1836 super.startIntentSenderForResult(intent, requestCode,
1837 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1838 } catch (IntentSender.SendIntentException e) {
1839 throw new ActivityNotFoundException();
1840 }
1841 }
1842
Winson Chung70d72102011-08-12 11:24:35 -07001843 /**
1844 * Indicates that we want global search for this activity by setting the globalSearch
1845 * argument for {@link #startSearch} to true.
1846 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001848 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001850
Karl Rosaen138a0412009-04-23 19:00:21 -07001851 if (initialQuery == null) {
1852 // Use any text typed in the launcher as the initial query
1853 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 if (appSearchData == null) {
1856 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001857 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001859
Sunny Goyal6f28e712016-09-13 14:19:29 -07001860 if (mLauncherCallbacks == null ||
1861 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1862 // Starting search from the callbacks failed. Start the default global search.
1863 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001864 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001865
1866 // We need to show the workspace after starting the search
1867 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001868 }
1869
Ian Parkinson047036e2014-09-01 15:40:53 +01001870 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001871 * Starts the global search activity. This code is a copied from SearchManager
1872 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001873 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001874 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001875 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001876 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1877 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1878 if (globalSearchActivity == null) {
1879 Log.w(TAG, "No global search activity found.");
1880 return;
1881 }
1882 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1883 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1884 intent.setComponent(globalSearchActivity);
1885 // Make sure that we have a Bundle to put source in
1886 if (appSearchData == null) {
1887 appSearchData = new Bundle();
1888 } else {
1889 appSearchData = new Bundle(appSearchData);
1890 }
Adam Cohen9211d422014-10-07 18:14:53 -07001891 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001892 if (!appSearchData.containsKey("source")) {
1893 appSearchData.putString("source", getPackageName());
1894 }
1895 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1896 if (!TextUtils.isEmpty(initialQuery)) {
1897 intent.putExtra(SearchManager.QUERY, initialQuery);
1898 }
1899 if (selectInitialQuery) {
1900 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1901 }
1902 intent.setSourceBounds(sourceBounds);
1903 try {
1904 startActivity(intent);
1905 } catch (ActivityNotFoundException ex) {
1906 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909
Yura4f93ec62014-02-04 14:15:21 +00001910 public boolean isOnCustomContent() {
1911 return mWorkspace.isOnOrMovingToCustomContent();
1912 }
1913
Winson Chung70d72102011-08-12 11:24:35 -07001914 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001915 public boolean onPrepareOptionsMenu(Menu menu) {
1916 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001917 if (mLauncherCallbacks != null) {
1918 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1919 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001920 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001923 @Override
1924 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001925 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001926 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001927 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001928 }
1929
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001931 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 }
1933
Adam Cohen517a7f52014-03-01 12:12:59 -08001934 public boolean isWorkspaceLoading() {
1935 return mWorkspaceLoading;
1936 }
1937
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001938 private void setWorkspaceLoading(boolean value) {
1939 boolean isLocked = isWorkspaceLocked();
1940 mWorkspaceLoading = value;
1941 if (isLocked != isWorkspaceLocked()) {
1942 onWorkspaceLockedChanged();
1943 }
1944 }
1945
Sunny Goyal2100c782016-08-22 16:00:03 -07001946 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001947 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001948 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001949 if (isLocked != isWorkspaceLocked()) {
1950 onWorkspaceLockedChanged();
1951 }
1952 }
1953
Adam Cohen9211d422014-10-07 18:14:53 -07001954 protected void onWorkspaceLockedChanged() {
1955 if (mLauncherCallbacks != null) {
1956 mLauncherCallbacks.onWorkspaceLockedChanged();
1957 }
1958 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001959
Sunny Goyal2100c782016-08-22 16:00:03 -07001960 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1961 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001962 if (LOGD) {
1963 Log.d(TAG, "Adding widget from drop");
1964 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07001965 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1966 }
1967
Sunny Goyal2100c782016-08-22 16:00:03 -07001968 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
1969 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001970 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001971 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001972 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07001973
Bjorn Bringert7984c942009-12-09 15:38:25 +00001974 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07001975 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
1976 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001978 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001979 Runnable onComplete = new Runnable() {
1980 @Override
1981 public void run() {
1982 // Exit spring loaded mode if necessary after adding the widget
1983 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1984 null);
1985 }
1986 };
Sunny Goyal2100c782016-08-22 16:00:03 -07001987 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07001988 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990 }
Romain Guycbb89e42009-06-08 15:52:54 -07001991
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001992 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001993 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001994 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001995 mWorkspace.moveToCustomContentScreen(animate);
1996 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001997
1998 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1999 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002000 info.container = container;
2001 info.screenId = screenId;
2002 if (cell != null) {
2003 info.cellX = cell[0];
2004 info.cellY = cell[1];
2005 }
2006 info.spanX = spanX;
2007 info.spanY = spanY;
2008
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002009 switch (info.itemType) {
2010 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2011 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002012 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002013 break;
2014 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002015 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002016 break;
2017 default:
2018 throw new IllegalStateException("Unknown item type: " + info.itemType);
2019 }
2020 }
2021
Adam Cohenfbba09b2011-07-18 21:43:05 -07002022 /**
2023 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002024 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002025 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002026 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2027 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2028 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002029 }
2030
Adam Cohenfbba09b2011-07-18 21:43:05 -07002031 /**
2032 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002033 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002034 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002035 AppWidgetHostView hostView = info.boundWidget;
2036 int appWidgetId;
2037 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002038 // In the case where we've prebound the widget, we remove it from the DragLayer
2039 if (LOGD) {
2040 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2041 }
2042 getDragLayer().removeView(hostView);
2043
Adam Cohened66b2b2012-01-23 17:28:51 -08002044 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002045 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002046
2047 // Clear the boundWidget so that it doesn't get destroyed.
2048 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002049 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002050 // In this case, we either need to start an activity to get permission to bind
2051 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002052 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002053 Bundle options = info.bindOptions;
2054
Sunny Goyalffe83f12014-08-14 17:39:34 -07002055 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2056 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002057 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002058 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002059 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002060 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002061 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2062 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2063 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002064 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002065 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002066 // TODO: we need to make sure that this accounts for the options bundle.
2067 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002068 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2069 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002070 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002071 }
2072
Adam Cohendcd297f2013-06-18 13:13:40 -07002073 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002074 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002075 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 folderInfo.title = getText(R.string.folder_name);
2077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002079 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2080 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081
2082 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 FolderIcon newFolder =
2084 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002085 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002087 // Force measure the new folder icon
2088 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2089 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002090 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
Romain Guycbb89e42009-06-08 15:52:54 -07002092
Winsonc0b52fe2015-09-09 16:38:15 -07002093 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002094 * Unbinds the view for the specified item, and removes the item and all its children.
2095 *
2096 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002097 * @param itemInfo the {@link ItemInfo} for this view.
2098 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002099 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002100 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002101 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002102 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002103 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2104 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002105 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002106 } else {
2107 mWorkspace.removeWorkspaceItem(v);
2108 }
Winsonc0b52fe2015-09-09 16:38:15 -07002109 if (deleteFromDb) {
2110 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2111 }
2112 } else if (itemInfo instanceof FolderInfo) {
2113 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002114 if (v instanceof FolderIcon) {
2115 ((FolderIcon) v).removeListeners();
2116 }
Winsonc0b52fe2015-09-09 16:38:15 -07002117 mWorkspace.removeWorkspaceItem(v);
2118 if (deleteFromDb) {
2119 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2120 }
2121 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2122 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002123 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002124 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002125 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002126 }
2127 } else {
2128 return false;
2129 }
2130 return true;
2131 }
2132
2133 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002134 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002135 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002136 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002137 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002138 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002139 // Deleting an app widget ID is a void call but writes to disk before returning
2140 // to the caller...
2141 new AsyncTask<Void, Void, Void>() {
2142 public Void doInBackground(Void ... args) {
2143 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2144 return null;
2145 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002146 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002147 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002148 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002149 }
2150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 @Override
2152 public boolean dispatchKeyEvent(KeyEvent event) {
2153 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2154 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002156 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002157 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002158 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002159 dumpState();
2160 return true;
2161 }
2162 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002163 }
2164 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2165 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002166 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 return true;
2168 }
2169 }
2170
2171 return super.dispatchKeyEvent(event);
2172 }
2173
Joe Onorato88ec0992009-11-19 13:16:06 -08002174 @Override
2175 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002176 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2177 return;
2178 }
2179
Sunny Goyal45478022015-06-08 16:52:41 -07002180 if (mDragController.isDragging()) {
2181 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002182 return;
2183 }
2184
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002185 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2186 if (topView != null) {
2187 if (topView.getActiveTextView() != null) {
2188 topView.getActiveTextView().dispatchBackKey();
2189 } else {
2190 topView.close(true);
2191 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002192 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002193 showWorkspace(true);
2194 } else if (isWidgetsViewVisible()) {
2195 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002196 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002197 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002198 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002199 mWorkspace.exitWidgetResizeMode();
2200
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002201 // Back button is a no-op here, but give at least some feedback for the button press
2202 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002203 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002204 }
2205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 * Launches the intent referred by the clicked shortcut.
2208 *
2209 * @param v The view representing the clicked shortcut.
2210 */
2211 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002212 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2213 // view has detached (it's possible for this to happen if the view is removed mid touch).
2214 if (v.getWindowToken() == null) {
2215 return;
2216 }
2217
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002218 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002219 return;
2220 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002221
Adam Cohen1697b792013-09-17 19:08:21 -07002222 if (v instanceof Workspace) {
2223 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002224 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002225 }
2226 return;
2227 }
2228
2229 if (v instanceof CellLayout) {
2230 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002231 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2232 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002233 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002234 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002235 }
2236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002238 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002239 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002241 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002242 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002243 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002244 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2245 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002246 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002247 } else if (tag instanceof AppInfo) {
2248 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002249 } else if (tag instanceof LauncherAppWidgetInfo) {
2250 if (v instanceof PendingAppWidgetHostView) {
2251 onClickPendingWidget((PendingAppWidgetHostView) v);
2252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 }
2254 }
2255
Sunny Goyal70660032015-05-14 00:07:08 -07002256 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002257 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002258 return false;
2259 }
2260
Michael Jurkaaf442092010-06-10 17:01:57 -07002261 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002262 * Event handler for the app widget view which has not fully restored.
2263 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002264 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002265 if (mIsSafeModeEnabled) {
2266 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2267 return;
2268 }
2269
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002270 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002271 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002272 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2273 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2274 // This should not happen, as we make sure that an Id is allocated during bind.
2275 return;
2276 }
2277 LauncherAppWidgetProviderInfo appWidgetInfo =
2278 mAppWidgetManager.findProvider(info.providerName, info.user);
2279 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002280 setWaitingForResult(PendingRequestArgs
2281 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002282
Sunny Goyald478c832016-04-01 12:04:16 -07002283 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002284 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002285 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002286 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002287 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2288 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2289 }
2290 } else {
2291 LauncherAppWidgetProviderInfo appWidgetInfo =
2292 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2293 if (appWidgetInfo != null) {
2294 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2295 }
Sunny Goyalff572272014-07-23 13:58:07 -07002296 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002297 } else if (info.installProgress < 0) {
2298 // The install has not been queued
2299 final String packageName = info.providerName.getPackageName();
2300 showBrokenAppInstallDialog(packageName,
2301 new DialogInterface.OnClickListener() {
2302 public void onClick(DialogInterface dialog, int id) {
2303 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2304 }
2305 });
2306 } else {
2307 // Download has started.
2308 final String packageName = info.providerName.getPackageName();
2309 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002310 }
2311 }
2312
Sunny Goyald478c832016-04-01 12:04:16 -07002313 private void startRestoredWidgetReconfigActivity(
2314 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002315 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002316 mAppWidgetManager.startConfigActivity(provider,
2317 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2318 }
2319
Sunny Goyalff572272014-07-23 13:58:07 -07002320 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002321 * Event handler for the "grid" button that appears on the home screen, which
2322 * enters all apps mode.
2323 *
2324 * @param v The view that was clicked.
2325 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002326 protected void onClickAllAppsButton(View v) {
2327 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002328 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002329 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002330 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002331 showAppsView(true /* animated */, true /* updatePredictedApps */,
2332 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002333 }
2334 }
2335
2336 protected void onLongClickAllAppsButton(View v) {
2337 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2338 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002339 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002340 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002341 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002342 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002343 }
2344 }
2345
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002346 private void showBrokenAppInstallDialog(final String packageName,
2347 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002348 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002349 .setTitle(R.string.abandoned_promises_title)
2350 .setMessage(R.string.abandoned_promise_explanation)
2351 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2352 .setNeutralButton(R.string.abandoned_clean_this,
2353 new DialogInterface.OnClickListener() {
2354 public void onClick(DialogInterface dialog, int id) {
2355 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2356 mWorkspace.removeAbandonedPromise(packageName, user);
2357 }
2358 })
2359 .create().show();
2360 return;
2361 }
2362
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002363 /**
2364 * Event handler for an app shortcut click.
2365 *
2366 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2367 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002368 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002369 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2370 Object tag = v.getTag();
2371 if (!(tag instanceof ShortcutInfo)) {
2372 throw new IllegalArgumentException("Input must be a Shortcut");
2373 }
2374
2375 // Open shortcut
2376 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002377
2378 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002379 if ((shortcut.isDisabled &
2380 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2381 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2382 // If the app is only disabled because of the above flags, launch activity anyway.
2383 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002384 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002385 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2386 // Use a message specific to this shortcut, if it has one.
2387 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2388 return;
2389 }
2390 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002391 int error = R.string.activity_not_available;
2392 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2393 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002394 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2395 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002396 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002397 }
2398 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2399 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002400 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002401 }
2402
Chris Wren40c5ed32014-06-24 18:24:23 -04002403 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002404 if ((v instanceof BubbleTextView)
2405 && shortcut.isPromise()
2406 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002407 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002408 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002409 new DialogInterface.OnClickListener() {
2410 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002411 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002412 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002413 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002414 return;
2415 }
2416
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002417 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002418 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002419 }
2420
Sunny Goyala7ce1662016-05-31 15:01:35 -07002421 private void startAppShortcutOrInfoActivity(View v) {
2422 ItemInfo item = (ItemInfo) v.getTag();
2423 Intent intent = item.getIntent();
2424 if (intent == null) {
2425 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002426 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002427 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002428 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002429
2430 if (success && v instanceof BubbleTextView) {
2431 mWaitingForResume = (BubbleTextView) v;
2432 mWaitingForResume.setStayPressed(true);
2433 }
2434 }
2435
2436 /**
2437 * Event handler for a folder icon click.
2438 *
2439 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2440 */
2441 protected void onClickFolderIcon(View v) {
2442 if (LOGD) Log.d(TAG, "onClickFolder");
2443 if (!(v instanceof FolderIcon)){
2444 throw new IllegalArgumentException("Input must be a FolderIcon");
2445 }
2446
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002447 Folder folder = ((FolderIcon) v).getFolder();
2448 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002449 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002450 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002451 }
Michael Jurka5130e402011-10-13 04:55:35 -07002452 }
2453
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002454 /**
2455 * Event handler for the (Add) Widgets button that appears after a long press
2456 * on the home screen.
2457 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002458 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002459 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002460 if (mIsSafeModeEnabled) {
2461 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2462 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002463 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002464 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002465 }
2466
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002467 /**
2468 * Event handler for the wallpaper picker button that appears after a long press
2469 * on the home screen.
2470 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002471 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002472 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002473 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2474 return;
2475 }
2476
Tony Wickham785f7a52015-08-31 17:28:32 -07002477 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2478 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal2100c782016-08-22 16:00:03 -07002479 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002480 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002481 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002482
2483 String pickerPackage = getString(R.string.wallpaper_picker_package);
2484 boolean hasTargetPackage = TextUtils.isEmpty(pickerPackage);
2485 if (!hasTargetPackage) {
2486 intent.setPackage(pickerPackage);
2487 }
2488
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002489 intent.setSourceBounds(getViewBounds(v));
Sunny Goyalc677b6b2016-10-17 09:59:01 +01002490 try {
2491 startActivityForResult(intent, REQUEST_PICK_WALLPAPER,
2492 // If there is no target package, use the default intent chooser animation
2493 hasTargetPackage ? getActivityLaunchOptions(v) : null);
2494 } catch (ActivityNotFoundException e) {
2495 setWaitingForResult(null);
2496 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2497 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002498 }
2499
2500 /**
2501 * Event handler for a click on the settings button that appears after a long press
2502 * on the home screen.
2503 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002504 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002506 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2507 .setPackage(getPackageName());
2508 intent.setSourceBounds(getViewBounds(v));
2509 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 }
2511
Adam Cohen61f560d2013-09-30 15:58:20 -07002512 public View.OnTouchListener getHapticFeedbackTouchListener() {
2513 if (mHapticFeedbackTouchListener == null) {
2514 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002515 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002516 @Override
2517 public boolean onTouch(View v, MotionEvent event) {
2518 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2519 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2520 }
2521 return false;
2522 }
2523 };
2524 }
2525 return mHapticFeedbackTouchListener;
2526 }
2527
Tony Wickhame2217252016-03-22 16:34:23 -07002528 @Override
2529 public void onAccessibilityStateChanged(boolean enabled) {
2530 mDragLayer.onAccessibilityStateChanged(enabled);
2531 }
2532
Sunny Goyal06e21a22016-08-11 16:02:02 -07002533 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002534 if (isOnCustomContent()) {
2535 // Custom content screen doesn't participate in drag and drop. If on custom
2536 // content screen, move to default.
2537 moveWorkspaceToDefaultScreen();
2538 }
Adam Cohen9211d422014-10-07 18:14:53 -07002539 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002540
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002541 /**
2542 * Called when the user stops interacting with the launcher.
2543 * This implies that the user is now on the homescreen and is not doing housekeeping.
2544 */
Adam Cohen9211d422014-10-07 18:14:53 -07002545 protected void onInteractionEnd() {
2546 if (mLauncherCallbacks != null) {
2547 mLauncherCallbacks.onInteractionEnd();
2548 }
2549 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002550
2551 /**
2552 * Called when the user starts interacting with the launcher.
2553 * The possible interactions are:
2554 * - open all apps
2555 * - reorder an app shortcut, or a widget
2556 * - open the overview mode.
2557 * This is a good time to stop doing things that only make sense
2558 * when the user is on the homescreen and not doing housekeeping.
2559 */
Adam Cohen9211d422014-10-07 18:14:53 -07002560 protected void onInteractionBegin() {
2561 if (mLauncherCallbacks != null) {
2562 mLauncherCallbacks.onInteractionBegin();
2563 }
2564 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002565
Winson Chungcd99cd32015-04-29 11:03:24 -07002566 /** Updates the interaction state. */
2567 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002568 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002569 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002570 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2571 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002572 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002573 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002574 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002575 onInteractionEnd();
2576 }
2577 }
2578
Sunny Goyala7ce1662016-05-31 15:01:35 -07002579 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002581 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2582 try {
2583 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2584 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2585 // is enabled by default on NYC.
2586 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2587 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002588
2589 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2590 String id = ((ShortcutInfo) info).getDeepShortcutId();
2591 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002592 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2593 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002594 } else {
2595 // Could be launching some bookkeeping activity
2596 startActivity(intent, optsBundle);
2597 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002598 } finally {
2599 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002600 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002602 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2603 // corresponding permission. Show the appropriate permission prompt if that
2604 // is the case.
2605 if (intent.getComponent() == null
2606 && Intent.ACTION_CALL.equals(intent.getAction())
2607 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2608 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002609
2610 setWaitingForResult(PendingRequestArgs
2611 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002612 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2613 REQUEST_PERMISSION_CALL_PHONE);
2614 } else {
2615 // No idea why this was thrown.
2616 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002617 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 }
2619 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002620
Sunny Goyalfe770c92016-09-27 14:38:58 -07002621 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002622 private Bundle getActivityLaunchOptions(View v) {
2623 if (Utilities.ATLEAST_MARSHMALLOW) {
2624 int left = 0, top = 0;
2625 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2626 if (v instanceof TextView) {
2627 // Launch from center of icon, not entire view
2628 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2629 if (icon != null) {
2630 Rect bounds = icon.getBounds();
2631 left = (width - bounds.width()) / 2;
2632 top = v.getPaddingTop();
2633 width = bounds.width();
2634 height = bounds.height();
2635 }
2636 }
2637 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2638 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2639 // On L devices, we use the device default slide-up transition.
2640 // On L MR1 devices, we use a custom version of the slide-up transition which
2641 // doesn't have the delay present in the device default.
2642 return ActivityOptions.makeCustomAnimation(
2643 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2644 }
2645 return null;
2646 }
2647
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002648 private Rect getViewBounds(View v) {
2649 int[] pos = new int[2];
2650 v.getLocationOnScreen(pos);
2651 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2652 }
2653
Sunny Goyala7ce1662016-05-31 15:01:35 -07002654 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002655 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2656 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2657 return false;
2658 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002659 // Only launch using the new animation if the shortcut has not opted out (this is a
2660 // private contract between launcher and may be ignored in the future).
2661 boolean useLaunchAnimation = (v != null) &&
2662 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2663 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2664
2665 UserHandleCompat user = null;
2666 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2667 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2668 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002669 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002670
2671 // Prepare intent
2672 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2673 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002674 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002675 }
2676 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002677 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2678 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002679 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2680 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002681 // Shortcuts need some special checks due to legacy reasons.
2682 startShortcutIntentSafely(intent, optsBundle, item);
2683 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2684 // Could be launching some bookkeeping activity
2685 startActivity(intent, optsBundle);
2686 } else {
2687 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2688 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2689 }
2690 return true;
2691 } catch (ActivityNotFoundException|SecurityException e) {
2692 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2693 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2694 }
2695 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002696 }
2697
Tony Wickhamdadb3042016-02-24 11:07:00 -08002698 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002699 public boolean dispatchTouchEvent(MotionEvent ev) {
2700 mLastDispatchTouchEventX = ev.getX();
2701 return super.dispatchTouchEvent(ev);
2702 }
2703
2704 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002706 if (!isDraggingEnabled()) return false;
2707 if (isWorkspaceLocked()) return false;
2708 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002710 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2711 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002712 onLongClickAllAppsButton(v);
2713 return true;
2714 }
2715
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002716
Jon Miranda51f037d2016-11-07 08:37:20 -08002717 boolean ignoreLongPressToOverview =
2718 mDeviceProfile.shouldIgnoreLongPressToOverview(mLastDispatchTouchEventX);
Jon Miranda379198e2016-09-23 08:54:40 -07002719
Adam Cohen1697b792013-09-17 19:08:21 -07002720 if (v instanceof Workspace) {
2721 if (!mWorkspace.isInOverviewMode()) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002722 if (!mWorkspace.isTouchActive() && !ignoreLongPressToOverview) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002723 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2724 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2725 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002726 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002727 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2728 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2729 return true;
2730 } else {
2731 return false;
2732 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002733 } else {
2734 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002735 }
Adam Cohen1697b792013-09-17 19:08:21 -07002736 }
2737
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002738 CellLayout.CellInfo longClickCellInfo = null;
2739 View itemUnderLongClick = null;
2740 if (v.getTag() instanceof ItemInfo) {
2741 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002742 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002743 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002744 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 }
2746
Winson Chung3d503fb2011-07-13 17:25:49 -07002747 // The hotseat touch handling does not go through Workspace, and we always allow long press
2748 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002749 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002750 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002751 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002752 if (mWorkspace.isInOverviewMode()) {
2753 mWorkspace.startReordering(v);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002754 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2755 LauncherLogProto.Action.NONE, LauncherLogProto.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002756 } else {
Jon Mirandacc42c5b2016-10-27 17:15:27 -07002757 if (ignoreLongPressToOverview) {
Jon Miranda379198e2016-09-23 08:54:40 -07002758 return false;
2759 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002760 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2761 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2762 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002763 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002764 }
Jon Miranda379198e2016-09-23 08:54:40 -07002765 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2766 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002768 final boolean isAllAppsButton =
2769 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2770 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2771 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002772 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002774 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 }
2776 }
2777 }
2778 return true;
2779 }
2780
Winson Chung3d503fb2011-07-13 17:25:49 -07002781 boolean isHotseatLayout(View layout) {
2782 return mHotseat != null && layout != null &&
2783 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2784 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002785
Winson Chung3d503fb2011-07-13 17:25:49 -07002786 /**
2787 * Returns the CellLayout of the specified container at the specified screen.
2788 */
Sunny Goyal92820592015-03-02 11:31:35 -08002789 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002790 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2791 if (mHotseat != null) {
2792 return mHotseat.getLayout();
2793 } else {
2794 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002795 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002796 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002797 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002798 }
2799 }
2800
Winson Chungb745afb2015-03-02 11:51:23 -08002801 /**
2802 * For overridden classes.
2803 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002804 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002805 return isAppsViewVisible();
2806 }
2807
2808 public boolean isAppsViewVisible() {
2809 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2810 }
2811
2812 public boolean isWidgetsViewVisible() {
2813 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002814 }
2815
Michael Jurkae326f182011-11-21 14:05:46 -08002816 @Override
2817 public void onTrimMemory(int level) {
2818 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002819 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2820 // The widget preview db can result in holding onto over
2821 // 3MB of memory for caching which isn't necessary.
2822 SQLiteDatabase.releaseMemory();
2823
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002824 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002825 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002826 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002827 if (mLauncherCallbacks != null) {
2828 mLauncherCallbacks.onTrimMemory(level);
2829 }
Michael Jurkae326f182011-11-21 14:05:46 -08002830 }
2831
Winson5c6bdbb2015-09-03 11:36:19 -07002832 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002833 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002834 }
2835
Winson5c6bdbb2015-09-03 11:36:19 -07002836 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002837 boolean changed = mState != State.WORKSPACE ||
2838 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002839 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002840 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002841 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002842 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002843
Michael Jurkab3e22d92011-10-31 15:58:33 -07002844 // Set focus to the AppsCustomize button
2845 if (mAllAppsButton != null) {
2846 mAllAppsButton.requestFocus();
2847 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002848 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002849
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002850 // Change the state *after* we've called all the transition code
2851 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002852
Winson Chung0f785722015-04-08 10:27:49 -07002853 if (changed) {
2854 // Send an accessibility event to announce the context change
2855 getWindow().getDecorView()
2856 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002857 }
Winson5c6bdbb2015-09-03 11:36:19 -07002858 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002859 }
2860
Winsone9f27272015-10-13 10:47:51 -07002861 /**
2862 * Shows the overview button.
2863 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002864 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002865 showOverviewMode(animated, false);
2866 }
2867
2868 /**
2869 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2870 * onto one of the overview panel buttons.
2871 */
2872 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2873 Runnable postAnimRunnable = null;
2874 if (requestButtonFocus) {
2875 postAnimRunnable = new Runnable() {
2876 @Override
2877 public void run() {
2878 // Hitting the menu button when in touch mode does not trigger touch mode to
2879 // be disabled, so if requested, force focus on one of the overview panel
2880 // buttons.
2881 mOverviewPanel.requestFocusFromTouch();
2882 }
2883 };
2884 }
Adam Cohened307df2013-10-02 09:37:31 -07002885 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002886 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002887 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002888 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002889 // If animated from long press, then don't allow any of the controller in the drag
2890 // layer to intercept any remaining touch.
2891 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002892 }
2893
Winson Chungb745afb2015-03-02 11:51:23 -08002894 /**
2895 * Shows the apps view.
2896 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002897 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002898 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002899 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002900 if (updatePredictedApps) {
2901 tryAndUpdatePredictedApps();
2902 }
Winson Chung76648c52015-07-10 14:33:23 -07002903 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002904 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002905
Winson Chungb745afb2015-03-02 11:51:23 -08002906 /**
2907 * Shows the widgets view.
2908 */
2909 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002910 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002911 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002912 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002913 }
Winson Chung76648c52015-07-10 14:33:23 -07002914 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002915
2916 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002917 @Override
2918 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002919 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002920 }
2921 });
Winson Chungb745afb2015-03-02 11:51:23 -08002922 }
2923
2924 /**
2925 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002926 *
2927 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002928 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002929 // TODO: calling method should use the return value so that when {@code false} is returned
2930 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002931 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002932 if (!(mState == State.WORKSPACE ||
2933 mState == State.APPS_SPRING_LOADED ||
2934 mState == State.WIDGETS_SPRING_LOADED ||
2935 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002936 return false;
2937 }
2938 if (toState != State.APPS && toState != State.WIDGETS) {
2939 return false;
2940 }
Winson Chungb745afb2015-03-02 11:51:23 -08002941
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002942 // This is a safe and supported transition to bypass spring_loaded mode.
2943 if (mExitSpringLoadedModeRunnable != null) {
2944 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2945 mExitSpringLoadedModeRunnable = null;
2946 }
2947
Winson Chungb745afb2015-03-02 11:51:23 -08002948 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07002949 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
2950 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002951 } else {
Winson Chung006ee262015-08-03 14:40:11 -07002952 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002953 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002954
Michael Jurkab3e22d92011-10-31 15:58:33 -07002955 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002956 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002957 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002958
2959 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002960 getWindow().getDecorView()
2961 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002962 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002963 }
2964
Winson Chungcd99cd32015-04-29 11:03:24 -07002965 /**
2966 * Updates the workspace and interaction state on state change, and return the animation to this
2967 * new state.
2968 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002969 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002970 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002971 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002972 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002973 updateInteraction(fromState, toState);
2974 return anim;
2975 }
2976
Hyunyoung Song3f471442015-04-08 19:01:34 -07002977 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002978 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002979 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002980 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002981 }
Winson Chungb745afb2015-03-02 11:51:23 -08002982
Winson Chung006ee262015-08-03 14:40:11 -07002983 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002984 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002985 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07002986
2987 if (isAppsViewVisible()) {
2988 mState = State.APPS_SPRING_LOADED;
2989 } else if (isWidgetsViewVisible()) {
2990 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08002991 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002992 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08002993 } else {
2994 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07002995 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002996 }
Adam Cohen7777d962011-08-18 18:58:38 -07002997
Hyunyoung Song3f471442015-04-08 19:01:34 -07002998 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002999 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003000 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003001
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003002 if (mExitSpringLoadedModeRunnable != null) {
3003 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3004 }
3005 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003006 @Override
3007 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003008 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003009 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3010 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003011 // Before we show workspace, hide all apps again because
3012 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3013 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003014 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003015 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003016 } else {
3017 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003018 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003019 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003020 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003021 };
3022 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003023 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003024
Tony Wickhame0c33232016-02-08 11:37:04 -08003025 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003026 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3027 || mState == State.WIDGETS_SPRING_LOADED;
3028 }
3029
Michael Jurkad3ef3062010-11-23 16:23:58 -08003030 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003031 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003032 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003033 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003034 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003035 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003036 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3037 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003039 }
3040
Winson Chung4ac30062015-05-08 17:34:17 -07003041 /**
3042 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3043 * resumed.
3044 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003045 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003046 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003047 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003048 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003049 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003050 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003051 }
3052 }
3053 }
3054
Michael Jurkab3e22d92011-10-31 15:58:33 -07003055 void lockAllApps() {
3056 // TODO
3057 }
3058
3059 void unlockAllApps() {
3060 // TODO
3061 }
3062
Michael Jurkad7c28052012-04-27 15:43:36 -07003063 @Override
3064 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003065 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003066 final List<CharSequence> text = event.getText();
3067 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003068 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003069 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003070 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003071 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003072 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003073 } else if (mWorkspace != null) {
3074 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003075 } else {
3076 text.add(getString(R.string.all_apps_home_button_label));
3077 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003078 return result;
3079 }
3080
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003081 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003082 * If the activity is currently paused, signal that we need to run the passed Runnable
3083 * in onResume.
3084 *
3085 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003086 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3087 * wrong when we're not running, and if the activity comes back to what the configuration was
3088 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003089 *
3090 * Implementation of the method from LauncherModel.Callbacks.
3091 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003092 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003093 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003094 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003095 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003096 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003097 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003098 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003099 }
3100 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003101 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003102 return true;
3103 } else {
3104 return false;
3105 }
3106 }
3107
Michael Jurkac402cd92013-05-20 15:49:32 +02003108 private boolean waitUntilResume(Runnable run) {
3109 return waitUntilResume(run, false);
3110 }
3111
Michael Jurka1e2f4652013-07-08 18:03:46 -07003112 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003113 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003114 }
3115
Michael Jurka7607c2f2013-04-03 14:33:19 -07003116 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003117 * If the activity is currently paused, signal that we need to re-run the loader
3118 * in onResume.
3119 *
3120 * This needs to be called from incoming places where resources might have been loaded
3121 * while we are paused. That is becaues the Configuration might be wrong
3122 * when we're not running, and if it comes back to what it was when we
3123 * were paused, we are not restarted.
3124 *
3125 * Implementation of the method from LauncherModel.Callbacks.
3126 *
3127 * @return true if we are currently paused. The caller might be able to
3128 * skip some work in that case since we will come back again.
3129 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003130 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003131 public boolean setLoadOnResume() {
3132 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003133 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003134 mOnResumeNeedsLoad = true;
3135 return true;
3136 } else {
3137 return false;
3138 }
3139 }
3140
3141 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003142 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003144 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003146 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003147 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003148 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003149 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003151 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003152
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003154 * Clear any pending bind callbacks. This is called when is loader is planning to
3155 * perform a full rebind from scratch.
3156 */
3157 @Override
3158 public void clearPendingBinds() {
3159 mBindOnResumeCallbacks.clear();
3160 if (mPendingExecutor != null) {
3161 mPendingExecutor.markCompleted();
3162 mPendingExecutor = null;
3163 }
3164 }
3165
3166 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167 * Refreshes the shortcuts shown on the workspace.
3168 *
3169 * Implementation of the method from LauncherModel.Callbacks.
3170 */
3171 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003172 if (LauncherAppState.PROFILE_STARTUP) {
3173 Trace.beginSection("Starting page bind");
3174 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003175 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003176
Winson Chungd64d1762013-08-20 14:37:16 -07003177 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003178 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003179 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003180
Winson Chung3d503fb2011-07-13 17:25:49 -07003181 if (mHotseat != null) {
3182 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003184 if (LauncherAppState.PROFILE_STARTUP) {
3185 Trace.endSection();
3186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 }
3188
Adam Cohendcd297f2013-06-18 13:13:40 -07003189 @Override
3190 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003191 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003192 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3193 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003194 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3195 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3196 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003197 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3198 // If there are no screens, we need to have an empty screen
3199 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003200 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003201 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003202
3203 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003204 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003205 if (hasCustomContentToLeft()) {
3206 mWorkspace.createCustomContentContainer();
3207 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003208 }
Winsonc7d2e832016-07-28 12:24:55 -07003209
3210 // After we have added all the screens, if the wallpaper was locked to the default state,
3211 // then notify to indicate that it can be released and a proper wallpaper offset can be
3212 // computed before the next layout
3213 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003214 }
3215
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003216 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003217 int count = orderedScreenIds.size();
3218 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003219 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003220 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003221 // No need to bind the first screen, as its always bound.
3222 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3223 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003224 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003225 }
3226
Winson Chungd64d1762013-08-20 14:37:16 -07003227 public void bindAppsAdded(final ArrayList<Long> newScreens,
3228 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003229 final ArrayList<ItemInfo> addAnimated,
3230 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003231 Runnable r = new Runnable() {
3232 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003233 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003234 }
3235 };
3236 if (waitUntilResume(r)) {
3237 return;
3238 }
3239
Winson Chungd64d1762013-08-20 14:37:16 -07003240 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003241 if (newScreens != null) {
3242 bindAddScreens(newScreens);
3243 }
Winson Chungd64d1762013-08-20 14:37:16 -07003244
3245 // We add the items without animation on non-visible pages, and with
3246 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003247 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003248 bindItems(addNotAnimated, 0,
3249 addNotAnimated.size(), false);
3250 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003251 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003252 bindItems(addAnimated, 0,
3253 addAnimated.size(), true);
3254 }
Winson Chungc58497e2013-09-03 17:48:37 -07003255
Winson Chung87412982013-10-03 18:34:14 -07003256 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003257 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003258
Winson Chungb745afb2015-03-02 11:51:23 -08003259 if (addedApps != null && mAppsView != null) {
3260 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003261 }
Winson Chungd64d1762013-08-20 14:37:16 -07003262 }
3263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003264 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003265 * Bind the items start-end from the list.
3266 *
3267 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003269 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003270 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3271 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003272 Runnable r = new Runnable() {
3273 public void run() {
3274 bindItems(shortcuts, start, end, forceAnimateIcons);
3275 }
3276 };
3277 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003278 return;
3279 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003280
Winson Chungf0c6ae02012-03-21 16:10:31 -07003281 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003282 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3283 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003284 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003285 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003286 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003287 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003288 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003289
3290 // Short circuit if we are loading dock items for a configuration which has no dock
3291 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3292 mHotseat == null) {
3293 continue;
3294 }
3295
Sunny Goyal639e9062015-08-19 19:17:06 -07003296 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003297 switch (item.itemType) {
3298 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3299 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003300 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003301 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003302 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003304 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003305 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003306 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003307 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003308 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003309 default:
3310 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003312
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003313 /*
3314 * Remove colliding items.
3315 */
3316 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3317 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3318 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3319 View v = cl.getChildAt(item.cellX, item.cellY);
3320 Object tag = v.getTag();
3321 String desc = "Collision while binding workspace item: " + item
3322 + ". Collides with " + tag;
3323 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3324 throw (new RuntimeException(desc));
3325 } else {
3326 Log.d(TAG, desc);
3327 LauncherModel.deleteItemFromDatabase(this, item);
3328 continue;
3329 }
3330 }
3331 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003332 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3333 item.cellY, 1, 1);
3334 if (animateIcons) {
3335 // Animate all the applications up now
3336 view.setAlpha(0f);
3337 view.setScaleX(0f);
3338 view.setScaleY(0f);
3339 bounceAnims.add(createNewAppBounceAnimation(view, i));
3340 newShortcutsScreenId = item.screenId;
3341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003342 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003343
Winson Chung997a9232013-07-24 15:33:46 -07003344 if (animateIcons) {
3345 // Animate to the correct page
3346 if (newShortcutsScreenId > -1) {
3347 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003348 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003349 final Runnable startBounceAnimRunnable = new Runnable() {
3350 public void run() {
3351 anim.playTogether(bounceAnims);
3352 anim.start();
3353 }
3354 };
Winson Chung997a9232013-07-24 15:33:46 -07003355 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003356 // We post the animation slightly delayed to prevent slowdowns
3357 // when we are loading right after we return to launcher.
3358 mWorkspace.postDelayed(new Runnable() {
3359 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003360 if (mWorkspace != null) {
3361 mWorkspace.snapToPage(newScreenIndex);
3362 mWorkspace.postDelayed(startBounceAnimRunnable,
3363 NEW_APPS_ANIMATION_DELAY);
3364 }
Winson Chung94d67682013-09-25 16:29:40 -07003365 }
3366 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003367 } else {
3368 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003369 }
3370 }
Winson Chung64359a52013-07-08 17:17:08 -07003371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003373 }
3374
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003375 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3376 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3377 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003378 view.updateAppWidget(null);
3379 view.setOnClickListener(this);
3380 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003381 mWorkspace.requestLayout();
3382 }
3383
Joe Onorato9c1289c2009-08-17 11:03:03 -04003384 /**
3385 * Add the views for a widget to the workspace.
3386 *
3387 * Implementation of the method from LauncherModel.Callbacks.
3388 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003389 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003390 Runnable r = new Runnable() {
3391 public void run() {
3392 bindAppWidget(item);
3393 }
3394 };
3395 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003396 return;
3397 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003398
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003399 if (mIsSafeModeEnabled) {
3400 bindSafeModeWidget(item);
3401 return;
3402 }
3403
Daniel Sandler843e8602010-06-07 14:59:01 -04003404 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3405 if (DEBUG_WIDGETS) {
3406 Log.d(TAG, "bindAppWidget: " + item);
3407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003409 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003410
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003411 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3412 // If the provider is not ready, bind as a pending widget.
3413 appWidgetInfo = null;
3414 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3415 // The widget id is not valid. Try to find the widget based on the provider info.
3416 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3417 } else {
3418 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3419 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003420
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003421 // If the provider is ready, but the width is not yet restored, try to restore it.
3422 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3423 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003424 if (appWidgetInfo == null) {
3425 if (DEBUG_WIDGETS) {
3426 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3427 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003428 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003429 }
3430 LauncherModel.deleteItemFromDatabase(this, item);
3431 return;
3432 }
Sunny Goyalff572272014-07-23 13:58:07 -07003433
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003434 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003435 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003436 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3437 // Id has not been allocated yet. Allocate a new id.
3438 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3439 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003440
Sunny Goyald478c832016-04-01 12:04:16 -07003441 // Also try to bind the widget. If the bind fails, the user will be shown
3442 // a click to setup UI, which will ask for the bind permission.
3443 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3444 pendingInfo.spanX = item.spanX;
3445 pendingInfo.spanY = item.spanY;
3446 pendingInfo.minSpanX = item.minSpanX;
3447 pendingInfo.minSpanY = item.minSpanY;
3448 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003449
3450 boolean isDirectConfig =
3451 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3452 if (isDirectConfig && item.bindOptions != null) {
3453 Bundle newOptions = item.bindOptions.getExtras();
3454 if (options != null) {
3455 newOptions.putAll(options);
3456 }
3457 options = newOptions;
3458 }
Sunny Goyald478c832016-04-01 12:04:16 -07003459 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3460 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003461
Sunny Goyal86df1382016-08-10 15:03:22 -07003462 // We tried to bind once. If we were not able to bind, we would need to
3463 // go through the permission dialog, which means we cannot skip the config
3464 // activity.
3465 item.bindOptions = null;
3466 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3467
Sunny Goyald478c832016-04-01 12:04:16 -07003468 // Bind succeeded
3469 if (success) {
3470 // If the widget has a configure activity, it is still needs to set it up,
3471 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003472 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003473 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3474 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003475 }
Sunny Goyald478c832016-04-01 12:04:16 -07003476
3477 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003478 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003479 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003480 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003481 // The widget was marked as UI not ready, but there is no configure activity to
3482 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003483 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3484 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003485 }
Sunny Goyalff572272014-07-23 13:58:07 -07003486 }
3487
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003488 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003489 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003490 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3491 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003492 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003493
Sunny Goyal56c73602015-09-25 12:55:01 -07003494 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003495 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003496 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003497 deleteWidgetInfo(item);
3498 return;
3499 }
3500
Sunny Goyal233ee962015-08-03 13:05:01 -07003501 item.minSpanX = appWidgetInfo.minSpanX;
3502 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003503 addAppWidgetToWorkspace(
3504 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3505 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003506 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003507 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003508 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003509 view.updateAppWidget(null);
3510 view.setOnClickListener(this);
3511 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003512 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003513 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514
Daniel Sandler843e8602010-06-07 14:59:01 -04003515 if (DEBUG_WIDGETS) {
3516 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3517 + (SystemClock.uptimeMillis()-start) + "ms");
3518 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 }
3520
Sunny Goyalff572272014-07-23 13:58:07 -07003521 /**
3522 * Restores a pending widget.
3523 *
3524 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003525 */
Sunny Goyald478c832016-04-01 12:04:16 -07003526 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003527 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3528 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3529 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003530 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003531 }
3532
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003533 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003534 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003535
3536 mWorkspace.reinflateWidgetsIfNecessary();
3537 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003538 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003539 }
3540
Adam Cohen1462de32012-07-24 22:34:36 -07003541 public void onPageBoundSynchronously(int page) {
3542 mSynchronouslyBoundPages.add(page);
3543 }
3544
Sunny Goyal527c7d32015-08-28 15:19:36 -07003545 @Override
3546 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3547 if (mPendingExecutor != null) {
3548 mPendingExecutor.markCompleted();
3549 }
3550 mPendingExecutor = executor;
3551 executor.attachTo(this);
3552 }
3553
3554 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3555 if (mPendingExecutor == executor) {
3556 mPendingExecutor = null;
3557 }
3558 }
3559
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003560 @Override
3561 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3562 Runnable r = new Runnable() {
3563 public void run() {
3564 finishFirstPageBind(executor);
3565 }
3566 };
3567 if (waitUntilResume(r)) {
3568 return;
3569 }
3570
3571 Runnable onComplete = new Runnable() {
3572 @Override
3573 public void run() {
3574 if (executor != null) {
3575 executor.onLoadAnimationCompleted();
3576 }
3577 }
3578 };
3579 if (mDragLayer.getAlpha() < 1) {
3580 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3581 } else {
3582 onComplete.run();
3583 }
3584 }
3585
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 /**
3587 * Callback saying that there aren't any more items to bind.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003591 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003592 Runnable r = new Runnable() {
3593 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003594 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003595 }
3596 };
3597 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003598 return;
3599 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003600 if (LauncherAppState.PROFILE_STARTUP) {
3601 Trace.beginSection("Page bind completed");
3602 }
Adam Cohen1462de32012-07-24 22:34:36 -07003603 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003605 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003606
Sunny Goyal2100c782016-08-22 16:00:03 -07003607 if (mPendingActivityResult != null) {
3608 handleActivityResult(mPendingActivityResult.requestCode,
3609 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3610 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003611 }
3612
Sunny Goyal756adbc2015-04-16 15:20:51 -07003613 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003614
3615 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003616 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003617 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003618 if (LauncherAppState.PROFILE_STARTUP) {
3619 Trace.endSection();
3620 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003621 }
3622
Winson Chunga2413752012-04-03 14:22:34 -07003623 private boolean canRunNewAppsAnimation() {
3624 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003625 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003626 }
3627
Winson Chungc9168342013-06-26 14:54:55 -07003628 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003629 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003630 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3631 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003632 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003633 return bounceAnim;
3634 }
3635
Adam Cohen27772732013-11-11 14:00:56 +00003636 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003637 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003638 }
3639
Tony Wickham55616cd2015-09-23 14:55:17 -07003640 public int getSearchBarHeight() {
3641 if (mLauncherCallbacks != null) {
3642 return mLauncherCallbacks.getSearchBarHeight();
3643 }
3644 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3645 }
3646
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003647 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003648 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3649 * multiple calls to bind the same list.)
3650 */
3651 @Thunk ArrayList<AppInfo> mTmpAppsList;
3652 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3653 public void run() {
3654 bindAllApplications(mTmpAppsList);
3655 mTmpAppsList = null;
3656 }
3657 };
3658
3659 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 * Add the icons for all apps.
3661 *
3662 * Implementation of the method from LauncherModel.Callbacks.
3663 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003664 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003665 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3666 mTmpAppsList = apps;
3667 return;
3668 }
3669
Winson Chungb745afb2015-03-02 11:51:23 -08003670 if (mAppsView != null) {
3671 mAppsView.setApps(apps);
3672 }
Adam Cohen9211d422014-10-07 18:14:53 -07003673 if (mLauncherCallbacks != null) {
3674 mLauncherCallbacks.bindAllApplications(apps);
3675 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676 }
3677
3678 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003679 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3680 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3681 */
3682 @Override
3683 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
3684 mDeepShortcutMap = deepShortcutMapCopy;
3685 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
3686 }
3687
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003688 public List<String> getShortcutIdsForItem(ItemInfo info) {
3689 if (!DeepShortcutManager.supportsShortcuts(info)) {
3690 return Collections.EMPTY_LIST;
3691 }
3692 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07003693 if (component == null) {
3694 return Collections.EMPTY_LIST;
3695 }
3696
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003697 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
3698 return ids == null ? Collections.EMPTY_LIST : ids;
3699 }
3700
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003701 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 * A package was updated.
3703 *
3704 * Implementation of the method from LauncherModel.Callbacks.
3705 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003706 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003707 Runnable r = new Runnable() {
3708 public void run() {
3709 bindAppsUpdated(apps);
3710 }
3711 };
3712 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003713 return;
3714 }
3715
Winson Chungb745afb2015-03-02 11:51:23 -08003716 if (mAppsView != null) {
3717 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003718 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 }
3720
Sunny Goyal4390ace2014-10-13 11:33:11 -07003721 @Override
3722 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3723 Runnable r = new Runnable() {
3724 public void run() {
3725 bindWidgetsRestored(widgets);
3726 }
3727 };
3728 if (waitUntilResume(r)) {
3729 return;
3730 }
3731 mWorkspace.widgetsRestored(widgets);
3732 }
3733
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003735 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003736 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003737 *
3738 * @param updated list of shortcuts which have changed.
3739 * @param removed list of shortcuts which were deleted in the background. This can happen when
3740 * an app gets removed from the system or some of its components are no longer
3741 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003742 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003743 @Override
3744 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
3745 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003746 Runnable r = new Runnable() {
3747 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003748 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003749 }
3750 };
3751 if (waitUntilResume(r)) {
3752 return;
3753 }
3754
Sunny Goyal4390ace2014-10-13 11:33:11 -07003755 if (!updated.isEmpty()) {
3756 mWorkspace.updateShortcuts(updated);
3757 }
3758
3759 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003760 HashSet<ComponentName> removedComponents = new HashSet<>();
3761 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3762
Sunny Goyal4390ace2014-10-13 11:33:11 -07003763 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003764 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003765 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003766 } else {
3767 removedComponents.add(si.getTargetComponent());
3768 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003769 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003770
3771 if (!removedComponents.isEmpty()) {
3772 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3773 mWorkspace.removeItemsByMatcher(matcher);
3774 mDragController.onAppsRemoved(matcher);
3775 }
3776
3777 if (!removedDeepShortcuts.isEmpty()) {
3778 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3779 mWorkspace.removeItemsByMatcher(matcher);
3780 mDragController.onAppsRemoved(matcher);
3781 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003782 }
3783 }
3784
3785 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003786 * Update the state of a package, typically related to install state.
3787 *
3788 * Implementation of the method from LauncherModel.Callbacks.
3789 */
Sunny Goyale755d462014-07-22 13:48:29 -07003790 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003791 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3792 Runnable r = new Runnable() {
3793 public void run() {
3794 bindRestoreItemsChange(updates);
3795 }
3796 };
3797 if (waitUntilResume(r)) {
3798 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003799 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003800
Sunny Goyal756adbc2015-04-16 15:20:51 -07003801 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003802 }
3803
3804 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003805 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003806 * in addition to specific applications to remove, the reason being that
3807 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003808 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003809 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003811 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003812 public void bindWorkspaceComponentsRemoved(
3813 final HashSet<String> packageNames, final HashSet<ComponentName> components,
3814 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003815 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003816 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003817 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003818 }
Winson Chungd64d1762013-08-20 14:37:16 -07003819 };
3820 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003821 return;
3822 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003823 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003824 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3825 mWorkspace.removeItemsByMatcher(matcher);
3826 mDragController.onAppsRemoved(matcher);
3827
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003828 }
3829 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003830 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3831 mWorkspace.removeItemsByMatcher(matcher);
3832 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003833 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003834 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003835
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003836 @Override
3837 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3838 Runnable r = new Runnable() {
3839 public void run() {
3840 bindAppInfosRemoved(appInfos);
3841 }
3842 };
3843 if (waitUntilResume(r)) {
3844 return;
Joe Onorato36115782010-06-17 13:28:48 -04003845 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003846
Winson Chungdf95eb12013-10-16 14:57:07 -07003847 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003848 if (mAppsView != null) {
3849 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 }
Joe Onoratobe386092009-11-17 17:32:16 -08003852
Sunny Goyald164b7f2016-10-12 20:49:31 -07003853 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003854 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003855 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003856 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003857 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003858
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003859 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003860 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3861 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3862 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003863 return;
3864 }
3865
Sunny Goyald164b7f2016-10-12 20:49:31 -07003866 if (mWidgetsView != null && allWidgets != null) {
3867 mWidgetsView.setWidgets(allWidgets);
3868 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003869 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003870 }
3871
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003872 @Override
3873 public void notifyWidgetProvidersChanged() {
3874 if (mWorkspace.getState().shouldUpdateWidget) {
3875 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3876 }
3877 }
3878
Winson Chung400438b2011-01-16 17:53:48 -08003879 private int mapConfigurationOriActivityInfoOri(int configOri) {
3880 final Display d = getWindowManager().getDefaultDisplay();
3881 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3882 switch (d.getRotation()) {
3883 case Surface.ROTATION_0:
3884 case Surface.ROTATION_180:
3885 // We are currently in the same basic orientation as the natural orientation
3886 naturalOri = configOri;
3887 break;
3888 case Surface.ROTATION_90:
3889 case Surface.ROTATION_270:
3890 // We are currently in the other basic orientation to the natural orientation
3891 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3892 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3893 break;
3894 }
3895
3896 int[] oriMap = {
3897 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3898 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3899 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3900 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3901 };
3902 // Since the map starts at portrait, we need to offset if this device's natural orientation
3903 // is landscape.
3904 int indexOffset = 0;
3905 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3906 indexOffset = 1;
3907 }
3908 return oriMap[(d.getRotation() + indexOffset) % 4];
3909 }
Adam Cohen446e9402011-09-15 18:21:21 -07003910
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003911 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07003912 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003913 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003914 if (Utilities.ATLEAST_JB_MR2) {
3915 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
3916 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003917 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3918 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003919 }
Winson Chung4b919f82012-05-01 10:44:08 -07003920 }
3921 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003922
Winson Chung4b919f82012-05-01 10:44:08 -07003923 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003924 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003925 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003926 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003927 } else {
3928 mHandler.postDelayed(new Runnable() {
3929 public void run() {
3930 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3931 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003932 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003933 }
Winson Chung4b919f82012-05-01 10:44:08 -07003934 }
Winson Chung400438b2011-01-16 17:53:48 -08003935 }
3936
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003937 private void markAppsViewShown() {
3938 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3939 return;
3940 }
3941 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3942 }
3943
3944 private boolean shouldShowDiscoveryBounce() {
3945 if (mState != mState.WORKSPACE) {
3946 return false;
3947 }
3948 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3949 return true;
3950 }
3951 if (!mIsResumeFromActionScreenOff) {
3952 return false;
3953 }
3954 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3955 return false;
3956 }
3957 return true;
3958 }
3959
Winson Chung5ffd51d2015-06-25 11:36:50 -07003960 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003961 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00003962 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003963 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003964 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003965 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003966 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
3967 if (userHandle != null) {
3968 user = UserHandleCompat.fromUser(userHandle);
3969 }
3970 }
3971 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07003972 }
3973
3974 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003975 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003976 if (user == null) {
3977 user = UserHandleCompat.myUserHandle();
3978 }
3979
3980 // Called from search suggestion, add the profile extra to the intent to ensure that we
3981 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01003982 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003983 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01003984 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00003985 return null;
3986 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07003987 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00003988 }
3989
Winson Chung5ffd51d2015-06-25 11:36:50 -07003990 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00003991 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
3992 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003993 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01003994 UserHandleCompat.myUserHandle());
3995 }
3996
Winson Chung5ffd51d2015-06-25 11:36:50 -07003997 protected void moveWorkspaceToDefaultScreen() {
3998 mWorkspace.moveToDefaultScreen(false);
3999 }
4000
Winson Chung80baf5a2010-08-09 16:03:15 -07004001 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004002 * Returns a FastBitmapDrawable with the icon, accurately sized.
4003 */
4004 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4005 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4006 d.setFilterBitmap(true);
4007 resizeIconDrawable(d);
4008 return d;
4009 }
4010
4011 /**
4012 * Resizes an icon drawable to the correct icon size.
4013 */
Winson5fbe0742015-09-30 15:33:00 -07004014 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004015 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004016 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004017 }
4018
4019 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004020 * Prints out out state for debugging.
4021 */
4022 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004023 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004024 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004025 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4026 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004027 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004028 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004029
Daniel Sandler325dc232013-06-05 22:57:57 -04004030 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004031 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004032
4033 @Override
4034 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4035 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004036 // Dump workspace
4037 writer.println(prefix + "Workspace Items");
4038 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4039 writer.println(prefix + " Homescreen " + i);
4040
4041 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4042 for (int j = 0; j < layout.getChildCount(); j++) {
4043 Object tag = layout.getChildAt(j).getTag();
4044 if (tag != null) {
4045 writer.println(prefix + " " + tag.toString());
4046 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004047 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004048 }
Sunny Goyala1365452015-10-01 15:46:24 -07004049
4050 writer.println(prefix + " Hotseat");
4051 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4052 for (int j = 0; j < layout.getChildCount(); j++) {
4053 Object tag = layout.getChildAt(j).getTag();
4054 if (tag != null) {
4055 writer.println(prefix + " " + tag.toString());
4056 }
4057 }
4058
Sunny Goyal713edfc2016-05-06 09:58:34 -07004059 try {
4060 FileLog.flushAll(writer);
4061 } catch (Exception e) {
4062 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004063 }
4064
Adam Cohen9211d422014-10-07 18:14:53 -07004065 if (mLauncherCallbacks != null) {
4066 mLauncherCallbacks.dump(prefix, fd, writer, args);
4067 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004068 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004069
Sunny Goyal66b24572016-09-21 15:57:55 -07004070 @Override
4071 @TargetApi(Build.VERSION_CODES.N)
4072 public void onProvideKeyboardShortcuts(
4073 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4074
4075 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4076 if (mState == State.WORKSPACE) {
4077 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4078 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4079 }
4080 View currentFocus = getCurrentFocus();
4081 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4082 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4083 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4084 }
4085 if (currentFocus instanceof BubbleTextView &&
4086 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4087 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4088 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4089 }
4090 if (!shortcutInfos.isEmpty()) {
4091 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4092 }
4093
4094 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4095 }
4096
4097 @Override
4098 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4099 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4100 switch (keyCode) {
4101 case KeyEvent.KEYCODE_A:
4102 if (mState == State.WORKSPACE) {
4103 showAppsView(true, true, false);
4104 return true;
4105 }
4106 break;
4107 case KeyEvent.KEYCODE_S: {
4108 View focusedView = getCurrentFocus();
4109 if (focusedView instanceof BubbleTextView
4110 && focusedView.getTag() instanceof ItemInfo
4111 && mAccessibilityDelegate.performAction(focusedView,
4112 (ItemInfo) focusedView.getTag(),
4113 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07004114 DeepShortcutsContainer.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004115 return true;
4116 }
4117 break;
4118 }
4119 case KeyEvent.KEYCODE_O:
4120 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4121 return true;
4122 }
4123 break;
4124 }
4125 }
4126 return super.onKeyShortcut(keyCode, event);
4127 }
4128
Adam Cohen59400422014-03-05 18:07:04 -08004129 public static CustomAppWidget getCustomAppWidget(String name) {
4130 return sCustomAppWidgets.get(name);
4131 }
4132
4133 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4134 return sCustomAppWidgets;
4135 }
4136
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004137 public static Launcher getLauncher(Context context) {
4138 if (context instanceof Launcher) {
4139 return (Launcher) context;
4140 }
4141 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4142 }
4143
Sunny Goyal745bad92016-05-02 10:54:12 -07004144 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4145
4146 @Override
4147 public void onSharedPreferenceChanged(
4148 SharedPreferences sharedPreferences, String key) {
4149 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4150 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4151 if (!waitUntilResume(this, true)) {
4152 run();
4153 }
4154 }
4155 }
4156
4157 @Override
4158 public void run() {
4159 setOrientation();
4160 }
4161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004162}